/test3/gmpy_test_mpz.py

http://gmpy.googlecode.com/ · Python · 967 lines · 917 code · 3 blank · 47 comment · 19 complexity · 7bf893f396aded8043f1750ca43ed50a MD5 · raw file

  1. # partial unit test for gmpy2 mpz functionality
  2. # relies on Tim Peters' "doctest.py" test-driver
  3. r'''
  4. >>> list(filter(lambda x: not x.startswith('_'), dir(_g)))
  5. ['Default', 'DivisionByZeroError', 'ExponentOutOfBoundsError', 'InexactResultError', 'InvalidOperationError', 'OverflowResultError', 'RangeError', 'RoundAwayZero', 'RoundDown', 'RoundToNearest', 'RoundToZero', 'RoundUp', 'UnderflowResultError', 'acos', 'acosh', 'add', 'agm', 'ai', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'bincoef', 'bit_clear', 'bit_flip', 'bit_length', 'bit_mask', 'bit_scan0', 'bit_scan1', 'bit_set', 'bit_test', 'c_div', 'c_div_2exp', 'c_divmod', 'c_divmod_2exp', 'c_mod', 'c_mod_2exp', 'cbrt', 'ceil', 'check_range', 'comb', 'const_catalan', 'const_euler', 'const_log2', 'const_pi', 'context', 'copy_sign', 'cos', 'cosh', 'cot', 'coth', 'csc', 'csch', 'degrees', 'denom', 'digamma', 'digits', 'div', 'div_2exp', 'divexact', 'divm', 'eint', 'erf', 'erfc', 'exp', 'exp10', 'exp2', 'expm1', 'f2q', 'f_div', 'f_div_2exp', 'f_divmod', 'f_divmod_2exp', 'f_mod', 'f_mod_2exp', 'fac', 'factorial', 'fib', 'fib2', 'floor', 'fma', 'fmod', 'fms', 'frac', 'frexp', 'from_binary', 'fsum', 'gamma', 'gcd', 'gcdext', 'get_cache', 'get_context', 'get_emax_max', 'get_emin_min', 'get_exp', 'get_max_precision', 'hamdist', 'hypot', 'ieee', 'inf', 'invert', 'iroot', 'iroot_rem', 'is_bpsw_prp', 'is_euler_prp', 'is_even', 'is_extra_strong_lucas_prp', 'is_fermat_prp', 'is_fibonacci_prp', 'is_finite', 'is_inf', 'is_infinite', 'is_integer', 'is_lessgreater', 'is_lucas_prp', 'is_nan', 'is_number', 'is_odd', 'is_power', 'is_prime', 'is_regular', 'is_selfridge_prp', 'is_signed', 'is_square', 'is_strong_bpsw_prp', 'is_strong_lucas_prp', 'is_strong_prp', 'is_strong_selfridge_prp', 'is_unordered', 'is_zero', 'isqrt', 'isqrt_rem', 'j0', 'j1', 'jacobi', 'jn', 'kronecker', 'lcm', 'legendre', 'lgamma', 'li2', 'license', 'lngamma', 'local_context', 'log', 'log10', 'log1p', 'log2', 'lucas', 'lucas2', 'lucasu', 'lucasu_mod', 'lucasv', 'lucasv_mod', 'max2', 'maxnum', 'min2', 'minnum', 'modf', 'mp_limbsize', 'mp_version', 'mpc', 'mpc_random', 'mpc_version', 'mpfr', 'mpfr_from_old_binary', 'mpfr_grandom', 'mpfr_random', 'mpfr_version', 'mpq', 'mpq_from_old_binary', 'mpz', 'mpz_from_old_binary', 'mpz_random', 'mpz_rrandomb', 'mpz_urandomb', 'mul', 'mul_2exp', 'nan', 'next_above', 'next_below', 'next_prime', 'next_toward', 'norm', 'num_digits', 'numer', 'pack', 'phase', 'polar', 'popcount', 'powmod', 'proj', 'qdiv', 'radians', 'random_state', 'rec_sqrt', 'rect', 'reldiff', 'remainder', 'remove', 'remquo', 'rint', 'rint_ceil', 'rint_floor', 'rint_round', 'rint_trunc', 'root', 'round2', 'round_away', 'sec', 'sech', 'set_cache', 'set_context', 'set_exp', 'set_sign', 'sign', 'sin', 'sin_cos', 'sinh', 'sinh_cosh', 'sqrt', 'square', 'sub', 't_div', 't_div_2exp', 't_divmod', 't_divmod_2exp', 't_mod', 't_mod_2exp', 'tan', 'tanh', 'to_binary', 'trunc', 'unpack', 'version', 'xbit_mask', 'xmpz', 'y0', 'y1', 'yn', 'zero', 'zeta']
  6. >>> list([x for x in dir(a) if x != '__dir__'])
  7. ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__index__', '__init__', '__int__', '__invert__', '__ipow__', '__irshift__', '__isub__', '__le__', '__len__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_clear', 'bit_flip', 'bit_length', 'bit_scan0', 'bit_scan1', 'bit_set', 'bit_test', 'denominator', 'digits', 'num_digits', 'numerator']
  8. >>>
  9. '''
  10. import gmpy2 as _g, doctest, sys, operator, gc
  11. __test__={}
  12. a=_g.mpz(123)
  13. b=_g.mpz(456)
  14. # Disable tests since they are not reliable with Python 3.1 but left behind
  15. # in case it is needed in the future.
  16. if sys.platform in ('__DISABLE__linux2', '__DISABLE__darwin'):
  17. def _memsize():
  18. """ this function tries to return a measurement of how much memory
  19. this process is consuming (if it doesn't manage to, it returns 0).
  20. """
  21. import os
  22. try: x = int(os.popen('ps -p %d -o vsz|tail -1' % os.getpid()).read())
  23. except: x = 0
  24. return x
  25. else:
  26. def _memsize():
  27. return 0
  28. def factorize(x):
  29. r'''
  30. >>> factorize(a)
  31. [3, 41]
  32. >>> factorize(b)
  33. [2, 2, 2, 3, 19]
  34. >>>
  35. '''
  36. savex=x
  37. prime=2
  38. x=_g.mpz(x)
  39. factors=[]
  40. while x>=prime:
  41. newx,mult=_g.remove(x,prime)
  42. if mult:
  43. factors.extend([int(prime)]*mult)
  44. x=newx
  45. prime=_g.next_prime(prime)
  46. for factor in factors: assert _g.is_prime(factor)
  47. from operator import mul
  48. from functools import reduce
  49. assert reduce(mul, factors)==savex
  50. return factors
  51. __test__['index']=\
  52. r'''
  53. >>> range(333)[a]
  54. 123
  55. >>> range(333)[b]
  56. Traceback (innermost last):
  57. ...
  58. IndexError: range object index out of range
  59. '''
  60. __test__['elemop']=\
  61. r'''
  62. >>> a+b
  63. mpz(579)
  64. >>> a-b
  65. mpz(-333)
  66. >>> a*b
  67. mpz(56088)
  68. >>> a//b
  69. mpz(0)
  70. >>> a/b
  71. mpfr('0.26973684210526316')
  72. >>> b//a
  73. mpz(3)
  74. >>> b/a
  75. mpfr('3.7073170731707319')
  76. >>> a%b
  77. mpz(123)
  78. >>> 0%b
  79. mpz(0)
  80. >>> b+a
  81. mpz(579)
  82. >>> b-a
  83. mpz(333)
  84. >>> b*a
  85. mpz(56088)
  86. >>> b%a
  87. mpz(87)
  88. >>> divmod(a,b)
  89. (mpz(0), mpz(123))
  90. >>> divmod(b,a)
  91. (mpz(3), mpz(87))
  92. >>> divmod(0,b)
  93. (mpz(0), mpz(0))
  94. >>> -a
  95. mpz(-123)
  96. >>> a+1
  97. mpz(124)
  98. >>> a+(-1)
  99. mpz(122)
  100. >>> (-1)+a
  101. mpz(122)
  102. >>> 1+a
  103. mpz(124)
  104. >>> a-1
  105. mpz(122)
  106. >>> a-(-1)
  107. mpz(124)
  108. >>> 1-a
  109. mpz(-122)
  110. >>> (-1)-a
  111. mpz(-124)
  112. >>> a+True
  113. mpz(124)
  114. >>> a+False
  115. mpz(123)
  116. >>> a*False
  117. mpz(0)
  118. >>> a//True
  119. mpz(123)
  120. >>> abs(-a)==a
  121. 1
  122. >>> pow(a,10)
  123. mpz(792594609605189126649)
  124. >>> pow(a,7,b)
  125. mpz(99)
  126. >>> _g.sign(b-a)
  127. 1
  128. >>> _g.sign(b-b)
  129. 0
  130. >>> _g.sign(a-b)
  131. -1
  132. >>> _g.sign(a)
  133. 1
  134. >>> _g.sign(-a)
  135. -1
  136. >>> z=b-b; _g.sign(z)
  137. 0
  138. >>> import pickle
  139. >>> pickle.loads(pickle.dumps(_g.mpz(12346789)))
  140. mpz(12346789)
  141. >>> s='12345678901234567890123456789'
  142. >>> int(s) == _g.mpz(s)
  143. True
  144. >>> _g.mpz(s) == int(s)
  145. True
  146. >>> del s
  147. >>> _g.is_even(a)
  148. False
  149. >>> _g.is_odd(a)
  150. True
  151. >>> _g.is_even(b)
  152. True
  153. >>> _g.is_odd(b)
  154. False
  155. >>> _g.is_even(2)
  156. True
  157. '''
  158. __test__['special'] = \
  159. r'''
  160. >>> _g.context().trap_divzero == False
  161. True
  162. >>> a == float('Inf')
  163. False
  164. >>> a != float('Inf')
  165. True
  166. >>> a > float('Inf')
  167. False
  168. >>> a >= float('Inf')
  169. False
  170. >>> a < float('Inf')
  171. True
  172. >>> a <= float('Inf')
  173. True
  174. >>> a == float('-Inf')
  175. False
  176. >>> a != float('-Inf')
  177. True
  178. >>> a > float('-Inf')
  179. True
  180. >>> a >= float('-Inf')
  181. True
  182. >>> a < float('-Inf')
  183. False
  184. >>> a <= float('-Inf')
  185. False
  186. >>> a == float('nan')
  187. False
  188. >>> a != float('nan')
  189. True
  190. >>> a > float('nan')
  191. False
  192. >>> a >= float('nan')
  193. False
  194. >>> a < float('nan')
  195. False
  196. >>> a <= float('nan')
  197. False
  198. >>> float('Inf') == a
  199. False
  200. >>> float('Inf') != a
  201. True
  202. >>> float('Inf') > a
  203. True
  204. >>> float('Inf') >= a
  205. True
  206. >>> float('Inf') < a
  207. False
  208. >>> float('Inf') <= a
  209. False
  210. >>> float('-Inf') == a
  211. False
  212. >>> float('-Inf') != a
  213. True
  214. >>> float('-Inf') > a
  215. False
  216. >>> float('-Inf') >= a
  217. False
  218. >>> float('-Inf') < a
  219. True
  220. >>> float('-Inf') <= a
  221. True
  222. >>> float('nan') == a
  223. False
  224. >>> float('nan') != a
  225. True
  226. >>> float('nan') > a
  227. False
  228. >>> float('nan') >= a
  229. False
  230. >>> float('nan') < a
  231. False
  232. >>> float('nan') <= a
  233. False
  234. >>> a + float('Inf')
  235. mpfr('inf')
  236. >>> float('Inf') + a
  237. mpfr('inf')
  238. >>> a + float('-Inf')
  239. mpfr('-inf')
  240. >>> float('-Inf') + a
  241. mpfr('-inf')
  242. >>> a + float('nan')
  243. mpfr('nan')
  244. >>> float('nan') + a
  245. mpfr('nan')
  246. >>> a - float('Inf')
  247. mpfr('-inf')
  248. >>> float('Inf') - a
  249. mpfr('inf')
  250. >>> a - float('-Inf')
  251. mpfr('inf')
  252. >>> float('-Inf') - a
  253. mpfr('-inf')
  254. >>> a - float('nan')
  255. mpfr('nan')
  256. >>> float('nan') - a
  257. mpfr('nan')
  258. >>> a * float('Inf')
  259. mpfr('inf')
  260. >>> float('Inf') * a
  261. mpfr('inf')
  262. >>> a * float('-Inf')
  263. mpfr('-inf')
  264. >>> float('-Inf') * a
  265. mpfr('-inf')
  266. >>> -a * float('Inf')
  267. mpfr('-inf')
  268. >>> float('Inf') * -a
  269. mpfr('-inf')
  270. >>> -a * float('-Inf')
  271. mpfr('inf')
  272. >>> float('-Inf') * -a
  273. mpfr('inf')
  274. >>> a * float('nan')
  275. mpfr('nan')
  276. >>> float('nan') * a
  277. mpfr('nan')
  278. >>> _g.mpz(0) * float('Inf')
  279. mpfr('nan')
  280. >>> _g.mpz(0) * float('-Inf')
  281. mpfr('nan')
  282. >>> float('Inf') * _g.mpz(0)
  283. mpfr('nan')
  284. >>> float('-Inf') * _g.mpz(0)
  285. mpfr('nan')
  286. >>> a / float('Inf')
  287. mpfr('0.0')
  288. >>> -a / float('Inf')
  289. mpfr('-0.0')
  290. >>> float('Inf') / a
  291. mpfr('inf')
  292. >>> float('Inf') / -a
  293. mpfr('-inf')
  294. >>> a / float('-Inf')
  295. mpfr('-0.0')
  296. >>> -a / float('-Inf')
  297. mpfr('0.0')
  298. >>> float('-Inf') / a
  299. mpfr('-inf')
  300. >>> float('-Inf') / -a
  301. mpfr('inf')
  302. >>> a / float('nan')
  303. mpfr('nan')
  304. >>> float('nan') / a
  305. mpfr('nan')
  306. >>> float('nan') / _g.mpz(0)
  307. mpfr('nan')
  308. >>> float('nan') / _g.mpz(0)
  309. mpfr('nan')
  310. >>> divmod(a, float('Inf'))
  311. (mpfr('0.0'), mpfr('123.0'))
  312. >>> divmod(a, float('-Inf'))
  313. (mpfr('-1.0'), mpfr('-inf'))
  314. >>> divmod(-a, float('Inf'))
  315. (mpfr('-1.0'), mpfr('inf'))
  316. >>> divmod(-a, float('-Inf'))
  317. (mpfr('0.0'), mpfr('-123.0'))
  318. >>> divmod(a, float('nan'))
  319. (mpfr('nan'), mpfr('nan'))
  320. >>> divmod(-a, float('nan'))
  321. (mpfr('nan'), mpfr('nan'))
  322. >>> divmod(_g.mpz(0), float('Inf'))
  323. (mpfr('0.0'), mpfr('0.0'))
  324. >>> divmod(_g.mpz(0), float('-Inf'))
  325. (mpfr('-0.0'), mpfr('-0.0'))
  326. >>> divmod(_g.mpz(0), float('nan'))
  327. (mpfr('nan'), mpfr('nan'))
  328. >>> divmod(float('Inf'), a)
  329. (mpfr('nan'), mpfr('nan'))
  330. >>> divmod(float('-Inf'), a)
  331. (mpfr('nan'), mpfr('nan'))
  332. >>> divmod(float('Inf'), -a)
  333. (mpfr('nan'), mpfr('nan'))
  334. >>> divmod(float('-Inf'), -a)
  335. (mpfr('nan'), mpfr('nan'))
  336. >>> divmod(float('nan'), a)
  337. (mpfr('nan'), mpfr('nan'))
  338. >>> divmod(float('nan'), -a)
  339. (mpfr('nan'), mpfr('nan'))
  340. >>> divmod(float('Inf'), _g.mpz(0))
  341. (mpfr('nan'), mpfr('nan'))
  342. >>> divmod(float('-Inf'), _g.mpz(0))
  343. (mpfr('nan'), mpfr('nan'))
  344. >>> divmod(float('nan'), _g.mpz(0))
  345. (mpfr('nan'), mpfr('nan'))
  346. >>> divmod(_g.mpfr('Inf'), _g.mpz(0))
  347. (mpfr('nan'), mpfr('nan'))
  348. >>> divmod(_g.mpfr('-Inf'), _g.mpz(0))
  349. (mpfr('nan'), mpfr('nan'))
  350. >>> divmod(_g.mpfr('nan'), _g.mpz(0))
  351. (mpfr('nan'), mpfr('nan'))
  352. '''
  353. __test__['divexact']=\
  354. r'''
  355. >>> a=_g.mpz('1234567912345678912345679')
  356. >>> b=_g.mpz('789789789789789789789789')
  357. >>> c=a*b
  358. >>> _g.divexact(c,a)
  359. mpz(789789789789789789789789)
  360. >>>
  361. '''
  362. __test__['divmod']=\
  363. r'''
  364. >>> _g.c_divmod(17,5)
  365. (mpz(4), mpz(-3))
  366. >>> _g.c_divmod(-17,5)
  367. (mpz(-3), mpz(-2))
  368. >>> _g.c_divmod(17,-5)
  369. (mpz(-3), mpz(2))
  370. >>> _g.c_divmod(-17,-5)
  371. (mpz(4), mpz(3))
  372. >>> _g.f_divmod(17,5)
  373. (mpz(3), mpz(2))
  374. >>> _g.f_divmod(-17,5)
  375. (mpz(-4), mpz(3))
  376. >>> _g.f_divmod(17,-5)
  377. (mpz(-4), mpz(-3))
  378. >>> _g.f_divmod(-17,-5)
  379. (mpz(3), mpz(-2))
  380. >>> _g.t_divmod(17,5)
  381. (mpz(3), mpz(2))
  382. >>> _g.t_divmod(-17,5)
  383. (mpz(-3), mpz(-2))
  384. >>> _g.t_divmod(17,-5)
  385. (mpz(-3), mpz(2))
  386. >>> _g.t_divmod(-17,-5)
  387. (mpz(3), mpz(-2))
  388. '''
  389. __test__['cmpr']=\
  390. r'''
  391. >>> c=_g.mpz(a)
  392. >>> c is a
  393. 1
  394. >>> c==a
  395. 1
  396. >>> c>a
  397. 0
  398. >>> c<a
  399. 0
  400. >>> a>b
  401. 0
  402. >>> a<b
  403. 1
  404. >>> not _g.mpz(0)
  405. 1
  406. >>> not a
  407. 0
  408. >>> _g.mpz(1) == None
  409. False
  410. >>> _g.mpz(1) == '1'
  411. False
  412. >>> _g.mpz(1) == 'abc'
  413. False
  414. >>> [_g.mpz(23), None].count(None)
  415. 1
  416. >>> _g.mpz(3.14)
  417. mpz(3)
  418. >>> _g.mpz(_g.mpq(17,3))
  419. mpz(5)
  420. >>> _g.mpz(23)
  421. mpz(23)
  422. >>> _g.mpz(-23)
  423. mpz(-23)
  424. >>> x=1000*1000*1000*1000*1000*1000*1000
  425. >>> _g.mpz(x)
  426. mpz(1000000000000000000000)
  427. >>> a == float('Inf')
  428. False
  429. >>> a != float('Inf')
  430. True
  431. >>> a > float('Inf')
  432. False
  433. >>> a >= float('Inf')
  434. False
  435. >>> a < float('Inf')
  436. True
  437. >>> a <= float('Inf')
  438. True
  439. >>> a == float('-Inf')
  440. False
  441. >>> a != float('-Inf')
  442. True
  443. >>> a > float('-Inf')
  444. True
  445. >>> a >= float('-Inf')
  446. True
  447. >>> a < float('-Inf')
  448. False
  449. >>> a <= float('-Inf')
  450. False
  451. >>> a == float('nan')
  452. False
  453. >>> a != float('nan')
  454. True
  455. >>> a > float('nan')
  456. False
  457. >>> a >= float('nan')
  458. False
  459. >>> a < float('nan')
  460. False
  461. >>> a <= float('nan')
  462. False
  463. '''
  464. __test__['bitops']=\
  465. r'''
  466. >>> ~a
  467. mpz(-124)
  468. >>> a&b
  469. mpz(72)
  470. >>> a|b
  471. mpz(507)
  472. >>> a^b
  473. mpz(435)
  474. >>> a<<1
  475. mpz(246)
  476. >>> a>>1
  477. mpz(61)
  478. >>> a<<-1
  479. Traceback (innermost last):
  480. ...
  481. ValueError: negative shift count
  482. >>> a>>-2
  483. Traceback (innermost last):
  484. ...
  485. ValueError: negative shift count
  486. >>> a<<0
  487. mpz(123)
  488. >>> a>>0
  489. mpz(123)
  490. >>> _g.popcount(a)
  491. 6
  492. >>> _g.popcount(b)
  493. 4
  494. >>> _g.popcount(-7)
  495. -1
  496. >>> _g.popcount(0)
  497. 0
  498. >>> _g.hamdist(a,b)
  499. 6
  500. >>> _g.hamdist(3)
  501. Traceback (innermost last):
  502. ...
  503. TypeError: hamdist() requires 'mpz','mpz' arguments
  504. >>> _g.hamdist(a)
  505. Traceback (innermost last):
  506. ...
  507. TypeError: hamdist() requires 'mpz','mpz' arguments
  508. >>> _g.hamdist(a, 3, 4)
  509. Traceback (innermost last):
  510. ...
  511. TypeError: hamdist() requires 'mpz','mpz' arguments
  512. >>> _g.f_mod_2exp(a,5)
  513. mpz(27)
  514. >>> _g.f_mod_2exp(b,5)
  515. mpz(8)
  516. >>> _g.f_mod_2exp(b,5)==(b%32)
  517. 1
  518. >>> _g.f_mod_2exp(a,5)==(a%32)
  519. 1
  520. >>> a.bit_set(20)
  521. mpz(1048699)
  522. >>> a.bit_clear(0)
  523. mpz(122)
  524. >>> for i in range(8):
  525. ... print(a.bit_test(i))
  526. ...
  527. True
  528. True
  529. False
  530. True
  531. True
  532. True
  533. True
  534. False
  535. >>> for i in range(10):
  536. ... print(b.bit_test(i))
  537. ...
  538. False
  539. False
  540. False
  541. True
  542. False
  543. False
  544. True
  545. True
  546. True
  547. False
  548. >>> [a.bit_scan0(j) for j in range(33)]
  549. [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]
  550. >>> [a.bit_scan1(j) for j in range(10)]
  551. [0, 1, 3, 3, 4, 5, 6, None, None, None]
  552. >>> n=_g.mpz(-(7+6*16+5*256+7*4092))
  553. >>> [n.bit_scan0(j) for j in range(18)]
  554. [1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
  555. >>> [n.bit_scan1(j) for j in range(33)]
  556. [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]
  557. >>> _g.mpz(0).bit_length()
  558. 0
  559. >>> _g.mpz(12345).bit_length()
  560. 14
  561. >>> _g.bit_mask(9)
  562. mpz(511)
  563. '''
  564. __test__['format']=\
  565. r'''
  566. >>> str(a)
  567. '123'
  568. >>> repr(a)
  569. 'mpz(123)'
  570. >>> hex(a)
  571. '0x7b'
  572. >>> oct(a)
  573. '0o173'
  574. >>> _g.mpz('123')
  575. mpz(123)
  576. >>> _g.mpz('1001001011',2)
  577. mpz(587)
  578. >>> _g.mpz('1001001011',2).digits(2)
  579. '1001001011'
  580. >>> for i in range(2,63):
  581. ... print(a.digits(i))
  582. ...
  583. 1111011
  584. 11120
  585. 1323
  586. 443
  587. 323
  588. 234
  589. 173
  590. 146
  591. 123
  592. 102
  593. a3
  594. 96
  595. 8b
  596. 83
  597. 7b
  598. 74
  599. 6f
  600. 69
  601. 63
  602. 5i
  603. 5d
  604. 58
  605. 53
  606. 4n
  607. 4j
  608. 4f
  609. 4b
  610. 47
  611. 43
  612. 3u
  613. 3r
  614. 3o
  615. 3l
  616. 3i
  617. 3f
  618. 3C
  619. 39
  620. 36
  621. 33
  622. 30
  623. 2d
  624. 2b
  625. 2Z
  626. 2X
  627. 2V
  628. 2T
  629. 2R
  630. 2P
  631. 2N
  632. 2L
  633. 2J
  634. 2H
  635. 2F
  636. 2D
  637. 2B
  638. 29
  639. 27
  640. 25
  641. 23
  642. 21
  643. 1z
  644. >>> print(a.digits(63))
  645. Traceback (innermost last):
  646. ...
  647. ValueError: base must be in the interval 2 ... 62
  648. >>> _g.mpz('43')
  649. mpz(43)
  650. >>> _g.mpz('043')
  651. mpz(43)
  652. >>> _g.mpz('43',0)
  653. mpz(43)
  654. >>> _g.mpz('0o43')
  655. mpz(35)
  656. >>> _g.mpz('0x43')
  657. mpz(67)
  658. >>> _g.mpz('0x43',10)
  659. Traceback (innermost last):
  660. File "<pyshell#181>", line 1, in ?
  661. _g.mpz('0x43')
  662. ValueError: invalid digits
  663. >>>
  664. '''
  665. __test__['binio']=\
  666. r'''
  667. >>> a == _g.from_binary(_g.to_binary(a))
  668. True
  669. >>> -a == _g.from_binary(_g.to_binary(-a))
  670. True
  671. >>> z = _g.mpz(573406620562849222387053)
  672. >>> for i in range(2,37):
  673. ... print(i,z.num_digits(i))
  674. ...
  675. 2 79
  676. 3 50
  677. 4 40
  678. 5 35
  679. 6 31
  680. 7 29
  681. 8 27
  682. 9 25
  683. 10 24
  684. 11 23
  685. 12 23
  686. 13 22
  687. 14 21
  688. 15 21
  689. 16 20
  690. 17 20
  691. 18 19
  692. 19 19
  693. 20 19
  694. 21 18
  695. 22 18
  696. 23 18
  697. 24 18
  698. 25 18
  699. 26 17
  700. 27 17
  701. 28 17
  702. 29 17
  703. 30 17
  704. 31 16
  705. 32 16
  706. 33 16
  707. 34 16
  708. 35 16
  709. 36 16
  710. >>> _g.num_digits(23)
  711. 2
  712. >>> _g.num_digits(23,2)
  713. 5
  714. >>> _g.num_digits(23,99)
  715. Traceback (most recent call last):
  716. File "<string>", line 1, in ?
  717. ValueError: base must be in the interval 2 ... 62
  718. >>> hash(a)
  719. 123
  720. >>> hash(b)
  721. 456
  722. >>> hash(z) == hash(int(z))
  723. True
  724. >>> int(_g.mpz(-3))
  725. -3
  726. '''
  727. __test__['number']=\
  728. r'''
  729. >>> _g.iroot_rem(a,2)
  730. (mpz(11), mpz(2))
  731. >>> _g.iroot_rem(a,3)
  732. (mpz(4), mpz(59))
  733. >>> _g.iroot_rem(a*a)
  734. Traceback (most recent call last):
  735. File "<stdin>", line 1, in <module>
  736. TypeError: iroot_rem() requires 'mpz','int' arguments
  737. >>> _g.iroot_rem(a*a,2)
  738. (mpz(123), mpz(0))
  739. >>> print(_g.isqrt(a))
  740. 11
  741. >>> print(_g.isqrt(b))
  742. 21
  743. >>> print(_g.isqrt_rem(a))
  744. (mpz(11), mpz(2))
  745. >>> print(_g.isqrt_rem(b))
  746. (mpz(21), mpz(15))
  747. >>> for i in range(5):
  748. ... print(_g.iroot(a,i+1),_g.iroot(b,i+1))
  749. ...
  750. (mpz(123), True) (mpz(456), True)
  751. (mpz(11), False) (mpz(21), False)
  752. (mpz(4), False) (mpz(7), False)
  753. (mpz(3), False) (mpz(4), False)
  754. (mpz(2), False) (mpz(3), False)
  755. >>> _g.is_square(a)
  756. 0
  757. >>> _g.is_power(a)
  758. 0
  759. >>> _g.is_square(99*99)
  760. 1
  761. >>> _g.is_square(99*99*99)
  762. 0
  763. >>> _g.is_square(0)
  764. 1
  765. >>> _g.is_square(-1)
  766. 0
  767. >>> _g.is_power(99*99*99)
  768. 1
  769. >>> _g.gcd(a,b)
  770. mpz(3)
  771. >>> temp=_g.gcdext(a,b)
  772. >>> temp[0]==a*temp[1]+b*temp[2]
  773. True
  774. >>> _g.lcm(a,b)
  775. mpz(18696)
  776. >>> _g.fac(7)
  777. mpz(5040)
  778. >>> _g.fib(17)
  779. mpz(1597)
  780. >>> for i in range(10):
  781. ... print(_g.bincoef(10,i))
  782. ...
  783. 1
  784. 10
  785. 45
  786. 120
  787. 210
  788. 252
  789. 210
  790. 120
  791. 45
  792. 10
  793. >>> _g.divm(b,a,20)
  794. mpz(12)
  795. >>> _g.divm(a,b,100)
  796. Traceback (innermost last):
  797. File "<pyshell#184>", line 1, in ?
  798. _g.divm(a,b,100)
  799. ZeroDivisionError: not invertible
  800. >>> _g.divm(6,12,14)
  801. mpz(4)
  802. >>> _g.divm(0,1,2)
  803. mpz(0)
  804. >>> # guard against regression of an ancient gmpy bug: divm w/non-coprime parms
  805. >>> _g.divm(4,8,20)
  806. mpz(3)
  807. >>> _g.divm(4,8,20)
  808. mpz(3)
  809. >>> _g.mpz(20)
  810. mpz(20)
  811. >>> _g.mpz(8)
  812. mpz(8)
  813. >>> _g.mpz(4)
  814. mpz(4)
  815. >>> # guard against regression of a memory leak in divm
  816. >>> __ = gc.collect()
  817. >>> _siz = 87654
  818. >>> _siz = _memsize()
  819. >>> for x in range(45678):
  820. ... _xx=_g.divm(b,a,20)
  821. >>> del _xx
  822. >>> __ = gc.collect()
  823. >>> (_memsize()-_siz) <= 32
  824. True
  825. >>> _g.invert(a,100)
  826. mpz(87)
  827. >>> _g.invert(b,100)
  828. Traceback (most recent call last):
  829. ...
  830. ZeroDivisionError: invert() no inverse exists
  831. >>> _g.invert(3)
  832. Traceback (innermost last):
  833. ...
  834. TypeError: invert() requires 'mpz','mpz' arguments
  835. >>> _g.invert()
  836. Traceback (innermost last):
  837. ...
  838. TypeError: invert() requires 'mpz','mpz' arguments
  839. >>> _g.comb(3,-1)
  840. Traceback (most recent call last):
  841. File "<stdin>", line 1, in ?
  842. ValueError: binomial coefficient with negative k
  843. >>> _g.isqrt(-1)
  844. Traceback (most recent call last):
  845. File "<stdin>", line 1, in ?
  846. ValueError: isqrt() of negative number
  847. >>> _g.isqrt_rem(-1)
  848. Traceback (most recent call last):
  849. File "<stdin>", line 1, in ?
  850. ValueError: isqrt_rem() of negative number
  851. >>> _g.remove(3,-1)
  852. Traceback (most recent call last):
  853. File "<stdin>", line 1, in ?
  854. ValueError: factor must be > 1
  855. >>> _g.remove(3)
  856. Traceback (innermost last):
  857. ...
  858. TypeError: remove() requires 'mpz','mpz' arguments
  859. >>> _g.remove()
  860. Traceback (innermost last):
  861. ...
  862. TypeError: remove() requires 'mpz','mpz' arguments
  863. >>> _g.is_prime(3,-3)
  864. Traceback (most recent call last):
  865. File "<stdin>", line 1, in ?
  866. ValueError: repetition count for is_prime() must be positive
  867. >>> _g.jacobi(10,3)
  868. 1
  869. >>> _g.jacobi(10,-3)
  870. Traceback (most recent call last):
  871. File "<stdin>", line 1, in ?
  872. ValueError: y must be odd and >0
  873. >>> _g.jacobi(3)
  874. Traceback (innermost last):
  875. ...
  876. TypeError: jacobi() requires 'mpz','mpz' arguments
  877. >>> _g.jacobi()
  878. Traceback (innermost last):
  879. ...
  880. TypeError: jacobi() requires 'mpz','mpz' arguments
  881. >>> _g.legendre(10,3)
  882. 1
  883. >>> _g.legendre(10,-3)
  884. Traceback (most recent call last):
  885. File "<stdin>", line 1, in ?
  886. ValueError: y must be odd and >0
  887. >>> _g.legendre(3)
  888. Traceback (innermost last):
  889. ...
  890. TypeError: legendre() requires 'mpz','mpz' arguments
  891. >>> _g.legendre()
  892. Traceback (innermost last):
  893. ...
  894. TypeError: legendre() requires 'mpz','mpz' arguments
  895. >>> _g.kronecker(10,3)
  896. 1
  897. >>> _g.kronecker(10,-3)
  898. 1
  899. >>> _g.kronecker(3)
  900. Traceback (innermost last):
  901. ...
  902. TypeError: kronecker() requires 'mpz','mpz' arguments
  903. >>> _g.kronecker()
  904. Traceback (innermost last):
  905. ...
  906. TypeError: kronecker() requires 'mpz','mpz' arguments
  907. >>> a=10**20
  908. >>> b=a+39
  909. >>> _g.jacobi(a,b)
  910. 1
  911. >>> _g.legendre(a,b)
  912. 1
  913. >>> _g.kronecker(a,b)
  914. 1
  915. '''
  916. def _test(chat=None):
  917. if chat:
  918. print("Unit tests for gmpy2 (mpz functionality)")
  919. print(" on Python %s" % sys.version)
  920. print("Testing gmpy2 {0}".format(_g.version()))
  921. print(" Mutliple-precision library: {0}".format(_g.mp_version()))
  922. print(" Floating-point library: {0}".format(_g.mpfr_version()))
  923. print(" Complex library: {0}".format(_g.mpc_version()))
  924. print(" Caching Values: (Number) {0}".format(_g.get_cache()[0]))
  925. print(" Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
  926. thismod = sys.modules.get(__name__)
  927. doctest.testmod(thismod, report=0)
  928. if chat: print("Repeating tests, with caching disabled")
  929. _g.set_cache(0,128)
  930. sav = sys.stdout
  931. class _Dummy:
  932. encoding = None
  933. def write(self,*whatever):
  934. pass
  935. try:
  936. sys.stdout = _Dummy()
  937. doctest.testmod(thismod, report=0)
  938. finally:
  939. sys.stdout = sav
  940. if chat:
  941. print()
  942. print("Overall results for mpz:")
  943. return doctest.master.summarize(chat)
  944. if __name__=='__main__':
  945. _test(1)