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.Sun, 20 Oct 2013 13:13:40 -0500exact factortinghttps://ask.sagemath.org/question/10638/exact-factorting/How do you get sage to factor into exact values. For instance, I want it to factor x^2-2 and return (x-sqrt(2))*(x+sqrt(2))
However, when I input
realpoly.<x> = PolynomialRing(CC)
factor(x^2-2,x)
Sage returns
(x - 1.41421356237310) * (x + 1.41421356237310)
Any ideas? Sun, 20 Oct 2013 12:43:06 -0500https://ask.sagemath.org/question/10638/exact-factorting/Answer by tmonteil for <p>How do you get sage to factor into exact values. For instance, I want it to factor x^2-2 and return (x-sqrt(2))*(x+sqrt(2))</p>
<p>However, when I input</p>
<p>realpoly.<x> = PolynomialRing(CC)
factor(x^2-2,x)</p>
<p>Sage returns</p>
<p>(x - 1.41421356237310) * (x + 1.41421356237310)</p>
<p>Any ideas? </p>
https://ask.sagemath.org/question/10638/exact-factorting/?answer=15574#post-id-15574The notation `CC` (as well for `RR`) is misleading: `CC` does not corresponds to genuine complex numbers (unlike for `NN`, `ZZ`, `QQ`, `AA`, `QQbar`), but only refers to floating-point approximation of complex numbers.
In your case, if you want exact computations, you should work on the algebraic field `QQbar` instead of `CC`:
sage: QQbar
Algebraic Field
sage: realpoly.<x> = PolynomialRing(QQbar)
sage: factor(x^2-2,x)
(x - 1.414213562373095?) * (x + 1.414213562373095?)
This looks the same, but you should notice the question mark after `1.414213562373095`, which means that `1.414213562373095?` is only a representation of some algebraic number. You can check that it is really `sqrt(2)` as follows:
sage: s2 = x-factor(x^2-2,x)[0][0]
sage: s2
1.414213562373095?
sage: s2 == sqrt(QQbar(2))
True
sage: QQbar(s2).as_number_field_element()
(Number Field in a with defining polynomial y^2 - 2,
a,
Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?)
Sun, 20 Oct 2013 13:13:40 -0500https://ask.sagemath.org/question/10638/exact-factorting/?answer=15574#post-id-15574