ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 11 Mar 2019 13:27:03 -0500convert .solve output to functionhttp://ask.sagemath.org/question/45692/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.
Wed, 06 Mar 2019 16:35:42 -0600http://ask.sagemath.org/question/45692/convert-solve-output-to-function/Answer by slelievre for <p>I have a function of two variables and would like to solve for one of the variables and make that a function, eg</p>
<pre><code>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
</code></pre>
<p>and then somehow have the output of that:</p>
<pre><code>[w == -2/(5*e^(I*t) + 3)]
</code></pre>
<p>become a function </p>
<pre><code>f(t)=-2/(5*e^(I*t)+3)
</code></pre>
<p>Does anyone know a good way to do this? The main issue I'm having is the w== part.</p>
http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?answer=45693#post-id-45693Most 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[0]
w == -2/(5*e^(I*t) + 3)
and whose left hand side and right hand side are
sage: solns[0].lhs()
w
sage: solns[0].rhs()
-2/(5*e^(I*t) + 3)
so we can define
sage: f(t) = solns[0].rhs()
sage: f(t)
-2/(5*e^(I*t) + 3)
Wed, 06 Mar 2019 17:47:01 -0600http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?answer=45693#post-id-45693Comment by meresar for <p>Most of the work has been done with the code in the question.</p>
<p>Only a little step remains.</p>
<p>After defining</p>
<pre><code>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)
</code></pre>
<p>the name <code>solns</code> refers to as a list of solutions</p>
<pre><code>sage: solns
[w == -2/(5*e^(I*t) + 3)]
</code></pre>
<p>which in this case has only one element, accessed as</p>
<pre><code>sage: solns[0]
w == -2/(5*e^(I*t) + 3)
</code></pre>
<p>and whose left hand side and right hand side are</p>
<pre><code>sage: solns[0].lhs()
w
sage: solns[0].rhs()
-2/(5*e^(I*t) + 3)
</code></pre>
<p>so we can define</p>
<pre><code>sage: f(t) = solns[0].rhs()
sage: f(t)
-2/(5*e^(I*t) + 3)
</code></pre>
http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?comment=45760#post-id-45760Thank you! This is much better than what I was trying to do which involved translating to a string and back.Mon, 11 Mar 2019 13:27:03 -0500http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?comment=45760#post-id-45760Answer by Juanjo for <p>I have a function of two variables and would like to solve for one of the variables and make that a function, eg</p>
<pre><code>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
</code></pre>
<p>and then somehow have the output of that:</p>
<pre><code>[w == -2/(5*e^(I*t) + 3)]
</code></pre>
<p>become a function </p>
<pre><code>f(t)=-2/(5*e^(I*t)+3)
</code></pre>
<p>Does anyone know a good way to do this? The main issue I'm having is the w== part.</p>
http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?answer=45694#post-id-45694Once you get `solns`, which is a list, extract the first element and take the right hand side with the `.rhs` method to define `f`, that is,
f(t) = solns[0].rhs()
Wed, 06 Mar 2019 17:52:58 -0600http://ask.sagemath.org/question/45692/convert-solve-output-to-function/?answer=45694#post-id-45694