test_continued_fraction.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import itertools
  2. from sympy.core import GoldenRatio as phi
  3. from sympy.core.numbers import (Rational, pi)
  4. from sympy.core.singleton import S
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.ntheory.continued_fraction import \
  7. (continued_fraction_periodic as cf_p,
  8. continued_fraction_iterator as cf_i,
  9. continued_fraction_convergents as cf_c,
  10. continued_fraction_reduce as cf_r,
  11. continued_fraction as cf)
  12. from sympy.testing.pytest import raises
  13. def test_continued_fraction():
  14. assert cf_p(1, 1, 10, 0) == cf_p(1, 1, 0, 1)
  15. assert cf_p(1, -1, 10, 1) == cf_p(-1, 1, 10, -1)
  16. t = sqrt(2)
  17. assert cf((1 + t)*(1 - t)) == cf(-1)
  18. for n in [0, 2, Rational(2, 3), sqrt(2), 3*sqrt(2), 1 + 2*sqrt(3)/5,
  19. (2 - 3*sqrt(5))/7, 1 + sqrt(2), (-5 + sqrt(17))/4]:
  20. assert (cf_r(cf(n)) - n).expand() == 0
  21. assert (cf_r(cf(-n)) + n).expand() == 0
  22. raises(ValueError, lambda: cf(sqrt(2 + sqrt(3))))
  23. raises(ValueError, lambda: cf(sqrt(2) + sqrt(3)))
  24. raises(ValueError, lambda: cf(pi))
  25. raises(ValueError, lambda: cf(.1))
  26. raises(ValueError, lambda: cf_p(1, 0, 0))
  27. raises(ValueError, lambda: cf_p(1, 1, -1))
  28. assert cf_p(4, 3, 0) == [1, 3]
  29. assert cf_p(0, 3, 5) == [0, 1, [2, 1, 12, 1, 2, 2]]
  30. assert cf_p(1, 1, 0) == [1]
  31. assert cf_p(3, 4, 0) == [0, 1, 3]
  32. assert cf_p(4, 5, 0) == [0, 1, 4]
  33. assert cf_p(5, 6, 0) == [0, 1, 5]
  34. assert cf_p(11, 13, 0) == [0, 1, 5, 2]
  35. assert cf_p(16, 19, 0) == [0, 1, 5, 3]
  36. assert cf_p(27, 32, 0) == [0, 1, 5, 2, 2]
  37. assert cf_p(1, 2, 5) == [[1]]
  38. assert cf_p(0, 1, 2) == [1, [2]]
  39. assert cf_p(6, 7, 49) == [1, 1, 6]
  40. assert cf_p(3796, 1387, 0) == [2, 1, 2, 1, 4]
  41. assert cf_p(3245, 10000) == [0, 3, 12, 4, 13]
  42. assert cf_p(1932, 2568) == [0, 1, 3, 26, 2]
  43. assert cf_p(6589, 2569) == [2, 1, 1, 3, 2, 1, 3, 1, 23]
  44. def take(iterator, n=7):
  45. return list(itertools.islice(iterator, n))
  46. assert take(cf_i(phi)) == [1, 1, 1, 1, 1, 1, 1]
  47. assert take(cf_i(pi)) == [3, 7, 15, 1, 292, 1, 1]
  48. assert list(cf_i(Rational(17, 12))) == [1, 2, 2, 2]
  49. assert list(cf_i(Rational(-17, 12))) == [-2, 1, 1, 2, 2]
  50. assert list(cf_c([1, 6, 1, 8])) == [S.One, Rational(7, 6), Rational(8, 7), Rational(71, 62)]
  51. assert list(cf_c([2])) == [S(2)]
  52. assert list(cf_c([1, 1, 1, 1, 1, 1, 1])) == [S.One, S(2), Rational(3, 2), Rational(5, 3),
  53. Rational(8, 5), Rational(13, 8), Rational(21, 13)]
  54. assert list(cf_c([1, 6, Rational(-1, 2), 4])) == [S.One, Rational(7, 6), Rational(5, 4), Rational(3, 2)]
  55. assert take(cf_c([[1]])) == [S.One, S(2), Rational(3, 2), Rational(5, 3), Rational(8, 5),
  56. Rational(13, 8), Rational(21, 13)]
  57. assert take(cf_c([1, [1, 2]])) == [S.One, S(2), Rational(5, 3), Rational(7, 4), Rational(19, 11),
  58. Rational(26, 15), Rational(71, 41)]
  59. cf_iter_e = (2 if i == 1 else i // 3 * 2 if i % 3 == 0 else 1 for i in itertools.count(1))
  60. assert take(cf_c(cf_iter_e)) == [S(2), S(3), Rational(8, 3), Rational(11, 4), Rational(19, 7),
  61. Rational(87, 32), Rational(106, 39)]
  62. assert cf_r([1, 6, 1, 8]) == Rational(71, 62)
  63. assert cf_r([3]) == S(3)
  64. assert cf_r([-1, 5, 1, 4]) == Rational(-24, 29)
  65. assert (cf_r([0, 1, 1, 7, [24, 8]]) - (sqrt(3) + 2)/7).expand() == 0
  66. assert cf_r([1, 5, 9]) == Rational(55, 46)
  67. assert (cf_r([[1]]) - (sqrt(5) + 1)/2).expand() == 0
  68. assert cf_r([-3, 1, 1, [2]]) == -1 - sqrt(2)