Use `solve`output to create a function

asked 2017-11-26 14:25:48 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi there,

I want to use the ouptut of solve to create a function from it. My system depends on some parameter, say $a$, it is a tiny linear system $2\times 2$, and I know there is always a solution. The right-hand term also contains some $a$ into it. What I want to do is to use the first and the second component to buy functions, calle them x1(a) and x2(a) that I will call later. I tried something like

var("v1, v2, a")

Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])

x1(a) = Sols[0][0].rhs()

x2(a) = Sols[0][1].rhs()

This gives the expected expressions for x1 and x2 but I cannot use these to do further computations, for instance, using x1(1) wont evaluate the expression when $a=1$, as I would expect.

The very precise example above is not what I actually want to handle, my actual system is actually more complex.

Many thanks!

edit retag flag offensive close merge delete

Comments

Works for me on Sage 8.1.rc2:

sage: var("v1, v2, a")
....: Sols = solve([v1+a*v2 == a, -a*v1 + v2 ==0],[v1,v2])
....: x1(a) = Sols[0][0].rhs()
....: x2(a) = Sols[0][1].rhs()
(v1, v2, a)
sage: x1(2)
2/5
sage: x1(1)
1/2
tmonteil gravatar imagetmonteil ( 2017-11-26 16:03:15 -0600 )edit

Works ditto for me on Sage 8.0.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2017-11-27 02:50:26 -0600 )edit