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.Wed, 11 May 2016 20:39:08 +0200Solving polynomial equations with Groebner basis in $\mathbb{R}$https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/ I am trying to solve the following system of polynomial equations:
$$x^2+y^2+z^2=4\\
x^2+2y^2=5\\
xz=1$$
I used the following command to get a Groebner basis
P.<x,y,z> = PolynomialRing(QQ,order='lex')
PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
I = ideal(PList)
B = I.groebner_basis(); B
[x + 2*z^3 - 3*z, y^2 - z^2 - 1, z^4 - 3/2*z^2 + 1/2]
Now I want to use B.subs() to plug the solutions for $z$ to solve for $x,y$. It worked well for $z=\pm 1$.
B.subs(z=1)
[x - 1, y^2 - 2, 0]
but not for the $1/\sqrt{2}$ since this is in polynomial ring $\mathbb{Q}[x,y,z]$. If I change the original QQ to RR, the numbers become all decimals.
B.subs(z=1/sqrt(2))
[x - 1.41421356237310, y^2 - 1.50000000000000, 0]
How can I still get exact solutions when using RR? I know I can compute this example by hands, but I'd like to know how to use Sage to solve this kind of problems.
Thank you for any help!
Wed, 11 May 2016 12:44:15 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/Answer by vdelecroix for <p>I am trying to solve the following system of polynomial equations:
$$x^2+y^2+z^2=4\
x^2+2y^2=5\
xz=1$$</p>
<p>I used the following command to get a Groebner basis</p>
<pre><code>P.<x,y,z> = PolynomialRing(QQ,order='lex')
PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
I = ideal(PList)
B = I.groebner_basis(); B
[x + 2*z^3 - 3*z, y^2 - z^2 - 1, z^4 - 3/2*z^2 + 1/2]
</code></pre>
<p>Now I want to use B.subs() to plug the solutions for $z$ to solve for $x,y$. It worked well for $z=\pm 1$.</p>
<pre><code>B.subs(z=1)
[x - 1, y^2 - 2, 0]
</code></pre>
<p>but not for the $1/\sqrt{2}$ since this is in polynomial ring $\mathbb{Q}[x,y,z]$. If I change the original QQ to RR, the numbers become all decimals. </p>
<pre><code>B.subs(z=1/sqrt(2))
[x - 1.41421356237310, y^2 - 1.50000000000000, 0]
</code></pre>
<p>How can I still get exact solutions when using RR? I know I can compute this example by hands, but I'd like to know how to use Sage to solve this kind of problems. </p>
<p>Thank you for any help!</p>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?answer=33367#post-id-33367You should not use RR which stands for floating point approximations of real numbers.
The simples to get exact solutions is to use QQbar (= algebraic closure of QQ)
sage: sqrt2 = QQbar(2) ^ (1/2)
sage: sqrt2
1.414213562373095?
sage: sqrt2.n(100)
sage: sqrt2.minpoly()
x^2 - 2
But mixing it with groebner bases I got
sage: B.subs(z=QQbar(2).sqrt())
Traceback (most recent call last):
...
ValueError: Cannot coerce irrational Algebraic Real 1.414213562373095? to Rational
Wed, 11 May 2016 14:55:18 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?answer=33367#post-id-33367Comment by KittyL for <p>You should not use RR which stands for floating point approximations of real numbers.</p>
<p>The simples to get exact solutions is to use QQbar (= algebraic closure of QQ)</p>
<pre><code>sage: sqrt2 = QQbar(2) ^ (1/2)
sage: sqrt2
1.414213562373095?
sage: sqrt2.n(100)
sage: sqrt2.minpoly()
x^2 - 2
</code></pre>
<p>But mixing it with groebner bases I got</p>
<pre><code>sage: B.subs(z=QQbar(2).sqrt())
Traceback (most recent call last):
...
ValueError: Cannot coerce irrational Algebraic Real 1.414213562373095? to Rational
</code></pre>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33375#post-id-33375Thank you for your reply! I can actually use $z=QQbar(2)^{1/2}$. But I wish they would give the answer $\sqrt{2}$.Wed, 11 May 2016 17:50:21 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33375#post-id-33375Answer by tmonteil for <p>I am trying to solve the following system of polynomial equations:
$$x^2+y^2+z^2=4\
x^2+2y^2=5\
xz=1$$</p>
<p>I used the following command to get a Groebner basis</p>
<pre><code>P.<x,y,z> = PolynomialRing(QQ,order='lex')
PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
I = ideal(PList)
B = I.groebner_basis(); B
[x + 2*z^3 - 3*z, y^2 - z^2 - 1, z^4 - 3/2*z^2 + 1/2]
</code></pre>
<p>Now I want to use B.subs() to plug the solutions for $z$ to solve for $x,y$. It worked well for $z=\pm 1$.</p>
<pre><code>B.subs(z=1)
[x - 1, y^2 - 2, 0]
</code></pre>
<p>but not for the $1/\sqrt{2}$ since this is in polynomial ring $\mathbb{Q}[x,y,z]$. If I change the original QQ to RR, the numbers become all decimals. </p>
<pre><code>B.subs(z=1/sqrt(2))
[x - 1.41421356237310, y^2 - 1.50000000000000, 0]
</code></pre>
<p>How can I still get exact solutions when using RR? I know I can compute this example by hands, but I'd like to know how to use Sage to solve this kind of problems. </p>
<p>Thank you for any help!</p>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?answer=33371#post-id-33371You can change the ring over which your polynomial ring is defined, to be the field of algebraic numbers (note the `QQbar` instead of `QQ` in the first line):
sage: P.<x,y,z> = PolynomialRing(QQbar, order='lex')
sage: PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
sage: I = ideal(PList)
sage: B = I.groebner_basis(); B
verbose 0 (3369: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
[x + 2*z^3 + (-3)*z, y^2 - z^2 - 1, z^4 + (-3/2)*z^2 + 1/2]
Then:
sage: B.subs(z=1)
[x - 1, y^2 - 2, 0]
sage: B.subs(z=1/sqrt(2))
[x - 1.414213562373095?, y^2 - 3/2, 0]
Wed, 11 May 2016 16:51:27 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?answer=33371#post-id-33371Comment by KittyL for <p>You can change the ring over which your polynomial ring is defined, to be the field of algebraic numbers (note the <code>QQbar</code> instead of <code>QQ</code> in the first line):</p>
<pre><code>sage: P.<x,y,z> = PolynomialRing(QQbar, order='lex')
sage: PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
sage: I = ideal(PList)
sage: B = I.groebner_basis(); B
verbose 0 (3369: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
[x + 2*z^3 + (-3)*z, y^2 - z^2 - 1, z^4 + (-3/2)*z^2 + 1/2]
</code></pre>
<p>Then:</p>
<pre><code>sage: B.subs(z=1)
[x - 1, y^2 - 2, 0]
sage: B.subs(z=1/sqrt(2))
[x - 1.414213562373095?, y^2 - 3/2, 0]
</code></pre>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33374#post-id-33374Thank you for your reply! That looks much better! Is there a way to get $\sqrt{2}$ instead of $x-1.414....$? Sorry I don't have enough points to upvote.Wed, 11 May 2016 17:48:19 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33374#post-id-33374Comment by KittyL for <p>You can change the ring over which your polynomial ring is defined, to be the field of algebraic numbers (note the <code>QQbar</code> instead of <code>QQ</code> in the first line):</p>
<pre><code>sage: P.<x,y,z> = PolynomialRing(QQbar, order='lex')
sage: PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
sage: I = ideal(PList)
sage: B = I.groebner_basis(); B
verbose 0 (3369: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
[x + 2*z^3 + (-3)*z, y^2 - z^2 - 1, z^4 + (-3/2)*z^2 + 1/2]
</code></pre>
<p>Then:</p>
<pre><code>sage: B.subs(z=1)
[x - 1, y^2 - 2, 0]
sage: B.subs(z=1/sqrt(2))
[x - 1.414213562373095?, y^2 - 3/2, 0]
</code></pre>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33377#post-id-33377Thank you! That is very helpful!Wed, 11 May 2016 20:39:08 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33377#post-id-33377Comment by tmonteil for <p>You can change the ring over which your polynomial ring is defined, to be the field of algebraic numbers (note the <code>QQbar</code> instead of <code>QQ</code> in the first line):</p>
<pre><code>sage: P.<x,y,z> = PolynomialRing(QQbar, order='lex')
sage: PList = [x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1]
sage: I = ideal(PList)
sage: B = I.groebner_basis(); B
verbose 0 (3369: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation.
[x + 2*z^3 + (-3)*z, y^2 - z^2 - 1, z^4 + (-3/2)*z^2 + 1/2]
</code></pre>
<p>Then:</p>
<pre><code>sage: B.subs(z=1)
[x - 1, y^2 - 2, 0]
sage: B.subs(z=1/sqrt(2))
[x - 1.414213562373095?, y^2 - 3/2, 0]
</code></pre>
https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33376#post-id-33376Note that `1.414213562373095?` is only a screen representation, but it is a genuine algebraic number, not a floating-point approximation. You can see this because of the question mark at the end of the number. You can check it as follows:
sage: c = B.subs(z=1/sqrt(2))[0].constant_coefficient()
sage: c
-1.414213562373095?
sage: c.parent()
Algebraic Field
sage: c^2 == 2
True
sage: c.minpoly()
x^2 - 2
Not every algebraic number has a representation with radicals such as "sqrt(2)", but for the algebraic numbers that do, there are plans to implement such a representation, but it is not done yet.Wed, 11 May 2016 18:29:51 +0200https://ask.sagemath.org/question/33364/solving-polynomial-equations-with-groebner-basis-in-mathbbr/?comment=33376#post-id-33376