Reputation: 331
I have some symbolic expressions that I have stored in a text file (because they are too large) to read them later in order to find their numerical estimates. So, after I read those expressions (say foo_sym
), I convert those expressions into python function as follow:
import sympy as sym
foo_fn = sym.lambdify(tuple(foo_sym.free_symbols), foo_sym)
Some of the symbolic variables in the expression are arrays, so I setup the vectorized version of the function as:
import numpy as np
foo_fn_vec = np.vectorize(foo_fn)
Now, I find the numerical estimate of the vectorized function as follow:
foo_num = foo_fn_vec(var_1, var_2, var_3, ...., var_12)
In above code, the order of symbolic values in var_1, var_2, var_3, ...., var_12
is consistent with the order of symbols I get through tuple(foo_sym.free_symbols)
. However, every time I restart python and run the above code, I get a ValueError: math domain error
because the order of symbolic variables as given by tuple(foo_sym.free_symbols)
seem to have changed, but the order of values in foo_fn_vec(var_1, var_2, var_3, ...., var_12)
is unchanged.
Is there a method to hold the order of symbols constant in both steps, i.e. while converting to function in sym.lambdify((var_1, var_2, var_3, ...., var_12), foo_sym)
and while finding the numerical value of the function as foo_num = foo_fn_vec(var_1, var_2, var_3, ...., var_12)
?
Upvotes: 3
Views: 356
Reputation: 19115
If you use sym=tuple(ordered(foo_sym.free_symbols))
the symbols retrieved will always be in the same order. You may then want to use these as foo_fn_vec(*sym)
and then sym.lambdify(sym, foo_sym)
.
Upvotes: 1