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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|