# convert .solve output to function

I have a function of two variables and would like to solve for one of the variables and make that a function, eg

t=var('t',domain='real')
z,w=var('z,w')
p(z,w)=5*z^2*w + 3*z*w +2*z
solns=p(e^(I*t),w).solve(w)
solns


and then somehow have the output of that:

[w == -2/(5*e^(I*t) + 3)]


become a function

f(t)=-2/(5*e^(I*t)+3)


Does anyone know a good way to do this? The main issue I'm having is the w== part.

edit retag close merge delete

Sort by » oldest newest most voted

Most of the work has been done with the code in the question.

Only a little step remains.

After defining

sage: t = var('t', domain='real')
sage: z, w = var('z w')
sage: p(z, w) = 5*z^2*w + 3*z*w + 2*z
sage: solns = p(e^(I*t), w).solve(w)


the name solns refers to as a list of solutions

sage: solns
[w == -2/(5*e^(I*t) + 3)]


which in this case has only one element, accessed as

sage: solns
w == -2/(5*e^(I*t) + 3)


and whose left hand side and right hand side are

sage: solns.lhs()
w
sage: solns.rhs()
-2/(5*e^(I*t) + 3)


so we can define

sage: f(t) = solns.rhs()
sage: f(t)
-2/(5*e^(I*t) + 3)

more

Thank you! This is much better than what I was trying to do which involved translating to a string and back.