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.Wed, 21 Jan 2015 02:20:03 -0600How to pick out the largest root of an equation?http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/ I tried the following but it didn't work,
p = x^2 - 7*a*x + 5;
a=5;
m = max((p == 0).solve([x]))
Mon, 19 Jan 2015 22:22:48 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/Answer by rws for <p>I tried the following but it didn't work,</p>
<p>p = x^2 - 7<em>a</em>x + 5;
a=5;
m = max((p == 0).solve([x]))</p>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?answer=25571#post-id-25571Use `subs` (wrong solution):
sage: p = x^2 - 7*a*x + 5; p.subs(a=5)
x^2 - 35*x + 5
sage: max(p.solve([x]))
x == -1/2*sqrt(1205) + 35/2
Tue, 20 Jan 2015 03:29:00 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?answer=25571#post-id-25571Comment by tmonteil for <p>Use <code>subs</code> (wrong solution):</p>
<pre><code>sage: p = x^2 - 7*a*x + 5; p.subs(a=5)
x^2 - 35*x + 5
sage: max(p.solve([x]))
x == -1/2*sqrt(1205) + 35/2
</code></pre>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25577#post-id-25577The problem with your approach is that you take the maximum of symbolic expressions, which are equalities (not values of solutions), so the ordering of those is not very clear.Tue, 20 Jan 2015 09:36:26 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25577#post-id-25577Comment by Phoenix for <p>Use <code>subs</code> (wrong solution):</p>
<pre><code>sage: p = x^2 - 7*a*x + 5; p.subs(a=5)
x^2 - 35*x + 5
sage: max(p.solve([x]))
x == -1/2*sqrt(1205) + 35/2
</code></pre>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25573#post-id-25573But this not the largest root...Tue, 20 Jan 2015 07:50:29 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25573#post-id-25573Answer by tmonteil for <p>I tried the following but it didn't work,</p>
<p>p = x^2 - 7<em>a</em>x + 5;
a=5;
m = max((p == 0).solve([x]))</p>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?answer=25576#post-id-25576 First, here is a classical way to get solutions of you equation:
sage: a = 5
sage: p = x^2 - 7*a*x + 5
sage: p.solve(x)
[x == -1/2*sqrt(1205) + 35/2, x == 1/2*sqrt(1205) + 35/2]
So, you have a list of solutions. Each solution is of the form `x == -1/2*sqrt(1205) + 35/2` which is a symbolic expression. You can get the right hand side of such an equality with the `rhs()` method:
sage: [s.rhs() for s in p.solve(x)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
Then, you can take the maximal element of this list:
sage: max([s.rhs() for s in p.solve(x)])
1/2*sqrt(1205) + 35/2
Alternatively, instead of getting solutions as symbolic expressions, you can get them as Python dictionaries:
sage: p.solve(x, solution_dict=True)
[{x: -1/2*sqrt(1205) + 35/2}, {x: 1/2*sqrt(1205) + 35/2}]
So, you can get each solution by looking at the `x` values:
sage: [s[x] for s in p.solve(x, solution_dict=True)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
Then, as before, you can take the maximal element of this list:
sage: max([s[x] for s in p.solve(x, solution_dict=True)])
1/2*sqrt(1205) + 35/2
Tue, 20 Jan 2015 09:35:25 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?answer=25576#post-id-25576Comment by tmonteil for <p>First, here is a classical way to get solutions of you equation:</p>
<pre><code>sage: a = 5
sage: p = x^2 - 7*a*x + 5
sage: p.solve(x)
[x == -1/2*sqrt(1205) + 35/2, x == 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>So, you have a list of solutions. Each solution is of the form <code>x == -1/2*sqrt(1205) + 35/2</code> which is a symbolic expression. You can get the right hand side of such an equality with the <code>rhs()</code> method:</p>
<pre><code>sage: [s.rhs() for s in p.solve(x)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>Then, you can take the maximal element of this list:</p>
<pre><code>sage: max([s.rhs() for s in p.solve(x)])
1/2*sqrt(1205) + 35/2
</code></pre>
<p>Alternatively, instead of getting solutions as symbolic expressions, you can get them as Python dictionaries:</p>
<pre><code>sage: p.solve(x, solution_dict=True)
[{x: -1/2*sqrt(1205) + 35/2}, {x: 1/2*sqrt(1205) + 35/2}]
</code></pre>
<p>So, you can get each solution by looking at the <code>x</code> values:</p>
<pre><code>sage: [s[x] for s in p.solve(x, solution_dict=True)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>Then, as before, you can take the maximal element of this list:</p>
<pre><code>sage: max([s[x] for s in p.solve(x, solution_dict=True)])
1/2*sqrt(1205) + 35/2
</code></pre>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25588#post-id-25588No, since Sage gives an ordering between complex numbers, you will get the maximum for this ordering:
sage: max([1+I, 2*I])
I + 1
A possibility is to assume that `x` is real:
sage: p = x^2 + 1
sage: p.solve(x)
[x == -I, x == I]
sage: assume(x, 'real')
sage: p.solve(x)
[]Wed, 21 Jan 2015 02:20:03 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25588#post-id-25588Comment by phoenix for <p>First, here is a classical way to get solutions of you equation:</p>
<pre><code>sage: a = 5
sage: p = x^2 - 7*a*x + 5
sage: p.solve(x)
[x == -1/2*sqrt(1205) + 35/2, x == 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>So, you have a list of solutions. Each solution is of the form <code>x == -1/2*sqrt(1205) + 35/2</code> which is a symbolic expression. You can get the right hand side of such an equality with the <code>rhs()</code> method:</p>
<pre><code>sage: [s.rhs() for s in p.solve(x)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>Then, you can take the maximal element of this list:</p>
<pre><code>sage: max([s.rhs() for s in p.solve(x)])
1/2*sqrt(1205) + 35/2
</code></pre>
<p>Alternatively, instead of getting solutions as symbolic expressions, you can get them as Python dictionaries:</p>
<pre><code>sage: p.solve(x, solution_dict=True)
[{x: -1/2*sqrt(1205) + 35/2}, {x: 1/2*sqrt(1205) + 35/2}]
</code></pre>
<p>So, you can get each solution by looking at the <code>x</code> values:</p>
<pre><code>sage: [s[x] for s in p.solve(x, solution_dict=True)]
[-1/2*sqrt(1205) + 35/2, 1/2*sqrt(1205) + 35/2]
</code></pre>
<p>Then, as before, you can take the maximal element of this list:</p>
<pre><code>sage: max([s[x] for s in p.solve(x, solution_dict=True)])
1/2*sqrt(1205) + 35/2
</code></pre>
http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25578#post-id-25578If some of the roots turned out to be complex then would "max" throw up some kind of an error message which as an user I can catch ?Tue, 20 Jan 2015 14:53:22 -0600http://ask.sagemath.org/question/25568/how-to-pick-out-the-largest-root-of-an-equation/?comment=25578#post-id-25578