PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_mpz_functions.txt

http://gmpy.googlecode.com/
Plain Text | 627 lines | 517 code | 110 blank | 0 comment | 0 complexity | b6da4fc0bad1b5892064d487b4a06b89 MD5 | raw file
  1MPZ Related Functions
  2=====================
  3
  4    >>> import gmpy2 as G
  5    >>> from gmpy2 import mpz, mpq, mpfr
  6    >>> a = mpz(123)
  7    >>> b = mpz(456)
  8
  9Test gmpy2.add
 10--------------
 11    >>> G.add(a, b)
 12    mpz(579)
 13    >>> G.add(a, 456)
 14    mpz(579)
 15    >>> G.add(123, 456)
 16    mpz(579)
 17    >>> G.add(123, b)
 18    mpz(579)
 19
 20Test gmpy2.bincoef
 21------------------
 22
 23    >>> for i in range(10):
 24    ...     print(G.bincoef(10,i))
 25    ...
 26    1
 27    10
 28    45
 29    120
 30    210
 31    252
 32    210
 33    120
 34    45
 35    10
 36
 37Test gmpy2.bit_clear
 38--------------------
 39
 40    >>> a.bit_clear(0)
 41    mpz(122)
 42
 43Test gmpy2.bit_flip
 44-------------------
 45    >>> bin(a)
 46    '0b1111011'
 47    >>> bin(a.bit_flip(1))
 48    '0b1111001'
 49    >>> bin(a.bit_flip(2))
 50    '0b1111111'
 51
 52Test gmpy2.bit_length
 53---------------------
 54
 55    >>> G.mpz(0).bit_length()
 56    0
 57    >>> G.mpz(12345).bit_length()
 58    14
 59
 60Test gmpy2.bit_mask
 61-------------------
 62
 63    >>> G.bit_mask(9)
 64    mpz(511)
 65
 66Test gmpy2.bit_scan0
 67--------------------
 68
 69    >>> [a.bit_scan0(j) for j in range(33)]
 70    [2, 2, 2, 7, 7, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
 71    >>> n=G.mpz(-(7+6*16+5*256+7*4092))
 72    >>> [n.bit_scan0(j) for j in range(18)]
 73    [1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
 74    >>> del n
 75
 76Test gmpy2.bit_scan1
 77--------------------
 78
 79    >>> [a.bit_scan1(j) for j in range(10)]
 80    [0, 1, 3, 3, 4, 5, 6, None, None, None]
 81    >>> n=G.mpz(-(7+6*16+5*256+7*4092))
 82    >>> [n.bit_scan1(j) for j in range(33)]
 83    [0, 2, 2, 4, 4, 5, 7, 7, 9, 9, 11, 11, 15, 15, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
 84    >>> del n
 85
 86Test gmpy2.bit_set
 87------------------
 88
 89    >>> a.bit_set(20)
 90    mpz(1048699)
 91
 92Test gmpy2.bit_test
 93-------------------
 94
 95    >>> [a.bit_test(i) for i in range(8)]
 96    [True, True, False, True, True, True, True, False]
 97    >>> [b.bit_test(i) for i in range(10)]
 98    [False, False, False, True, False, False, True, True, True, False]
 99
100Test gmpy2.c_div
101----------------
102
103    >>> G.c_div(b,64)
104    mpz(8)
105    >>> G.c_div(b,-64)
106    mpz(-7)
107    >>> G.c_div(-b,64)
108    mpz(-7)
109    >>> G.c_div(-b,-64)
110    mpz(8)
111
112Test gmpy2.c_div_2exp
113---------------------
114
115    >>> G.c_div_2exp(b, 6)
116    mpz(8)
117    >>> G.c_div_2exp(b, -6)
118    Traceback (most recent call last):
119      File "<stdin>", line 1, in <module>
120    OverflowError: can't convert negative value to unsigned int
121    >>> G.c_div_2exp(-b, 6)
122    mpz(-7)
123
124Test gmpy2.c_divmod
125-------------------
126
127    >>> G.c_divmod(b,64)
128    (mpz(8), mpz(-56))
129    >>> G.c_divmod(b,-64)
130    (mpz(-7), mpz(8))
131    >>> G.c_divmod(-b,64)
132    (mpz(-7), mpz(-8))
133    >>> G.c_divmod(-b,-64)
134    (mpz(8), mpz(56))
135    >>> G.c_divmod(17,5)
136    (mpz(4), mpz(-3))
137    >>> G.c_divmod(-17,5)
138    (mpz(-3), mpz(-2))
139    >>> G.c_divmod(17,-5)
140    (mpz(-3), mpz(2))
141    >>> G.c_divmod(-17,-5)
142    (mpz(4), mpz(3))
143    >>> G.c_divmod(b, 4.0)
144    Traceback (most recent call last):
145      File "<stdin>", line 1, in <module>
146    TypeError: c_divmod() requires 'mpz','mpz' arguments
147    >>> G.c_divmod('a', b)
148    Traceback (most recent call last):
149      File "<stdin>", line 1, in <module>
150    TypeError: c_divmod() requires 'mpz','mpz' arguments
151    >>> G.c_divmod(b)
152    Traceback (most recent call last):
153      File "<stdin>", line 1, in <module>
154    TypeError: c_divmod() requires 'mpz','mpz' arguments
155
156Test gmpy2.c_divmod_2exp
157------------------------
158
159    >>> G.c_divmod_2exp(b, 6)
160    (mpz(8), mpz(-56))
161    >>> G.c_divmod_2exp(-b, 6)
162    (mpz(-7), mpz(-8))
163
164Test gmpy2.c_mod
165----------------
166
167    >>> G.c_mod(b,64)
168    mpz(-56)
169    >>> G.c_mod(b,-64)
170    mpz(8)
171    >>> G.c_mod(-b,64)
172    mpz(-8)
173    >>> G.c_mod(-b,-64)
174    mpz(56)
175
176Test gmpy2.c_mod_2exp
177---------------------
178
179    >>> G.c_mod_2exp(b, 6)
180    mpz(-56)
181    >>> G.c_mod_2exp(-b, 6)
182    mpz(-8)
183
184Test gmpy2.comb
185---------------
186
187    >>> G.comb(3,-1)
188    Traceback (most recent call last):
189      ...
190    ValueError: binomial coefficient with negative k
191    >>> G.comb(8,4)
192    mpz(70)
193
194Test gmpy2.divexact
195-------------------
196
197    >>> aa=G.mpz('1234567912345678912345679')
198    >>> bb=G.mpz('789789789789789789789789')
199    >>> cc=aa*bb
200    >>> print(G.divexact(cc,aa))
201    789789789789789789789789
202    >>> del aa,bb,cc
203
204Test gmpy2.divm
205---------------
206
207    >>> G.divm(b,a,20)
208    mpz(12)
209    >>> G.divm(a,b,100)
210    Traceback (innermost last):
211      ...
212    ZeroDivisionError: not invertible
213    >>> G.divm(6,12,14)
214    mpz(4)
215    >>> G.divm(0,1,2)
216    mpz(0)
217    >>> G.divm(4,8,20)
218    mpz(3)
219
220Test gmpy2.f_div
221----------------
222
223Test gmpy2.f_div_2exp
224---------------------
225
226Test gmpy2.f_divmod
227-------------------
228
229    >>> G.f_divmod(17,5)
230    (mpz(3), mpz(2))
231    >>> G.f_divmod(-17,5)
232    (mpz(-4), mpz(3))
233    >>> G.f_divmod(17,-5)
234    (mpz(-4), mpz(-3))
235    >>> G.f_divmod(-17,-5)
236    (mpz(3), mpz(-2))
237    >>> G.f_divmod(b,64)
238    (mpz(7), mpz(8))
239    >>> G.f_divmod(-b,64)
240    (mpz(-8), mpz(56))
241    >>> G.f_divmod(b,-64)
242    (mpz(-8), mpz(-56))
243    >>> G.f_divmod(-b,-64)
244    (mpz(7), mpz(-8))
245
246Test gmpy2.f_divmod_2exp
247------------------------
248
249    >>> G.f_divmod_2exp(b,6)
250    (mpz(7), mpz(8))
251    >>> G.f_divmod_2exp(-b,6)
252    (mpz(-8), mpz(56))
253
254Test gmpy2.f_mod
255----------------
256
257Test gmpy2.f_mod_2exp
258---------------------
259
260    >>> G.f_mod_2exp(a,5)
261    mpz(27)
262    >>> G.f_mod_2exp(b,5)
263    mpz(8)
264    >>> G.f_mod_2exp(b,5) == (b%32)
265    True
266    >>> G.f_mod_2exp(a,5) == (a%32)
267    True
268
269Test gmpy2.fac
270--------------
271
272    >>> G.fac(7)
273    mpz(5040)
274
275Test gmpy2.fib
276--------------
277
278    >>> G.fib(17)
279    mpz(1597)
280
281Test gmpy2.fib2
282---------------
283
284    >>> G.fib2(17)
285    (mpz(1597), mpz(987))
286
287Test gmpy2.gcd
288--------------
289
290    >>> G.gcd(a,b)
291    mpz(3)
292
293Test gmpy2.gcdext
294-----------------
295
296    >>> temp=G.gcdext(a,b)
297    >>> temp[0]==a*temp[1]+b*temp[2]
298    True
299    >>> temp=G.gcdext(123,456)
300    >>> temp[0]==a*temp[1]+b*temp[2]
301    True
302    >>> del temp
303
304Test gmpy2.hamdist
305------------------
306
307    >>> G.hamdist(a,b)
308    6
309    >>> G.hamdist(3)
310    Traceback (innermost last):
311      ...
312    TypeError: hamdist() requires 'mpz','mpz' arguments
313    >>> G.hamdist(a)
314    Traceback (innermost last):
315      ...
316    TypeError: hamdist() requires 'mpz','mpz' arguments
317    >>> G.hamdist(a, 3, 4)
318    Traceback (innermost last):
319      ...
320    TypeError: hamdist() requires 'mpz','mpz' arguments
321
322Test gmpy2.invert
323-----------------
324
325    >>> G.invert(a,100)
326    mpz(87)
327    >>> G.invert(b,100)
328    mpz(0)
329    >>> G.invert(3)
330    Traceback (innermost last):
331      ...
332    TypeError: invert() requires 'mpz','mpz' arguments
333    >>> G.invert()
334    Traceback (innermost last):
335      ...
336    TypeError: invert() requires 'mpz','mpz' arguments
337
338Test gmpy2.iroot
339----------------
340
341    >>> for i in range(5):
342    ...    print(G.iroot(a,i+1),G.iroot(b,i+1))
343    ...
344    (mpz(123), True) (mpz(456), True)
345    (mpz(11), False) (mpz(21), False)
346    (mpz(4), False) (mpz(7), False)
347    (mpz(3), False) (mpz(4), False)
348    (mpz(2), False) (mpz(3), False)
349    >>> G.iroot(9,2)
350    (mpz(3), True)
351
352Test gmpy2.iroot_rem
353--------------------
354
355    >>> G.iroot_rem(a,2)
356    (mpz(11), mpz(2))
357    >>> G.iroot_rem(a,3)
358    (mpz(4), mpz(59))
359    >>> G.iroot_rem(a*a)
360    Traceback (most recent call last):
361      ...
362    TypeError: iroot_rem() requires 'mpz','int' arguments
363    >>> G.iroot_rem(a*a,2)
364    (mpz(123), mpz(0))
365
366Test gmpy2.is_even
367------------------
368
369    >>> G.is_even(a)
370    False
371    >>> G.is_even(b)
372    True
373
374Test gmpy2.is_odd
375-----------------
376
377    >>> G.is_odd(a)
378    True
379    >>> G.is_odd(b)
380    False
381
382Test gmpy2.is_power
383-------------------
384
385    >>> G.is_power()
386    Traceback (most recent call last):
387      File "<stdin>", line 1, in <module>
388    TypeError: is_power() takes exactly one argument (0 given)    >>> a.is_power()
389    >>> G.is_power(99*99*99)
390    True
391    >>> G.is_power(99*98)
392    False
393
394Test gmpy2.is_prime
395-------------------
396
397    >>> G.is_prime(3,-3)
398    Traceback (most recent call last):
399      ...
400    ValueError: repetition count for is_prime must be positive
401    >>> G.is_prime(12345)
402    False
403    >>> G.is_prime(80**81 + 81**80)
404    True
405
406Test gmpy2.is_square
407--------------------
408
409    >>> a.is_square()
410    False
411    >>> G.is_square(99*99)
412    True
413    >>> G.is_square(99*99*99)
414    False
415    >>> G.is_square(0)
416    True
417    >>> G.is_square(-1)
418    False
419
420Test gmpy2.isqrt
421----------------
422
423    >>> print(G.isqrt(a))
424    11
425    >>> print(G.isqrt(b))
426    21
427    >>> G.isqrt(-1)
428    Traceback (most recent call last):
429      ...
430    ValueError: isqrt() of negative number
431
432Test gmpy2.isqrt_rem
433--------------------
434
435    >>> print(G.isqrt_rem(a))
436    (mpz(11), mpz(2))
437    >>> print(G.isqrt_rem(b))
438    (mpz(21), mpz(15))
439    >>> G.isqrt_rem(-1)
440    Traceback (most recent call last):
441      ...
442    ValueError: isqrt_rem() of negative number
443
444Test gmpy2.jacobi
445-----------------
446
447    >>> G.jacobi(10,3)
448    1
449    >>> G.jacobi(10,-3)
450    Traceback (most recent call last):
451      ...
452    ValueError: jacobi's y must be odd prime > 0
453    >>> G.jacobi(3)
454    Traceback (innermost last):
455      ...
456    TypeError: jacobi() requires 'mpz','mpz' arguments
457    >>> G.jacobi()
458    Traceback (innermost last):
459      ...
460    TypeError: jacobi() requires 'mpz','mpz' arguments
461
462Test gmpy2.kronecker
463--------------------
464
465    >>> G.kronecker(10,3)
466    1
467    >>> G.kronecker(10,-3)
468    1
469    >>> G.kronecker(3)
470    Traceback (innermost last):
471      ...
472    TypeError: kronecker() requires 'mpz','mpz' arguments
473    >>> G.kronecker()
474    Traceback (innermost last):
475      ...
476    TypeError: kronecker() requires 'mpz','mpz' arguments
477    >>> aaa = 10**20
478    >>> bbb = aaa+39
479    >>> G.jacobi(aaa,bbb)
480    1
481    >>> G.legendre(aaa,bbb)
482    1
483    >>> G.kronecker(aaa,bbb)
484    1
485    >>> del aaa,bbb
486
487Test gmpy2.lcm
488--------------
489
490    >>> G.lcm(a,b)
491    mpz(18696)
492
493Test gmpy2.legendre
494-------------------
495
496    >>> G.legendre(10,3)
497    1
498    >>> G.legendre(10,-3)
499    Traceback (most recent call last):
500      ...
501    ValueError: legendre's y must be odd and > 0
502    >>> G.legendre(3)
503    Traceback (innermost last):
504      ...
505    TypeError: legendre() requires 'mpz','mpz' arguments
506    >>> G.legendre()
507    Traceback (innermost last):
508      ...
509    TypeError: legendre() requires 'mpz','mpz' arguments
510
511Test gmpy2.lucas
512----------------
513
514Test gmpy2.lucas2
515-----------------
516
517Test gmpy2.mul
518--------------
519
520Test gmpy2.mul_2exp
521-------------------
522
523Test gmpy2.next_prime
524---------------------
525
526Test gmpy2.numdigits
527--------------------
528
529    >>> G.numdigits(23)
530    2
531    >>> G.numdigits(23,2)
532    5
533    >>> G.numdigits(23,99)
534    Traceback (most recent call last):
535      ...
536    ValueError: base must be either 0 or in the interval 2 ... 62
537
538Test gmpy2.pack
539---------------
540
541Test gmpy2.popcount
542-------------------
543
544    >>> G.popcount(a)
545    6
546    >>> G.popcount(b)
547    4
548    >>> G.popcount(-7)
549    -1
550    >>> G.popcount(0)
551    0
552
553Test gmpy2.remove
554-----------------
555
556gmpy2.remove factors out multiple copies of a factor from a larger integer.
557The factor must be greater than or equal to 2.
558
559    >>> G.remove(a,2)
560    (mpz(123), 0)
561    >>> G.remove(a,3)
562    (mpz(41), 1)
563    >>> G.remove(b,2)
564    (mpz(57), 3)
565    >>> G.remove(b,3)
566    (mpz(152), 1)
567    >>> G.remove(b,1)
568    Traceback (most recent call last):
569      File "<stdin>", line 1, in <module>
570    ValueError: factor must be > 1
571    >>> G.remove(b,0)
572    Traceback (most recent call last):
573      File "<stdin>", line 1, in <module>
574    ValueError: factor must be > 1
575    >>> G.remove(b,789)
576    (mpz(456), 0)
577    >>> G.remove(b,-3)
578    Traceback (most recent call last):
579      File "<stdin>", line 1, in <module>
580    ValueError: factor must be > 1
581    >>> G.remove(b,float('NaN'))
582    Traceback (most recent call last):
583      File "<stdin>", line 1, in <module>
584    TypeError: remove() requires 'mpz','mpz' arguments
585    >>> G.remove(3,-1)
586    Traceback (most recent call last):
587      ...
588    ValueError: factor must be > 1
589    >>> G.remove(3)
590    Traceback (innermost last):
591      ...
592    TypeError: remove() requires 'mpz','mpz' arguments
593    >>> G.remove()
594    Traceback (innermost last):
595      ...
596    TypeError: remove() requires 'mpz','mpz' arguments
597
598Test gmpy2.t_div
599----------------
600
601Test gmpy2.t_div_2exp
602---------------------
603
604Test gmpy2.t_divmod
605-------------------
606
607    >>> G.t_divmod(17,5)
608    (mpz(3), mpz(2))
609    >>> G.t_divmod(-17,5)
610    (mpz(-3), mpz(-2))
611    >>> G.t_divmod(17,-5)
612    (mpz(-3), mpz(2))
613    >>> G.t_divmod(-17,-5)
614    (mpz(3), mpz(-2))
615
616Test gmpy2.t_divmod_2exp
617------------------------
618
619Test gmpy2.t_mod
620----------------
621
622Test gmpy2.t_mod_2exp
623---------------------
624
625
626
627