Module: sage.rings.integer_ring
The class
classIntegerRing represents
the ring
of (arbitrary precision) integers. Each integer
is an instance of the class
classInteger, which is defined
in a Pyrex extension module that wraps GMP integers
(the
classmpz_t type in GMP).
sage: Z = IntegerRing(); Z Integer Ring sage: Z.characteristic() 0 sage: Z.is_field() False
There is a unique instances of class
classIntegerRing. To create
an
classInteger, coerce either a Python int, long, or a string.
Various other types will also coerce to the integers, when it makes
sense.
sage: a = Z(1234); b = Z(5678); print a, b 1234 5678 sage: type(a) <type 'integer.Integer'> sage: a + b 6912 sage: Z('94803849083985934859834583945394') 94803849083985934859834583945394
Module-level Functions
X, [xgcd=None]) |
Compute and return a Chinese Remainder Theorem basis for the list X of coprime integers.
INPUT: X -- a list of Integers that are coprime in pairs OUTPUT: E -- a list of Integers such that E[i] = 1 (mod X[i]) and E[i] = 0 (mod X[j]) for all j!=i.
The E[i] have the property that if A is a list of objects, e.g., integers, vectors, matrices, etc., where A[i] is moduli X[i], then a CRT lift of A is simply sum E[i] * A[i].
ALGORITHM: To compute E[i], compute integers s and t such that
s * X[i] + t * (prod over i!=j of X[j]) = 1. (*)
Then E[i] = t * (prod over i!=j of X[j]). Notice that equation (*) implies that E[i] is congruent to 1 modulo X[i] and to 0 modulo the other X[j] for j!=i.
COMPLEXITY: We compute len(X) extended GCD's.
sage: X = [11,20,31,51] sage: E = crt_basis([11,20,31,51]) sage: E[0]%X[0]; E[1]%X[0]; E[2]%X[0]; E[3]%X[0] 1 0 0 0 sage: E[0]%X[1]; E[1]%X[1]; E[2]%X[1]; E[3]%X[1] 0 1 0 0 sage: E[0]%X[2]; E[1]%X[2]; E[2]%X[2]; E[3]%X[2] 0 0 1 0 sage: E[0]%X[3]; E[1]%X[3]; E[2]%X[3]; E[3]%X[3] 0 0 0 1
n, [algorithm=pari]) |
Return the factorization of the positive integer
as a list of
tuples
such that
.
Class: IntegerRing
In order to introduce the ring
of integers, we illustrate
creation, calling a few functions, and working with its
elements.
sage: Z = IntegerRing(); Z Integer Ring sage: Z.characteristic() 0 sage: Z.is_field() False
We next illustrate basic arithmetic in
:
sage: a = Z(1234); b = Z(5678); print a, b 1234 5678 sage: type(a) <type 'integer.Integer'> sage: a + b 6912 sage: b + a 6912 sage: a * b 7006652 sage: b * a 7006652 sage: a - b -4444 sage: b - a 4444
When we divide to integers using /, the result is automatically coerced to the field of rational numbers, even if the result is an integer.
sage: a / b 617/2839 sage: type(a/b) <type 'rational.Rational'> sage: a/a 1 sage: type(a/a) <type 'rational.Rational'>
For floor division, instead using the // operator:
sage: a // b 0 sage: type(a//b) <type 'integer.Integer'>
Next we illustrate arithmetic with automatic coercion. The types that coerce are: str, int, long, Integer.
sage: a + 17 1251 sage: a * 374 461516 sage: 374 * a 461516 sage: a/19 1234/19 sage: 0 + Z(-64) -64
Integers can be coerced:
sage: a = Z(-64) sage: int(a) -64
Functions: characteristic,
fraction_field,
gen,
gens,
is_atomic_repr,
is_field,
is_finite,
krull_dimension,
ngens,
order,
quotient,
random,
zeta
self) |
Return 0 as a Python int.
self) |
Return True, since elements of the integers do not have to be printed with paranethesis around them, when they are coefficients, e.g., in a polynomial.
self) |
Return False.
self) |
Return the Krull dimension of the integers, which is 1.
self, I) |
Return the quotient of
by the ideal
or integer
.
sage: Z/(3*Z) Ring of integers modulo 3 sage: Z/(0*Z) Integer Ring sage: Z/3 Ring of integers modulo 3 sage: Z/(3*Q) Traceback (most recent call last): ... TypeError: I (=Principal ideal (1) of Rational Field) must be an ideal of ZZ
self, [bound=5]) |
Return a random integer between -bound and bound, including both endpoints.
Special Functions: __call__,
__cmp__,
__iter__,
__repr__,
_coerce_,
_gap_init_,
_is_valid_homomorphism_,
_latex_,
_magma_init_
self) |
Iterate over all integers. 0 1 -1 2 -2 3 -3 ...
self, x) |
sage: k = GF(7) sage: k._coerce_(2/3) Traceback (most recent call last): ... TypeError: Unable to coerce 2/3 into Finite Field of size 7 sage: k._coerce_(5) # works since there's a natural hom ZZ --> GF(7). 5 sage: ZZ._coerce_(GF(7)(2)) Traceback (most recent call last): ... TypeError: no canonical coercion of 2 to an integer
self) |
sage: gap(ZZ) Integers
self) |
sage: magma(ZZ) # optional Integer Ring