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
>>> from sage.all import *
>>> QQ
Rational Field
QQ

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()                                                             # needs sage.rings.real_mpfr
3.1
sage: QQ(RealField(9).pi())                                                         # needs sage.rings.real_interval_field sage.rings.real_mpfr
22/7
sage: QQ(RealField().pi())                                                          # needs sage.rings.real_interval_field sage.rings.real_mpfr
245850922/78256779
sage: QQ(35)
35
sage: QQ('12/347')
12/347
sage: QQ(exp(pi*I))                                                                 # needs sage.symbolic
-1
sage: x = polygen(ZZ)
sage: QQ((3*x)/(4*x))
3/4
>>> from sage.all import *
>>> RealField(Integer(9)).pi()                                                             # needs sage.rings.real_mpfr
3.1
>>> QQ(RealField(Integer(9)).pi())                                                         # needs sage.rings.real_interval_field sage.rings.real_mpfr
22/7
>>> QQ(RealField().pi())                                                          # needs sage.rings.real_interval_field sage.rings.real_mpfr
245850922/78256779
>>> QQ(Integer(35))
35
>>> QQ('12/347')
12/347
>>> QQ(exp(pi*I))                                                                 # needs sage.symbolic
-1
>>> x = polygen(ZZ)
>>> QQ((Integer(3)*x)/(Integer(4)*x))
3/4
RealField(9).pi()                                                             # needs sage.rings.real_mpfr
QQ(RealField(9).pi())                                                         # needs sage.rings.real_interval_field sage.rings.real_mpfr
QQ(RealField().pi())                                                          # needs sage.rings.real_interval_field sage.rings.real_mpfr
QQ(35)
QQ('12/347')
QQ(exp(pi*I))                                                                 # needs sage.symbolic
x = polygen(ZZ)
QQ((3*x)/(4*x))

AUTHORS:

  • Niles Johnson (2010-08): Issue #3893: random_element() should pass on *args and **kwds.

  • Travis Scrimshaw (2012-10-18): Added additional docstrings for full coverage. Removed duplicates of discriminant() and signature().

  • Anna Haensch (2018-03): Added function quadratic_defect()

class sage.rings.rational_field.RationalField[source]

Bases: Singleton, NumberField

The class RationalField represents the field \(\QQ\) of rational numbers.

EXAMPLES:

sage: a = 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))                 # needs sage.libs.pari
39029384023840928309482842098430284398243982394
sage: QQ('sage')
Traceback (most recent call last):
...
TypeError: unable to convert 'sage' to a rational
>>> from sage.all import *
>>> a = Integer(901824309821093821093812093810928309183091832091)
>>> b = QQ(a); b
901824309821093821093812093810928309183091832091
>>> QQ(b)
901824309821093821093812093810928309183091832091
>>> QQ(int(Integer(93820984323)))
93820984323
>>> QQ(ZZ(Integer(901824309821093821093812093810928309183091832091)))
901824309821093821093812093810928309183091832091
>>> QQ('-930482/9320842317')
-930482/9320842317
>>> QQ((-Integer(930482), Integer(9320842317)))
-930482/9320842317
>>> QQ([Integer(9320842317)])
9320842317
>>> QQ(pari(Integer(39029384023840928309482842098430284398243982394)))                 # needs sage.libs.pari
39029384023840928309482842098430284398243982394
>>> QQ('sage')
Traceback (most recent call last):
...
TypeError: unable to convert 'sage' to a rational
a = 901824309821093821093812093810928309183091832091
b = QQ(a); b
QQ(b)
QQ(int(93820984323))
QQ(ZZ(901824309821093821093812093810928309183091832091))
QQ('-930482/9320842317')
QQ((-930482, 9320842317))
QQ([9320842317])
QQ(pari(39029384023840928309482842098430284398243982394))                 # needs sage.libs.pari
QQ('sage')

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                                                         # needs sage.rings.real_mpfr
0
>>> from sage.all import *
>>> QQ(RR(Integer(3929329)/Integer(32)))
3929329/32
>>> QQ(-RR(Integer(3929329)/Integer(32)))
-3929329/32
>>> QQ(RR(Integer(1)/Integer(7))) - Integer(1)/Integer(7)                                                         # needs sage.rings.real_mpfr
0
QQ(RR(3929329/32))
QQ(-RR(3929329/32))
QQ(RR(1/7)) - 1/7                                                         # needs sage.rings.real_mpfr

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

sage: # needs sage.rings.real_mpfr
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
>>> from sage.all import *
>>> # needs sage.rings.real_mpfr
>>> QQ(RealNumber('23.2'), Integer(2))
6530219459687219/281474976710656
>>> RealNumber('6530219459687219.0')/Integer(281474976710656)
23.20000000000000
>>> a = RealNumber('23.2'); a
23.2000000000000
>>> QQ(a, Integer(10))
116/5
# needs sage.rings.real_mpfr
QQ(23.2, 2)
6530219459687219.0/281474976710656
a = 23.2; a
QQ(a, 10)

Here’s a nice example involving elliptic curves:

sage: # needs sage.rings.real_mpfr sage.schemes
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
>>> from sage.all import *
>>> # needs sage.rings.real_mpfr sage.schemes
>>> E = EllipticCurve('11a')
>>> L = E.lseries().at1(Integer(300))[Integer(0)]; L
0.2538418608559106843377589233...
>>> O = E.period_lattice().omega(); O
1.26920930427955
>>> t = L/O; t
0.200000000000000
>>> QQ(RealField(Integer(45))(t))
1/5
# needs sage.rings.real_mpfr sage.schemes
E = EllipticCurve('11a')
L = E.lseries().at1(300)[0]; L
O = E.period_lattice().omega(); O
t = L/O; t
QQ(RealField(45)(t))
absolute_degree()[source]

Return the absolute degree of \(\QQ\), which is 1.

EXAMPLES:

sage: QQ.absolute_degree()
1
>>> from sage.all import *
>>> QQ.absolute_degree()
1
QQ.absolute_degree()
absolute_discriminant()[source]

Return the absolute discriminant, which is 1.

EXAMPLES:

sage: QQ.absolute_discriminant()
1
>>> from sage.all import *
>>> QQ.absolute_discriminant()
1
QQ.absolute_discriminant()
absolute_polynomial()[source]

Return a defining polynomial of \(\QQ\), as for other number fields.

This is also aliased to defining_polynomial() and absolute_polynomial().

EXAMPLES:

sage: QQ.polynomial()
x
>>> from sage.all import *
>>> QQ.polynomial()
x
QQ.polynomial()
algebraic_closure()[source]

Return the algebraic closure of self (which is \(\QQbar\)).

EXAMPLES:

sage: QQ.algebraic_closure()                                                # needs sage.rings.number_field
Algebraic Field
>>> from sage.all import *
>>> QQ.algebraic_closure()                                                # needs sage.rings.number_field
Algebraic Field
QQ.algebraic_closure()                                                # needs sage.rings.number_field
automorphisms()[source]

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
]
>>> from sage.all import *
>>> QQ.automorphisms()
[
Ring endomorphism of Rational Field
  Defn: 1 |--> 1
]
QQ.automorphisms()
characteristic()[source]

Return 0 since the rational field has characteristic 0.

EXAMPLES:

sage: c = QQ.characteristic(); c
0
sage: parent(c)
Integer Ring
>>> from sage.all import *
>>> c = QQ.characteristic(); c
0
>>> parent(c)
Integer Ring
c = QQ.characteristic(); c
parent(c)
class_number()[source]

Return the class number of the field of rational numbers, which is 1.

EXAMPLES:

sage: QQ.class_number()
1
>>> from sage.all import *
>>> QQ.class_number()
1
QQ.class_number()
completion(p, prec, extras={})[source]

Return the completion of \(\QQ\) at \(p\).

EXAMPLES:

sage: QQ.completion(infinity, 53)                                           # needs sage.rings.real_mpfr
Real Field with 53 bits of precision
sage: QQ.completion(5, 15, {'print_mode': 'bars'})                          # needs sage.rings.padics
5-adic Field with capped relative precision 15
>>> from sage.all import *
>>> QQ.completion(infinity, Integer(53))                                           # needs sage.rings.real_mpfr
Real Field with 53 bits of precision
>>> QQ.completion(Integer(5), Integer(15), {'print_mode': 'bars'})                          # needs sage.rings.padics
5-adic Field with capped relative precision 15
QQ.completion(infinity, 53)                                           # needs sage.rings.real_mpfr
QQ.completion(5, 15, {'print_mode': 'bars'})                          # needs sage.rings.padics
complex_embedding(prec=53)[source]

Return embedding of the rational numbers into the complex numbers.

EXAMPLES:

sage: QQ.complex_embedding()                                                # needs sage.rings.real_mpfr
Ring morphism:
  From: Rational Field
  To:   Complex Field with 53 bits of precision
  Defn: 1 |--> 1.00000000000000
sage: QQ.complex_embedding(20)                                              # needs sage.rings.real_mpfr
Ring morphism:
  From: Rational Field
  To:   Complex Field with 20 bits of precision
  Defn: 1 |--> 1.0000
>>> from sage.all import *
>>> QQ.complex_embedding()                                                # needs sage.rings.real_mpfr
Ring morphism:
  From: Rational Field
  To:   Complex Field with 53 bits of precision
  Defn: 1 |--> 1.00000000000000
>>> QQ.complex_embedding(Integer(20))                                              # needs sage.rings.real_mpfr
Ring morphism:
  From: Rational Field
  To:   Complex Field with 20 bits of precision
  Defn: 1 |--> 1.0000
QQ.complex_embedding()                                                # needs sage.rings.real_mpfr
QQ.complex_embedding(20)                                              # needs sage.rings.real_mpfr
construction()[source]

Return a pair (functor, parent) such that functor(parent) returns self.

This is the construction of \(\QQ\) as the fraction field of \(\ZZ\).

EXAMPLES:

sage: QQ.construction()
(FractionField, Integer Ring)
>>> from sage.all import *
>>> QQ.construction()
(FractionField, Integer Ring)
QQ.construction()
defining_polynomial()[source]

Return a defining polynomial of \(\QQ\), as for other number fields.

This is also aliased to defining_polynomial() and absolute_polynomial().

EXAMPLES:

sage: QQ.polynomial()
x
>>> from sage.all import *
>>> QQ.polynomial()
x
QQ.polynomial()
degree()[source]

Return the degree of \(\QQ\), which is 1.

EXAMPLES:

sage: QQ.degree()
1
>>> from sage.all import *
>>> QQ.degree()
1
QQ.degree()
discriminant()[source]

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

EXAMPLES:

sage: QQ.discriminant()
1
>>> from sage.all import *
>>> QQ.discriminant()
1
QQ.discriminant()
embeddings(K)[source]

Return the list containing the unique embedding of \(\QQ\) into \(K\), if it exists, and an empty list otherwise.

EXAMPLES:

sage: QQ.embeddings(QQ)
[Identity endomorphism of Rational Field]
sage: QQ.embeddings(CyclotomicField(5))                                     # needs sage.rings.number_field
[Coercion map:
   From: Rational Field
   To:   Cyclotomic Field of order 5 and degree 4]
>>> from sage.all import *
>>> QQ.embeddings(QQ)
[Identity endomorphism of Rational Field]
>>> QQ.embeddings(CyclotomicField(Integer(5)))                                     # needs sage.rings.number_field
[Coercion map:
   From: Rational Field
   To:   Cyclotomic Field of order 5 and degree 4]
QQ.embeddings(QQ)
QQ.embeddings(CyclotomicField(5))                                     # needs sage.rings.number_field

The field \(K\) must have characteristic \(0\) for an embedding of \(\QQ\) to exist:

sage: QQ.embeddings(GF(3))
[]
>>> from sage.all import *
>>> QQ.embeddings(GF(Integer(3)))
[]
QQ.embeddings(GF(3))
extension(poly, names, **kwds)[source]

Create a field extension of \(\QQ\).

EXAMPLES:

We make a single absolute extension:

sage: x = polygen(QQ, 'x')
sage: K.<a> = QQ.extension(x^3 + 5); K                                      # needs sage.rings.number_field
Number Field in a with defining polynomial x^3 + 5
>>> from sage.all import *
>>> x = polygen(QQ, 'x')
>>> K = QQ.extension(x**Integer(3) + Integer(5), names=('a',)); (a,) = K._first_ngens(1); K                                      # needs sage.rings.number_field
Number Field in a with defining polynomial x^3 + 5
x = polygen(QQ, 'x')
K.<a> = QQ.extension(x^3 + 5); K                                      # needs sage.rings.number_field

We make an extension generated by roots of two polynomials:

sage: K.<a,b> = QQ.extension([x^3 + 5, x^2 + 3]); K                         # needs sage.rings.number_field
Number Field in a with defining polynomial x^3 + 5 over its base field
sage: b^2                                                                   # needs sage.rings.number_field
-3
sage: a^3                                                                   # needs sage.rings.number_field
-5
>>> from sage.all import *
>>> K = QQ.extension([x**Integer(3) + Integer(5), x**Integer(2) + Integer(3)], names=('a', 'b',)); (a, b,) = K._first_ngens(2); K                         # needs sage.rings.number_field
Number Field in a with defining polynomial x^3 + 5 over its base field
>>> b**Integer(2)                                                                   # needs sage.rings.number_field
-3
>>> a**Integer(3)                                                                   # needs sage.rings.number_field
-5
K.<a,b> = QQ.extension([x^3 + 5, x^2 + 3]); K                         # needs sage.rings.number_field
b^2                                                                   # needs sage.rings.number_field
a^3                                                                   # needs sage.rings.number_field
gen(n=0)[source]

Return the n-th generator of \(\QQ\).

There is only the 0-th generator, which is 1.

EXAMPLES:

sage: QQ.gen()
1
>>> from sage.all import *
>>> QQ.gen()
1
QQ.gen()
gens()[source]

Return a tuple of generators of \(\QQ\), which is only (1,).

EXAMPLES:

sage: QQ.gens()
(1,)
>>> from sage.all import *
>>> QQ.gens()
(1,)
QQ.gens()
hilbert_symbol_negative_at_S(S, b, check=True)[source]

Return an integer that has a negative Hilbert symbol with respect to a given rational number and a given set of primes (or places).

The function is algorithm 3.4.1 in [Kir2016]. It finds an integer \(a\) that has negative Hilbert symbol with respect to a given rational number exactly at a given set of primes (or places).

INPUT:

  • S – list of rational primes, the infinite place as real embedding of \(\QQ\) or as \(-1\)

  • b – a nonzero rational number which is a non-square locally at every prime in S

  • check – boolean (default: True); perform additional checks on input and confirm the output

OUTPUT:

  • An integer \(a\) that has negative Hilbert symbol \((a,b)_p\) for every place \(p\) in \(S\) and no other place.

EXAMPLES:

sage: QQ.hilbert_symbol_negative_at_S([-1,5,3,2,7,11,13,23], -10/7)         # needs sage.rings.padics
-9867
sage: QQ.hilbert_symbol_negative_at_S([3, 5, QQ.places()[0], 11], -15)      # needs sage.rings.padics
-33
sage: QQ.hilbert_symbol_negative_at_S([3, 5], 2)                            # needs sage.rings.padics
15
>>> from sage.all import *
>>> QQ.hilbert_symbol_negative_at_S([-Integer(1),Integer(5),Integer(3),Integer(2),Integer(7),Integer(11),Integer(13),Integer(23)], -Integer(10)/Integer(7))         # needs sage.rings.padics
-9867
>>> QQ.hilbert_symbol_negative_at_S([Integer(3), Integer(5), QQ.places()[Integer(0)], Integer(11)], -Integer(15))      # needs sage.rings.padics
-33
>>> QQ.hilbert_symbol_negative_at_S([Integer(3), Integer(5)], Integer(2))                            # needs sage.rings.padics
15
QQ.hilbert_symbol_negative_at_S([-1,5,3,2,7,11,13,23], -10/7)         # needs sage.rings.padics
QQ.hilbert_symbol_negative_at_S([3, 5, QQ.places()[0], 11], -15)      # needs sage.rings.padics
QQ.hilbert_symbol_negative_at_S([3, 5], 2)                            # needs sage.rings.padics

AUTHORS:

  • Simon Brandhorst, Juanita Duque, Anna Haensch, Manami Roy, Sandi Rudzinski (10-24-2017)

is_absolute()[source]

\(\QQ\) is an absolute extension of \(\QQ\).

EXAMPLES:

sage: QQ.is_absolute()
True
>>> from sage.all import *
>>> QQ.is_absolute()
True
QQ.is_absolute()
is_prime_field()[source]

Return True since \(\QQ\) is a prime field.

EXAMPLES:

sage: QQ.is_prime_field()
True
>>> from sage.all import *
>>> QQ.is_prime_field()
True
QQ.is_prime_field()
maximal_order()[source]

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
>>> from sage.all import *
>>> QQ.maximal_order()
Integer Ring
>>> QQ.ring_of_integers ()
Integer Ring
QQ.maximal_order()
QQ.ring_of_integers ()
ngens()[source]

Return the number of generators of \(\QQ\), which is 1.

EXAMPLES:

sage: QQ.ngens()
1
>>> from sage.all import *
>>> QQ.ngens()
1
QQ.ngens()
number_field()[source]

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
>>> from sage.all import *
>>> QQ.number_field() is QQ
True
QQ.number_field() is QQ
order()[source]

Return the order of \(\QQ\), which is \(\infty\).

EXAMPLES:

sage: QQ.order()
+Infinity
>>> from sage.all import *
>>> QQ.order()
+Infinity
QQ.order()
places(all_complex=False, prec=None)[source]

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. If prec is not None, we simply return homomorphisms into RealField(prec) (or RDF if prec=53).

There is an optional flag all_complex, which defaults to False. If all_complex is True, then the real embeddings are returned as embeddings into the corresponding complex field.

For consistency with non-trivial number fields.

EXAMPLES:

sage: QQ.places()                                                           # needs sage.rings.real_mpfr
[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)                                 # needs sage.rings.real_mpfr
[Ring morphism:
  From: Rational Field
  To:   Complex Field with 200 bits of precision
  Defn: 1 |--> 1.0000000000000000000000000000000000000000000000000000000000]
>>> from sage.all import *
>>> QQ.places()                                                           # needs sage.rings.real_mpfr
[Ring morphism:
  From: Rational Field
  To:   Real Field with 53 bits of precision
  Defn: 1 |--> 1.00000000000000]
>>> QQ.places(prec=Integer(53))
[Ring morphism:
  From: Rational Field
  To:   Real Double Field
  Defn: 1 |--> 1.0]
>>> QQ.places(prec=Integer(200), all_complex=True)                                 # needs sage.rings.real_mpfr
[Ring morphism:
  From: Rational Field
  To:   Complex Field with 200 bits of precision
  Defn: 1 |--> 1.0000000000000000000000000000000000000000000000000000000000]
QQ.places()                                                           # needs sage.rings.real_mpfr
QQ.places(prec=53)
QQ.places(prec=200, all_complex=True)                                 # needs sage.rings.real_mpfr
polynomial()[source]

Return a defining polynomial of \(\QQ\), as for other number fields.

This is also aliased to defining_polynomial() and absolute_polynomial().

EXAMPLES:

sage: QQ.polynomial()
x
>>> from sage.all import *
>>> QQ.polynomial()
x
QQ.polynomial()
power_basis()[source]

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]
>>> from sage.all import *
>>> QQ.power_basis()
[1]
QQ.power_basis()
primes_of_bounded_norm_iter(B)[source]

Iterator yielding all primes less than or equal to \(B\).

INPUT:

  • B – 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)                                                              # needs sage.libs.pari
[2, 3, 5, 7]
sage: list(QQ.primes_of_bounded_norm_iter(1))
[]
>>> from sage.all import *
>>> it = QQ.primes_of_bounded_norm_iter(Integer(10))
>>> list(it)                                                              # needs sage.libs.pari
[2, 3, 5, 7]
>>> list(QQ.primes_of_bounded_norm_iter(Integer(1)))
[]
it = QQ.primes_of_bounded_norm_iter(10)
list(it)                                                              # needs sage.libs.pari
list(QQ.primes_of_bounded_norm_iter(1))
quadratic_defect(a, p, check=True)[source]

Return the valuation of the quadratic defect of \(a\) at \(p\).

INPUT:

  • a – an element of self

  • p – a prime ideal or a prime number

  • check – (default: True) check if \(p\) is prime

REFERENCE:

[Kir2016]

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
>>> from sage.all import *
>>> QQ.quadratic_defect(Integer(0), Integer(7))
+Infinity
>>> QQ.quadratic_defect(Integer(5), Integer(7))
0
>>> QQ.quadratic_defect(Integer(5), Integer(2))
2
>>> QQ.quadratic_defect(Integer(5), Integer(5))
1
QQ.quadratic_defect(0, 7)
QQ.quadratic_defect(5, 7)
QQ.quadratic_defect(5, 2)
QQ.quadratic_defect(5, 5)
random_element(num_bound=None, den_bound=None, *args, **kwds)[source]

Return a random element of \(\QQ\).

Elements are constructed by randomly choosing integers for the numerator and denominator, not necessarily coprime.

INPUT:

  • num_bound – positive integer, specifying a bound on the absolute value of the numerator. If absent, no bound is enforced.

  • den_bound – 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().parent() is QQ
True
sage: while QQ.random_element() != 0:
....:     pass
sage: while QQ.random_element() != -1/2:
....:     pass
>>> from sage.all import *
>>> QQ.random_element().parent() is QQ
True
>>> while QQ.random_element() != Integer(0):
...     pass
>>> while QQ.random_element() != -Integer(1)/Integer(2):
...     pass
QQ.random_element().parent() is QQ
while QQ.random_element() != 0:
    pass
while QQ.random_element() != -1/2:
    pass

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: q = QQ.random_element(5, 10)
sage: -5/1 <= q <= 5/1
True
sage: q.denominator() <= 10
True
sage: q.numerator() <= 5
True
>>> from sage.all import *
>>> q = QQ.random_element(Integer(5), Integer(10))
>>> -Integer(5)/Integer(1) <= q <= Integer(5)/Integer(1)
True
>>> q.denominator() <= Integer(10)
True
>>> q.numerator() <= Integer(5)
True
q = QQ.random_element(5, 10)
-5/1 <= q <= 5/1
q.denominator() <= 10
q.numerator() <= 5

Extra positional or keyword arguments are passed through:

sage: QQ.random_element(distribution='1/n').parent() is QQ
True
sage: QQ.random_element(distribution='1/n').parent() is QQ
True
>>> from sage.all import *
>>> QQ.random_element(distribution='1/n').parent() is QQ
True
>>> QQ.random_element(distribution='1/n').parent() is QQ
True
QQ.random_element(distribution='1/n').parent() is QQ
QQ.random_element(distribution='1/n').parent() is QQ
range_by_height(start, end=None)[source]

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(), type range? 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]
>>> from sage.all import *
>>> list(QQ.range_by_height(Integer(4)))
[0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3, -3, 2/3, -2/3, 3/2, -3/2]
>>> [a.height() for a in QQ.range_by_height(Integer(4))]
[1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3]
list(QQ.range_by_height(4))
[a.height() for a in QQ.range_by_height(4)]

All rational numbers with height 2:

sage: list(QQ.range_by_height(2, 3))
[1/2, -1/2, 2, -2]
>>> from sage.all import *
>>> list(QQ.range_by_height(Integer(2), Integer(3)))
[1/2, -1/2, 2, -2]
list(QQ.range_by_height(2, 3))

Nonsensical integer arguments will return an empty generator:

sage: list(QQ.range_by_height(3, 3))
[]
sage: list(QQ.range_by_height(10, 1))
[]
>>> from sage.all import *
>>> list(QQ.range_by_height(Integer(3), Integer(3)))
[]
>>> list(QQ.range_by_height(Integer(10), Integer(1)))
[]
list(QQ.range_by_height(3, 3))
list(QQ.range_by_height(10, 1))

There are no rational numbers with height \(\leq 0\):

sage: list(QQ.range_by_height(-10, 1))
[]
>>> from sage.all import *
>>> list(QQ.range_by_height(-Integer(10), Integer(1)))
[]
list(QQ.range_by_height(-10, 1))
relative_discriminant()[source]

Return the relative discriminant, which is 1.

EXAMPLES:

sage: QQ.relative_discriminant()
1
>>> from sage.all import *
>>> QQ.relative_discriminant()
1
QQ.relative_discriminant()
residue_field(p, check=True)[source]

Return the residue field of \(\QQ\) at the prime \(p\), for consistency with other number fields.

INPUT:

  • p – 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))                                    # needs sage.rings.finite_rings
Residue field of Integers modulo 1000000007
>>> from sage.all import *
>>> QQ.residue_field(Integer(5))
Residue field of Integers modulo 5
>>> QQ.residue_field(next_prime(Integer(10)**Integer(9)))                                    # needs sage.rings.finite_rings
Residue field of Integers modulo 1000000007
QQ.residue_field(5)
QQ.residue_field(next_prime(10^9))                                    # needs sage.rings.finite_rings
selmer_generators(S, m, proof=True, orders=False)[source]

Return generators of the group \(\QQ(S,m)\).

INPUT:

  • S – set of primes

  • m – positive integer

  • proof – ignored

  • orders – (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\).

See also

RationalField.selmer_space(), which gives additional output when \(m=p\) is prime: as well as generators, it gives an abstract vector space over \(\GF{p}\) isomorphic to \(\QQ(S,p)\) and maps implementing the isomorphism between this space and \(\QQ(S,p)\) as a subgroup of \(\QQ^*/(\QQ^*)^p\).

EXAMPLES:

sage: QQ.selmer_generators((), 2)
[-1]
sage: QQ.selmer_generators((3,), 2)
[-1, 3]
sage: QQ.selmer_generators((5,), 2)
[-1, 5]
>>> from sage.all import *
>>> QQ.selmer_generators((), Integer(2))
[-1]
>>> QQ.selmer_generators((Integer(3),), Integer(2))
[-1, 3]
>>> QQ.selmer_generators((Integer(5),), Integer(2))
[-1, 5]
QQ.selmer_generators((), 2)
QQ.selmer_generators((3,), 2)
QQ.selmer_generators((5,), 2)

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_generators((2,3,5,7,), 2, orders=True)
([-1, 2, 3, 5, 7], [2, 2, 2, 2, 2])
sage: QQ.selmer_generators((2,3,5,7,), 3, orders=True)
([2, 3, 5, 7], [3, 3, 3, 3])
>>> from sage.all import *
>>> QQ.selmer_generators((Integer(2),Integer(3),Integer(5),Integer(7),), Integer(2), orders=True)
([-1, 2, 3, 5, 7], [2, 2, 2, 2, 2])
>>> QQ.selmer_generators((Integer(2),Integer(3),Integer(5),Integer(7),), Integer(3), orders=True)
([2, 3, 5, 7], [3, 3, 3, 3])
QQ.selmer_generators((2,3,5,7,), 2, orders=True)
QQ.selmer_generators((2,3,5,7,), 3, orders=True)
selmer_group(*args, **kwds)[source]

Deprecated: Use selmer_generators() instead. See Issue #31345 for details.

selmer_group_iterator(S, m, proof=True)[source]

Return an iterator through elements of the finite group \(\QQ(S,m)\).

INPUT:

  • S – set of primes

  • m – positive integer

  • proof – ignored

OUTPUT:

An iterator yielding the distinct elements of \(\QQ(S,m)\). See the docstring for selmer_generators() 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]
>>> from sage.all import *
>>> list(QQ.selmer_group_iterator((), Integer(2)))
[1, -1]
>>> list(QQ.selmer_group_iterator((Integer(2),), Integer(2)))
[1, 2, -1, -2]
>>> list(QQ.selmer_group_iterator((Integer(2),Integer(3)), Integer(2)))
[1, 3, 2, 6, -1, -3, -2, -6]
>>> list(QQ.selmer_group_iterator((Integer(5),), Integer(2)))
[1, 5, -1, -5]
list(QQ.selmer_group_iterator((), 2))
list(QQ.selmer_group_iterator((2,), 2))
list(QQ.selmer_group_iterator((2,3), 2))
list(QQ.selmer_group_iterator((5,), 2))
selmer_space(S, p, proof=None)[source]

Compute the group \(\QQ(S,p)\) as a vector space with maps to and from \(\QQ^*\).

INPUT:

  • S – list of prime numbers

  • p – a prime number

OUTPUT:

(tuple) QSp, QSp_gens, from_QSp, to_QSp where

  • QSp is an abstract vector space over \(\GF{p}\) isomorphic to \(\QQ(S,p)\);

  • QSp_gens is a list of elements of \(\QQ^*\) generating \(\QQ(S,p)\);

  • from_QSp is a function from QSp to \(\QQ^*\) implementing the isomorphism from the abstract \(\QQ(S,p)\) to \(\QQ(S,p)\) as a subgroup of \(\QQ^*/(\QQ^*)^p\);

  • to_QSP is a partial function from \(\QQ^*\) to QSp, defined on elements \(a\) whose image in \(\QQ^*/(\QQ^*)^p\) lies in \(\QQ(S,p)\), mapping them via the inverse isomorphism to the abstract vector space QSp.

The group \(\QQ(S,p)\) is the finite subgroup of \(\QQ^*/(\QQ^*)^p\) consisting of elements whose valuation at all primes not in \(S\) is a multiple of \(p\). It contains the subgroup of those \(a\in \QQ^*\) such that \(\QQ(\sqrt[p]{a})/\QQ\) is unramified at all primes of \(\QQ\) outside of \(S\), but may contain it properly when \(p\) is not in \(S\).

EXAMPLES:

When \(S\) is empty, \(\QQ(S,p)\) is only nontrivial for \(p=2\):

sage: QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([], 2)                 # needs sage.rings.number_field
sage: QS2                                                                   # needs sage.rings.number_field
Vector space of dimension 1 over Finite Field of size 2
sage: QS2gens                                                               # needs sage.rings.number_field
[-1]

sage: all(QQ.selmer_space([], p)[0].dimension() == 0                        # needs sage.libs.pari sage.rings.number_field
....:     for p in primes(3, 10))
True
>>> from sage.all import *
>>> QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([], Integer(2))                 # needs sage.rings.number_field
>>> QS2                                                                   # needs sage.rings.number_field
Vector space of dimension 1 over Finite Field of size 2
>>> QS2gens                                                               # needs sage.rings.number_field
[-1]

>>> all(QQ.selmer_space([], p)[Integer(0)].dimension() == Integer(0)                        # needs sage.libs.pari sage.rings.number_field
...     for p in primes(Integer(3), Integer(10)))
True
QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([], 2)                 # needs sage.rings.number_field
QS2                                                                   # needs sage.rings.number_field
QS2gens                                                               # needs sage.rings.number_field
all(QQ.selmer_space([], p)[0].dimension() == 0                        # needs sage.libs.pari sage.rings.number_field
    for p in primes(3, 10))

In general there is one generator for each \(p\in S\), and an additional generator of \(-1\) when \(p=2\):

sage: # needs sage.modules sage.rings.number_field
sage: QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([5,7], 2)
sage: QS2
Vector space of dimension 3 over Finite Field of size 2
sage: QS2gens
[5, 7, -1]
sage: toQS2(-7)
(0, 1, 1)
sage: fromQS2((0,1,1))
-7
>>> from sage.all import *
>>> # needs sage.modules sage.rings.number_field
>>> QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([Integer(5),Integer(7)], Integer(2))
>>> QS2
Vector space of dimension 3 over Finite Field of size 2
>>> QS2gens
[5, 7, -1]
>>> toQS2(-Integer(7))
(0, 1, 1)
>>> fromQS2((Integer(0),Integer(1),Integer(1)))
-7
# needs sage.modules sage.rings.number_field
QS2, QS2gens, fromQS2, toQS2 = QQ.selmer_space([5,7], 2)
QS2
QS2gens
toQS2(-7)
fromQS2((0,1,1))

The map fromQS2 is only well-defined modulo \(p\)-th powers (in this case, modulo squares):

sage: toQS2(-5/7)                                                           # needs sage.modules sage.rings.number_field
(1, 1, 1)
sage: fromQS2((1,1,1))                                                      # needs sage.modules sage.rings.number_field
-35
sage: ((-5/7)/(-35)).is_square()
True
>>> from sage.all import *
>>> toQS2(-Integer(5)/Integer(7))                                                           # needs sage.modules sage.rings.number_field
(1, 1, 1)
>>> fromQS2((Integer(1),Integer(1),Integer(1)))                                                      # needs sage.modules sage.rings.number_field
-35
>>> ((-Integer(5)/Integer(7))/(-Integer(35))).is_square()
True
toQS2(-5/7)                                                           # needs sage.modules sage.rings.number_field
fromQS2((1,1,1))                                                      # needs sage.modules sage.rings.number_field
((-5/7)/(-35)).is_square()

The map toQS2 is not defined on all of \(\QQ^*\), only on those numbers which are squares away from \(5\) and \(7\):

sage: toQS2(210)                                                            # needs sage.modules sage.rings.number_field
Traceback (most recent call last):
...
ValueError: argument 210 should have valuations divisible by 2
at all primes in [5, 7]
>>> from sage.all import *
>>> toQS2(Integer(210))                                                            # needs sage.modules sage.rings.number_field
Traceback (most recent call last):
...
ValueError: argument 210 should have valuations divisible by 2
at all primes in [5, 7]
toQS2(210)                                                            # needs sage.modules sage.rings.number_field
signature()[source]

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)
>>> from sage.all import *
>>> QQ.signature()
(1, 0)
QQ.signature()
some_elements()[source]

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,
 ...)
>>> from sage.all import *
>>> 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,
 ...)
tuple(QQ.some_elements())
valuation(p)[source]

Return the discrete valuation with uniformizer p.

EXAMPLES:

sage: v = QQ.valuation(3); v                                                # needs sage.rings.padics
3-adic valuation
sage: v(1/3)                                                                # needs sage.rings.padics
-1
>>> from sage.all import *
>>> v = QQ.valuation(Integer(3)); v                                                # needs sage.rings.padics
3-adic valuation
>>> v(Integer(1)/Integer(3))                                                                # needs sage.rings.padics
-1
v = QQ.valuation(3); v                                                # needs sage.rings.padics
v(1/3)                                                                # needs sage.rings.padics
zeta(n=2)[source]

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 n-th root of unity in rational field
>>> from sage.all import *
>>> QQ.zeta()
-1
>>> QQ.zeta(Integer(2))
-1
>>> QQ.zeta(Integer(1))
1
>>> QQ.zeta(Integer(3))
Traceback (most recent call last):
...
ValueError: no n-th root of unity in rational field
QQ.zeta()
QQ.zeta(2)
QQ.zeta(1)
QQ.zeta(3)
sage.rings.rational_field.frac(n, d)[source]

Return the fraction n/d.

EXAMPLES:

sage: from sage.rings.rational_field import frac
sage: frac(1,2)
1/2
>>> from sage.all import *
>>> from sage.rings.rational_field import frac
>>> frac(Integer(1),Integer(2))
1/2
from sage.rings.rational_field import frac
frac(1,2)
sage.rings.rational_field.is_RationalField(x)[source]

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)
doctest:warning...
DeprecationWarning: The function is_RationalField is deprecated;
use 'isinstance(..., RationalField)' instead.
See https://github.com/sagemath/sage/issues/38128 for details.
True
sage: is_RF(ZZ)
False
>>> from sage.all import *
>>> from sage.rings.rational_field import is_RationalField as is_RF
>>> is_RF(QQ)
doctest:warning...
DeprecationWarning: The function is_RationalField is deprecated;
use 'isinstance(..., RationalField)' instead.
See https://github.com/sagemath/sage/issues/38128 for details.
True
>>> is_RF(ZZ)
False
from sage.rings.rational_field import is_RationalField as is_RF
is_RF(QQ)
is_RF(ZZ)