ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 23 Feb 2012 17:09:49 -0600Extract solutions from solvehttps://ask.sagemath.org/question/8726/extract-solutions-from-solve/I am new to sage. So, sorry if my question is trivial.
How can I get the values of a solve operation. Say I have
x = var('x');
f = x^2 - 5*x + 6;
z = solve(f = 0, x);
show(z);
gives me something like
[x == 2, x == 3]
How can I take the value 2 or 3 from that?
Also, when I was trying to solve another equation (quiet a long one), I get the solution as
[0 = something something.....]
What does that zero mean?Wed, 22 Feb 2012 04:27:44 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/Answer by kcrisman for <p>I am new to sage. So, sorry if my question is trivial.</p>
<p>How can I get the values of a solve operation. Say I have</p>
<pre><code>x = var('x');
f = x^2 - 5*x + 6;
z = solve(f = 0, x);
show(z);
</code></pre>
<p>gives me something like</p>
<pre><code>[x == 2, x == 3]
</code></pre>
<p>How can I take the value 2 or 3 from that?</p>
<p>Also, when I was trying to solve another equation (quiet a long one), I get the solution as</p>
<pre><code>[0 = something something.....]
</code></pre>
<p>What does that zero mean?</p>
https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13290#post-id-13290> I am new to sage. So, sorry if my question is trivial.
Not at all! Likely you could have found this by doing a little more reconnaissance, but hopefully others in the same boat will now find this instead.
Incidentally, `x` is the only predefined variable, so your first line is not needed. Also, the semicolons are not necessary to suppress output.
I hope this example answers your questions.
sage: f = x^2 - 5*x + 6
sage: z = solve(f,x) # f==0 is implicit
sage: z
[x == 3, x == 2]
sage: z[0]
x == 3
sage: z[1]
x == 2
sage: z[0].rhs()
3
sage: z[0].lhs()
x
Essentially, Sage returns Python [lists](http://docs.python.org/tutorial/introduction.html#lists), and this is how to extract stuff from them. It returns a symbolic equality, and then the `rhs()` method gets the "right hand side".
There is another way to get solutions as Python [dicts](http://docs.python.org/tutorial/datastructures.html#dictionaries), but that is slightly more advanced so maybe someone else can comment on that.Wed, 22 Feb 2012 05:45:00 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13290#post-id-13290Comment by kcrisman for <blockquote>
<p>I am new to sage. So, sorry if my question is trivial.</p>
</blockquote>
<p>Not at all! Likely you could have found this by doing a little more reconnaissance, but hopefully others in the same boat will now find this instead.</p>
<p>Incidentally, <code>x</code> is the only predefined variable, so your first line is not needed. Also, the semicolons are not necessary to suppress output.</p>
<p>I hope this example answers your questions. </p>
<pre><code>sage: f = x^2 - 5*x + 6
sage: z = solve(f,x) # f==0 is implicit
sage: z
[x == 3, x == 2]
sage: z[0]
x == 3
sage: z[1]
x == 2
sage: z[0].rhs()
3
sage: z[0].lhs()
x
</code></pre>
<p>Essentially, Sage returns Python <a href="http://docs.python.org/tutorial/introduction.html#lists">lists</a>, and this is how to extract stuff from them. It returns a symbolic equality, and then the <code>rhs()</code> method gets the "right hand side".</p>
<p>There is another way to get solutions as Python <a href="http://docs.python.org/tutorial/datastructures.html#dictionaries">dicts</a>, but that is slightly more advanced so maybe someone else can comment on that.</p>
https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?comment=20257#post-id-20257No problem! In fact, semicolons *do* have a purpose in Python. Compare two lines `2+2` and `2+3` with `2+2; 2+3` in a notebook cell.Thu, 23 Feb 2012 01:20:03 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?comment=20257#post-id-20257Comment by Rejeesh for <blockquote>
<p>I am new to sage. So, sorry if my question is trivial.</p>
</blockquote>
<p>Not at all! Likely you could have found this by doing a little more reconnaissance, but hopefully others in the same boat will now find this instead.</p>
<p>Incidentally, <code>x</code> is the only predefined variable, so your first line is not needed. Also, the semicolons are not necessary to suppress output.</p>
<p>I hope this example answers your questions. </p>
<pre><code>sage: f = x^2 - 5*x + 6
sage: z = solve(f,x) # f==0 is implicit
sage: z
[x == 3, x == 2]
sage: z[0]
x == 3
sage: z[1]
x == 2
sage: z[0].rhs()
3
sage: z[0].lhs()
x
</code></pre>
<p>Essentially, Sage returns Python <a href="http://docs.python.org/tutorial/introduction.html#lists">lists</a>, and this is how to extract stuff from them. It returns a symbolic equality, and then the <code>rhs()</code> method gets the "right hand side".</p>
<p>There is another way to get solutions as Python <a href="http://docs.python.org/tutorial/datastructures.html#dictionaries">dicts</a>, but that is slightly more advanced so maybe someone else can comment on that.</p>
https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?comment=20261#post-id-20261@kcrisman Thanks a lot for that. Those semicolons come from C (and maybe a bit from Octave/Matlab). I just can't think of a world without semicolons. And like you said, a bit of Google and some docs would have answered me.Wed, 22 Feb 2012 06:21:59 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?comment=20261#post-id-20261Answer by Rejeesh for <p>I am new to sage. So, sorry if my question is trivial.</p>
<p>How can I get the values of a solve operation. Say I have</p>
<pre><code>x = var('x');
f = x^2 - 5*x + 6;
z = solve(f = 0, x);
show(z);
</code></pre>
<p>gives me something like</p>
<pre><code>[x == 2, x == 3]
</code></pre>
<p>How can I take the value 2 or 3 from that?</p>
<p>Also, when I was trying to solve another equation (quiet a long one), I get the solution as</p>
<pre><code>[0 = something something.....]
</code></pre>
<p>What does that zero mean?</p>
https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13291#post-id-13291@kcrisman
Thanks a lot for that. Those semicolons come from C (and maybe a bit from Octave/Matlab). I just can't think of a world without semicolons. And like you said, a bit of Google and some docs would have answered me.Wed, 22 Feb 2012 06:12:56 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13291#post-id-13291Answer by mhampton for <p>I am new to sage. So, sorry if my question is trivial.</p>
<p>How can I get the values of a solve operation. Say I have</p>
<pre><code>x = var('x');
f = x^2 - 5*x + 6;
z = solve(f = 0, x);
show(z);
</code></pre>
<p>gives me something like</p>
<pre><code>[x == 2, x == 3]
</code></pre>
<p>How can I take the value 2 or 3 from that?</p>
<p>Also, when I was trying to solve another equation (quiet a long one), I get the solution as</p>
<pre><code>[0 = something something.....]
</code></pre>
<p>What does that zero mean?</p>
https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13294#post-id-13294Sometimes - especially in more complicated multivariate cases - using the option solution_dict = True is convenient. It doesn't really help here, but for your example:
x = var('x')
f = x^2 - 5*x + 6
z = solve(f, x, solution_dict=True)
for solution in z:
print x.subs(solution)
3
2Thu, 23 Feb 2012 17:09:49 -0600https://ask.sagemath.org/question/8726/extract-solutions-from-solve/?answer=13294#post-id-13294