test_continued_fraction.py 3.0 KB

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