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?
I can propose you two solutions.
Firstly, there is
DeferedVectorthat was created for use withlambdify:However lambdify does too much magic for my taste.
Another option is to use the
.subsmethod: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 thedtype, otherwise it will default todtype=object.