Module: sage.rings.rational
Rational Numbers
Author Log:
TESTS:
sage: a = -2/3 sage: a == loads(dumps(a)) True
Module-level Functions
| ) |
| ) |
| ) |
| ) |
| ) |
Find the rational reconstruction of a mod m, if it exists. Input:
Class: int_to_Q
Special Functions: __init__,
_repr_type
| ) |
Class: Rational
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
| ) |
Return the additive order of self.
sage: QQ(0).additive_order() 1 sage: QQ(1).additive_order() +Infinity
| ) |
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
| ) |
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
| ) |
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
| ) |
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
| ) |
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
| ) |
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
| ) |
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:
and hence,
sage: gcd(2/3,7/5) 1/15
In this example:
sage: gcd(1/3,1/6) 1/6
In this example:
sage: gcd(6/7,9/7) 3/7
In this example:
| ) |
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
| ) |
Determine if a rational number is integral (i.e is in
).
sage: QQ(1/2).is_integral() False sage: QQ(4/4).is_integral() True
| ) |
Determine if a rational number is one.
sage: QQ(1/2).is_one() False sage: QQ(4/4).is_one() True
| ) |
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
| ) |
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:
and hence,
sage: lcm(2/3,7/5) 14
In this example:
sage: lcm(1/3,1/5) 1
In this example:
sage: lcm(1/3,1/6) 1/3
In this example:
| ) |
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]
| ) |
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
| ) |
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
| ) |
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
| ) |
Computes the nth root of self, or raises a ValueError if self is not a perfect nth power.
Input:
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
| ) |
Return the numerator of this rational number.
sage: x = -5/11 sage: x.numer() -5
| ) |
Return the numerator of this rational number.
sage: x = 5/11 sage: x.numerator() 5
sage: x = 9/3 sage: x.numerator() 3
| ) |
Return the period of the repeating part of the decimal expansion of this rational number.
ALGORITHM: When a rational number
with
is
expanded, the period begins after
terms and has length
, where
and
are the smallest numbers satisfying
. When
is coprime to 10, this
becomes a purely periodic decimal with
.
(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
| ) |
Returns the nearest integer to self.
Input:
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
| ) |
The square root function.
Input:
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
| ) |
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
| ) |
Return the square free part of
, i.e., an integer z such that
,
for a perfect square
.
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
| ) |
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
| ) |
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)
| ) |
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
| ) |
| ) |
| ) |
| ) |
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
| ) |
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
| ) |
| ) |
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
| ) |
| ) |
| ) |
| ) |
| ) |
| ) |
| ) |
| ) |
| ) |
Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).
| ) |
| ) |
| ) |
sage: kash(3/1).Type() # optional elt-fld^rat sage: magma(3/1).Type() # optional FldRatElt
| ) |
| ) |
Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).
| ) |
| ) |
| ) |
Class: Z_to_Q
Special Functions: __init__,
_repr_type
| ) |
See About this document... for information on suggesting changes.