Ask Your Question
2

Substitute list of expressions

asked 2011-11-30 02:50:02 +0100

Aleksey_R gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
8

answered 2011-11-30 03:55:00 +0100

Daniel Krenn gravatar image

The nice solution:

sage: model.substitute_expression(*sol)
x |--> -2.0000000000000031*x^3 + 14.500000000000021*x^2 - 31.500000000000046*x + 23.000000000000028
edit flag offensive delete link more

Comments

Thanks! That's really nice solution!

Aleksey_R gravatar imageAleksey_R ( 2011-12-02 16:38:16 +0100 )edit
0

answered 2011-11-30 03:46:06 +0100

Daniel Krenn gravatar image

updated 2011-11-30 03:48:00 +0100

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-11-30 02:50:02 +0100

Seen: 1,003 times

Last updated: Nov 30 '11