Changeset 6076:10d38f936bdc
- Timestamp:
- 09/02/07 15:03:02 (6 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/rings/integer.pyx (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/rings/integer.pyx
r6075 r6076 306 306 307 307 def __reduce__(self): 308 """ 309 This is used when pickling integers. 310 311 EXAMPLES: 312 sage: n = 5 313 sage: t = n.__reduce__(); t 314 (<built-in function make_integer>, ('5',)) 315 sage: t[0](*t[1]) 316 5 317 sage: loads(dumps(n)) == n 318 True 319 """ 308 320 # This single line below took me HOURS to figure out. 309 321 # It is the *trick* needed to pickle pyrex extension types. … … 398 410 399 411 def __repr__(self): 412 """ 413 Return string representation of this integer. 414 415 EXAMPLES: 416 sage: n = -5; n.__repr__() 417 '-5' 418 """ 400 419 return self.str() 401 420 402 421 def _latex_(self): 422 """ 423 Return latex representation of this integer. This is just the 424 underlying string representation and nothing more. This 425 is called by the latex function. 426 427 EXAMPLES: 428 sage: n = -5; n._latex_() 429 '-5' 430 sage: latex(n) 431 -5 432 """ 403 433 return self.str() 404 434 … … 1187 1217 1188 1218 def rational_reconstruction(self, Integer m): 1219 """ 1220 Return the rational reconstruction of this integer modulo m, i.e., 1221 the unique (if it exists) rational number that reduces to self 1222 modulo m and whose numerator and denominator is bounded by 1223 sqrt(m/2). 1224 1225 EXAMPLES: 1226 sage: (3/7)%100 1227 29 1228 sage: (29).rational_reconstruction(100) 1229 3/7 1230 """ 1189 1231 import rational 1190 1232 return rational.pyrex_rational_reconstruction(self, m) … … 1229 1271 1230 1272 def __int__(self): 1273 """ 1274 Return the Python int (or long) corresponding to this SAGE integer. 1275 1276 EXAMPLES: 1277 sage: n = 920938; n 1278 920938 1279 sage: int(n) 1280 920938 1281 sage: type(n.__int__()) 1282 <type 'int'> 1283 sage: n = 99028390823409823904823098490238409823490820938; n 1284 99028390823409823904823098490238409823490820938 1285 sage: type(n.__int__()) 1286 <type 'long'> 1287 """ 1231 1288 # TODO -- this crashes on sage.math, since it is evidently written incorrectly. 1232 1289 #return mpz_get_pyintlong(self.value) … … 1234 1291 1235 1292 def __long__(self): 1293 """ 1294 Return long integer corresponding to this SAGE integer. 1295 1296 EXAMPLES: 1297 sage: n = 9023408290348092849023849820934820938490234290; n 1298 9023408290348092849023849820934820938490234290 1299 sage: long(n) 1300 9023408290348092849023849820934820938490234290L 1301 sage: n = 920938; n 1302 920938 1303 sage: long(n) 1304 920938L 1305 sage: n.__long__() 1306 920938L 1307 """ 1236 1308 return mpz_get_pylong(self.value) 1237 1309 … … 1240 1312 1241 1313 def __hash__(self): 1314 """ 1315 Return the hash of this integer. 1316 1317 This agrees with the Python hash of the corresponding 1318 Python int or long. 1319 1320 EXAMPLES: 1321 sage: n = -920384; n.__hash__() 1322 -920384 1323 sage: hash(int(n)) 1324 -920384 1325 sage: n = -920390823904823094890238490238484; n.__hash__() 1326 -873977844 1327 sage: hash(long(n)) 1328 -873977844 1329 """ 1242 1330 return mpz_pythonhash(self.value) 1243 1331 … … 1252 1340 * 'kash' -- use KASH computer algebra system (requires 1253 1341 the optional kash package be installed) 1342 1343 EXAMPLES: 1344 sage: n = 2^100 - 1; n.factor() 1345 3 * 5^3 * 11 * 31 * 41 * 101 * 251 * 601 * 1801 * 4051 * 8101 * 268501 1254 1346 """ 1255 1347 import sage.rings.integer_ring … … 2553 2645 2554 2646 def LCM_list(v): 2647 """ 2648 Return the LCM of a list v of integers. Element of v is 2649 converted to a SAGE integer if it isn't one already. 2650 2651 This function is used, e.g., by rings/arith.py 2652 2653 INPUT: 2654 v -- list or tuple 2655 2656 OUTPUT: 2657 integer 2658 2659 EXAMPLES: 2660 sage: from sage.rings.integer import LCM_list 2661 sage: w = LCM_list([3,9,30]); w 2662 90 2663 sage: type(w) 2664 <type 'sage.rings.integer.Integer'> 2665 2666 The inputs are converted to SAGE integers. 2667 sage: w = LCM_list([int(3), int(9), '30']); w 2668 90 2669 sage: type(w) 2670 <type 'sage.rings.integer.Integer'> 2671 """ 2555 2672 cdef int i, n 2556 2673 cdef mpz_t z … … 2590 2707 2591 2708 def GCD_list(v): 2709 """ 2710 Return the GCD of a list v of integers. Element of v is 2711 converted to a SAGE integer if it isn't one already. 2712 2713 This function is used, e.g., by rings/arith.py 2714 2715 INPUT: 2716 v -- list or tuple 2717 2718 OUTPUT: 2719 integer 2720 2721 EXAMPLES: 2722 sage: from sage.rings.integer import GCD_list 2723 sage: w = GCD_list([3,9,30]); w 2724 3 2725 sage: type(w) 2726 <type 'sage.rings.integer.Integer'> 2727 2728 The inputs are converted to SAGE integers. 2729 sage: w = GCD_list([int(3), int(9), '30']); w 2730 3 2731 sage: type(w) 2732 <type 'sage.rings.integer.Integer'> 2733 """ 2592 2734 cdef int i, n 2593 2735 cdef mpz_t z … … 2631 2773 2632 2774 def make_integer(s): 2775 """ 2776 Create a SAGE integer from the base-32 Python *string* s. 2777 This is used in unpickling integers. 2778 2779 EXAMPLES: 2780 sage: from sage.rings.integer import make_integer 2781 sage: make_integer('-29') 2782 -73 2783 sage: make_integer(29) 2784 Traceback (most recent call last): 2785 ... 2786 TypeError: expected string or Unicode object, sage.rings.integer.Integer found 2787 """ 2633 2788 cdef Integer r 2634 2789 r = PY_NEW(Integer) … … 2636 2791 return r 2637 2792 2638 # TODO: where is this used? Never doctested. Should I call IntegerRing.random_element()?2639 from random import randint2640 def random_integer(min=-2, max=2):2641 cdef Integer x2642 cdef int _min, _max, r2643 try:2644 _min = min2645 _max = max2646 x = PY_NEW(Integer)2647 r = randint() % (_max - _min + 1) + _min2648 mpz_set_si(x.value, r)2649 return x2650 except OverflowError:2651 return Integer(randint(min,max))2652 2653 2793 2654 2794 ############### INTEGER CREATION CODE ##################### 2655 ######## There is nothing to see here, move along #######2656 2795 2657 2796 cdef extern from *: … … 2946 3085 2947 3086 def time_alloc_list(n): 3087 """ 3088 Allocate n a list of n SAGE integers using PY_NEW. 3089 (Used for timing purposes.) 3090 3091 EXAMPLES: 3092 sage: from sage.rings.integer import time_alloc_list 3093 sage: v = time_alloc_list(100) 3094 """ 2948 3095 cdef int i 2949 3096 l = [] … … 2954 3101 2955 3102 def time_alloc(n): 3103 """ 3104 Time allocating n integers using PY_NEW. 3105 Used for timing purposes. 3106 3107 EXAMPLES: 3108 sage: from sage.rings.integer import time_alloc 3109 sage: time_alloc(100) 3110 """ 2956 3111 cdef int i 2957 3112 for i from 0 <= i < n: … … 2959 3114 2960 3115 def pool_stats(): 2961 print "Used pool %s / %s times" % (use_pool, total_alloc) 2962 print "Pool contains %s / %s items" % (integer_pool_count, integer_pool_size) 3116 """ 3117 Returns information about the Integer object pool. 3118 3119 EXAMPLES: 3120 sage: from sage.rings.integer import pool_stats 3121 sage: pool_stats() # random-ish output 3122 Used pool 0 / 0 times 3123 Pool contains 3 / 100 items 3124 """ 3125 return ["Used pool %s / %s times" % (use_pool, total_alloc), 3126 "Pool contains %s / %s items" % (integer_pool_count, integer_pool_size)] 2963 3127 2964 3128 cdef integer(x):
Note: See TracChangeset
for help on using the changeset viewer.
