Combining numpy with sympy
numpy, python, sympy
Solution
I can propose you two solutions.
Firstly, there is `DeferedVector` that was created for use with `lambdify`:
In [1]: from sympy.matrices import DeferredVector
In [2]: v = DeferredVector('v')
In [3]: func = lambdify(v, Matrix([v[1], 2*v[2]]))
In [4]: func(np.array([10,20,30]))
Out[4]:
[[20]
[60]]
However lambdify does too much magic for my taste.
Another option is to use the `.subs` method:
In [11]: x1, x2, x3 = symbols('x1:4')
In [12]: m = Matrix([x2,2*x1,x3/2])
In [13]: m.subs({x1:10, x2:20, x3:30})
Out[13]:
⎡20⎤
⎢ ⎥
⎢20⎥
⎢ ⎥
⎣15⎦
You can create the dictionary for the substitution like that:
`dict(zip(symbols('x1:4'), your_value_array))`.
Do not forget that all the return objects are sympy matrices. To convert them to numpy arrays just use `np.array(the_matrix_in_question)` and do not forget to specify the `dtype`, otherwise it will default to `dtype=object`.
Problem
I have a the following code: ``` p = classp(); for i in range(1,10): x = numpy.array([[2],[4],[5]]) print p.update(x) class classp: def __init__(self): self.mymodel = array([2*x[1]], [3*x[0]], [x[2]]); def update(self, x): return self.mymodel #replace x(0)...x(1) with the given parameter ``` My question is related the code above, I would like to define a model using sympy if it's possible, afterwards in the update function replace the sympy variables with the x values. Is it possible? How can I do that?