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, 21 Apr 2023 17:57:46 +0200How to work with polynomial rings with real or complex coefficientshttps://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/I'm trying to learn more basics in Sage, and I'm struggling with polynomial rings with real and complex coefficients. Many of the basic functions seem to return errors from the Singular or Pari implementation, and I wonder if there is a simple workaround I'm missing. Here is an example:
P.<x,y> = PolynomialRing(RealField(), 2)
I = P.ideal(x^2 - y + 1, -x^2 - y - 1)
I.is_prime()
Returns an error:
TypeError: cannot call Singular function 'primdecSY' with ring parameter of type '<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain_with_category'>'
Running the same code with `QQ` instead of `RealField()` works fine, so I assume the issue is that the Singular function can't work with Sage's implementation of Real numbers. Is there a way around this to do basic operations on polynomial rings with coefficients in numerically approximated fields like the reals and complexes?Thu, 20 Apr 2023 17:06:13 +0200https://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/Comment by Max Alekseyev for <p>I'm trying to learn more basics in Sage, and I'm struggling with polynomial rings with real and complex coefficients. Many of the basic functions seem to return errors from the Singular or Pari implementation, and I wonder if there is a simple workaround I'm missing. Here is an example:</p>
<pre><code>P.<x,y> = PolynomialRing(RealField(), 2)
I = P.ideal(x^2 - y + 1, -x^2 - y - 1)
I.is_prime()
</code></pre>
<p>Returns an error:</p>
<pre><code>TypeError: cannot call Singular function 'primdecSY' with ring parameter of type '<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain_with_category'>'
</code></pre>
<p>Running the same code with <code>QQ</code> instead of <code>RealField()</code> works fine, so I assume the issue is that the Singular function can't work with Sage's implementation of Real numbers. Is there a way around this to do basic operations on polynomial rings with coefficients in numerically approximated fields like the reals and complexes?</p>
https://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/?comment=67746#post-id-67746For ideals to work, one needs exact field - like `AA` or `QQbar`.Fri, 21 Apr 2023 02:38:25 +0200https://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/?comment=67746#post-id-67746Answer by Max Alekseyev for <p>I'm trying to learn more basics in Sage, and I'm struggling with polynomial rings with real and complex coefficients. Many of the basic functions seem to return errors from the Singular or Pari implementation, and I wonder if there is a simple workaround I'm missing. Here is an example:</p>
<pre><code>P.<x,y> = PolynomialRing(RealField(), 2)
I = P.ideal(x^2 - y + 1, -x^2 - y - 1)
I.is_prime()
</code></pre>
<p>Returns an error:</p>
<pre><code>TypeError: cannot call Singular function 'primdecSY' with ring parameter of type '<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain_with_category'>'
</code></pre>
<p>Running the same code with <code>QQ</code> instead of <code>RealField()</code> works fine, so I assume the issue is that the Singular function can't work with Sage's implementation of Real numbers. Is there a way around this to do basic operations on polynomial rings with coefficients in numerically approximated fields like the reals and complexes?</p>
https://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/?answer=67757#post-id-67757The problem with real or complex fields is that their elements are approximate, and operations there are subject to rounding errors, which may accumulate with the number of performed operations.
This is unacceptable situation for the ideals machinery, where accumulating errors destroy basic properties and make it impossible to perform basis operations (e.g., membership testing).
In other words, for ideals to work they **have to** be defined over **exact** fields/rings, which do not allow rounding errors of any kind. Unfortunately, real and complex fields cannot be implemented exactly.
Nevertheless, there are suitable substitutes for real and complex fields:
- rational field `QQ` and rational complex field `QQ[I]`;
- [field of algebraic numbers `QQbar` and its real subfield `AA`](https://doc.sagemath.org/html/en/reference/number_fields/sage/rings/qqbar.html).
We can approximate real and complex numbers with elements of `AA` and `QQbar` using [algdep()](https://doc.sagemath.org/html/en/reference/rings_standard/sage/arith/misc.html#sage.arith.misc.algdep) function, where we can choose *accuracy* in the form of polynomial degree.
Clearly, rational fields `QQ` and `QQ[I]` are subfields of `AA` and `QQbar`, respectively, and appear when we approximate with polynomials of degree 1.
For example, we can approximate $\pi$ with a polynomial root of degree 5 as follows:
eps = 1e-6
pi_aa = AA.polynomial_root(algdep(pi,5),RIF(pi-eps,pi+eps))
print(pi_aa)
print(pi_aa.parent())
which gives
3.141592653589794?
Algebraic Real FieldFri, 21 Apr 2023 17:57:46 +0200https://ask.sagemath.org/question/67740/how-to-work-with-polynomial-rings-with-real-or-complex-coefficients/?answer=67757#post-id-67757