Field \(\QQ\) of Rational Numbers¶
The class RationalField
represents the field \(\QQ\) 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
AUTHORS:
 Niles Johnson (201008): trac ticket #3893:
random_element()
should pass on*args
and**kwds
.  Travis Scrimshaw (20121018): Added additional docstrings for full coverage.
Removed duplicates of
discriminant()
andsignature()
.  Anna Haensch (201803): Added function
quadratic_defect()

class
sage.rings.rational_field.
RationalField
¶ Bases:
sage.misc.fast_methods.Singleton
,sage.rings.number_field.number_field_base.NumberField
The class
RationalField
represents the field \(\QQ\) of rational numbers.EXAMPLES:
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 sage: QQ(u'5/7') 5/7
Conversion from the reals to the rationals 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.20000000000000 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.2538418608559106843377589233... sage: O = E.period_lattice().omega(); O 1.26920930427955 sage: t = L/O; t 0.200000000000000 sage: QQ(RealField(45)(t)) 1/5

absolute_degree
()¶ Return the absolute degree of \(\QQ\) which is 1.
EXAMPLES:
sage: QQ.absolute_degree() 1

absolute_discriminant
()¶ Return the absolute discriminant, which is 1.
EXAMPLES:
sage: QQ.absolute_discriminant() 1

algebraic_closure
()¶ Return the algebraic closure of self (which is \(\QQbar\)).
EXAMPLES:
sage: QQ.algebraic_closure() Algebraic Field

automorphisms
()¶ Return all Galois automorphisms of
self
.OUTPUT:
 a sequence containing just the identity morphism
EXAMPLES:
sage: QQ.automorphisms() [ Ring endomorphism of Rational Field Defn: 1 > 1 ]

characteristic
()¶ Return 0 since the rational field has characteristic 0.
EXAMPLES:
sage: c = QQ.characteristic(); c 0 sage: parent(c) Integer Ring

class_number
()¶ Return the class number of the field of rational numbers, which is 1.
EXAMPLES:
sage: QQ.class_number() 1

completion
(p, prec, extras={})¶ Return the completion of \(\QQ\) at \(p\).
EXAMPLES:
sage: QQ.completion(infinity, 53) Real Field with 53 bits of precision sage: QQ.completion(5, 15, {'print_mode': 'bars'}) 5adic Field with capped relative precision 15

complex_embedding
(prec=53)¶ Return embedding of the rational numbers into the complex numbers.
EXAMPLES:
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

construction
()¶ Returns a pair
(functor, parent)
such thatfunctor(parent)
returnsself
.This is the construction of \(\QQ\) as the fraction field of \(\ZZ\).
EXAMPLES:
sage: QQ.construction() (FractionField, Integer Ring)

degree
()¶ Return the degree of \(\QQ\) which is 1.
EXAMPLES:
sage: QQ.degree() 1

discriminant
()¶ Return the discriminant of the field of rational numbers, which is 1.
EXAMPLES:
sage: QQ.discriminant() 1

embeddings
(K)¶ Return list of the one embedding of \(\QQ\) into \(K\), if it exists.
EXAMPLES:
sage: QQ.embeddings(QQ) [Identity endomorphism of Rational Field] sage: QQ.embeddings(CyclotomicField(5)) [Coercion map: 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
(poly, names, **kwds)¶ Create a field extension of \(\QQ\).
EXAMPLES:
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
(n=0)¶ Return the
n
th generator of \(\QQ\).There is only the 0th generator which is 1.
EXAMPLES:
sage: QQ.gen() 1

gens
()¶ Return a tuple of generators of \(\QQ\) which is only
(1,)
.EXAMPLES:
sage: QQ.gens() (1,)

is_absolute
()¶ \(\QQ\) is an absolute extension of \(\QQ\).
EXAMPLES:
sage: QQ.is_absolute() True

is_field
(proof=True)¶ Return
True
, since the rational field is a field.EXAMPLES:
sage: QQ.is_field() True

is_finite
()¶ Return
False
, since the rational field is not finite.EXAMPLES:
sage: QQ.is_finite() False

is_prime_field
()¶ Return
True
since \(\QQ\) is a prime field.EXAMPLES:
sage: QQ.is_prime_field() True

maximal_order
()¶ Return the maximal order of the rational numbers, i.e., the ring \(\ZZ\) of integers.
EXAMPLES:
sage: QQ.maximal_order() Integer Ring sage: QQ.ring_of_integers () Integer Ring

ngens
()¶ Return the number of generators of \(\QQ\) which is 1.
EXAMPLES:
sage: QQ.ngens() 1

number_field
()¶ Return the number field associated to \(\QQ\). Since \(\QQ\) is a number field, this just returns \(\QQ\) again.
EXAMPLES:
sage: QQ.number_field() is QQ True

order
()¶ Return the order of \(\QQ\) which is \(\infty\).
EXAMPLES:
sage: QQ.order() +Infinity

places
(all_complex=False, prec=None)¶ Return the collection of all infinite places of self, which in this case is just the embedding of self into \(\RR\).
By default, this returns homomorphisms into
RR
. Ifprec
is not None, we simply return homomorphisms intoRealField(prec)
(orRDF
ifprec=53
).There is an optional flag
all_complex
, which defaults to False. Ifall_complex
is True, then the real embeddings are returned as embeddings into the corresponding complex field.For consistency with nontrivial number fields.
EXAMPLES:
sage: QQ.places() [Ring morphism: From: Rational Field To: Real Field with 53 bits of precision Defn: 1 > 1.00000000000000] sage: QQ.places(prec=53) [Ring morphism: From: Rational Field To: Real Double Field Defn: 1 > 1.0] sage: QQ.places(prec=200, all_complex=True) [Ring morphism: From: Rational Field To: Complex Field with 200 bits of precision Defn: 1 > 1.0000000000000000000000000000000000000000000000000000000000]

power_basis
()¶ Return a power basis for this number field over its base field.
The power basis is always
[1]
for the rational field. This method is defined to make the rational field behave more like a number field.EXAMPLES:
sage: QQ.power_basis() [1]

primes_of_bounded_norm_iter
(B)¶ Iterator yielding all primes less than or equal to \(B\).
INPUT:
B
– a positive integer; upper bound on the primes generated.
OUTPUT:
An iterator over all integer primes less than or equal to \(B\).
Note
This function exists for compatibility with the related number field method, though it returns prime integers, not ideals.
EXAMPLES:
sage: it = QQ.primes_of_bounded_norm_iter(10) sage: list(it) [2, 3, 5, 7] sage: list(QQ.primes_of_bounded_norm_iter(1)) []

quadratic_defect
(a, p, check=True)¶ Return the valuation of the quadratic defect of \(a\) at \(p\).
INPUT:
a
– an element ofself
p
– a prime ideal or a prime numbercheck
– (default:True
); check if \(p\) is prime
REFERENCE:
EXAMPLES:
sage: QQ.quadratic_defect(0, 7) +Infinity sage: QQ.quadratic_defect(5, 7) 0 sage: QQ.quadratic_defect(5, 2) 2 sage: QQ.quadratic_defect(5, 5) 1

random_element
(num_bound=None, den_bound=None, *args, **kwds)¶ Return an random element of \(\QQ\).
Elements are constructed by randomly choosing integers for the numerator and denominator, not necessarily coprime.
INPUT:
num_bound
– a positive integer, specifying a bound on the absolute value of the numerator. If absent, no bound is enforced.den_bound
– a positive integer, specifying a bound on the value of the denominator. If absent, the bound for the numerator will be reused.
Any extra positional or keyword arguments are passed through to
sage.rings.integer_ring.IntegerRing_class.random_element()
.EXAMPLES:
sage: QQ.random_element() 4 sage: QQ.random_element() 0 sage: QQ.random_element() 1/2
In the following example, the resulting numbers range from 5/1 to 5/1 (both inclusive), while the smallest possible positive value is 1/10:
sage: QQ.random_element(5, 10) 2/7
Extra positional or keyword arguments are passed through:
sage: QQ.random_element(distribution='1/n') 0 sage: QQ.random_element(distribution='1/n') 1

range_by_height
(start, end=None)¶ Range function for rational numbers, ordered by height.
Returns a Python generator for the list of rational numbers with heights in
range(start, end)
. Follows the same convention as Python range, seerange?
for details.See also
__iter__()
.EXAMPLES:
All rational numbers with height strictly less than 4:
sage: list(QQ.range_by_height(4)) [0, 1, 1, 1/2, 1/2, 2, 2, 1/3, 1/3, 3, 3, 2/3, 2/3, 3/2, 3/2] sage: [a.height() for a in QQ.range_by_height(4)] [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3]
All rational numbers with height 2:
sage: list(QQ.range_by_height(2, 3)) [1/2, 1/2, 2, 2]
Nonsensical integer arguments will return an empty generator:
sage: list(QQ.range_by_height(3, 3)) [] sage: list(QQ.range_by_height(10, 1)) []
There are no rational numbers with height \(\leq 0\):
sage: list(QQ.range_by_height(10, 1)) []

relative_discriminant
()¶ Return the relative discriminant, which is 1.
EXAMPLES:
sage: QQ.relative_discriminant() 1

residue_field
(p, check=True)¶ Return the residue field of \(\QQ\) at the prime \(p\), for consistency with other number fields.
INPUT:
p
 a prime integer.check
(default True)  if True check the primality of \(p\), else do not.
OUTPUT: The residue field at this prime.
EXAMPLES:
sage: QQ.residue_field(5) Residue field of Integers modulo 5 sage: QQ.residue_field(next_prime(10^9)) Residue field of Integers modulo 1000000007

selmer_group
(S, m, proof=True, orders=False)¶ Compute the group \(\QQ(S,m)\).
INPUT:
S
– a set of primesm
– a positive integerproof
– ignoredorders
(default False) – if True, output two lists, the generators and their orders
OUTPUT:
A list of generators of \(\QQ(S,m)\) (and, optionally, their orders in \(\QQ^\times/(\QQ^\times)^m\)). This is the subgroup of \(\QQ^\times/(\QQ^\times)^m\) consisting of elements \(a\) such that the valuation of \(a\) is divisible by \(m\) at all primes not in \(S\). It is equal to the group of \(S\)units modulo \(m\)th powers. The group \(\QQ(S,m)\) contains the subgroup of those \(a\) such that \(\QQ(\sqrt[m]{a})/\QQ\) is unramified at all primes of \(\QQ\) outside of \(S\), but may contain it properly when not all primes dividing \(m\) are in \(S\).
EXAMPLES:
sage: QQ.selmer_group((), 2) [1] sage: QQ.selmer_group((3,), 2) [1, 3] sage: QQ.selmer_group((5,), 2) [1, 5]
The previous examples show that the group generated by the output may be strictly larger than the ‘true’ Selmer group of elements giving extensions unramified outside \(S\).
When \(m\) is even, \(1\) is a generator of order \(2\):
sage: QQ.selmer_group((2,3,5,7,), 2, orders=True) ([1, 2, 3, 5, 7], [2, 2, 2, 2, 2]) sage: QQ.selmer_group((2,3,5,7,), 3, orders=True) ([2, 3, 5, 7], [3, 3, 3, 3])

selmer_group_iterator
(S, m, proof=True)¶ Return an iterator through elements of the finite group \(\QQ(S,m)\).
INPUT:
S
– a set of primesm
– a positive integerproof
– ignored
OUTPUT:
An iterator yielding the distinct elements of \(\QQ(S,m)\). See the docstring for
selmer_group()
for more information.EXAMPLES:
sage: list(QQ.selmer_group_iterator((), 2)) [1, 1] sage: list(QQ.selmer_group_iterator((2,), 2)) [1, 2, 1, 2] sage: list(QQ.selmer_group_iterator((2,3), 2)) [1, 3, 2, 6, 1, 3, 2, 6] sage: list(QQ.selmer_group_iterator((5,), 2)) [1, 5, 1, 5]

signature
()¶ Return the signature of the rational field, which is \((1,0)\), since there are 1 real and no complex embeddings.
EXAMPLES:
sage: QQ.signature() (1, 0)

some_elements
()¶ Return some elements of \(\QQ\).
See
TestSuite()
for a typical use case.OUTPUT:
An iterator over 100 elements of \(\QQ\).
EXAMPLES:
sage: tuple(QQ.some_elements()) (1/2, 1/2, 2, 2, 0, 1, 1, 42, 2/3, 2/3, 3/2, 3/2, 4/5, 4/5, 5/4, 5/4, 6/7, 6/7, 7/6, 7/6, 8/9, 8/9, 9/8, 9/8, 10/11, 10/11, 11/10, 11/10, 12/13, 12/13, 13/12, 13/12, 14/15, 14/15, 15/14, 15/14, 16/17, 16/17, 17/16, 17/16, 18/19, 18/19, 19/18, 19/18, 20/441, 20/441, 441/20, 441/20, 22/529, 22/529, 529/22, 529/22, 24/625, 24/625, 625/24, 625/24, ...)

valuation
(p)¶ Return the discrete valuation with uniformizer
p
.EXAMPLES:
sage: v = QQ.valuation(3); v 3adic valuation sage: v(1/3) 1

zeta
(n=2)¶ Return a root of unity in
self
.INPUT:
n
– integer (default: 2) order of the root of unity
EXAMPLES:
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 nth root of unity in rational field


sage.rings.rational_field.
frac
(n, d)¶ Return the fraction
n/d
.EXAMPLES:
sage: from sage.rings.rational_field import frac sage: frac(1,2) 1/2

sage.rings.rational_field.
is_RationalField
(x)¶ Check to see if
x
is the rational field.EXAMPLES:
sage: from sage.rings.rational_field import is_RationalField as is_RF sage: is_RF(QQ) True sage: is_RF(ZZ) False