24.5 Field $ \mathbf{Q}$ of Rational Numbers

Module: sage.rings.rational_field

Field $ \mathbf{Q}$ of Rational Numbers.

The class RationalField represents the field $ \mathbf{Q}$ of (arbitrary precision) rational numbers. Each rational number is an instance of the class Rational.

Interactively, an instance of RationalField is available as QQ.

sage: QQ
Rational Field

Values of various types can be converted to rational numbers by using the __call__ method of RationalField (that is, by treating QQ as a function).

sage: RealField(9).pi()
3.1
sage: QQ(RealField(9).pi())
22/7
sage: QQ(RealField().pi())
245850922/78256779
sage: QQ(35)
35
sage: QQ('12/347')
12/347
sage: QQ(exp(pi*I))
-1
sage: x = polygen(ZZ)
sage: QQ((3*x)/(4*x))
3/4

TEST:

sage: Q = RationalField()
sage: Q == loads(dumps(Q))
True
sage: RationalField() is RationalField()
True

Module-level Functions

factor( x)

frac( n, d)

is_RationalField( x)

Class: RationalField

class RationalField
The class RationalField represents the field $ \mathbf{Q}$ of rational numbers.
RationalField( self)

We create the rational numbers $ \mathbf{Q}$ , and call a few functions:

sage: Q = RationalField(); Q
Rational Field
sage: Q.characteristic()
0
sage: Q.is_field()
True
sage: Q.zeta()
-1

We next illustrate arithmetic in $ \mathbf{Q}$ .

sage: Q('49/7')
7
sage: type(Q('49/7'))
<type 'sage.rings.rational.Rational'>
sage: a = Q('19/374'); b = Q('17/371'); print a, b
19/374 17/371
sage: a + b
13407/138754
sage: b + a
13407/138754
sage: a * b
19/8162
sage: b * a
19/8162
sage: a - b
691/138754
sage: b - a
-691/138754
sage: a / b
7049/6358
sage: b / a
6358/7049
sage: b < a
True
sage: a < b
False

Next finally illustrate arithmetic with automatic coercion. The types that coerce into the rational field include str, int, long, Integer.

sage: a + Q('17/371')
13407/138754
sage: a * 374
19
sage: 374 * a
19
sage: a/19
1/374
sage: a + 1
393/374

TESTS:

sage: QQ.variable_name()
'x'
sage: QQ.variable_names()
('x',)

Functions: absolute_degree,$ \,$ characteristic,$ \,$ coerce_map_from_impl,$ \,$ completion,$ \,$ complex_embedding,$ \,$ construction,$ \,$ degree,$ \,$ discriminant,$ \,$ embeddings,$ \,$ extension,$ \,$ gen,$ \,$ gens,$ \,$ is_absolute,$ \,$ is_atomic_repr,$ \,$ is_field,$ \,$ is_finite,$ \,$ is_prime_field,$ \,$ is_subring,$ \,$ maximal_order,$ \,$ ngens,$ \,$ number_field,$ \,$ order,$ \,$ random_element,$ \,$ signature,$ \,$ zeta

absolute_degree( self)

sage: QQ.absolute_degree()
1

characteristic( self)

Return 0, since the rational field has characteristic 0.

sage: c = QQ.characteristic(); c
0
sage: parent(c)
Integer Ring

coerce_map_from_impl( self, S)

sage: f = QQ.coerce_map_from(ZZ); f
Natural morphism:
  From: Integer Ring
  To:   Rational Field
sage: f(3)
3
sage: f(3^99) - 3^99
0
sage: f = QQ.coerce_map_from(int); f
Native morphism:
  From: Set of Python objects of type 'int'
  To:   Rational Field
sage: f(44)
44

complex_embedding( self, [prec=53])

Return embedding of the rational numbers into the complex numbers.

sage: QQ.complex_embedding()
Ring morphism:
  From: Rational Field
  To:   Complex Field with 53 bits of precision
  Defn: 1 |--> 1.00000000000000
sage: QQ.complex_embedding(20)
Ring morphism:
  From: Rational Field
  To:   Complex Field with 20 bits of precision
  Defn: 1 |--> 1.0000

degree( self)

sage: QQ.degree()
1

discriminant( self)

Return the discriminant of the field of rational numbers, which is 1.

sage: QQ.discriminant()
1

embeddings( self, K)

Return list of the one embedding of $ \mathbf{Q}$ into $ K$ , if it exists.

sage: QQ.embeddings(QQ)
[Ring Coercion endomorphism of Rational Field]
sage: QQ.embeddings(CyclotomicField(5))
[Ring Coercion morphism:
  From: Rational Field
  To:   Cyclotomic Field of order 5 and degree 4]

$ K$ must have characteristic 0:

sage: QQ.embeddings(GF(3))
Traceback (most recent call last):
...
ValueError: no embeddings of the rational field into K.

extension( self, poly, names, [check=True])

We make a single absolute extension:

sage: K.<a> = QQ.extension(x^3 + 5); K
Number Field in a with defining polynomial x^3 + 5

We make an extension generated by roots of two polynomials:

sage: K.<a,b> = QQ.extension([x^3 + 5, x^2 + 3]); K
Number Field in a with defining polynomial x^3 + 5 over its base field
sage: b^2
-3
sage: a^3
-5

gen( self, [n=0])

sage: QQ.gen()
1

gens( self)

sage: QQ.gens()
(1,)

is_absolute( self)

$ \mathbf{Q}$ is an absolute extension of $ \mathbf{Q}$ .

sage: QQ.is_absolute()
True

is_field( self)

Return True, since the rational field is a field.

sage: QQ.is_field()
True

is_finite( self)

Return False, since the rational field is not finite.

sage: QQ.is_finite()
False

is_prime_field( self)

Return True, since $ \mathbf{Q}$ is a prime field.

sage: QQ.is_prime_field()
True

is_subring( self, K)

Return True if $ \mathbf{Q}$ is a subring of $ K$ .

We are only able to determine this in some cases, e.g., when $ K$ is a field or of positive characteristic.

sage: QQ.is_subring(QQ)
True
sage: QQ.is_subring(QQ['x'])
True
sage: QQ.is_subring(GF(7))
False
sage: QQ.is_subring(CyclotomicField(7))
True
sage: QQ.is_subring(ZZ)
False
sage: QQ.is_subring(Frac(ZZ))
True

maximal_order( self)

Return the maximal order of the rational numbers, i.e., the ring $ \mathbf{Z}$ of integers.

sage: QQ.maximal_order()
Integer Ring
sage: QQ.ring_of_integers ()
Integer Ring

ngens( self)

sage: QQ.ngens()
1

number_field( self)

Return the number field associated to $ \mathbf{Q}$ . Since $ \mathbf{Q}$ is a number field, this just returns $ \mathbf{Q}$ again.

sage: QQ.number_field() is QQ
True

order( self)

sage: QQ.order()
+Infinity

random_element( self, [num_bound=None], [den_bound=None], [distribution=None])

sage: QQ.random_element(10,10) # random output
-5/3

signature( self)

Return the signature of the rational field, which is (1,0), since there are 1 real and no complex embeddings.

sage: QQ.signature()
(1, 0)

zeta( self, [n=2])

Return a root of unity in self.

Input:

n
- integer (default: 2) order of the root of unity

sage: QQ.zeta() 
-1
sage: QQ.zeta(2) 
-1
sage: QQ.zeta(1) 
1
sage: QQ.zeta(3) 
Traceback (most recent call last):
...
ValueError: no n-th root of unity in rational field

Special Functions: __call__,$ \,$ __init__,$ \,$ __iter__,$ \,$ __len__,$ \,$ _an_element_impl,$ \,$ _coerce_impl,$ \,$ _gap_init_,$ \,$ _is_valid_homomorphism_,$ \,$ _latex_,$ \,$ _magma_init_,$ \,$ _repr_

__call__( self, x, [base=0])

Coerce $ x$ into the field of rational numbers.

sage: a = long(901824309821093821093812093810928309183091832091)
sage: b = QQ(a); b
901824309821093821093812093810928309183091832091
sage: QQ(b)
901824309821093821093812093810928309183091832091
sage: QQ(int(93820984323))
93820984323
sage: QQ(ZZ(901824309821093821093812093810928309183091832091))
901824309821093821093812093810928309183091832091
sage: QQ('-930482/9320842317')
-930482/9320842317
sage: QQ((-930482, 9320842317))
-930482/9320842317
sage: QQ([9320842317])
9320842317
sage: QQ(pari(39029384023840928309482842098430284398243982394))
39029384023840928309482842098430284398243982394
sage: QQ('sage')
Traceback (most recent call last):
...
TypeError: unable to convert sage to a rational

Coercion from the reals to the rational is done by default using continued fractions.

sage: QQ(RR(3929329/32))
3929329/32
sage: QQ(-RR(3929329/32))
-3929329/32
sage: QQ(RR(1/7)) - 1/7
0

If you specify an optional second base argument, then the string representation of the float is used.

sage: QQ(23.2, 2)
6530219459687219/281474976710656
sage: 6530219459687219.0/281474976710656
23.199999999999999
sage: a = 23.2; a
23.2000000000000
sage: QQ(a, 10)
116/5

Here's a nice example involving elliptic curves:

sage: E = EllipticCurve('11a')
sage: L = E.lseries().at1(300)[0]; L
0.253841860855911
sage: O = E.period_lattice().omega(); O
1.2692093042795534216887946167545473052194922418306086679671369212304083386
13     # 32-bit
1.2692093042795534216887946167545473052194922418306086679671369212304083386
127777226903623059215126073116452962783212874372817003284768439764927140105
7075        # 64-bit
sage: t = L/O; t
0.200000000000000
sage: QQ(RealField(45)(t))
1/5

Elements from the extended rational field can be coerced back into the rational field.

sage: E = ExtendedRationalField
sage: QQ(E(2))
2
sage: type(_)
<type 'sage.rings.rational.Rational'>

__iter__( self)

Creates an iterator that generates the rational numbers without repetition. It uses the sequence defined by $ a_0=0$ and $ a_{n+1}=\frac{1}{2\lfloor a_n\rfloor+1-a_n}$ and generates the sequence

$\displaystyle a_0,a_1,-a_1,a_2,-a_2,\ldots$

This example creates a list consisting of the first 10 terms generated by this function.

sage: import itertools
sage: [a for a in itertools.islice(Rationals(),10)]
[0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3/2]

NOTES: A proof of the correctness of this formula is attributed to Sam Vandervelde and Don Zagier [A002487], but a better reference for the origin of this formula would be welcome.

REFERENCES: [A002487] Sloane's OLEIS, http://www.research.att.com/ njas/sequences/A002487

Author: - Nils Bruin (2007-02-20)

_gap_init_( self)

sage: gap(QQ)                     
Rationals

_magma_init_( self)

sage: magma(QQ)                       # optional
Rational Field

See About this document... for information on suggesting changes.