I am getting crazy with this kind of problem:
I have a list of string representing functions (for eval), I need first to replace the variables with generic x[0], x[1],...
.
Some time ago I discovered that I can do this using subs()
. Then I need to generate a list of functions (to define constraints in SciPy minimize).
I am trying something like:
el=[budget.v.values()[0],silly.v.values()[0]] # my list of string/equations
fl=[]
for i in range(len(el)):
def sos(v):
vdict = dict(zip(q.v.values(),v))
return eval(el[i]).subs(vdict)
fl.append(sos)
del sos # this may be unnecessary
The result for fl is:
[<function sos at 0x19a26aa28>, <function sos at 0x199e3f398>]
but the two functions always give the same result (corresponding to the last 'sos' definition). How can I retain different function definitions?