Skip to content

Conversions

func_to_op_const(expr)

Examples:

>>> e_1 = 'x_1*cubed(x_1*3-squared(c_1/x_1*2)-x_3)-squared(x)'
>>> func_to_op_const(e_1)
'x_1*(x_1*3-(c_1/x_1*2)**2-x_3)**3-(x)**2'
>>> func_to_op_const('cubed(x_1)')
'(x_1)**3'
Source code in src/equation_tree/util/conversions.py
656
657
658
659
660
661
662
663
664
665
666
667
def func_to_op_const(expr):
    """
    Examples:
        >>> e_1 = 'x_1*cubed(x_1*3-squared(c_1/x_1*2)-x_3)-squared(x)'
        >>> func_to_op_const(e_1)
        'x_1*(x_1*3-(c_1/x_1*2)**2-x_3)**3-(x)**2'
        >>> func_to_op_const('cubed(x_1)')
        '(x_1)**3'
    """
    for key, el in CONVERSIONS_FUNC_OP_CONST.items():
        expr = _func_op_const_rec(expr, key, el)
    return expr

infix_to_prefix(infix, function_test, operator_test)

Transforms prefix notation to infix notation

Example

is_function = lambda x: x in ['sin', 'cos'] is_operator = lambda x : x in ['+', '-', '*', '/'] infix_to_prefix('x_2-x_1', is_function, is_operator) ['-', 'x_2', 'x_1']

infix_to_prefix('x_1-(x_2+x_4)', is_function, is_operator) ['-', 'x_1', '+', 'x_2', 'x_4']

infix_to_prefix('x_1cos(c_1+x_2)', is_function, is_operator) ['', 'x_1', 'cos', '+', 'c_1', 'x_2']

is_function = lambda x: x in ['sin', 'cos', 'e'] is_operator = lambda x: x in ['+', '-', '', '^', 'max', '*', '/'] infix_to_prefix('x_1 + max(x_2, x_3)', is_function, is_operator) ['+', 'x_1', 'max', 'x_2', 'x_3']

infix_to_prefix('x_1-(x_2/(x_3-x_4))',is_function, is_operator) ['-', 'x_1', '/', 'x_2', '-', 'x_3', 'x_4']

infix_to_prefix('x_1^(sin(x_2)/x_3)', is_function, is_operator) ['^', 'x_1', '/', 'sin', 'x_2', 'x_3']

infix_to_prefix('sin(x_1)-x_2', is_function, is_operator) ['-', 'sin', 'x_1', 'x_2']

Source code in src/equation_tree/util/conversions.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def infix_to_prefix(infix, function_test, operator_test):
    """
    Transforms prefix notation to infix notation

    Example:
        >>> is_function = lambda x: x in ['sin', 'cos']
        >>> is_operator = lambda x : x in ['+', '-', '*', '/']
        >>> infix_to_prefix('x_2-x_1', is_function, is_operator)
        ['-', 'x_2', 'x_1']

        >>> infix_to_prefix('x_1-(x_2+x_4)', is_function, is_operator)
        ['-', 'x_1', '+', 'x_2', 'x_4']

        >>> infix_to_prefix('x_1*cos(c_1+x_2)', is_function, is_operator)
        ['*', 'x_1', 'cos', '+', 'c_1', 'x_2']

        >>> is_function = lambda x: x in ['sin', 'cos', 'e']
        >>> is_operator = lambda x: x in ['+', '-', '*', '^', 'max', '**', '/']
        >>> infix_to_prefix('x_1 + max(x_2, x_3)', is_function, is_operator)
        ['+', 'x_1', 'max', 'x_2', 'x_3']

        >>> infix_to_prefix('x_1-(x_2/(x_3-x_4))',is_function, is_operator)
        ['-', 'x_1', '/', 'x_2', '-', 'x_3', 'x_4']

        >>> infix_to_prefix('x_1^(sin(x_2)/x_3)', is_function, is_operator)
        ['^', 'x_1', '/', 'sin', 'x_2', 'x_3']

        >>> infix_to_prefix('sin(x_1)-x_2', is_function, is_operator)
        ['-', 'sin', 'x_1', 'x_2']
    """

    # n = len(infix)

    # infix = list(infix[::-1].lower())
    #
    # for i in range(n):
    #     if infix[i] == "(":
    #         infix[i] = ")"
    #     elif infix[i] == ")":
    #         infix[i] = "("
    #
    # infix = "".join(infix)
    postfix = _infix_to_postfix(infix, function_test, operator_test)
    prefix = postfix[::-1]

    return prefix

op_const_to_func(expr)

Known operators with constants to functions. For exampl, e2->squared Examples: >>> op_const_to_func('x_1(x_13-(c_1/x_1*2)2-x_3)3-(x)2') 'x_1cubed(x_13-squared(c_1/x_12)-x_3)-squared(x)' >>> op_const_to_func('(x_1)3') 'cubed(x_1)' >>> op_const_to_func('x2') 'squared(x)' >>> op_const_to_func('c_13') 'cubed(c_1)' >>> op_const_to_func('(x_2*2)') 'squared(x_2)'

Source code in src/equation_tree/util/conversions.py
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
def op_const_to_func(expr):
    """
    Known operators with constants to functions. For exampl, e**2->squared
    Examples:
        >>> op_const_to_func('x_1*(x_1*3-(c_1/x_1*2)**2-x_3)**3-(x)**2')
        'x_1*cubed(x_1*3-squared(c_1/x_1*2)-x_3)-squared(x)'
        >>> op_const_to_func('(x_1)**3')
        'cubed(x_1)'
        >>> op_const_to_func('x**2')
        'squared(x)'
        >>> op_const_to_func('c_1**3')
        'cubed(c_1)'
        >>> op_const_to_func('(x_2**2)')
        'squared(x_2)'
    """

    for key, el in CONVERSION_OP_CONST_FUNC.items():
        expr = _op_const_func_rec(expr, key, el)
    expr = _remove_unnecessary_parentheses(expr)

    return expr

prefix_to_infix(prefix, function_test=lambda : False, operator_test=lambda : False)

Transforms prefix notation to infix notation

Example

is_function = lambda x: x in ['sin', 'cos'] is_operator = lambda x : x in ['+', '-', '', 'max', '*'] prefix_to_infix(['-', 'x_1', 'x_2'], is_function, is_operator) '(x_1-x_2)'

prefix_to_infix( ... ['', 'x', 'cos', '+', 'y', 'z'], is_function, is_operator) '(xcos((y+z)))'

prefix_to_infix(['max', 'x_1', 'x_2'], is_function, is_operator) 'max(x_1,x_2)'

prefix_to_infix(['', 'x_1', 'x_2'], is_function, is_operator) '(x_1x_2)'

Source code in src/equation_tree/util/conversions.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def prefix_to_infix(
    prefix, function_test=lambda _: False, operator_test=lambda _: False
):
    """
    Transforms prefix notation to infix notation

    Example:
        >>> is_function = lambda x: x in ['sin', 'cos']
        >>> is_operator = lambda x : x in ['+', '-', '*', 'max', '**']
        >>> prefix_to_infix(['-', 'x_1', 'x_2'], is_function, is_operator)
        '(x_1-x_2)'

        >>> prefix_to_infix(
        ...     ['*', 'x', 'cos', '+', 'y', 'z'], is_function, is_operator)
        '(x*cos((y+z)))'

        >>> prefix_to_infix(['max', 'x_1', 'x_2'], is_function, is_operator)
        'max(x_1,x_2)'

        >>> prefix_to_infix(['**', 'x_1', 'x_2'], is_function, is_operator)
        '(x_1**x_2)'

    """
    stack = []
    for i in range(len(prefix) - 1, -1, -1):
        if function_test(prefix[i]):
            # symbol in unary operator
            stack.append(prefix[i] + "(" + stack.pop() + ")")
        elif (operator_test(prefix[i]) or prefix[i] == "**") and prefix[i] in [
            "+",
            "-",
            "/",
            "^",
            "*",
            "**",
        ]:
            # symbol is binary operator
            str = "(" + stack.pop() + prefix[i] + stack.pop() + ")"
            stack.append(str)
        elif operator_test(prefix[i]):
            str = prefix[i] + "(" + stack.pop() + "," + stack.pop() + ")"
            stack.append(str)
        else:
            # symbol is operand
            stack.append(prefix[i])
    return stack.pop()

standardize_sympy(sympy_expr, variable_test=lambda : False, constant_test=lambda : False)

replace all variables and constants with standards

Example

from sympy import sympify expr = sympify('x + A * cos(z+y)') expr A*cos(y + z) + x

is_variable = lambda x : x in ['x', 'y', 'z'] is_constant = lambda x : x in ['A'] standardize_sympy(expr, is_variable, is_constant) c_1*cos(x_2 + x_3) + x_1

expr = sympify('x_a+By') expr By + x_a is_variable = lambda x : '_' in x or x in ['y'] is_constant = lambda x : x == 'B' standardize_sympy(expr, is_variable, is_constant) c_1*x_2 + x_1

expr = sympify('x ** x') expr xx is_variable = lambda x: x in ['x'] standardize_sympy(expr, is_variable) x_1x_1

expr = sympify('sin(Cx) + cos(Cx)') expr sin(Cx) + cos(Cx)

is_variable = lambda x: x == 'x' is_constant = lambda x: x == 'C' standardize_sympy(expr, is_variable, is_constant) sin(c_1x_1) + cos(c_1x_1)

Source code in src/equation_tree/util/conversions.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def standardize_sympy(
    sympy_expr, variable_test=lambda _: False, constant_test=lambda _: False
):
    """
    replace all variables and constants with standards

    Example:
        >>> from sympy import sympify
        >>> expr = sympify('x + A * cos(z+y)')
        >>> expr
        A*cos(y + z) + x

        >>> is_variable = lambda x : x in ['x', 'y', 'z']
        >>> is_constant = lambda x : x in ['A']
        >>> standardize_sympy(expr, is_variable, is_constant)
        c_1*cos(x_2 + x_3) + x_1

        >>> expr = sympify('x_a+B*y')
        >>> expr
        B*y + x_a
        >>> is_variable = lambda x : '_' in x or x in ['y']
        >>> is_constant = lambda x : x == 'B'
        >>> standardize_sympy(expr, is_variable, is_constant)
        c_1*x_2 + x_1

        >>> expr = sympify('x ** x')
        >>> expr
        x**x
        >>> is_variable = lambda x: x in ['x']
        >>> standardize_sympy(expr, is_variable)
        x_1**x_1

        >>> expr = sympify('sin(C*x) + cos(C*x)')
        >>> expr
        sin(C*x) + cos(C*x)

        >>> is_variable = lambda x: x == 'x'
        >>> is_constant = lambda x: x == 'C'
        >>> standardize_sympy(expr, is_variable, is_constant)
        sin(c_1*x_1) + cos(c_1*x_1)
    """
    variable_count = 0
    constant_count = 0
    variables = {}
    constants = {}

    def replace_symbols(node):
        nonlocal variable_count, constant_count, variables, constants
        if variable_test(str(node)):
            if is_variable_formatted(str(node)):
                new_symbol = symbols(str(node))
                variables[str(node)] = new_symbol
                return new_symbol
            if not str(node) in variables.keys() or str(node):
                variable_count += 1
                new_symbol = symbols(f"x_{variable_count}")
                variables[str(node)] = new_symbol
            else:
                new_symbol = variables[str(node)]
            return new_symbol
        elif constant_test(str(node)) and not is_numeric(str(node)):
            if is_constant_formatted(str(node)):
                new_symbol = symbols(str(node))
                constants[str(node)] = new_symbol
                return new_symbol
            if not str(node) in constants.keys():
                constant_count += 1
                new_symbol = symbols(f"c_{constant_count}")
                constants[str(node)] = new_symbol
            else:
                new_symbol = constants[str(node)]
            return new_symbol
        else:
            return node

    def recursive_replace(node):
        if node.is_Function or node.is_Add or node.is_Mul or node.is_Pow:
            return node.func(*[recursive_replace(arg) for arg in node.args])
        return replace_symbols(node)

    new_expression = recursive_replace(sympy_expr)
    return new_expression

unary_minus_to_binary(expr, operator_test)

replace unary minus with binary

Examples:

>>> o = lambda x: x in ['+', '-', '*', '/', '^']
>>> o_ = lambda x : x in ['+', '-', '*', '/', '**']
>>> unary_minus_to_binary('-x_1+x_2', o)
'x_2-x_1'
>>> unary_minus_to_binary('x_1-x_2', o)
'x_1-x_2'
>>> unary_minus_to_binary('x_1+(-x_2+x_3)', o)
'x_1+(x_3-x_2)'
>>> unary_minus_to_binary('-tan(x_1-exp(x_2))', o)
'(0-tan(x_1-exp(x_2)))'
>>> unary_minus_to_binary('-x_2', o)
'(0-x_2)'
>>> unary_minus_to_binary('exp(-x_1)*log(x_2)', o)
'exp((0-x_1))*log(x_2)'
>>> unary_minus_to_binary('(c_1 + x_2)*(-c_2 + x_3)', o)
'(c_1+x_2)*(x_3-c_2)'
>>> unary_minus_to_binary('-(c_1 - x_1)^2', o)
'(0-(c_1-x_1))^2'
>>> unary_minus_to_binary('-(c_1 - x_2)*(x_1 + x_2)', o)
'(0-(c_1-x_2))*(x_1+x_2)'
>>> unary_minus_to_binary('x_1**2 + x_2', o_)
'x_1**2+x_2'
Source code in src/equation_tree/util/conversions.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def unary_minus_to_binary(expr, operator_test):
    """
    replace unary minus with binary

    Examples:
        >>> o = lambda x: x in ['+', '-', '*', '/', '^']
        >>> o_ = lambda x : x in ['+', '-', '*', '/', '**']
        >>> unary_minus_to_binary('-x_1+x_2', o)
        'x_2-x_1'

        >>> unary_minus_to_binary('x_1-x_2', o)
        'x_1-x_2'

        >>> unary_minus_to_binary('x_1+(-x_2+x_3)', o)
        'x_1+(x_3-x_2)'

        >>> unary_minus_to_binary('-tan(x_1-exp(x_2))', o)
        '(0-tan(x_1-exp(x_2)))'

        >>> unary_minus_to_binary('-x_2', o)
        '(0-x_2)'

        >>> unary_minus_to_binary('exp(-x_1)*log(x_2)', o)
        'exp((0-x_1))*log(x_2)'

        >>> unary_minus_to_binary('(c_1 + x_2)*(-c_2 + x_3)', o)
        '(c_1+x_2)*(x_3-c_2)'

        >>> unary_minus_to_binary('-(c_1 - x_1)^2', o)
        '(0-(c_1-x_1))^2'

        >>> unary_minus_to_binary('-(c_1 - x_2)*(x_1 + x_2)', o)
        '(0-(c_1-x_2))*(x_1+x_2)'

        >>> unary_minus_to_binary('x_1**2 + x_2', o_)
        'x_1**2+x_2'

    """
    _temp = _find_unary("-", str(expr), operator_test)
    while "#" in _temp:
        _temp = _move_placeholder(_temp, operator_test)
    _temp = _find_unary("+", _temp, operator_test)
    _temp = __remove_character_from_string(_temp, "#")
    _temp = _find_unary("-", _temp, operator_test)
    while "#" in _temp:
        _temp = _replace_with_zero_minus(_temp, operator_test)
    return _temp