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