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.Mon, 02 Apr 2012 05:10:58 -0500solving polynomial equations with to_poly_solvehttps://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/I have a system of (quadratic) polynomial equations which I'd like to solve with Sage. Here and elsewhere I've seen references to the fact that `to_poly_solve` is faster than just calling `solve` in Sage.
Can I somehow tell `to_poly_solve` that I'd like to work over `RR` or `RDF` instead of whatever symbolic ring it's using by default? The coefficients of the equations I'm using are floats, but my solutions are coming out as huge fractions. I imagine it would be faster if the solver were working over `RR`.
EDIT: Upon further reflection, I suspect this is really a Maxima question, since the entire solving process is carried out there. Nevertheless, I would like to know if such functionality exists in Maxima and whether I can use it from Sage.
If you'd like to know, here are some test equations:
vars = var('a0, a1, a2, R')
eqs = []
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 2*a0 - 1.56835186587759*a1 - 1.79444137578129*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.56835186587759*a0 - 2*a1 - 1.12952287573422*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.79444137578129*a0 - 1.12952287573422*a1 - 2*a2 + 1]
eqs += [a0 + a1 + a2 == 1]
maxima.load('topoly_solver')
meqs = maxima(eqs)
solns = meqs.solve(vars)
For example, here is the value of `a0` and `R` in this case:
sage: solns[0][0]
a0=-127216901572440833725/188266808300071978748
sage: solns[0][3]
R=9*sqrt(149350213112030161)/sqrt(47066702075017994687)
sage: solns[0][0].rhs().parent()
Maxima
Fri, 30 Mar 2012 10:51:55 -0500https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/Answer by Volker Braun for <p>I have a system of (quadratic) polynomial equations which I'd like to solve with Sage. Here and elsewhere I've seen references to the fact that <code>to_poly_solve</code> is faster than just calling <code>solve</code> in Sage. </p>
<p>Can I somehow tell <code>to_poly_solve</code> that I'd like to work over <code>RR</code> or <code>RDF</code> instead of whatever symbolic ring it's using by default? The coefficients of the equations I'm using are floats, but my solutions are coming out as huge fractions. I imagine it would be faster if the solver were working over <code>RR</code>.</p>
<p>EDIT: Upon further reflection, I suspect this is really a Maxima question, since the entire solving process is carried out there. Nevertheless, I would like to know if such functionality exists in Maxima and whether I can use it from Sage.</p>
<p>If you'd like to know, here are some test equations:</p>
<pre><code>vars = var('a0, a1, a2, R')
eqs = []
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 2*a0 - 1.56835186587759*a1 - 1.79444137578129*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.56835186587759*a0 - 2*a1 - 1.12952287573422*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.79444137578129*a0 - 1.12952287573422*a1 - 2*a2 + 1]
eqs += [a0 + a1 + a2 == 1]
maxima.load('topoly_solver')
meqs = maxima(eqs)
solns = meqs.solve(vars)
</code></pre>
<p>For example, here is the value of <code>a0</code> and <code>R</code> in this case:</p>
<pre><code>sage: solns[0][0]
a0=-127216901572440833725/188266808300071978748
sage: solns[0][3]
R=9*sqrt(149350213112030161)/sqrt(47066702075017994687)
sage: solns[0][0].rhs().parent()
Maxima
</code></pre>
https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?answer=13412#post-id-13412You can use the [Groebner basis method](http://ask.sagemath.org/question/403/method-for-solving-a-large-system-of-under#762) that I mentioned previously:
sage: ring = PolynomialRing(RDF, 'a0,a1,a2,a3,R', order='lex')
sage: ideal = ring.ideal([
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 2*a0 - 1.56835186587759*a1 - 1.79444137578129*a2 + 1,
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.56835186587759*a0 - 2*a1 - 1.12952287573422*a2 + 1,
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.79444137578129*a0 - 1.12952287573422*a1 - 2*a2 + 1,
....: a0 + a1 + a2 - 1])
sage: ideal.groebner_basis()
[a0 + 0.675726767968, a1 - 0.750109938467, a2 - 0.9256168295, R^2 - 0.257026038676]
It seems we don't have Singular's support for floating point numbers wrapped, so its not as fast or nice as it could be. But the system of equations is so simple that it doesn't really matter here.Sun, 01 Apr 2012 02:43:47 -0500https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?answer=13412#post-id-13412Comment by kcrisman for <p>You can use the <a href="http://ask.sagemath.org/question/403/method-for-solving-a-large-system-of-under#762">Groebner basis method</a> that I mentioned previously:</p>
<pre><code>sage: ring = PolynomialRing(RDF, 'a0,a1,a2,a3,R', order='lex')
sage: ideal = ring.ideal([
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 2*a0 - 1.56835186587759*a1 - 1.79444137578129*a2 + 1,
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.56835186587759*a0 - 2*a1 - 1.12952287573422*a2 + 1,
....: -R^2 + a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.79444137578129*a0 - 1.12952287573422*a1 - 2*a2 + 1,
....: a0 + a1 + a2 - 1])
sage: ideal.groebner_basis()
[a0 + 0.675726767968, a1 - 0.750109938467, a2 - 0.9256168295, R^2 - 0.257026038676]
</code></pre>
<p>It seems we don't have Singular's support for floating point numbers wrapped, so its not as fast or nice as it could be. But the system of equations is so simple that it doesn't really matter here.</p>
https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?comment=20002#post-id-20002Good, I figured!Mon, 02 Apr 2012 05:10:58 -0500https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?comment=20002#post-id-20002Answer by kcrisman for <p>I have a system of (quadratic) polynomial equations which I'd like to solve with Sage. Here and elsewhere I've seen references to the fact that <code>to_poly_solve</code> is faster than just calling <code>solve</code> in Sage. </p>
<p>Can I somehow tell <code>to_poly_solve</code> that I'd like to work over <code>RR</code> or <code>RDF</code> instead of whatever symbolic ring it's using by default? The coefficients of the equations I'm using are floats, but my solutions are coming out as huge fractions. I imagine it would be faster if the solver were working over <code>RR</code>.</p>
<p>EDIT: Upon further reflection, I suspect this is really a Maxima question, since the entire solving process is carried out there. Nevertheless, I would like to know if such functionality exists in Maxima and whether I can use it from Sage.</p>
<p>If you'd like to know, here are some test equations:</p>
<pre><code>vars = var('a0, a1, a2, R')
eqs = []
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 2*a0 - 1.56835186587759*a1 - 1.79444137578129*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.56835186587759*a0 - 2*a1 - 1.12952287573422*a2 + 1]
eqs += [R^2 == a0^2 + 1.56835186587759*a0*a1 + 1.79444137578129*a0*a2 + a1^2 + 1.12952287573422*a1*a2 + a2^2 - 1.79444137578129*a0 - 1.12952287573422*a1 - 2*a2 + 1]
eqs += [a0 + a1 + a2 == 1]
maxima.load('topoly_solver')
meqs = maxima(eqs)
solns = meqs.solve(vars)
</code></pre>
<p>For example, here is the value of <code>a0</code> and <code>R</code> in this case:</p>
<pre><code>sage: solns[0][0]
a0=-127216901572440833725/188266808300071978748
sage: solns[0][3]
R=9*sqrt(149350213112030161)/sqrt(47066702075017994687)
sage: solns[0][0].rhs().parent()
Maxima
</code></pre>
https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?answer=13410#post-id-13410Use [polynomial rings](http://www.sagemath.org/doc/reference/polynomial_rings.html), I suspect. If there is in fact such functionality with Groebner bases etc. - you are closer to that environment than I am nowadays.
I tried using `eqs[0].roots(ring=RR)` and the like, but this gave a nasty error.
As for the Maxima, the point is exact solutions, so this is what to expect. There is something called `[algsys](http://maxima.sourceforge.net/docs/manual/en/maxima_20.html#algsys)` in Maxima that should be refinable to your needs, though Maxima does this rational substitution thing a lot, even when we ask it not to.Fri, 30 Mar 2012 17:51:21 -0500https://ask.sagemath.org/question/8821/solving-polynomial-equations-with-to_poly_solve/?answer=13410#post-id-13410