# Substitute list of expressions

How is it possible to substitute list of expressions?

For example:

sage: y=[4.0,2.0,5.0,1.0]

sage: x=[1.0,2.0,3.0,4.0]

sage: xy=zip(x,y)

sage: var('a,b,c,d,x')

(a, b, c, d, x)

sage: model(x)=ax^3+bx^2+c*x+d

sage: sol=find_fit(xy,model)

sage: sol

[a == -2.0000000000000031, b == 14.500000000000021, c == -31.500000000000046, d == 23.000000000000028]

Now sol is a list of expressions. How can I substitute it into my model? The only way I found is:

sage: func=model.substitute_expression(sol[0],sol[1],sol[2],sol[3])(x)

sage: func

-2.0000000000000031x^3 + 14.500000000000021x^2 - 31.500000000000046*x + 23.000000000000028

However, the expression like "sol[0],...,sol[3]" seems to be not a good style...

Best regards, Aleksey.

edit retag close merge delete

Sort by ยป oldest newest most voted

The nice solution:

sage: model.substitute_expression(*sol)
x |--> -2.0000000000000031*x^3 + 14.500000000000021*x^2 - 31.500000000000046*x + 23.000000000000028

more

Thanks! That's really nice solution!

( 2011-12-02 16:38:16 +0200 )edit

The following works (although, there might be a nicer solution):

sage: m = {}
sage: for eq in sol:
....:     m[eq.lhs()]=eq.rhs()
sage: model.subs(m)
x |--> -2.0000000000000031*x^3 + 14.500000000000021*x^2 - 31.500000000000046*x + 23.000000000000028

more