# why don't sage return exact value in some functions

Look at the following code:

realpoly.<z> = PolynomialRing(RR)
factor(z^2-2)


it returns:

(z - 1.41421356237310) * (z + 1.41421356237310)


why don't sage return

(z-sqrt(2))*(z+sqrt(2))


and how can i make it to do that?

edit retag close merge delete

Sort by » oldest newest most voted

First, you should notice that the factorization is done relative to the ring you considered. For example, since sqrt(2) is not integer nor rational, you will see that z^2-2 s already factorized in ZZ or QQ:

sage: integerpoly.<z> = PolynomialRing(QQ)
sage: factor(z^2-2)
z^2 - 2
sage: rationalpoly.<z> = PolynomialRing(ZZ)
sage: factor(z^2-2)
z^2 - 2


Second, RR is a representation of real numbers by floating point approximation, hence, the factorization will be done here:

sage: realpoly.<z> = PolynomialRing(RR)
sage: factor(z^2-2)
(z - 1.41421356237310) * (z + 1.41421356237310)
sage: sol = z - factor(z^2-2)[0][0]
sage: sol
1.41421356237310
sage: sol in RR
True
sage: sol^2
2.00000000000000
sage: sol^2 == 2
False


The last equality is False because there is some very small rounding issue that make sol^2 not exactly equalt to 2 (but very close):

sage: RR(sol^2).exact_rational()
4503599627370497/2251799813685248


So, if you want to deal with exact values, you have to use exact rings. In your case, the appropriate one is the field of real algebraic numbers AA:

sage: algebraicpoly.<z> = PolynomialRing(AA)
sage: factor(z^2-2)
(z - 1.414213562373095?) * (z + 1.414213562373095?)
sage: sol = z - factor(z^2-2)[0][0]
sage: sol^2 == 2
True
sage: AA(sol)
1.414213562373095?


Here AA(sol) is a genuine (exact) algebraic number, the floating point approximation is only a way to show it on your screen, this is indicated by the ? at the end of the representation.

There are plans to use some Galois theory to represent some algebraic numbers with radicals (to see sqrt(2) instead of 1.414213562373095?). But it is not done yet, see this trac ticket.

more

The first thing to realize is that the form the factorization should take depends on the base field for the polynomial. The classical example is the polynomial x^2+1. It can be factorized as (x+i)(x-i) but only over the complex numbers. It seems that most of the symbolic mathematics software chooses to work over the real numbers and does not factorize x^2+1. This is of course an arbitrary choice and is not what Sage is doing. This is one reason why you must specify a base field when defining a polynomial.

In your case, the answer is not exact because the field RR is not exact. Here's what the documentation for RR (obtainable by typing RR?) says:

An approximation to the field of real numbers using floating point numbers with any specified precision. Answers derived from calculations in this approximation may differ from what they would be if those calculations were performed in the true field of real numbers. This is due to the rounding errors inherent to finite precision calculations.

Instead of using RR, you could use the algebraic numbers QQbar.

realpoly.<z> = PolynomialRing(QQbar)
(z - 1.414213562373095?) * (z + 1.414213562373095?)


Notice the question mark in the decimal expansion. It indicates that the expansion continues but since it may be very long or infinite can not be fully displayed.

I'm not completely sure how to make Sage show the output you would like to get, but of course this is only going to work in simple situations where the roots of the polynomials can be written using radicals. The vast majority of algebraic numbers are not of this type.

Maybe Sage should have a field of numbers which can be written using radicals, then you could factor your polynomial over that field which, it seems to me, is what you would like to get. It seems to me like this might be a good field to have, but it doesn't seem to be implemented at this point.

more

so is it possible to get a (z-sqrt(2))*(z+sqrt(2))?

( 2015-01-14 06:27:28 +0200 )edit

@Chong Wang: Here you are in the situation where you already know the answer to a problem and you want the computer to show it to you in the exact form you had in mind. This rarely works and most often leads to frustration. A way out of frustration is to understand the reasons why the computer produced the answer you don't like. In the factorization above \sqrt(2) and 1.414213562373095? are equally valid names for the same number except that the second one is more general. The first one only applies for the rare numbers which can be written in terms of radicals.

( 2015-01-14 06:50:16 +0200 )edit