polynomials_on_simplices.generic_tools.str_utils module¶
Utility functions for generating math-related strings.
-
convert_float_to_fraction
(s, latex_fraction=False)[source]¶ Convert a float in string format to a fraction.
Parameters: Returns: Fraction string.
Examples
>>> convert_float_to_fraction('0.5') '1 / 2' >>> convert_float_to_fraction('0') '0' >>> convert_float_to_fraction('0.5', latex_fraction=True) '\\frac{1}{2}' >>> convert_float_to_fraction('0', latex_fraction=True) '0' >>> convert_float_to_fraction('-0.5', latex_fraction=True) '-\\frac{1}{2}' >>> convert_float_to_fraction('2', latex_fraction=True) '2'
-
is_nice_fraction
(f, n=10)[source]¶ Check if a scalar can be expressed as a ‘nice’ rational number. Nice in this sense means that it can be written as a fraction with a denominator less than a given number n.
Parameters: Returns: Whether or not the scalar is a ‘nice’ fraction.
Return type: Examples
>>> is_nice_fraction(0.1) True >>> is_nice_fraction(0.01) False >>> is_nice_fraction(0.01, 100) True
-
split_long_str
(string, sep=' ', long_str_threshold=-1, sep_replacement_before='', sep_replacement_after='')[source]¶ Split a long string into an array of parts.
The string will be split at occurrences of the given separator in a way that makes sure that each part is shorter than the given threshold length.
Parameters: - string (str) – String we want to split.
- sep (str) – The delimiter according which to split the string.
- long_str_threshold (int) – Don’t split at a delimiter if the resulting parts will be shorter than this threshold. The default value -1 indicates that we should always split at a delimiter.
- sep_replacement_before (str) – When splitting at a separator, the separator will be replaced with this string in the part before the split.
- sep_replacement_after (str) – When splitting at a separator, the separator will be replaced with this string in the part after the split.
Returns: Parts of the original string after the split.
Return type: List[str]
>>> split_long_str("Hello world", long_str_threshold=1) ['Hello', 'world'] >>> split_long_str("Hello world", long_str_threshold=15) ['Hello world'] >>> split_long_str("My quite long sentence example", long_str_threshold=15) ['My quite long', 'sentence', 'example'] >>> split_long_str("Hello world", sep_replacement_before="_") ['Hello_', 'world'] >>> split_long_str("Hello world", sep_replacement_after="_") ['Hello', '_world']
-
str_dot_product
(a, b, multiplication_character='')[source]¶ Generate the string for a dot product of two arrays, with array elements given by strings.
Parameters: Returns: String for the dot product of the two arrays.
Return type: Examples
>>> str_dot_product(["a1", "a2"], ["b1", "b2"]) 'a1 b1 + a2 b2' >>> str_dot_product(["a1", "a2"], ["b1", "b2"], multiplication_character="\\cdot") 'a1 \\cdot b1 + a2 \\cdot b2'
-
str_exponent
(base, exp, str_type='python')[source]¶ Generate the string for the power of one value raised to an exponent, given by strings.
Parameters: Returns: String for the base raised to the power of the exponent.
Return type: >>> str_exponent("e", "x") 'e**x' >>> str_exponent("e", "x", str_type="latex") 'e^x' >>> str_exponent("e", "x_2", str_type="latex") 'e^{x_2}' >>> str_exponent("e", "0") '1' >>> str_exponent("e", "1") 'e' >>> str_exponent("0", "0") '1'
-
str_multi_product
(a, multiplication_character='')[source]¶ Generate the string for the product of an array of values, given by strings.
Parameters: Returns: String for the product of the array of values.
Return type: Examples
>>> str_multi_product(["a1", "a2", "a3"]) 'a1 a2 a3' >>> str_multi_product(["a1", "-a2"], multiplication_character="\\times") '-a1 \\times a2' >>> str_multi_product(["a1", "0", "a3"]) '0' >>> str_multi_product(["a1", "1", "a3"]) 'a1 a3'
-
str_multi_sum
(a)[source]¶ Generate the string for the sum of an array of values, given by strings.
Parameters: a (List[str]) – Values in the sum. Returns: String for the sum of the values in the array. Return type: str Examples
>>> str_multi_sum(["a1", "a2"]) 'a1 + a2' >>> str_multi_sum(["a1", "-a2"]) 'a1 - a2' >>> str_multi_sum(["a1", "0", "a3"]) 'a1 + a3' >>> str_multi_sum(["0", "-a2", "a3"]) '-a2 + a3'
-
str_number
(a, latex_fraction=False, prettify_fractions=True, n=10)[source]¶ Convert a number to string.
Parameters: - a – Number we want to convert to string.
- latex_fraction (bool) – If the number is a fraction, whether or not to encode the fractions using the Latex “frac” syntax. Default is to just use ‘/’ for the fraction.
- prettify_fractions (bool) – Whether or not to convert a number which is a ‘nice’ fraction (see
is_nice_fraction()
) to its fraction representation. - n (int) – How large the denominator can be in a fraction for the fraction to be considered nice. Only used when prettify_fractions is True.
Returns: String representation of the number.
Return type: Examples
>>> str_number(1.0) '1' >>> str_number(0.0) '0' >>> str_number(0.5) '1 / 2' >>> str_number(0.5, prettify_fractions=False) '0.5'
-
str_number_array
(a, latex=False, latex_column_vector=True, latex_array_style='pmatrix')[source]¶ Convert an array of numbers to string.
Parameters: - a – Array of numbers we want to convert to string.
- latex (bool) – Whether or not to encode the array using Latex syntax.
- latex_column_vector (bool) – Whether the array should be encoded as a row or column vector. Only applicable if latex is true.
- latex_array_style – Latex array style to use. Only applicable if latex is true.
Returns: String representation of the array of numbers.
Return type: Examples
>>> str_number_array([1.0, 2.0]) '[1, 2]' >>> str_number_array([0.0, 0.5], latex=True) '\\begin{pmatrix}0 \\\\ \\frac{1}{2}\\end{pmatrix}' >>> str_number_array([3, 4], latex=True, latex_column_vector=False, latex_array_style="bmatrix") '\\begin{bmatrix}3 & 4\\end{bmatrix}'
-
str_product
(a, b, multiplication_character='')[source]¶ Generate the string for the product of two values, given by strings.
Parameters: Returns: String for the product of the two values.
Return type: Examples
>>> str_product("a1", "a2") 'a1 a2' >>> str_product("a1", "-a2", multiplication_character="\\times") '-a1 \\times a2' >>> str_product("a1", "0") '0' >>> str_product("a1", "1") 'a1'
-
str_sequence
(symbol, n, indexing='fortran', index_style='inline')[source]¶ Generate a sequence of enumerated strings, e.g. (a1, a2, a3).
Parameters: - symbol (str) – Base symbol(s) to use for each string.
- n (int) – Number of strings to generate.
- indexing – Which indexing to use. Can be “c” for 0-based indexing (0, 1, 2, …), or “fortran” for 1-based indexing (1, 2, 3, …).
- index_style – How indices should be added to the base symbol(s). Can be any of “inline”, “subscript”, “superscript”, “latex_subscript”, “latex_superscript” or “list”. See examples below. The “latex_subscript” differ from the “subscript” variant in that the index is wrapped in braces ({}) if it’s longer than one character, and similarly for the “latex_superscript” variant.
Returns: List of strings.
Examples
>>> str_sequence('a', 3) ['a1', 'a2', 'a3'] >>> str_sequence('a', 3, indexing="c") ['a0', 'a1', 'a2'] >>> str_sequence('a', 3, index_style="subscript") ['a_1', 'a_2', 'a_3'] >>> str_sequence('a', 3, index_style="superscript") ['a^1', 'a^2', 'a^3'] >>> str_sequence('a', 10, index_style="latex_subscript") ['a_1', 'a_2', 'a_3', 'a_4', 'a_5', 'a_6', 'a_7', 'a_8', 'a_9', 'a_{10}'] >>> str_sequence('a', 3, index_style="list") ['a[1]', 'a[2]', 'a[3]']
-
str_sum
(a, b)[source]¶ Generate the string for the sum of two values, given by strings.
Parameters: Returns: String for the sum of the two values.
Return type: Examples
>>> str_sum("a1", "a2") 'a1 + a2' >>> str_sum("a1", "-a2") 'a1 - a2' >>> str_sum("a1", "0") 'a1' >>> str_sum("0", "-a2") '-a2' >>> str_sum("-a1", "1") '-a1 + 1'