ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Jun 2012 04:01:31 +0200use output of solve() without 'var == 'https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/Is there a way to use the values of solve() without having that pesky 'var == ' in front of it?
in h = x^2-9; h
out x |--> x^2 - 9
in sols = solve(h == 0, x); sols
out [x == -3, x == 3]
in sols[0]
out x == -3
in h(sols[0])
out (x == -3)^2 - 9
What the heck is that last line?!
Is there a way to extract the value of sols[0] for use without needing to copy/paste the value?Fri, 01 Jun 2012 00:37:29 +0200https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/Answer by Shashank for <p>Is there a way to use the values of solve() without having that pesky 'var == ' in front of it?</p>
<pre><code>in h = x^2-9; h
out x |--> x^2 - 9
in sols = solve(h == 0, x); sols
out [x == -3, x == 3]
in sols[0]
out x == -3
in h(sols[0])
out (x == -3)^2 - 9
</code></pre>
<p>What the heck is that last line?!</p>
<p>Is there a way to extract the value of sols[0] for use without needing to copy/paste the value?</p>
https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?answer=13634#post-id-13634sols[0] is x==3 not a substitution command.
Use this instead.
h = x^2-9; h
sols = solve(h == 0, x); sols
h.subs(sols[0])Fri, 01 Jun 2012 01:18:17 +0200https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?answer=13634#post-id-13634Comment by daniel.e2718 for <p>sols[0] is x==3 not a substitution command.</p>
<p>Use this instead.</p>
<pre><code>h = x^2-9; h
sols = solve(h == 0, x); sols
h.subs(sols[0])
</code></pre>
https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?comment=19716#post-id-19716Conceptually, not quite what I had in mind. But a useful command to know, regardless.
EDIT: Actually, I came across a problem at GeoGebra forums where someone wanted to make a list of values of functions evaluated at a number. For 'for i in functionlist', where i became the function, i(x) gave a deprecation warning, whereas i.subs(x == evalnum) goes without warning.Fri, 01 Jun 2012 04:01:31 +0200https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?comment=19716#post-id-19716Answer by ndomes for <p>Is there a way to use the values of solve() without having that pesky 'var == ' in front of it?</p>
<pre><code>in h = x^2-9; h
out x |--> x^2 - 9
in sols = solve(h == 0, x); sols
out [x == -3, x == 3]
in sols[0]
out x == -3
in h(sols[0])
out (x == -3)^2 - 9
</code></pre>
<p>What the heck is that last line?!</p>
<p>Is there a way to extract the value of sols[0] for use without needing to copy/paste the value?</p>
https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?answer=13635#post-id-13635An other way:
solve returns a list of equations.
You can get the value with .rhs() ( right hand side )
h(x) = x^2-9
sols = solve(h == 0, x); print sols
x0 = sols[0].rhs()
h(x0)
Further you can use python dictionaries:
h(x) = x^2-9
sols = solve(h(x), x,solution_dict=True); print sols
h(sols[0][x])Fri, 01 Jun 2012 02:03:55 +0200https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?answer=13635#post-id-13635Comment by daniel.e2718 for <p>An other way:</p>
<p>solve returns a list of equations.</p>
<p>You can get the value with .rhs() ( right hand side )</p>
<pre><code>h(x) = x^2-9
sols = solve(h == 0, x); print sols
x0 = sols[0].rhs()
h(x0)
</code></pre>
<p>Further you can use python dictionaries:</p>
<pre><code>h(x) = x^2-9
sols = solve(h(x), x,solution_dict=True); print sols
h(sols[0][x])
</code></pre>
https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?comment=19717#post-id-19717rhs() is exactly what I was lookin for - extracting the value of the solution out of the equation. Now I don't have to hardcode numbers :DFri, 01 Jun 2012 03:56:58 +0200https://ask.sagemath.org/question/9019/use-output-of-solve-without-var/?comment=19717#post-id-19717