24.6 Rational Numbers

Module: sage.rings.rational

Rational Numbers

Author Log:

TESTS:

sage: a = -2/3
sage: a == loads(dumps(a))
True

Module-level Functions

clear_mpz_globals( )

gmp_randrange( )

init_mpz_globals( )

make_rational( )

pyrex_rational_reconstruction( )

Find the rational reconstruction of a mod m, if it exists. Input:

a
- Integer
m
- Integer
Output:
x
- rings.rational.Rational

Class: int_to_Q

class int_to_Q

Special Functions: __init__,$  $ _repr_type

_repr_type( )

Class: Rational

class Rational
A Rational number.

Rational numbers are implemented using the GMP C library.

sage: a = -2/3
sage: type(a)
<type 'sage.rings.rational.Rational'>
sage: parent(a)
Rational Field
sage: Rational('1/0')
Traceback (most recent call last):
...
TypeError: unable to convert 1/0 to a rational        
sage: Rational(1.5)
3/2
sage: Rational('9/6')
3/2
sage: Rational((2^99,2^100))
1/2
sage: Rational(("2", "10"), 16)
1/8
sage: Rational(QQbar(125/8).nth_root(3))
5/2
sage: Rational(AA(209735/343 - 17910/49*golden_ratio).nth_root(3) + 3*golden_ratio)
53/7

Functions: additive_order,$  $ ceil,$  $ charpoly,$  $ copy,$  $ denom,$  $ denominator,$  $ factor,$  $ floor,$  $ gamma,$  $ gcd,$  $ height,$  $ is_integral,$  $ is_one,$  $ is_square,$  $ lcm,$  $ list,$  $ minpoly,$  $ mod_ui,$  $ multiplicative_order,$  $ norm,$  $ nth_root,$  $ numer,$  $ numerator,$  $ period,$  $ round,$  $ sqrt,$  $ sqrt_approx,$  $ squarefree_part,$  $ str,$  $ trace,$  $ val_unit,$  $ valuation

additive_order( )

Return the additive order of self.

sage: QQ(0).additive_order()
1
sage: QQ(1).additive_order()
+Infinity

ceil( )

self.ceil(): Return the ceiling of this rational number.

If this rational number is an integer, this returns this number, otherwise it returns the floor of this number +1.

sage: n = 5/3; n.ceil()
2
sage: n = -17/19; n.ceil()
0
sage: n = -7/2; n.ceil()
-3
sage: n = 7/2; n.ceil()
4
sage: n = 10/2; n.ceil()
5

charpoly( )

Return the characteristic polynomial of this rational number. This will always be just x - self; this is really here so that code written for number fields won't crash when applied to rational numbers.

sage: (1/3).charpoly('x')
 x - 1/3

Author: Craig Citro

denom( )

self.denom(): Return the denominator of this rational number.

sage: x = 5/13
sage: x.denom()
13
sage: x = -9/3
sage: x.denom()
1

denominator( )

self.denominator(): Return the denominator of this rational number.

sage: x = -5/11
sage: x.denominator()
11
sage: x = 9/3
sage: x.denominator()
1

floor( )

self.floor(): Return the floor of this rational number as an integer.

sage: n = 5/3; n.floor()
1
sage: n = -17/19; n.floor()
-1
sage: n = -7/2; n.floor()
-4
sage: n = 7/2; n.floor()
3
sage: n = 10/2; n.floor()
5

gamma( )

Return the gamma function evaluated at self. This value is exact for integers and half-integers, otherwise a numerical approximation is returned.

sage: gamma(1/2)
sqrt(pi)
sage: gamma(7/2)
15*sqrt(pi)/8
sage: gamma(-3/2)
4*sqrt(pi)/3
sage: gamma(6/1)
120
sage: gamma(1/3)
2.67893853470775

This function accepts an optional precision argument:

sage: (1/3).gamma(prec=100)
2.6789385347077476336556929410
sage: (1/2).gamma(prec=100)
1.7724538509055160272981674833

gcd( )

Return the least common multiple of self and other.

One way to define this notion is the following:

Note that each rational positive rational number can be written as a product of primes with integer (positive or negative) powers in a unique way.

Then, the GCD of two rational numbers x,y can be defined by specifying that the exponent of every prime p in gcd(x,y) is the infimum of the exponents of p in x, and the exponent of p in y (The primes that does not appear in the decomposition of x or y are considered to have exponent zero).

This definition is consistent with the definition of the GCD in the rational integers. Our hopefully interesting notion of GCD for rational numbers is illustrated in the examples below.

sage: gcd(2/3,1/5)
1/15

This is consistent with the definition above, since:

$\displaystyle 2/3 = 2^1 * 3^{-1}*5^0
$

$\displaystyle 1/5 = 2^0 * 3^0 *5^{-1}
$

and hence,

$\displaystyle gcd(2/3,1/5)= 2^0*3^{-1}*5^{-1} = 1/15
$

sage: gcd(2/3,7/5)
1/15

In this example:

$\displaystyle 2/3 = 2^1*3^{-1}*5^0 * 7^0
$

$\displaystyle 7/5 = 2^0*3^0 *5^{-1} * 7^1
$

$\displaystyle gcd(2/3,7/5) = 2^0*3^{-1}*5^{-1}*7^0 = 1/15
$

sage: gcd(1/3,1/6)
1/6

In this example:

$\displaystyle 1/3 = 2^0*3^{-1}
$

$\displaystyle 1/6 = 2^{-1}*3^{-1}
$

$\displaystyle gcd(1/3,1/6)=2^{-1}*3^{-1}=1/6
$

sage: gcd(6/7,9/7)
3/7

In this example:

$\displaystyle 6/7 = 2^1*3^1*7^{-1}
$

$\displaystyle 9/7 = 2^0*3^2*7^{-1}
$

$\displaystyle gcd(6/7,9/7)=2^0*3^1*7^{-1}=3/7
$

height( )

The max absolute value of the numerator and denominator of self, as an Integer.

sage: a = 2/3
sage: a.height()
3
sage: a = 34/3
sage: a.height()
34
sage: a = -97/4
sage: a.height()
97

Author: Naqi Jaffery (2006-03-05): examples

is_integral( )

Determine if a rational number is integral (i.e is in $ \mathbf{Z}$ ).

sage: QQ(1/2).is_integral()
False
sage: QQ(4/4).is_integral()
True

is_one( )

Determine if a rational number is one.

sage: QQ(1/2).is_one()
False
sage: QQ(4/4).is_one()
True

is_square( )

sage: x = 9/4
sage: x.is_square()
True
sage: x = (7/53)^100
sage: x.is_square()
True
sage: x = 4/3
sage: x.is_square()
False
sage: x = -1/4
sage: x.is_square()
False

lcm( )

Return the least common multiple of self and other.

One way to define this notion is the following:

Note that each rational positive rational number can be written as a product of primes with integer (positive or negative) powers in a unique way.

Then, the LCM of two rational numbers x,y can be defined by specifying that the exponent of every prime p in lcm(x,y) is the supremum of the exponents of p in x, and the exponent of p in y (The primes that does not appear in the decomposition of x or y are considered to have exponent zero).

This definition is consistent with the definition of the LCM in the rational integers. Our hopefully interesting notion of LCM for rational numbers is illustrated in the examples below.

sage: lcm(2/3,1/5)
2

This is consistent with the definition above, since:

$\displaystyle 2/3 = 2^1 * 3^{-1}*5^0
$

$\displaystyle 1/5 = 2^0 * 3^0 *5^{-1}
$

and hence,

$\displaystyle lcm(2/3,1/5)= 2^1*3^0*5^0 = 2.
$

sage: lcm(2/3,7/5)
14

In this example:

$\displaystyle 2/3 = 2^1*3^{-1}*5^0 * 7^0
$

$\displaystyle 7/5 = 2^0*3^0 *5^{-1} * 7^1
$

$\displaystyle lcm(2/3,7/5) = 2^1*3^0*5^0*7^1 = 14
$

sage: lcm(1/3,1/5)
1

In this example:

$\displaystyle 1/3 = 3^{-1}*5^0
$

$\displaystyle 1/5 = 3^0 * 5^{-1}
$

$\displaystyle lcm(1/3,1/5)=3^0*5^0=1
$

sage: lcm(1/3,1/6)
1/3

In this example:

$\displaystyle 1/3 = 2^0*3^{-1}
$

$\displaystyle 1/6 = 2^{-1}*3^{-1}
$

$\displaystyle lcm(1/3,1/6)=2^0*3^{-1}=1/3
$

list( )

Return a list with the rational element in it, to be compatible with the method for number fields.

sage: m = 5/3
sage: m.list()
[5/3]

minpoly( )

Return the minimal polynomial of this rational number. This will always be just x - self; this is really here so that code written for number fields won't crash when applied to rational numbers.

sage: (1/3).minpoly()
 x - 1/3

Author: Craig Citro

multiplicative_order( )

Return the multiplicative order of self.

sage: QQ(1).multiplicative_order()
1
sage: QQ('1/-1').multiplicative_order()
2
sage: QQ(0).multiplicative_order()
+Infinity
sage: QQ('2/3').multiplicative_order()
+Infinity
sage: QQ('1/2').multiplicative_order()
+Infinity

norm( )

Returns the norm from Q to Q of x (which is just x). This was added for compatibility with NumberFields.

sage: (1/3).norm()
 1/3

Author: Craig Citro

nth_root( )

Computes the nth root of self, or raises a ValueError if self is not a perfect nth power.

Input:

n
- integer (must fit in C int type)

Author: David Harvey (2006-09-15)

sage: (25/4).nth_root(2)
5/2
sage: (125/8).nth_root(3)
5/2
sage: (-125/8).nth_root(3)
-5/2
sage: (25/4).nth_root(-2)
2/5

sage: (9/2).nth_root(2)
Traceback (most recent call last):
...
ValueError: not a perfect nth power

sage: (-25/4).nth_root(2)
Traceback (most recent call last):
...
ValueError: cannot take even root of negative number

numer( )

Return the numerator of this rational number.

sage: x = -5/11
sage: x.numer()
-5

numerator( )

Return the numerator of this rational number.

sage: x = 5/11
sage: x.numerator()
5

sage: x = 9/3
sage: x.numerator()
3

period( )

Return the period of the repeating part of the decimal expansion of this rational number.

ALGORITHM: When a rational number $ n/d$ with $ (n,d)==1$ is expanded, the period begins after $ s$ terms and has length $ t$ , where $ s$ and $ t$ are the smallest numbers satisfying $ 10^s=10^(s+t) (mod d)$ . When $ d$ is coprime to 10, this becomes a purely periodic decimal with $ 10^t=1 (mod d)$ . (Lehmer 1941 and Mathworld).

sage: (1/7).period()
6
sage: RR(1/7)
0.142857142857143
sage: (1/8).period()
1
sage: RR(1/8)
0.125000000000000
sage: RR(1/6)
0.166666666666667
sage: (1/6).period()
1
sage: x = 333/106
sage: x.period()
13
sage: RealField(200)(x)
3.1415094339622641509433962264150943396226415094339622641509

round( )

Returns the nearest integer to self.

Input:

self
- a rational number
mode
- a rounding mode for half integers: 'toward' (default) rounds toward zero 'away' rounds away from zero 'up' rounds up 'down' rounds down 'even' rounds toward the even integer 'odd' rounds toward the odd integer

sage: n = 4/3; n.round()
1
sage: n = -17/4; n.round()
-4
sage: n = -5/2; n.round()
-2
sage: n.round("away")
-3
sage: n.round("up")
-2
sage: n.round("down")
-3
sage: n.round("even")
-2
sage: n.round("odd")
-3

sqrt( )

The square root function.

Input:

prec
- integer (default: None): if None, returns an exact square root; otherwise returns a numerical square root if necessary, to the given bits of precision.
extend
- bool (default: True); if True, return a square root in an extension ring, if necessary. Otherwise, raise a ValueError if the square is not in the base ring.
all
- bool (default: False); if True, return all square roots of self, instead of just one.

sage: x = 25/9
sage: x.sqrt()
5/3
sage: x = 64/4
sage: x.sqrt()
4
sage: x = 100/1
sage: x.sqrt()
10
sage: x.sqrt(all=True)
[10, -10]
sage: x = 81/5
sage: x.sqrt()
9/sqrt(5)
sage: x = -81/3
sage: x.sqrt()
3*sqrt(3)*I

sage: n = 2/3
sage: n.sqrt()
sqrt(2)/sqrt(3)
sage: n.sqrt(prec=10)
0.82
sage: n.sqrt(prec=100)
0.81649658092772603273242802490
sage: n.sqrt(prec=100)^2
0.66666666666666666666666666667
sage: n.sqrt(prec=53, all=True)
[0.816496580927726, -0.816496580927726]
sage: n.sqrt(extend=False, all=True)
Traceback (most recent call last):
...
ValueError: square root of 2/3 not a rational number
sage: sqrt(-2/3, all=True)
[sqrt(2)*I/sqrt(3), -sqrt(2)*I/sqrt(3)]
sage: sqrt(-2/3, prec=53)
0.816496580927726*I
sage: sqrt(-2/3, prec=53, all=True)
[0.816496580927726*I, -0.816496580927726*I]

Author: Naqi Jaffery (2006-03-05): some examples

sqrt_approx( )

sage: (5/3).sqrt_approx()
1.29099444873581
sage: (990829038092384908234098239048230984/4).sqrt_approx()
4.9770197862083713747374920870362581922510725585130996993055116540856385e17
sage: (5/3).sqrt_approx(prec=200)
1.2909944487358056283930884665941332036109739017638636088625
sage: (9/4).sqrt_approx()        
3/2

squarefree_part( )

Return the square free part of $ x$ , i.e., an integer z such that $ x = z y^2$ , for a perfect square $ y^2$ .

sage: a = 1/2
sage: a.squarefree_part()
2
sage: b = a/a.squarefree_part()
sage: b, b.is_square()
(1/4, True)
sage: a = 24/5
sage: a.squarefree_part()
30

trace( )

Returns the trace from Q to Q of x (which is just x). This was added for compatibility with NumberFields.

sage: (1/3).trace()
 1/3

Author: Craig Citro

val_unit( )

Returns a pair: the p-adic valuation of self, and the p-adic unit of self, as a Rational.

We do not require the p be prime, but it must be at least 2. For more documentation see Integer.val_unit

Author: David Roe (4/12/07)

valuation( )

sage: x = -5/9
sage: x.valuation(5)
1
sage: x.valuation(3)
-2
sage: x.valuation(2)
0

Special Functions: __abs__,$  $ __eq__,$  $ __float__,$  $ __ge__,$  $ __getitem__,$  $ __gt__,$  $ __index__,$  $ __init__,$  $ __int__,$  $ __invert__,$  $ __le__,$  $ __long__,$  $ __lshift__,$  $ __lt__,$  $ __mod__,$  $ __ne__,$  $ __neg__,$  $ __pos__,$  $ __pow__,$  $ __reduce__,$  $ __repr__,$  $ __rlshift__,$  $ __rmod__,$  $ __rpow__,$  $ __rrshift__,$  $ __rshift__,$  $ __set_value,$  $ _gcd,$  $ _im_gens_,$  $ _integer_,$  $ _interface_init_,$  $ _latex_,$  $ _lcm,$  $ _mathml_,$  $ _pari_,$  $ _reduce_set

__abs__( )

__float__( )

__getitem__( )

__index__( )

Needed so integers can be used as list indices.

sage: v = [1,2,3,4,5]
sage: v[3/1]
4
sage: v[3/2]
Traceback (most recent call last):
...
TypeError: rational is not an integer

__int__( )

Return coercion of self to Python int.

This takes the floor of self if self has a denominator (which is consistent with Python's long(floats)).

sage: int(7/3)
2
sage: int(-7/3)
-3

__invert__( )

__long__( )

Return coercion of self to Python long.

This takes the floor of self if self has a denominator (which is consistent with Python's long(floats)).

sage: long(7/3)
2L
sage: long(-7/3)
-3L

__lshift__( )

__mod__( )

__neg__( )

__pos__( )

__reduce__( )

__repr__( )

__rshift__( )

__set_value( )

_gcd( )

Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).

_im_gens_( )

_integer_( )

_interface_init_( )

sage: kash(3/1).Type()              # optional
elt-fld^rat
sage: magma(3/1).Type()             # optional
FldRatElt

_latex_( )

_lcm( )

Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).

_mathml_( )

_pari_( )

_reduce_set( )

Class: Z_to_Q

class Z_to_Q

Special Functions: __init__,$  $ _repr_type

_repr_type( )

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