test_guess.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from sympy.concrete.guess import (
  2. find_simple_recurrence_vector,
  3. find_simple_recurrence,
  4. rationalize,
  5. guess_generating_function_rational,
  6. guess_generating_function,
  7. guess
  8. )
  9. from sympy.concrete.products import Product
  10. from sympy.core.function import Function
  11. from sympy.core.numbers import Rational
  12. from sympy.core.singleton import S
  13. from sympy.core.symbol import (Symbol, symbols)
  14. from sympy.core.sympify import sympify
  15. from sympy.functions.combinatorial.factorials import (RisingFactorial, factorial)
  16. from sympy.functions.combinatorial.numbers import fibonacci
  17. from sympy.functions.elementary.exponential import exp
  18. def test_find_simple_recurrence_vector():
  19. assert find_simple_recurrence_vector(
  20. [fibonacci(k) for k in range(12)]) == [1, -1, -1]
  21. def test_find_simple_recurrence():
  22. a = Function('a')
  23. n = Symbol('n')
  24. assert find_simple_recurrence([fibonacci(k) for k in range(12)]) == (
  25. -a(n) - a(n + 1) + a(n + 2))
  26. f = Function('a')
  27. i = Symbol('n')
  28. a = [1, 1, 1]
  29. for k in range(15): a.append(5*a[-1]-3*a[-2]+8*a[-3])
  30. assert find_simple_recurrence(a, A=f, N=i) == (
  31. -8*f(i) + 3*f(i + 1) - 5*f(i + 2) + f(i + 3))
  32. assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0,
  33. 1, 2, 85, 4, 5, 63]) == 0
  34. def test_rationalize():
  35. from mpmath import cos, pi, mpf
  36. assert rationalize(cos(pi/3)) == S.Half
  37. assert rationalize(mpf("0.333333333333333")) == Rational(1, 3)
  38. assert rationalize(mpf("-0.333333333333333")) == Rational(-1, 3)
  39. assert rationalize(pi, maxcoeff = 250) == Rational(355, 113)
  40. def test_guess_generating_function_rational():
  41. x = Symbol('x')
  42. assert guess_generating_function_rational([fibonacci(k)
  43. for k in range(5, 15)]) == ((3*x + 5)/(-x**2 - x + 1))
  44. def test_guess_generating_function():
  45. x = Symbol('x')
  46. assert guess_generating_function([fibonacci(k)
  47. for k in range(5, 15)])['ogf'] == ((3*x + 5)/(-x**2 - x + 1))
  48. assert guess_generating_function(
  49. [1, 2, 5, 14, 41, 124, 383, 1200, 3799, 12122, 38919])['ogf'] == (
  50. (1/(x**4 + 2*x**2 - 4*x + 1))**S.Half)
  51. assert guess_generating_function(sympify(
  52. "[3/2, 11/2, 0, -121/2, -363/2, 121, 4719/2, 11495/2, -8712, -178717/2]")
  53. )['ogf'] == (x + Rational(3, 2))/(11*x**2 - 3*x + 1)
  54. assert guess_generating_function([factorial(k) for k in range(12)],
  55. types=['egf'])['egf'] == 1/(-x + 1)
  56. assert guess_generating_function([k+1 for k in range(12)],
  57. types=['egf']) == {'egf': (x + 1)*exp(x), 'lgdegf': (x + 2)/(x + 1)}
  58. def test_guess():
  59. i0, i1 = symbols('i0 i1')
  60. assert guess([1, 2, 6, 24, 120], evaluate=False) == [Product(i1 + 1, (i1, 1, i0 - 1))]
  61. assert guess([1, 2, 6, 24, 120]) == [RisingFactorial(2, i0 - 1)]
  62. assert guess([1, 2, 7, 42, 429, 7436, 218348, 10850216], niter=4) == [
  63. 2**(i0 - 1)*(Rational(27, 16))**(i0**2/2 - 3*i0/2 +
  64. 1)*Product(RisingFactorial(Rational(5, 3), i1 - 1)*RisingFactorial(Rational(7, 3), i1
  65. - 1)/(RisingFactorial(Rational(3, 2), i1 - 1)*RisingFactorial(Rational(5, 2), i1 -
  66. 1)), (i1, 1, i0 - 1))]
  67. assert guess([1, 0, 2]) == []
  68. x, y = symbols('x y')
  69. assert guess([1, 2, 6, 24, 120], variables=[x, y]) == [RisingFactorial(2, x - 1)]