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.Thu, 21 Mar 2019 13:13:02 +0100Converting real numbers to rational formathttps://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/ I have a number of polynomials that are up to 200 terms long which I am copying and pasting into sage. They all have real-valued coefficients. When I try a and compute a Groebner basis I get the error:
AttributeError: 'sage.rings.real_mpfr.RealNumber' object has no attribute 'divides'
Is there a way to automatically approximate these coefficients as exact rationals? I could do it view convoluted string parsing in python but would prefer if there is a built in method in python. As a toy example, the following does not work (note the .1 in the first term)
R = PolynomialRing(QQ,'q0,q1,q2,q3,q4')
I = Ideal(
1522732369974.1*q0^3-8*q1^3+3924472*q0^2*q1-142784271977*q0^2*q2,
1423*q3^2*q4-1396461224121*q3^2+57684122*q0*q1*q2+q0*q1*q3+294*q3*q4^2
)
B = I.groebner_basis(); B
While if I represent the value as a fraction everything works fine.
R = PolynomialRing(QQ,'q0,q1,q2,q3,q4')
I = Ideal(
15227323699741/10*q0^3-8*q1^3+3924472*q0^2*q1-142784271977*q0^2*q2,
1423*q3^2*q4-1396461224121*q3^2+57684122*q0*q1*q2+q0*q1*q3+294*q3*q4^2
)
B = I.groebner_basis(); B
My actual use case has many terms, all of which have many more decimal places that this example so manually adjusting things would take a substantial amount of time. Tue, 19 Mar 2019 16:06:15 +0100https://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/Answer by rburing for <p>I have a number of polynomials that are up to 200 terms long which I am copying and pasting into sage. They all have real-valued coefficients. When I try a and compute a Groebner basis I get the error: </p>
<pre><code>AttributeError: 'sage.rings.real_mpfr.RealNumber' object has no attribute 'divides'
</code></pre>
<p>Is there a way to automatically approximate these coefficients as exact rationals? I could do it view convoluted string parsing in python but would prefer if there is a built in method in python. As a toy example, the following does not work (note the .1 in the first term)</p>
<pre><code>R = PolynomialRing(QQ,'q0,q1,q2,q3,q4')
I = Ideal(
1522732369974.1*q0^3-8*q1^3+3924472*q0^2*q1-142784271977*q0^2*q2,
1423*q3^2*q4-1396461224121*q3^2+57684122*q0*q1*q2+q0*q1*q3+294*q3*q4^2
)
B = I.groebner_basis(); B
</code></pre>
<p>While if I represent the value as a fraction everything works fine. </p>
<pre><code>R = PolynomialRing(QQ,'q0,q1,q2,q3,q4')
I = Ideal(
15227323699741/10*q0^3-8*q1^3+3924472*q0^2*q1-142784271977*q0^2*q2,
1423*q3^2*q4-1396461224121*q3^2+57684122*q0*q1*q2+q0*q1*q3+294*q3*q4^2
)
B = I.groebner_basis(); B
</code></pre>
<p>My actual use case has many terms, all of which have many more decimal places that this example so manually adjusting things would take a substantial amount of time. </p>
https://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/?answer=45842#post-id-45842Write `R.ideal(` instead of `Ideal(` which tries to guess the ring that you want (and gets it wrong here).Tue, 19 Mar 2019 22:47:52 +0100https://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/?answer=45842#post-id-45842Comment by noel for <p>Write <code>R.ideal(</code> instead of <code>Ideal(</code> which tries to guess the ring that you want (and gets it wrong here).</p>
https://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/?comment=45857#post-id-45857Thanks! I ended up just parsing the string in python and converting each number to a fraction there. It wasn't pretty but is solved the issue.Thu, 21 Mar 2019 13:13:02 +0100https://ask.sagemath.org/question/45837/converting-real-numbers-to-rational-format/?comment=45857#post-id-45857