test_rules_integrals.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from src.rules.integrals import indef, choose_constant, solve_integral, \
  2. match_solve_indef, solve_indef, match_integrate_variable_power, \
  3. integrate_variable_root, integrate_variable_exponent, \
  4. match_constant_integral, constant_integral, single_variable_integral, \
  5. match_factor_out_constant, split_negation_to_constant, \
  6. factor_out_constant, match_division_integral, division_integral, \
  7. extend_division_integral, match_function_integral, \
  8. logarithm_integral, sinus_integral, cosinus_integral
  9. from src.node import Scope
  10. from src.possibilities import Possibility as P
  11. from tests.rulestestcase import RulesTestCase, tree
  12. class TestRulesIntegrals(RulesTestCase):
  13. def test_choose_constant(self):
  14. a, b, c = tree('a, b, c')
  15. self.assertEqual(choose_constant(tree('int x ^ n')), c)
  16. self.assertEqual(choose_constant(tree('int x ^ c')), a)
  17. self.assertEqual(choose_constant(tree('int a ^ c da')), b)
  18. def test_match_solve_indef(self):
  19. root = tree('[x ^ 2]_a^b')
  20. self.assertEqualPos(match_solve_indef(root), [P(root, solve_indef)])
  21. def test_solve_integral(self):
  22. root, F, Fc = tree('int x ^ 2 dx, 1 / 3 x ^ 3, 1 / 3 x ^ 3 + c')
  23. self.assertEqual(solve_integral(root, F), Fc)
  24. x2, x, a, b = root = tree('int_a^b x ^ 2 dx')
  25. self.assertEqual(solve_integral(root, F), indef(Fc, a, b))
  26. def test_solve_integral_skip_indef(self):
  27. root, x, c, l1 = tree('int_a^b y ^ x dy, x, c, 1')
  28. F = tree('1 / (x + 1)y ^ (x + 1)')
  29. y, a, b = root[1:4]
  30. Fx = lambda y: l1 / (x + 1) * y ** (x + 1) + c
  31. self.assertEqual(solve_integral(root, F), Fx(b) - Fx(a))
  32. def test_solve_indef(self):
  33. root, expect = tree('[x ^ 2]_a^b, b ^ 2 - a ^ 2')
  34. self.assertEqual(solve_indef(root, ()), expect)
  35. def test_match_integrate_variable_power(self):
  36. root = tree('int x ^ n')
  37. self.assertEqualPos(match_integrate_variable_power(root),
  38. [P(root, integrate_variable_root)])
  39. root = tree('int x ^ n')
  40. self.assertEqualPos(match_integrate_variable_power(root),
  41. [P(root, integrate_variable_root)])
  42. root = tree('int -x ^ n')
  43. self.assertEqualPos(match_integrate_variable_power(root), [])
  44. for root in tree('int g ^ x, int g ^ x'):
  45. self.assertEqualPos(match_integrate_variable_power(root),
  46. [P(root, integrate_variable_exponent)])
  47. def test_integrate_variable_root(self):
  48. root, expect = tree('int x ^ n, 1 / (n + 1) * x ^ (n + 1) + c')
  49. self.assertEqual(integrate_variable_root(root, ()), expect)
  50. def test_integrate_variable_exponent(self):
  51. root, expect = tree('int g ^ x, g ^ x / ln(g) + c')
  52. self.assertEqual(integrate_variable_exponent(root, ()), expect)
  53. def test_match_constant_integral(self):
  54. root = tree('int x dx')
  55. self.assertEqualPos(match_constant_integral(root),
  56. [P(root, single_variable_integral)])
  57. root = tree('int 2')
  58. self.assertEqualPos(match_constant_integral(root),
  59. [P(root, constant_integral)])
  60. root = tree('int c dx')
  61. self.assertEqualPos(match_constant_integral(root),
  62. [P(root, constant_integral)])
  63. def test_single_variable_integral(self):
  64. root, expect = tree('int x, int x ^ 1')
  65. self.assertEqual(single_variable_integral(root, ()), expect)
  66. def test_constant_integral(self):
  67. root, expect = tree('int 2, 2x + c')
  68. self.assertEqual(constant_integral(root, ()), expect)
  69. root, expect = tree('int_0^4 2, [2x + c]_0^4')
  70. self.assertEqual(constant_integral(root, ()), expect)
  71. def test_match_factor_out_constant(self):
  72. root, c, cx = tree('int cx dx, c, cx')
  73. self.assertEqualPos(match_factor_out_constant(root),
  74. [P(root, factor_out_constant, (Scope(cx), c))])
  75. root = tree('int -x2 dx')
  76. self.assertEqualPos(match_factor_out_constant(root),
  77. [P(root, split_negation_to_constant)])
  78. def test_split_negation_to_constant(self):
  79. root, expect = tree('int -x ^ 2 dx, int (-1)x ^ 2 dx')
  80. self.assertEqual(split_negation_to_constant(root, ()), expect)
  81. def test_factor_out_constant(self):
  82. root, expect = tree('int cx dx, c int x dx')
  83. c, x2 = cx2 = root[0]
  84. self.assertEqual(factor_out_constant(root, (Scope(cx2), c)), expect)
  85. def test_match_division_integral(self):
  86. root0, root1 = tree('int 1 / x, int 2 / x')
  87. self.assertEqualPos(match_division_integral(root0),
  88. [P(root0, division_integral)])
  89. self.assertEqualPos(match_division_integral(root1),
  90. [P(root1, extend_division_integral)])
  91. def test_division_integral(self):
  92. root, expect = tree('int 1 / x dx, ln|x| + c')
  93. self.assertEqual(division_integral(root, ()), expect)
  94. def test_extend_division_integral(self):
  95. root, expect = tree('int a / x dx, int a(1 / x) dx')
  96. self.assertEqual(extend_division_integral(root, ()), expect)
  97. def test_match_division_integral_chain(self):
  98. self.assertRewrite([
  99. 'int a / x',
  100. 'int a * 1 / x dx',
  101. 'aint 1 / x dx',
  102. 'a(ln|x| + c)',
  103. 'aln|x| + ac',
  104. # FIXME: 'aln|x| + c', # ac -> c
  105. ])
  106. def test_match_function_integral(self):
  107. root0, root1, root2 = tree('int ln x, int sin x, int cos x')
  108. self.assertEqualPos(match_function_integral(root0),
  109. [P(root0, logarithm_integral)])
  110. self.assertEqualPos(match_function_integral(root1),
  111. [P(root1, sinus_integral)])
  112. self.assertEqualPos(match_function_integral(root2),
  113. [P(root2, cosinus_integral)])
  114. def test_logarithm_integral(self):
  115. root, expect = tree('int ln x, (xlnx - x) / ln e + c')
  116. self.assertEqual(logarithm_integral(root, ()), expect)
  117. def test_sinus_integral(self):
  118. root, expect = tree('int sin x, -cos x + c')
  119. self.assertEqual(sinus_integral(root, ()), expect)
  120. def test_cosinus_integral(self):
  121. root, expect = tree('int cos x, sin x + c')
  122. self.assertEqual(cosinus_integral(root, ()), expect)