|
- Method resolution order:
- Integer
- sage.structure.element.EuclideanDomainElement
- sage.structure.element.PrincipalIdealDomainElement
- sage.structure.element.DedekindDomainElement
- sage.structure.element.IntegralDomainElement
- sage.structure.element.CommutativeRingElement
- sage.structure.element.RingElement
- sage.structure.element.ModuleElement
- sage.structure.element.Element
- sage.structure.sage_object.SageObject
- __builtin__.object
Methods defined here:
- __abs__(...)
- File: sage/rings/integer.pyx (starting at line 2308)
Computes `|self|`
EXAMPLES::
sage: z = -1
sage: abs(z)
1
sage: abs(z) == abs(1)
True
- __and__(...)
- File: sage/rings/integer.pyx (starting at line 4459)
Return the bitwise and two integers.
EXAMPLES::
sage: n = Integer(6); m = Integer(2)
sage: n & m
2
sage: n.__and__(m)
2
- __copy__(...)
- File: sage/rings/integer.pyx (starting at line 723)
Return a copy of the integer.
EXAMPLES::
sage: n = 2
sage: copy(n)
2
sage: copy(n) is n
False
- __eq__(...)
- x.__eq__(y) <==> x==y
- __float__(...)
- File: sage/rings/integer.pyx (starting at line 2543)
Return double precision floating point representation of this
integer.
EXAMPLES::
sage: n = Integer(17); float(n)
17.0
sage: n = Integer(902834098234908209348209834092834098); float(n)
9.0283409823490813e+35
sage: n = Integer(-57); float(n)
-57.0
sage: n.__float__()
-57.0
sage: type(n.__float__())
<type 'float'>
- __floordiv__(...)
- File: sage/rings/integer.pyx (starting at line 1393)
Computes the whole part of `\frac{self}{other}`.
EXAMPLES::
sage: a = Integer(321) ; b = Integer(10)
sage: a // b
32
sage: z = Integer(-231)
sage: z // 2
-116
sage: z = Integer(231)
sage: z // 2
115
sage: z // -2
-116
sage: z // 0
Traceback (most recent call last):
...
ZeroDivisionError: other must be nonzero
- __ge__(...)
- x.__ge__(y) <==> x>=y
- __gt__(...)
- x.__gt__(y) <==> x>y
- __hash__(...)
- x.__hash__() <==> hash(x)
- __hex__(...)
- File: sage/rings/integer.pyx (starting at line 903)
Return the hexadecimal digits of self in lower case.
.. note::
'0x' is *not* prepended to the result like is done by the
corresponding Python function on int or long. This is for
efficiency sake--adding and stripping the string wastes
time; since this function is used for conversions from
integers to other C-library structures, it is important
that it be fast.
EXAMPLES::
sage: print hex(Integer(15))
f
sage: print hex(Integer(16))
10
sage: print hex(Integer(16938402384092843092843098243))
36bb1e3929d1a8fe2802f083
sage: print hex(long(16938402384092843092843098243))
0x36bb1e3929d1a8fe2802f083L
- __index__(...)
- File: sage/rings/integer.pyx (starting at line 638)
Needed so integers can be used as list indices.
EXAMPLES::
sage: v = [1,2,3,4,5]
sage: v[Integer(3)]
4
sage: v[Integer(2):Integer(4)]
[3, 4]
- __init__(...)
- x.__init__(...) initializes x; see x.__class__.__doc__ for signature
- __int__(...)
- File: sage/rings/integer.pyx (starting at line 2502)
Return the Python int (or long) corresponding to this Sage
integer.
EXAMPLES::
sage: n = 920938; n
920938
sage: int(n)
920938
sage: type(n.__int__())
<type 'int'>
sage: n = 99028390823409823904823098490238409823490820938; n
99028390823409823904823098490238409823490820938
sage: type(n.__int__())
<type 'long'>
- __invert__(...)
- File: sage/rings/integer.pyx (starting at line 4497)
Return the multiplicative interse of self, as a rational number.
EXAMPLE::
sage: n = 10
sage: 1/n
1/10
sage: n.__invert__()
1/10
- __le__(...)
- x.__le__(y) <==> x<=y
- __long__(...)
- File: sage/rings/integer.pyx (starting at line 2524)
Return long integer corresponding to this Sage integer.
EXAMPLES::
sage: n = 9023408290348092849023849820934820938490234290; n
9023408290348092849023849820934820938490234290
sage: long(n)
9023408290348092849023849820934820938490234290L
sage: n = 920938; n
920938
sage: long(n)
920938L
sage: n.__long__()
920938L
- __lshift__(...)
- File: sage/rings/integer.pyx (starting at line 4384)
Shift x y bits to the left.
EXAMPLES::
sage: 32 << 2
128
sage: 32 << int(2)
128
sage: int(32) << 2
128
sage: 1 << 2.5
Traceback (most recent call last):
...
TypeError: unsupported operands for <<
- __lt__(...)
- x.__lt__(y) <==> x<y
- __mod__(...)
- File: sage/rings/integer.pyx (starting at line 2325)
Returns self modulo the modulus.
EXAMPLES::
sage: z = 43
sage: z % 2
1
sage: z % 0
Traceback (most recent call last):
...
ZeroDivisionError: Integer modulo by zero
sage: -5 % 7
2
sage: -5 % -7
-5
sage: 5 % -7
-2
- __ne__(...)
- x.__ne__(y) <==> x!=y
- __nonzero__(...)
- x.__nonzero__() <==> x != 0
- __oct__(...)
- File: sage/rings/integer.pyx (starting at line 929)
Return the digits of self in base 8.
.. note::
'0' is *not* prepended to the result like is done by the
corresponding Python function on int or long. This is for
efficiency sake--adding and stripping the string wastes
time; since this function is used for conversions from
integers to other C-library structures, it is important
that it be fast.
EXAMPLES::
sage: print oct(Integer(800))
1440
sage: print oct(Integer(8))
10
sage: print oct(Integer(-50))
-62
sage: print oct(Integer(-899))
-1603
sage: print oct(Integer(16938402384092843092843098243))
15535436162247215217705000570203
Behavior of Sage integers vs. Python integers::
sage: oct(Integer(10))
'12'
sage: oct(int(10))
'012'
sage: oct(Integer(-23))
'-27'
sage: oct(int(-23))
'-027'
- __or__(...)
- File: sage/rings/integer.pyx (starting at line 4482)
Return the bitwise or of the integers x and y.
EXAMPLES::
sage: n = 8; m = 4
sage: n.__or__(m)
12
- __pos__(...)
- File: sage/rings/integer.pyx (starting at line 2298)
EXAMPLES::
sage: z=43434
sage: z.__pos__()
43434
- __pow__(...)
- x.__pow__(y[, z]) <==> pow(x, y[, z])
- __pyxdoc__init__(...)
- File: sage/rings/integer.pyx (starting at line 400)
You can create an integer from an int, long, string literal, or
integer modulo N.
EXAMPLES::
sage: Integer(495)
495
sage: Integer('495949209809328523')
495949209809328523
sage: Integer(Mod(3,7))
3
sage: 2^3
8
- __rand__(...)
- x.__rand__(y) <==> y&x
- __reduce__(...)
- File: sage/rings/integer.pyx (starting at line 604)
This is used when pickling integers.
EXAMPLES::
sage: n = 5
sage: t = n.__reduce__(); t
(<built-in function make_integer>, ('5',))
sage: t[0](*t[1])
5
sage: loads(dumps(n)) == n
True
- __repr__(...)
- File: sage/rings/integer.pyx (starting at line 758)
Return string representation of this integer.
EXAMPLES::
sage: n = -5; n.__repr__()
'-5'
- __rfloordiv__(...)
- x.__rfloordiv__(y) <==> y//x
- __rlshift__(...)
- x.__rlshift__(y) <==> y<<x
- __rmod__(...)
- x.__rmod__(y) <==> y%x
- __ror__(...)
- x.__ror__(y) <==> y|x
- __rpow__(...)
- y.__rpow__(x[, z]) <==> pow(x, y[, z])
- __rrshift__(...)
- x.__rrshift__(y) <==> y>>x
- __rshift__(...)
- File: sage/rings/integer.pyx (starting at line 4425)
EXAMPLES::
sage: 32 >> 2
8
sage: 32 >> int(2)
8
sage: int(32) >> 2
8
sage: 1 >> 2.5
Traceback (most recent call last):
...
TypeError: unsupported operands for >>
- __rxor__(...)
- x.__rxor__(y) <==> y^x
- __xor__(...)
- File: sage/rings/integer.pyx (starting at line 673)
Compute the exclusive or of x and y.
EXAMPLES::
sage: n = ZZ(2); m = ZZ(3)
sage: n.__xor__(m)
1
- additive_order(...)
- File: sage/rings/integer.pyx (starting at line 3882)
Return the additive order of self.
EXAMPLES::
sage: ZZ(0).additive_order()
1
sage: ZZ(1).additive_order()
+Infinity
- binary(...)
- File: sage/rings/integer.pyx (starting at line 968)
Return the binary digits of self as a string.
EXAMPLES::
sage: print Integer(15).binary()
1111
sage: print Integer(16).binary()
10000
sage: print Integer(16938402384092843092843098243).binary()
1101101011101100011110001110010010100111010001101010001111111000101000000000101111000010000011
- bits(...)
- File: sage/rings/integer.pyx (starting at line 983)
Return the bits in self as a list, least significant first.
EXAMPLES::
sage: 500.bits()
[0, 0, 1, 0, 1, 1, 1, 1, 1]
sage: 11.bits()
[1, 1, 0, 1]
- ceil(...)
- File: sage/rings/integer.pyx (starting at line 3174)
Return the ceiling of self, which is self since self is an
integer.
EXAMPLES::
sage: n = 6
sage: n.ceil()
6
- conjugate(...)
- File: sage/rings/integer.pyx (starting at line 4659)
Return the complex conjugate of this integer, which is the
integer itself.
EXAMPLES:
sage: n = 205
sage: n.conjugate()
205
- coprime_integers(...)
- File: sage/rings/integer.pyx (starting at line 2719)
Return the positive integers `< m` that are coprime to
self.
EXAMPLES::
sage: n = 8
sage: n.coprime_integers(8)
[1, 3, 5, 7]
sage: n.coprime_integers(11)
[1, 3, 5, 7, 9]
sage: n = 5; n.coprime_integers(10)
[1, 2, 3, 4, 6, 7, 8, 9]
sage: n.coprime_integers(5)
[1, 2, 3, 4]
sage: n = 99; n.coprime_integers(99)
[1, 2, 4, 5, 7, 8, 10, 13, 14, 16, 17, 19, 20, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 46, 47, 49, 50, 52, 53, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 79, 80, 82, 83, 85, 86, 89, 91, 92, 94, 95, 97, 98]
AUTHORS:
- Naqi Jaffery (2006-01-24): examples
ALGORITHM: Naive - compute lots of GCD's. If this isn't good enough
for you, please code something better and submit a patch.
- crt(...)
- File: sage/rings/integer.pyx (starting at line 4614)
Return the unique integer between `0` and `mn` that
is congruent to the integer modulo `m` and to `y`
modulo `n`. We assume that `m` and `n` are
coprime.
EXAMPLES::
sage: n = 17
sage: m = n.crt(5, 23, 11); m
247
sage: m%23
17
sage: m%11
5
- denominator(...)
- File: sage/rings/integer.pyx (starting at line 2980)
Return the denominator of this integer.
EXAMPLES::
sage: x = 5
sage: x.denominator()
1
sage: x = 0
sage: x.denominator()
1
- digits(...)
- File: sage/rings/integer.pyx (starting at line 1034)
Return a list of digits for self in the given base in little endian
order.
The return is unspecified if self is a negative number and the
digits are given.
INPUT:
- ``base`` - integer (default: 2)
- ``digits`` - optional indexable object as source for
the digits
- ``padto`` - the minimal length of the returned list,
sufficient number of zeros are added to make the list minimum that
length (default: 0)
EXAMPLE::
sage: 17.digits()
[7, 1]
sage: 5.digits(base=2, digits=["zero","one"])
['one', 'zero', 'one']
sage: 5.digits(3)
[2, 1]
sage: 0.digits(base=10) # 0 has 0 digits
[]
sage: 0.digits(base=2) # 0 has 0 digits
[]
sage: 10.digits(16,'0123456789abcdef')
['a']
sage: 0.digits(16,'0123456789abcdef')
[]
sage: 0.digits(16,'0123456789abcdef',padto=1)
['0']
sage: 123.digits(base=10,padto=5)
[3, 2, 1, 0, 0]
sage: 123.digits(base=2,padto=3) # padto is the minimal length
[1, 1, 0, 1, 1, 1, 1]
sage: 123.digits(base=2,padto=10,digits=(1,-1))
[-1, -1, 1, -1, -1, -1, -1, 1, 1, 1]
sage: a=9939082340; a.digits(10)
[0, 4, 3, 2, 8, 0, 9, 3, 9, 9]
sage: a.digits(512)
[100, 302, 26, 74]
sage: (-12).digits(10)
[-2, -1]
sage: (-12).digits(2)
[0, 0, -1, -1]
We support large bases
::
sage: n=2^6000
sage: n.digits(2^3000)
[0, 0, 1]
::
sage: base=3; n=25
sage: l=n.digits(base)
sage: # the next relationship should hold for all n,base
sage: sum(base^i*l[i] for i in range(len(l)))==n
True
sage: base=3; n=-30; l=n.digits(base); sum(base^i*l[i] for i in range(len(l)))==n
True
Note: In some cases it is faster to give a digits collection. This
would be particularly true for computing the digits of a series of
small numbers. In these cases, the code is careful to allocate as
few python objects as reasonably possible.
::
sage: digits = range(15)
sage: l=[ZZ(i).digits(15,digits) for i in range(100)]
sage: l[16]
[1, 1]
This function is comparable to ``str`` for speed.
::
sage: n=3^100000
sage: n.digits(base=10)[-1] # slightly slower than str
1
sage: n=10^10000
sage: n.digits(base=10)[-1] # slightly faster than str
1
AUTHORS:
- Joel B. Mohler (2008-03-02): significantly rewrote this entire function
- divide_knowing_divisible_by(...)
- File: sage/rings/integer.pyx (starting at line 2933)
Returns the integer self / right when self is divisible by right.
If self is not divisible by right, the return value is undefined,
and may not even be close to self/right for multi-word integers.
EXAMPLES::
sage: a = 8; b = 4
sage: a.divide_knowing_divisible_by(b)
2
sage: (100000).divide_knowing_divisible_by(25)
4000
sage: (100000).divide_knowing_divisible_by(26) # close (random)
3846
However, often it's way off.
::
sage: a = 2^70; a
1180591620717411303424
sage: a // 11 # floor divide
107326510974310118493
sage: a.divide_knowing_divisible_by(11) # way off and possibly random
43215361478743422388970455040
- divides(...)
- File: sage/rings/integer.pyx (starting at line 2752)
Return True if self divides n.
EXAMPLES::
sage: Z = IntegerRing()
sage: Z(5).divides(Z(10))
True
sage: Z(0).divides(Z(5))
False
sage: Z(10).divides(Z(5))
False
- divisors(...)
- File: sage/rings/integer.pyx (starting at line 2127)
Returns a list of all positive integer divisors of the integer
self.
EXAMPLES::
sage: a = -3; a.divisors()
[1, 3]
sage: a = 6; a.divisors()
[1, 2, 3, 6]
sage: a = 28; a.divisors()
[1, 2, 4, 7, 14, 28]
sage: a = 2^5; a.divisors()
[1, 2, 4, 8, 16, 32]
sage: a = 100; a.divisors()
[1, 2, 4, 5, 10, 20, 25, 50, 100]
sage: a = 1; a.divisors()
[1]
sage: a = 0; a.divisors()
Traceback (most recent call last):
...
ValueError: n must be nonzero
sage: a = 2^3 * 3^2 * 17; a.divisors()
[1, 2, 3, 4, 6, 8, 9, 12, 17, 18, 24, 34, 36, 51, 68, 72, 102, 136, 153, 204, 306, 408, 612, 1224]
sage: a = odd_part(factorial(31))
sage: v = a.divisors(); len(v)
172800
sage: prod(e+1 for p,e in factor(a))
172800
sage: all([t.divides(a) for t in v])
True
.. note::
If one first computes all the divisors and then sorts it,
the sorting step can easily dominate the runtime. Note,
however, that (non-negative) multiplication on the left
preserves relative order. One can leverage this fact to
keep the list in order as one computes it using a process
similar to that of the merge sort when adding new elements.
- exact_log(...)
- File: sage/rings/integer.pyx (starting at line 1831)
Returns the largest integer `k` such that
`m^k \leq \text{self}`, i.e., the floor of
`\log_m(\text{self})`.
This is guaranteed to return the correct answer even when the usual
log function doesn't have sufficient precision.
INPUT:
- ``m`` - integer >= 2
AUTHORS:
- David Harvey (2006-09-15)
- Joel B. Mohler (2009-04-08) -- rewrote this to handle small cases
and/or easy cases up to 100x faster..
EXAMPLES::
sage: Integer(125).exact_log(5)
3
sage: Integer(124).exact_log(5)
2
sage: Integer(126).exact_log(5)
3
sage: Integer(3).exact_log(5)
0
sage: Integer(1).exact_log(5)
0
sage: Integer(178^1700).exact_log(178)
1700
sage: Integer(178^1700-1).exact_log(178)
1699
sage: Integer(178^1700+1).exact_log(178)
1700
sage: # we need to exercise the large base code path too
sage: Integer(1780^1700-1).exact_log(1780)
1699
sage: # The following are very very fast.
sage: # Note that for base m a perfect power of 2, we get the exact log by counting bits.
sage: n=2983579823750185701375109835; m=32
sage: n.exact_log(m)
18
sage: # The next is a favorite of mine. The log2 approximate is exact and immediately provable.
sage: n=90153710570912709517902579010793251709257901270941709247901209742124;m=213509721309572
sage: n.exact_log(m)
4
::
sage: x = 3^100000
sage: RR(log(RR(x), 3))
100000.000000000
sage: RR(log(RR(x + 100000), 3))
100000.000000000
::
sage: x.exact_log(3)
100000
sage: (x+1).exact_log(3)
100000
sage: (x-1).exact_log(3)
99999
::
sage: x.exact_log(2.5)
Traceback (most recent call last):
...
TypeError: Attempt to coerce non-integral RealNumber to Integer
- exp(...)
- File: sage/rings/integer.pyx (starting at line 2047)
Returns the exponential function of self as a real number.
This function is provided only so that Sage integers may be treated
in the same manner as real numbers when convenient.
INPUT:
- ``prec`` - integer (default: None): if None, returns
symbolic, else to given bits of precision as in RealField
EXAMPLES::
sage: Integer(8).exp()
e^8
sage: Integer(8).exp(prec=100)
2980.9579870417282747435920995
sage: exp(Integer(8))
e^8
For even fairly large numbers, this may not be useful.
::
sage: y=Integer(145^145)
sage: y.exp()
e^25024207011349079210459585279553675697932183658421565260323592409432707306554163224876110094014450895759296242775250476115682350821522931225499163750010280453185147546962559031653355159703678703793369785727108337766011928747055351280379806937944746847277089168867282654496776717056860661614337004721164703369140625
sage: y.exp(prec=53) # default RealField precision
+infinity
- factor(...)
- File: sage/rings/integer.pyx (starting at line 2646)
Return the prime factorization of the integer as a list of pairs
`(p,e)`, where `p` is prime and `e` is a
positive integer.
INPUT:
- ``algorithm`` - string
- ``'pari'`` - (default) use the PARI c library
- ``'kash'`` - use KASH computer algebra system (requires
the optional kash package be installed)
- ``proof`` - bool (default: True) whether or not to
prove primality of each factor (only applicable for PARI).
- ``limit`` - int or None (default: None) if limit is
given it must fit in a signed int, and the factorization is done
using trial division and primes up to limit.
EXAMPLES::
sage: n = 2^100 - 1; n.factor()
3 * 5^3 * 11 * 31 * 41 * 101 * 251 * 601 * 1801 * 4051 * 8101 * 268501
We use proof=False, which doesn't prove correctness of the primes
that appear in the factorization::
sage: n = 920384092842390423848290348203948092384082349082
sage: n.factor(proof=False)
2 * 11 * 1531 * 4402903 * 10023679 * 619162955472170540533894518173
sage: n.factor(proof=True)
2 * 11 * 1531 * 4402903 * 10023679 * 619162955472170540533894518173
We factor using trial division only::
sage: n.factor(limit=1000)
2 * 11 * 41835640583745019265831379463815822381094652231
- factorial(...)
- File: sage/rings/integer.pyx (starting at line 3013)
Return the factorial `n! = 1 \cdot 2 \cdot 3 \cdots n`.
Self must fit in an ``unsigned long int``.
EXAMPLES::
sage: for n in srange(7):
... print n, n.factorial()
0 1
1 1
2 2
3 6
4 24
5 120
6 720
- floor(...)
- File: sage/rings/integer.pyx (starting at line 3161)
Return the floor of self, which is just self since self is an
integer.
EXAMPLES::
sage: n = 6
sage: n.floor()
6
- gamma(...)
- File: sage/rings/integer.pyx (starting at line 3138)
The gamma function on integers is the factorial function (shifted
by one) on positive integers, and `\pm \infty` on
non-positive integers.
EXAMPLES::
sage: gamma(5)
24
sage: gamma(0)
Infinity
sage: gamma(-1)
Infinity
sage: gamma(-2^150)
Infinity
- gcd(...)
- File: sage/rings/integer.pyx (starting at line 4590)
Return the greatest common divisor of self and `n`.
EXAMPLE::
sage: gcd(-1,1)
1
sage: gcd(0,1)
1
sage: gcd(0,0)
0
sage: gcd(2,2^6)
2
sage: gcd(21,2^6)
1
- inverse_mod(...)
- File: sage/rings/integer.pyx (starting at line 4536)
Returns the inverse of self modulo `n`, if this inverse
exists. Otherwise, raises a ``ZeroDivisionError``
exception.
INPUT:
- ``self`` - Integer
- ``n`` - Integer
OUTPUT:
- ``x`` - Integer such that x\*self = 1 (mod m), or
raises ZeroDivisionError.
IMPLEMENTATION:
Call the mpz_invert GMP library function.
EXAMPLES::
sage: a = Integer(189)
sage: a.inverse_mod(10000)
4709
sage: a.inverse_mod(-10000)
4709
sage: a.inverse_mod(1890)
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
sage: a = Integer(19)**100000
sage: b = a*a
sage: c = a.inverse_mod(b)
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
- inverse_of_unit(...)
- File: sage/rings/integer.pyx (starting at line 4511)
Return inverse of self if self is a unit in the integers, i.e.,
self is -1 or 1. Otherwise, raise a ZeroDivisionError.
EXAMPLES::
sage: (1).inverse_of_unit()
1
sage: (-1).inverse_of_unit()
-1
sage: 5.inverse_of_unit()
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
sage: 0.inverse_of_unit()
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
- is_integral(...)
- File: sage/rings/integer.pyx (starting at line 3215)
Return ``True`` since integers are integral, i.e.,
satisfy a monic polynomial with integer coefficients.
EXAMPLES::
sage: Integer(3).is_integral()
True
- is_irreducible(...)
- File: sage/rings/integer.pyx (starting at line 3563)
Returns ``True`` if self is irreducible, i.e. +/-
prime
EXAMPLES::
sage: z = 2^31 - 1
sage: z.is_irreducible()
True
sage: z = 2^31
sage: z.is_irreducible()
False
sage: z = 7
sage: z.is_irreducible()
True
sage: z = -7
sage: z.is_irreducible()
True
- is_one(...)
- File: sage/rings/integer.pyx (starting at line 3187)
Returns ``True`` if the integer is `1`,
otherwise ``False``.
EXAMPLES::
sage: Integer(1).is_one()
True
sage: Integer(0).is_one()
False
- is_perfect_power(...)
- File: sage/rings/integer.pyx (starting at line 3601)
Returns ``True`` if self is a perfect power.
EXAMPLES::
sage: z = 8
sage: z.is_perfect_power()
True
sage: 144.is_perfect_power()
True
sage: 10.is_perfect_power()
False
sage: (-8).is_perfect_power()
True
sage: (-4).is_perfect_power()
False
This is a test to make sure we workaround a bug in GMP. (See
trac \#4612.)
::
sage: [ -a for a in srange(100) if not (-a^3).is_perfect_power() ]
[]
- is_power(...)
- File: sage/rings/integer.pyx (starting at line 3257)
Returns ``True`` if self is a perfect power, ie if
there exist integers a and b, `b > 1` with
`self = a^b`.
EXAMPLES::
sage: Integer(-27).is_power()
True
sage: Integer(12).is_power()
False
- is_power_of(...)
- File: sage/rings/integer.pyx (starting at line 3392)
Returns ``True`` if there is an integer b with
`\mathtt{self} = n^b`.
EXAMPLES::
sage: Integer(64).is_power_of(4)
True
sage: Integer(64).is_power_of(16)
False
TESTS::
sage: Integer(-64).is_power_of(-4)
True
sage: Integer(-32).is_power_of(-2)
True
sage: Integer(1).is_power_of(1)
True
sage: Integer(-1).is_power_of(-1)
True
sage: Integer(0).is_power_of(1)
False
sage: Integer(0).is_power_of(0)
True
sage: Integer(1).is_power_of(0)
True
sage: Integer(1).is_power_of(8)
True
sage: Integer(-8).is_power_of(2)
False
.. note::
For large integers self, is_power_of() is faster than
is_power(). The following examples gives some indication of
how much faster.
::
sage: b = lcm(range(1,10000))
sage: b.exact_log(2)
14446
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power()
sage: cputime(t) # random
0.53203299999999976
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power_of(2)
sage: cputime(t) # random
0.0
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power_of(3)
sage: cputime(t) # random
0.032002000000000308
::
sage: b = lcm(range(1, 1000))
sage: b.exact_log(2)
1437
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power() # note that we change the range from the example above
sage: cputime(t) # random
0.17201100000000036
sage: t=cputime(); TWO=int(2)
sage: for a in range(2, 10000): k = b.is_power_of(TWO)
sage: cputime(t) # random
0.0040000000000000036
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power_of(3)
sage: cputime(t) # random
0.040003000000000011
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power_of(a)
sage: cputime(t) # random
0.02800199999999986
- is_prime(...)
- File: sage/rings/integer.pyx (starting at line 3534)
Returns ``True`` if self is prime.
.. note::
Integer primes are by definition *positive*! This is
different than Magma, but the same as in Pari. See also the
:meth:`.is_irreducible`` method.
EXAMPLES::
sage: z = 2^31 - 1
sage: z.is_prime()
True
sage: z = 2^31
sage: z.is_prime()
False
sage: z = 7
sage: z.is_prime()
True
sage: z = -7
sage: z.is_prime()
False
sage: z.is_irreducible()
True
- is_prime_power(...)
- File: sage/rings/integer.pyx (starting at line 3475)
Returns True if `x` is a prime power, and False otherwise.
INPUT:
- ``flag`` (for primality testing) - int
- ``0`` (default): use a combination of algorithms.
- ``1``: certify primality using the Pocklington-Lehmer
Test.
- ``2``: certify primality using the APRCL test.
EXAMPLES::
sage: (-10).is_prime_power()
False
sage: (10).is_prime_power()
False
sage: (64).is_prime_power()
True
sage: (3^10000).is_prime_power()
True
sage: (10000).is_prime_power(flag=1)
False
.. note::
Currently in the case when self is a perfect power, we call
self.factor, due to a bug in Pari's ispower function. See
Trac \#4777. We illustrate that this is fixed below.
::
sage: n = 150607571^14
sage: n.is_prime_power()
True
- is_pseudoprime(...)
- File: sage/rings/integer.pyx (starting at line 3586)
Returns ``True`` if self is a pseudoprime
EXAMPLES::
sage: z = 2^31 - 1
sage: z.is_pseudoprime()
True
sage: z = 2^31
sage: z.is_pseudoprime()
False
- is_square(...)
- File: sage/rings/integer.pyx (starting at line 3244)
Returns ``True`` if self is a perfect square
EXAMPLES::
sage: Integer(4).is_square()
True
sage: Integer(41).is_square()
False
- is_squarefree(...)
- File: sage/rings/integer.pyx (starting at line 3922)
Returns True if this integer is not divisible by the square of any
prime and False otherwise.
EXAMPLES::
sage: Integer(100).is_squarefree()
False
sage: Integer(102).is_squarefree()
True
- is_unit(...)
- File: sage/rings/integer.pyx (starting at line 3227)
Returns ``true`` if this integer is a unit, i.e., 1 or
`-1`.
EXAMPLES::
sage: for n in srange(-2,3):
... print n, n.is_unit()
-2 False
-1 True
0 False
1 True
2 False
- isqrt(...)
- File: sage/rings/integer.pyx (starting at line 4075)
Returns the integer floor of the square root of self, or raises an
``ValueError`` if self is negative.
EXAMPLE::
sage: a = Integer(5)
sage: a.isqrt()
2
::
sage: Integer(-102).isqrt()
Traceback (most recent call last):
...
ValueError: square root of negative integer not defined.
- jacobi(...)
- File: sage/rings/integer.pyx (starting at line 3644)
Calculate the Jacobi symbol `\left(\frac{self}{b}\right)`.
EXAMPLES::
sage: z = -1
sage: z.jacobi(17)
1
sage: z.jacobi(19)
-1
sage: z.jacobi(17*19)
-1
sage: (2).jacobi(17)
1
sage: (3).jacobi(19)
-1
sage: (6).jacobi(17*19)
-1
sage: (6).jacobi(33)
0
sage: a = 3; b = 7
sage: a.jacobi(b) == -b.jacobi(a)
True
- kronecker(...)
- File: sage/rings/integer.pyx (starting at line 3681)
Calculate the Kronecker symbol
`\left(\frac{self}{b}\right)` with the Kronecker extension
`(self/2)=(2/self)` when self odd, or `(self/2)=0`
when `self` even.
EXAMPLES::
sage: z = 5
sage: z.kronecker(41)
1
sage: z.kronecker(43)
-1
sage: z.kronecker(8)
-1
sage: z.kronecker(15)
0
sage: a = 2; b = 5
sage: a.kronecker(b) == b.kronecker(a)
True
- list(...)
- File: sage/rings/integer.pyx (starting at line 741)
Return a list with this integer in it, to be compatible with the
method for number fields.
EXAMPLES::
sage: m = 5
sage: m.list()
[5]
- log(...)
- File: sage/rings/integer.pyx (starting at line 1973)
Returns symbolic log by default, unless the logarithm is exact (for
an integer base). When precision is given, the RealField
approximation to that bit precision is used.
This function is provided primarily so that Sage integers may be
treated in the same manner as real numbers when convenient. Direct
use of exact_log is probably best for arithmetic log computation.
INPUT:
- ``m`` - default: natural log base e
- ``prec`` - integer (default: None): if None, returns
symbolic, else to given bits of precision as in RealField
EXAMPLES::
sage: Integer(124).log(5)
log(124)/log(5)
sage: Integer(124).log(5,100)
2.9950093311241087454822446806
sage: Integer(125).log(5)
3
sage: Integer(125).log(5,prec=53)
3.00000000000000
sage: log(Integer(125))
log(125)
For extremely large numbers, this works::
sage: x = 3^100000
sage: log(x,3)
100000
Do NOT try log(x) here, at least not with current Maxima symbolic
backend; it may take a very, very long time. But approximations
work well - up to the given precision, of course.
::
sage: x.log(3,53) # default precision for RealField
100000.000000000
sage: (x+1).log(3,53)
100000.000000000
sage: (x+1).log(3,1000)
100000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
We can use non-integer bases, with default e::
sage: x.log(2.5,prec=53)
119897.784671579
- multifactorial(...)
- File: sage/rings/integer.pyx (starting at line 3044)
Computes the k-th factorial `n!^{(k)}` of self. For k=1
this is the standard factorial, and for k greater than one it is
the product of every k-th terms down from self to k. The recursive
definition is used to extend this function to the negative
integers.
EXAMPLES::
sage: 5.multifactorial(1)
120
sage: 5.multifactorial(2)
15
sage: 23.multifactorial(2)
316234143225
sage: prod([1..23, step=2])
316234143225
sage: (-29).multifactorial(7)
1/2640
- multiplicative_order(...)
- File: sage/rings/integer.pyx (starting at line 3899)
Return the multiplicative order of self.
EXAMPLES::
sage: ZZ(1).multiplicative_order()
1
sage: ZZ(-1).multiplicative_order()
2
sage: ZZ(0).multiplicative_order()
+Infinity
sage: ZZ(2).multiplicative_order()
+Infinity
- nbits(...)
- File: sage/rings/integer.pyx (starting at line 996)
Return the number of bits in self.
EXAMPLES::
sage: 500.nbits()
9
sage: 5.nbits()
3
sage: 12345.nbits() == len(12345.binary())
True
- ndigits(...)
- File: sage/rings/integer.pyx (starting at line 1232)
Return the number of digits of self expressed in the given base.
INPUT:
- ``base`` - integer (default: 10)
EXAMPLES::
sage: n = 52
sage: n.ndigits()
2
sage: n = -10003
sage: n.ndigits()
5
sage: n = 15
sage: n.ndigits(2)
4
sage: n=1000**1000000+1
sage: n.ndigits()
3000001
sage: n=1000**1000000-1
sage: n.ndigits()
3000000
sage: n=10**10000000-10**9999990
sage: n.ndigits()
10000000
- next_prime(...)
- File: sage/rings/integer.pyx (starting at line 3837)
Returns the next prime after self.
INPUT:
- ``proof`` - bool or None (default: None, see
proof.arithmetic or sage.structure.proof) Note that the global Sage
default is proof=True
EXAMPLES::
sage: Integer(100).next_prime()
101
Use Proof = False, which is way faster::
sage: b = (2^1024).next_prime(proof=False)
::
sage: Integer(0).next_prime()
2
sage: Integer(1001).next_prime()
1009
- next_probable_prime(...)
- File: sage/rings/integer.pyx (starting at line 3816)
Returns the next probable prime after self, as determined by PARI.
EXAMPLES::
sage: (-37).next_probable_prime()
2
sage: (100).next_probable_prime()
101
sage: (2^512).next_probable_prime()
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171
sage: 0.next_probable_prime()
2
sage: 126.next_probable_prime()
127
sage: 144168.next_probable_prime()
144169
- nth_root(...)
- File: sage/rings/integer.pyx (starting at line 1554)
Returns the (possibly truncated) n'th root of self.
INPUT:
- ``n`` - integer >= 1 (must fit in C int type).
- ``truncate_mode`` - boolean, whether to allow truncation if
self is not an n'th power.
OUTPUT: If truncate_mode is 0 (default), then returns the
exact n'th root if self is an n'th power, or raises a
ValueError if it is not.
If truncate_mode is 1, then if either n is odd or self is
positive, returns a pair (root, exact_flag) where root is the
truncated nth root (rounded towards zero) and exact_flag is a
boolean indicating whether the root extraction was exact;
otherwise raises a ValueError.
AUTHORS:
- David Harvey (2006-09-15)
- Interface changed by John Cremona (2009-04-04)
EXAMPLES::
sage: Integer(125).nth_root(3)
5
sage: Integer(124).nth_root(3)
Traceback (most recent call last):
...
ValueError: 124 is not a 3rd power
sage: Integer(124).nth_root(3, truncate_mode=1)
(4, False)
sage: Integer(125).nth_root(3, truncate_mode=1)
(5, True)
sage: Integer(126).nth_root(3, truncate_mode=1)
(5, False)
::
sage: Integer(-125).nth_root(3)
-5
sage: Integer(-125).nth_root(3,truncate_mode=1)
(-5, True)
sage: Integer(-124).nth_root(3,truncate_mode=1)
(-4, False)
sage: Integer(-126).nth_root(3,truncate_mode=1)
(-5, False)
::
sage: Integer(125).nth_root(2, True)
(11, False)
sage: Integer(125).nth_root(3, True)
(5, True)
::
sage: Integer(125).nth_root(-5)
Traceback (most recent call last):
...
ValueError: n (=-5) must be positive
::
sage: Integer(-25).nth_root(2)
Traceback (most recent call last):
...
ValueError: cannot take even root of negative number
::
sage: a=9
sage: a.nth_root(3)
Traceback (most recent call last):
...
ValueError: 9 is not a 3rd power
sage: a.nth_root(22)
Traceback (most recent call last):
...
ValueError: 9 is not a 22nd power
sage: ZZ(2^20).nth_root(21)
Traceback (most recent call last):
...
ValueError: 1048576 is not a 21st power
sage: ZZ(2^20).nth_root(21, truncate_mode=1)
(1, False)
- numerator(...)
- File: sage/rings/integer.pyx (starting at line 2995)
Return the numerator of this integer.
EXAMPLE::
sage: x = 5
sage: x.numerator()
5
::
sage: x = 0
sage: x.numerator()
0
- ord(...)
- File: sage/rings/integer.pyx (starting at line 2895)
Synonym for valuation
EXAMPLES::
sage: n=12
sage: n.ord(3)
1
- ordinal_str(...)
- File: sage/rings/integer.pyx (starting at line 862)
Returns a string representation of the ordinal associated to self.
EXAMPLES::
sage: [ZZ(n).ordinal_str() for n in range(25)]
['0th',
'1st',
'2nd',
'3rd',
'4th',
...
'10th',
'11th',
'12th',
'13th',
'14th',
...
'20th',
'21st',
'22nd',
'23rd',
'24th']
sage: ZZ(1001).ordinal_str()
'1001st'
- powermod(...)
- File: sage/rings/integer.pyx (starting at line 2412)
Compute self\*\*exp modulo mod.
EXAMPLES::
sage: z = 2
sage: z.powermod(31,31)
2
sage: z.powermod(0,31)
1
sage: z.powermod(-31,31) == 2^-31 % 31
True
As expected, the following is invalid::
sage: z.powermod(31,0)
Traceback (most recent call last):
...
ZeroDivisionError: cannot raise to a power modulo 0
- powermodm_ui(...)
- File: sage/rings/integer.pyx (starting at line 2463)
Computes self\*\*exp modulo mod, where exp is an unsigned long
integer.
EXAMPLES::
sage: z = 32
sage: z.powermodm_ui(2, 4)
0
sage: z.powermodm_ui(2, 14)
2
sage: z.powermodm_ui(2^32-2, 14)
2
sage: z.powermodm_ui(2^32-1, 14)
Traceback (most recent call last): # 32-bit
... # 32-bit
OverflowError: exp (=4294967295) must be <= 4294967294 # 32-bit
8 # 64-bit
sage: z.powermodm_ui(2^65, 14)
Traceback (most recent call last):
...
OverflowError: exp (=36893488147419103232) must be <= 4294967294 # 32-bit
OverflowError: exp (=36893488147419103232) must be <= 18446744073709551614 # 64-bit
- prime_divisors(...)
- File: sage/rings/integer.pyx (starting at line 2105)
The prime divisors of self, sorted in increasing order. If n is
negative, we do *not* include -1 among the prime divisors, since
-1 is not a prime number.
EXAMPLES::
sage: a = 1; a.prime_divisors()
[]
sage: a = 100; a.prime_divisors()
[2, 5]
sage: a = -100; a.prime_divisors()
[2, 5]
sage: a = 2004; a.prime_divisors()
[2, 3, 167]
- prime_factors = prime_divisors(...)
- prime_to_m_part(...)
- File: sage/rings/integer.pyx (starting at line 2083)
Returns the prime-to-m part of self, i.e., the largest divisor of
self that is coprime to m.
INPUT:
- ``m`` - Integer
OUTPUT: Integer
EXAMPLES::
sage: z = 43434
sage: z.prime_to_m_part(20)
21717
- quo_rem(...)
- File: sage/rings/integer.pyx (starting at line 2364)
Returns the quotient and the remainder of self divided by other.
Note that the remainder returned is always either zero or of the
same sign as other.
INPUT:
- ``other`` - the integer the divisor
OUTPUT:
- ``q`` - the quotient of self/other
- ``r`` - the remainder of self/other
EXAMPLES::
sage: z = Integer(231)
sage: z.quo_rem(2)
(115, 1)
sage: z.quo_rem(-2)
(-116, -1)
sage: z.quo_rem(0)
Traceback (most recent call last):
...
ZeroDivisionError: other (=0) must be nonzero
- radical(...)
- File: sage/rings/integer.pyx (starting at line 3709)
Return the product of the prime divisors of self.
If self is 0, returns 1.
EXAMPLES::
sage: Integer(10).radical()
10
sage: Integer(20).radical()
10
sage: Integer(-20).radical()
-10
sage: Integer(0).radical()
1
sage: Integer(36).radical()
6
- rational_reconstruction(...)
- File: sage/rings/integer.pyx (starting at line 2446)
Return the rational reconstruction of this integer modulo m, i.e.,
the unique (if it exists) rational number that reduces to self
modulo m and whose numerator and denominator is bounded by
sqrt(m/2).
EXAMPLES::
sage: (3/7)%100
29
sage: (29).rational_reconstruction(100)
3/7
- sqrt(...)
- File: sage/rings/integer.pyx (starting at line 4126)
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. Ignored if prec
is not None.
- ``all`` - bool (default: False); if True, return all
square roots of self, instead of just one.
EXAMPLES::
sage: Integer(144).sqrt()
12
sage: sqrt(Integer(144))
12
sage: Integer(102).sqrt()
sqrt(102)
::
sage: n = 2
sage: n.sqrt(all=True)
[sqrt(2), -sqrt(2)]
sage: n.sqrt(prec=10)
1.4
sage: n.sqrt(prec=100)
1.4142135623730950488016887242
sage: n.sqrt(prec=100,all=True)
[1.4142135623730950488016887242, -1.4142135623730950488016887242]
sage: n.sqrt(extend=False)
Traceback (most recent call last):
...
ValueError: square root of 2 not an integer
sage: Integer(144).sqrt(all=True)
[12, -12]
sage: Integer(0).sqrt(all=True)
[0]
sage: type(Integer(5).sqrt())
<type 'sage.symbolic.expression.Expression'>
sage: type(Integer(5).sqrt(prec=53))
<type 'sage.rings.real_mpfr.RealNumber'>
sage: type(Integer(-5).sqrt(prec=53))
<type 'sage.rings.complex_number.ComplexNumber'>
- sqrt_approx(...)
- File: sage/rings/integer.pyx (starting at line 4104)
EXAMPLES::
sage: 5.sqrt_approx(prec=200)
doctest:1172: DeprecationWarning: This function is deprecated. Use sqrt with a given number of bits of precision instead.
2.2360679774997896964091736687312762354406183596115257242709
sage: 5.sqrt_approx()
2.23606797749979
sage: 4.sqrt_approx()
2
- sqrtrem(...)
- File: sage/rings/integer.pyx (starting at line 4045)
Return (s, r) where s is the integer square root of self and
r is the remainder such that `\text{self} = s^2 + r`.
Raises ``ValueError`` if self is negative.
EXAMPLES::
sage: 25.sqrtrem()
(5, 0)
sage: 27.sqrtrem()
(5, 2)
sage: 0.sqrtrem()
(0, 0)
::
sage: Integer(-102).sqrtrem()
Traceback (most recent call last):
...
ValueError: square root of negative integer not defined.
- squarefree_part(...)
- File: sage/rings/integer.pyx (starting at line 3736)
Return the square free part of `x` (=self), i.e., the
unique integer `z` that `x = z y^2`, with
`y^2` a perfect square and `z` square-free.
Use ``radical()`` for the product of the primes
that divide self.
If self is 0, just returns 0.
EXAMPLES::
sage: squarefree_part(100)
1
sage: squarefree_part(12)
3
sage: squarefree_part(17*37*37)
17
sage: squarefree_part(-17*32)
-34
sage: squarefree_part(1)
1
sage: squarefree_part(-1)
-1
sage: squarefree_part(-2)
-2
sage: squarefree_part(-4)
-1
::
sage: a = 8 * 5^6 * 101^2
sage: a.squarefree_part(bound=2).factor()
2 * 5^6 * 101^2
sage: a.squarefree_part(bound=5).factor()
2 * 101^2
sage: a.squarefree_part(bound=1000)
2
sage: a = 7^3 * next_prime(2^100)^2 * next_prime(2^200)
sage: a / a.squarefree_part(bound=1000)
49
- str(...)
- File: sage/rings/integer.pyx (starting at line 811)
Return the string representation of ``self`` in the
given base.
EXAMPLES::
sage: Integer(2^10).str(2)
'10000000000'
sage: Integer(2^10).str(17)
'394'
::
sage: two=Integer(2)
sage: two.str(1)
Traceback (most recent call last):
...
ValueError: base (=1) must be between 2 and 36
::
sage: two.str(37)
Traceback (most recent call last):
...
ValueError: base (=37) must be between 2 and 36
::
sage: big = 10^5000000
sage: s = big.str() # long time (> 20 seconds)
sage: len(s) # long time (depends on above defn of s)
5000001
sage: s[:10] # long time (depends on above defn of s)
'1000000000'
- support(...)
- File: sage/rings/integer.pyx (starting at line 2694)
Return a sorted list of the primes dividing this integer.
OUTPUT: The sorted list of primes appearing in the factorization of
this rational with positive exponent.
EXAMPLES::
sage: factorial(10).support()
[2, 3, 5, 7]
sage: (-999).support()
[3, 37]
Trying to find the support of 0 gives an arithmetic error::
sage: 0.support()
Traceback (most recent call last):
...
ArithmeticError: Support of 0 not defined.
- test_bit(...)
- File: sage/rings/integer.pyx (starting at line 4640)
Return the bit at ``index``.
EXAMPLES::
sage: w = 6
sage: w.str(2)
'110'
sage: w.test_bit(2)
1
sage: w.test_bit(-1)
0
- trailing_zero_bits(...)
- File: sage/rings/integer.pyx (starting at line 1011)
Return the number of trailing zero bits in self, i.e.
the exponent of the largest power of 2 dividing self.
EXAMPLES::
sage: 11.trailing_zero_bits()
0
sage: (-11).trailing_zero_bits()
0
sage: (11<<5).trailing_zero_bits()
5
sage: (-11<<5).trailing_zero_bits()
5
sage: 0.trailing_zero_bits()
0
- val_unit(...)
- File: sage/rings/integer.pyx (starting at line 2858)
Returns a pair: the p-adic valuation of self, and the p-adic unit
of self.
INPUT:
- ``p`` - an integer at least 2.
OUTPUT:
- ``v_p(self)`` - the p-adic valuation of
``self``
- ``u_p(self)`` - ``self`` /
`p^{v_p(\mathrm{self})}`
EXAMPLE::
sage: n = 60
sage: n.val_unit(2)
(2, 15)
sage: n.val_unit(3)
(1, 20)
sage: n.val_unit(7)
(0, 60)
sage: (2^11).val_unit(4)
(5, 2)
sage: 0.val_unit(2)
(+Infinity, 1)
- valuation(...)
- File: sage/rings/integer.pyx (starting at line 2827)
Return the p-adic valuation of self.
INPUT:
- ``p`` - an integer at least 2.
EXAMPLE::
sage: n = 60
sage: n.valuation(2)
2
sage: n.valuation(3)
1
sage: n.valuation(7)
0
sage: n.valuation(1)
Traceback (most recent call last):
...
ValueError: You can only compute the valuation with respect to a integer larger than 1.
We do not require that p is a prime::
sage: (2^11).valuation(4)
5
Data and other attributes defined here:
- __new__ = <built-in method __new__ of type object at 0x7f4f99a7ef20>
- T.__new__(S, ...) -> a new object with type S, a subtype of T
- __pyx_vtable__ = <PyCObject object at 0xf42440>
Methods inherited from sage.structure.element.EuclideanDomainElement:
- __divmod__(...)
- File: sage/structure/element.pyx (starting at line 2012)
Return the quotient and remainder of self divided by other.
EXAMPLES::
sage: divmod(5,3)
(1, 2)
sage: divmod(25r,12)
(2, 1)
sage: divmod(25,12r)
(2, 1)
- __rdivmod__(...)
- x.__rdivmod__(y) <==> divmod(y, x)
- degree(...)
- File: sage/structure/element.pyx (starting at line 1989)
- leading_coefficient(...)
- File: sage/structure/element.pyx (starting at line 2006)
Methods inherited from sage.structure.element.PrincipalIdealDomainElement:
- lcm(...)
- File: sage/structure/element.pyx (starting at line 1942)
Returns the least common multiple of self and right.
- xgcd(...)
- File: sage/structure/element.pyx (starting at line 1958)
Return the extended gcd of self and other, i.e., elements `r, s, t` such that
.. math::
r = s \cdot self + t \cdot other.
.. note::
There is no guarantee on minimality of the cofactors. In
the integer case, see documentation for Integer._xgcd() to
obtain minimal cofactors.
Methods inherited from sage.structure.element.IntegralDomainElement:
- is_nilpotent(...)
- File: sage/structure/element.pyx (starting at line 1922)
Methods inherited from sage.structure.element.CommutativeRingElement:
- mod(...)
- File: sage/structure/element.pyx (starting at line 1398)
Return a representative for self modulo the ideal I (or the ideal
generated by the elements of I if I is not an ideal.)
EXAMPLE: Integers
Reduction of 5 modulo an ideal::
sage: n = 5
sage: n.mod(3*ZZ)
2
Reduction of 5 modulo the ideal generated by 3::
sage: n.mod(3)
2
Reduction of 5 modulo the ideal generated by 15 and 6, which is `(3)`.
::
sage: n.mod([15,6])
2
EXAMPLE: Univiate polynomials
::
sage: R.<x> = PolynomialRing(QQ)
sage: f = x^3 + x + 1
sage: f.mod(x + 1)
-1
When little is implemented about a given ring, then mod may
return simply return `f`. For example, reduction is not
implemented for `\ZZ[x]` yet. (TODO!)
sage: R.<x> = PolynomialRing(ZZ)
sage: f = x^3 + x + 1
sage: f.mod(x + 1)
x^3 + x + 1
EXAMPLE: Multivariate polynomials
We reduce a polynomial in two variables modulo a polynomial
and an ideal::
sage: R.<x,y,z> = PolynomialRing(QQ, 3)
sage: (x^2 + y^2 + z^2).mod(x+y+z)
2*y^2 + 2*y*z + 2*z^2
Notice above that `x` is eliminated. In the next example,
both `y` and `z` are eliminated::
sage: (x^2 + y^2 + z^2).mod( (x - y, y - z) )
3*z^2
sage: f = (x^2 + y^2 + z^2)^2; f
x^4 + 2*x^2*y^2 + y^4 + 2*x^2*z^2 + 2*y^2*z^2 + z^4
sage: f.mod( (x - y, y - z) )
9*z^4
In this example `y` is eliminated::
sage: (x^2 + y^2 + z^2).mod( (x^3, y - z) )
x^2 + 2*z^2
Methods inherited from sage.structure.element.RingElement:
- __div__(...)
- File: sage/structure/element.pyx (starting at line 1236)
Top-level multiplication operator for ring elements.
See extensive documentation at the top of element.pyx.
- __idiv__(...)
- File: sage/structure/element.pyx (starting at line 1262)
Top-level division operator for ring elements.
See extensive documentation at the top of element.pyx.
- __imul__(...)
- File: sage/structure/element.pyx (starting at line 1163)
- __mul__(...)
- File: sage/structure/element.pyx (starting at line 1034)
Top-level multiplication operator for ring elements.
See extensive documentation at the top of element.pyx.
AUTHOR:
- Gonzalo Tornaria (2007-06-25) - write base-extending test cases and fix them
TESTS:
Here we test (scalar * vector) multiplication::
sage: x, y = var('x, y')
sage: parent(ZZ(1)*vector(ZZ,[1,2]))
Ambient free module of rank 2 over the principal ideal domain Integer Ring
sage: parent(QQ(1)*vector(ZZ,[1,2]))
Vector space of dimension 2 over Rational Field
sage: parent(ZZ(1)*vector(QQ,[1,2]))
Vector space of dimension 2 over Rational Field
sage: parent(QQ(1)*vector(QQ,[1,2]))
Vector space of dimension 2 over Rational Field
sage: parent(QQ(1)*vector(ZZ[x],[1,2]))
Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x](1)*vector(QQ,[1,2]))
Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ(1)*vector(ZZ[x][y],[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*vector(QQ,[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ[x](1)*vector(ZZ[x][y],[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*vector(QQ[x],[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ[y](1)*vector(ZZ[x][y],[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*vector(QQ[y],[1,2]))
Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x](1)*vector(ZZ[y],[1,2]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Integer Ring'
sage: parent(ZZ[x](1)*vector(QQ[y],[1,2]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in y over Rational Field'
sage: parent(QQ[x](1)*vector(ZZ[y],[1,2]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and 'Ambient free module of rank 2 over the integral domain Univariate Polynomial Ring in y over Integer Ring'
sage: parent(QQ[x](1)*vector(QQ[y],[1,2]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and 'Ambient free module of rank 2 over the principal ideal domain Univariate Polynomial Ring in y over Rational Field'
Here we test (scalar * matrix) multiplication::
sage: parent(ZZ(1)*matrix(ZZ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
sage: parent(QQ(1)*matrix(ZZ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: parent(ZZ(1)*matrix(QQ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: parent(QQ(1)*matrix(QQ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Rational Field
sage: parent(QQ(1)*matrix(ZZ[x],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x](1)*matrix(QQ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ(1)*matrix(ZZ[x][y],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*matrix(QQ,2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ[x](1)*matrix(ZZ[x][y],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*matrix(QQ[x],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(QQ[y](1)*matrix(ZZ[x][y],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x][y](1)*matrix(QQ[y],2,2,[1,2,3,4]))
Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(ZZ[x](1)*matrix(ZZ[y],2,2,[1,2,3,4]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Integer Ring'
sage: parent(ZZ[x](1)*matrix(QQ[y],2,2,[1,2,3,4]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Integer Ring' and 'Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Rational Field'
sage: parent(QQ[x](1)*matrix(ZZ[y],2,2,[1,2,3,4]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and 'Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Integer Ring'
sage: parent(QQ[x](1)*matrix(QQ[y],2,2,[1,2,3,4]))
Traceback (most recent call last):
...
TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and 'Full MatrixSpace of 2 by 2 dense matrices over Univariate Polynomial Ring in y over Rational Field'
- __rdiv__(...)
- x.__rdiv__(y) <==> y/x
- __rmul__(...)
- x.__rmul__(y) <==> y*x
- __rtruediv__(...)
- x.__rtruediv__(y) <==> y/x
- __truediv__(...)
- File: sage/structure/element.pyx (starting at line 1230)
- abs(...)
- File: sage/structure/element.pyx (starting at line 1334)
Return the absolute value of self. (This just calls the __abs__
method, so it is equivalent to the abs() built-in function.)
EXAMPLES::
sage: RR(-1).abs()
1.00000000000000
sage: ZZ(-1).abs()
1
sage: CC(I).abs()
1.00000000000000
sage: Mod(-15, 37).abs()
Traceback (most recent call last):
...
ArithmeticError: absolute valued not defined on integers modulo n.
- order(...)
- File: sage/structure/element.pyx (starting at line 1285)
Return the additive order of self.
This is deprecated; use ``additive_order`` instead.
EXAMPLES::
sage: a = Integers(12)(5)
sage: a.order()
doctest... DeprecationWarning: The function order is deprecated for ring elements; use additive_order or multiplicative_order instead.
12
Methods inherited from sage.structure.element.ModuleElement:
- __add__(...)
- File: sage/structure/element.pyx (starting at line 707)
Top-level addition operator for ModuleElements.
See extensive documentation at the top of element.pyx.
- __iadd__(...)
- File: sage/structure/element.pyx (starting at line 734)
- __isub__(...)
- File: sage/structure/element.pyx (starting at line 769)
- __neg__(...)
- File: sage/structure/element.pyx (starting at line 786)
Top-level negation operator for ModuleElements, which
may choose to implement _neg_ rather than __neg__ for
consistancy.
- __radd__(...)
- x.__radd__(y) <==> y+x
- __rsub__(...)
- x.__rsub__(y) <==> y-x
- __sub__(...)
- File: sage/structure/element.pyx (starting at line 751)
Top-level subtraction operator for ModuleElements.
See extensive documentation at the top of element.pyx.
Methods inherited from sage.structure.element.Element:
- __cmp__(...)
- x.__cmp__(y) <==> cmp(x,y)
- __getstate__(...)
- File: sage/structure/element.pyx (starting at line 263)
Returns a tuple describing the state of your object.
This should return all information that will be required to unpickle
the object. The functionality for unpickling is implemented in
__setstate__().
TESTS::
sage: R.<x,y> = QQ[]
sage: i = ideal(x^2 - y^2 + 1)
sage: i.__getstate__()
(Monoid of ideals of Multivariate Polynomial Ring in x, y over Rational Field, {'_Ideal_generic__ring': Multivariate Polynomial Ring in x, y over Rational Field, '_Ideal_generic__gens': (x^2 - y^2 + 1,)})
- __setstate__(...)
- File: sage/structure/element.pyx (starting at line 280)
Initializes the state of the object from data saved in a pickle.
During unpickling __init__ methods of classes are not called, the saved
data is passed to the class via this function instead.
TESTS::
sage: R.<x,y> = QQ[]
sage: i = ideal(x); i
Ideal (x) of Multivariate Polynomial Ring in x, y over Rational Field
sage: S.<y,z> = ZZ[]
sage: i.__setstate__((R,{'_Ideal_generic__ring':S,'_Ideal_generic__gens': (x^2 - y^2 + 1,)}))
sage: i
Ideal (x^2 - y^2 + 1) of Multivariate Polynomial Ring in y, z over Integer Ring
- base_extend(...)
- File: sage/structure/element.pyx (starting at line 322)
- base_ring(...)
- File: sage/structure/element.pyx (starting at line 325)
Returns the base ring of this element's parent (if that makes sense).
- category(...)
- File: sage/structure/element.pyx (starting at line 331)
- is_zero(...)
- File: sage/structure/element.pyx (starting at line 517)
Return True if self equals parent()(0). The default
implementation is to fall back to 'not self.__nonzero__'.
.. warning::
Do not re-implement this method in your subclass but
implement __nonzero__ instead.
- n(...)
- File: sage/structure/element.pyx (starting at line 397)
Return a numerical approximation of x with at least prec bits of
precision.
EXAMPLES::
sage: (2/3).n()
0.666666666666667
sage: a = 2/3
sage: pi.n(digits=10)
3.141592654
sage: pi.n(prec=20) # 20 bits
3.1416
- parent(...)
- File: sage/structure/element.pyx (starting at line 335)
Returns parent of this element; or, if the optional argument x is
supplied, the result of coercing x into the parent of this element.
- subs(...)
- File: sage/structure/element.pyx (starting at line 346)
Substitutes given generators with given values while not touching
other generators. This is a generic wrapper around __call__.
The syntax is meant to be compatible with the corresponding method
for symbolic expressions.
INPUT:
- ``in_dict`` - (optional) dictionary of inputs
- ``**kwds`` - named parameters
OUTPUT:
- new object if substitution is possible, otherwise self.
EXAMPLES::
sage: x, y = PolynomialRing(ZZ,2,'xy').gens()
sage: f = x^2 + y + x^2*y^2 + 5
sage: f((5,y))
25*y^2 + y + 30
sage: f.subs({x:5})
25*y^2 + y + 30
sage: f.subs(x=5)
25*y^2 + y + 30
sage: (1/f).subs(x=5)
1/(25*y^2 + y + 30)
sage: Integer(5).subs(x=4)
5
- substitute(...)
- File: sage/structure/element.pyx (starting at line 415)
This is an alias for subs().
INPUT:
- ``in_dict`` - (optional) dictionary of inputs
- ``**kwds`` - named parameters
OUTPUT:
- new object if substitution is possible, otherwise self.
EXAMPLES::
sage: x, y = PolynomialRing(ZZ,2,'xy').gens()
sage: f = x^2 + y + x^2*y^2 + 5
sage: f((5,y))
25*y^2 + y + 30
sage: f.substitute({x:5})
25*y^2 + y + 30
sage: f.substitute(x=5)
25*y^2 + y + 30
sage: (1/f).substitute(x=5)
1/(25*y^2 + y + 30)
sage: Integer(5).substitute(x=4)
5
Methods inherited from sage.structure.sage_object.SageObject:
- db(...)
- File: sage/structure/sage_object.pyx (starting at line 171)
Dumps self into the Sage database. Use db(name) by itself to
reload.
The database directory is ``$HOME/.sage/db``
- dump(...)
- File: sage/structure/sage_object.pyx (starting at line 152)
Same as save(filename, compress)
- dumps(...)
- File: sage/structure/sage_object.pyx (starting at line 158)
Dump self to a string s, which can later be reconstituted
as self using loads(s).
- rename(...)
- File: sage/structure/sage_object.pyx (starting at line 34)
Change self so it prints as x, where x is a string.
.. note::
This is *only* supported for Python classes that derive
from SageObject.
EXAMPLES::
sage: x = PolynomialRing(QQ,'x').gen()
sage: g = x^3 + x - 5
sage: g
x^3 + x - 5
sage: g.rename('a polynomial')
sage: g
a polynomial
sage: g + x
x^3 + 2*x - 5
sage: h = g^100
sage: str(h)[:20]
'x^300 + 100*x^298 - '
sage: h.rename('x^300 + ...')
sage: h
x^300 + ...
Real numbers are not Python classes, so rename is not supported::
sage: a = 3.14
sage: type(a)
<type 'sage.rings.real_mpfr.RealLiteral'>
sage: a.rename('pi')
Traceback (most recent call last):
...
NotImplementedError: object does not support renaming: 3.14000000000000
.. note::
The reason C-extension types are not supported by default
is if they were then every single one would have to carry
around an extra attribute, which would be slower and waste
a lot of memory.
To support them for a specific class, add a
``cdef public __custom_name`` attribute.
- reset_name(...)
- File: sage/structure/sage_object.pyx (starting at line 90)
- save(...)
- File: sage/structure/sage_object.pyx (starting at line 129)
Save self to the given filename.
EXAMPLES::
sage: f = x^3 + 5
sage: f.save(SAGE_TMP + '/file')
sage: load(SAGE_TMP + '/file.sobj')
x^3 + 5
- version(...)
- File: sage/structure/sage_object.pyx (starting at line 112)
The version of Sage.
Call this to save the version of Sage in this object.
If you then save and load this object it will know in what
version of Sage it was created.
This only works on Python classes that derive from SageObject.
|