Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#***************************************************************************** # Copyright (C) 2005 William Stein <wstein@gmail.com> # # Distributed under the terms of the GNU General Public License (GPL) # # This code is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # The full text of the GPL is available at: # # http://www.gnu.org/licenses/ #***************************************************************************** from __future__ import print_function
from cysignals.signals cimport sig_on, sig_off from sage.ext.cplusplus cimport ccrepr, ccreadstr
include 'misc.pxi' include 'decl.pxi'
from sage.rings.integer cimport Integer from sage.libs.ntl.convert cimport PyLong_to_ZZ from sage.misc.randstate cimport randstate, current_randstate from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE from cpython.int cimport PyInt_AS_LONG
cdef make_ZZ(ZZ_c* x): cdef ntl_ZZ y y = ntl_ZZ() y.x = x[0] del x sig_off() return y
############################################################################## # ZZ: Arbitrary precision integers ##############################################################################
cdef class ntl_ZZ(object): r""" The \class{ZZ} class is used to represent signed, arbitrary length integers.
Routines are provided for all of the basic arithmetic operations, as well as for some more advanced operations such as primality testing. Space is automatically managed by the constructors and destructors.
This module also provides routines for generating small primes, and fast routines for performing modular arithmetic on single-precision numbers. """ # See ntl.pxd for definition of data members def __init__(self, v=None): r""" Initializes and NTL integer.
EXAMPLES::
sage: ntl.ZZ(12r) 12 sage: ntl.ZZ(Integer(95413094)) 95413094 sage: ntl.ZZ(long(223895239852389582983)) 223895239852389582983 sage: ntl.ZZ('-1') -1 sage: ntl.ZZ('1L') 1 sage: ntl.ZZ('-1r') -1
TESTS::
sage: ntl.ZZ(int(2**40)) 1099511627776
AUTHOR: Joel B. Mohler (2007-06-14) """ # Note: This case should be first since on Python 3 long is int v = '0' raise ValueError("invalid integer: %s" % v)
def __repr__(self): """ Return the string representation of self.
EXAMPLES: sage: ntl.ZZ(5).__repr__() '5' """
def __reduce__(self): """ sage: from sage.libs.ntl.ntl_ZZ import ntl_ZZ sage: a = ntl_ZZ(-7) sage: loads(dumps(a)) -7 """
def __richcmp__(ntl_ZZ self, other, int op): """ Compare self to other.
EXAMPLES::
sage: f = ntl.ZZ(1) sage: g = ntl.ZZ(2) sage: h = ntl.ZZ(2) sage: w = ntl.ZZ(7) sage: h == g True sage: g >= h True sage: f == g False sage: h > w False sage: h < w True sage: h <= 3 True """ cdef ntl_ZZ b
def __hash__(self): """ Return the hash of this integer.
Agrees with the hash of the corresponding sage integer. """
def __mul__(self, other): """ sage: n=ntl.ZZ(2983)*ntl.ZZ(2) sage: n 5966 """ self = ntl_ZZ(self)
def __sub__(self, other): """ sage: n=ntl.ZZ(2983)-ntl.ZZ(2) sage: n 2981 sage: ntl.ZZ(2983)-2 2981 """ self = ntl_ZZ(self)
def __add__(self, other): """ sage: n=ntl.ZZ(2983)+ntl.ZZ(2) sage: n 2985 sage: ntl.ZZ(23)+2 25 """ self = ntl_ZZ(self)
def __neg__(ntl_ZZ self): """ sage: x = ntl.ZZ(38) sage: -x -38 sage: x.__neg__() -38 """
def __pow__(ntl_ZZ self, long e, ignored): """ sage: ntl.ZZ(23)^50 122008981252869411022491112993141891091036959856659100591281395343249 """
def __int__(self): """ Return self as an int.
EXAMPLES: sage: ntl.ZZ(22).__int__() 22 sage: type(ntl.ZZ(22).__int__()) <... 'int'>
sage: ntl.ZZ(10^30).__int__() 1000000000000000000000000000000L sage: type(ntl.ZZ(10^30).__int__()) # py2 <type 'long'> sage: type(ntl.ZZ(10^30).__int__()) # py3 <class 'int'> """
cdef int get_as_int(ntl_ZZ self): r""" Returns value as C int. Return value is only valid if the result fits into an int.
AUTHOR: David Harvey (2006-08-05) """ cdef int ans
def get_as_int_doctest(self): r""" This method exists solely for automated testing of get_as_int().
sage: x = ntl.ZZ(42) sage: i = x.get_as_int_doctest() sage: i 42 sage: type(i) <... 'int'> """
def _integer_(self, ZZ=None): r""" Gets the value as a sage int.
sage: n=ntl.ZZ(2983) sage: type(n._integer_()) <type 'sage.rings.integer.Integer'>
AUTHOR: Joel B. Mohler """ #return (<IntegerRing_class>ZZ_sage)._coerce_ZZ(&self.x)
cdef void set_from_int(ntl_ZZ self, int value): r""" Sets the value from a C int.
AUTHOR: David Harvey (2006-08-05) """
def set_from_sage_int(self, Integer value): r""" Sets the value from a sage int.
EXAMPLES: sage: n=ntl.ZZ(2983) sage: n 2983 sage: n.set_from_sage_int(1234) sage: n 1234
AUTHOR: Joel B. Mohler """
def set_from_int_doctest(self, value): r""" This method exists solely for automated testing of set_from_int().
sage: x = ntl.ZZ() sage: x.set_from_int_doctest(42) sage: x 42 """
def valuation(self, ntl_ZZ prime): """ Uses code in ``ntlwrap.cpp`` to compute the number of times prime divides self.
EXAMPLES::
sage: a = ntl.ZZ(5^7*3^4) sage: p = ntl.ZZ(5) sage: a.valuation(p) 7 sage: a.valuation(-p) 7 sage: b = ntl.ZZ(0) sage: b.valuation(p) +Infinity """ cdef long valuation
def val_unit(self, ntl_ZZ prime): """ Uses code in ``ntlwrap.cpp`` to compute p-adic valuation and unit of self.
EXAMPLES::
sage: a = ntl.ZZ(5^7*3^4) sage: p = ntl.ZZ(-5) sage: a.val_unit(p) (7, -81) sage: a.val_unit(ntl.ZZ(-3)) (4, 78125) sage: a.val_unit(ntl.ZZ(2)) (0, 6328125) """ cdef long valuation
def unpickle_class_value(cls, x): """ Here for unpickling.
EXAMPLES: sage: sage.libs.ntl.ntl_ZZ.unpickle_class_value(ntl.ZZ, 3) 3 sage: type(sage.libs.ntl.ntl_ZZ.unpickle_class_value(ntl.ZZ, 3)) <type 'sage.libs.ntl.ntl_ZZ.ntl_ZZ'> """
def unpickle_class_args(cls, x): """ Here for unpickling.
EXAMPLES: sage: sage.libs.ntl.ntl_ZZ.unpickle_class_args(ntl.ZZ, [3]) 3 sage: type(sage.libs.ntl.ntl_ZZ.unpickle_class_args(ntl.ZZ, [3])) <type 'sage.libs.ntl.ntl_ZZ.ntl_ZZ'> """
# Random-number generation def ntl_setSeed(x=None): r""" Seed the NTL random number generator.
This is automatically called when you set the main Sage random number seed, then call any NTL routine requiring random numbers; so you should never need to call this directly.
If for some reason you do need to call this directly, then you need to get a random number from NTL (so that Sage will seed NTL), then call this function and Sage will not notice.
EXAMPLES:
This is automatically seeded from the main Sage random number seed::
sage: ntl.ZZ_random(1000) 979
Now you can call this function, and it will not be overridden until the next time the main Sage random number seed is changed::
sage: ntl.ntl_setSeed(10) sage: ntl.ZZ_random(1000) 935 """ from random import randint seed = ntl_ZZ(randint(0,int(2)**64)) else:
ntl_setSeed()
def randomBnd(q): r""" Return a random number in the range [0,n).
According to the NTL documentation, these numbers are "cryptographically strong"; of course, that depends in part on how they are seeded.
EXAMPLES::
sage: [ntl.ZZ_random(99999) for i in range(5)] [30675, 84282, 80559, 6939, 44798]
AUTHOR:
- Didier Deshommes <dfdeshom@gmail.com> """
cdef ntl_ZZ w
cdef ntl_ZZ ans
def randomBits(long n): r""" Return a pseudo-random number between 0 and `2^n-1`.
EXAMPLES::
sage: [ntl.ZZ_random_bits(20) for i in range(3)] [948179, 477498, 1020180]
AUTHOR: -- Didier Deshommes <dfdeshom@gmail.com> """
cdef ntl_ZZ ans |