Ask Your Question

Revision history [back]

When you write

sage: x = polygen(ZZ)

you define the variable x as an element of the polynomial ring in one variable over the integers:

sage: x.parent()
Univariate Polynomial Ring in x over Integer Ring

Hence you can define any polynomial in $\mathbb{Z}[x]$ and find its roots as follows:

sage: P = 2*x - 3
sage: P
2*x - 3
sage: P.roots()
[]

As you can see, there is no root in $\mathbb{Z}$, if you want to have the roots in $\mathbb{Q}$:

sage: P.roots(QQ)
[(3/2, 1)]

You can also change the parent of the polynomial to be the rationals:

sage: Q = P.change_ring(QQ)
sage: Q
2*x - 3
sage: Q.parent()
Univariate Polynomial Ring in x over Rational Field
sage: Q.roots()
[(3/2, 1)]

Of course you can define x as being a monomial over the retionals:

sage: x = polygen(QQ)
sage: x.parent()
Univariate Polynomial Ring in x over Rational Field

Concerning complex roots, let me just add that the rings CC and CDF (faster) are floating -point approximation (fast, but inexact), there is also the field of algebraic numbers to work in:

sage: x = polygen(ZZ)
sage: P = x^2-2
sage: P.roots()
[]
sage: P.roots(QQ)
[]
sage: P.roots(CC)
[(-1.41421356237310, 1), (1.41421356237310, 1)]

sage: P.roots(CC)[0][0]^2
2.00000000000000
sage: P.roots(CC)[0][0]^2 == 2
False
sage: P.roots(CC)[0][0]^2 - 2
4.44089209850063e-16

sage: P.roots(QQbar)
[(-1.414213562373095?, 1), (1.414213562373095?, 1)]
sage: P.roots(QQbar)[0][0]^2 == 2
True