test_rules_integrals.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. from src.rules.integrals import indef, choose_constant, solve_integral, \
  16. match_solve_indef, solve_indef, match_integrate_variable_power, \
  17. integrate_variable_root, integrate_variable_exponent, \
  18. match_constant_integral, constant_integral, single_variable_integral, \
  19. match_factor_out_constant, factor_out_integral_negation, \
  20. factor_out_constant, match_division_integral, division_integral, \
  21. extend_division_integral, match_function_integral, \
  22. logarithm_integral, sinus_integral, cosinus_integral, \
  23. match_sum_rule_integral, sum_rule_integral, \
  24. match_remove_indef_constant, remove_indef_constant
  25. from src.node import Scope
  26. from src.possibilities import Possibility as P
  27. from tests.rulestestcase import RulesTestCase, tree
  28. class TestRulesIntegrals(RulesTestCase):
  29. def test_choose_constant(self):
  30. a, b, c = tree('A, B, C')
  31. self.assertEqual(choose_constant(tree('int x ^ n')), c)
  32. self.assertEqual(choose_constant(tree('int x ^ c')), a)
  33. self.assertEqual(choose_constant(tree('int a ^ c da')), b)
  34. def test_match_solve_indef(self):
  35. root = tree('[x ^ 2]_a^b')
  36. self.assertEqualPos(match_solve_indef(root), [P(root, solve_indef)])
  37. def test_solve_integral(self):
  38. root, F, Fc = tree('int x ^ 2 dx, 1 / 3 x ^ 3, 1 / 3 x ^ 3 + C')
  39. self.assertEqual(solve_integral(root, F), Fc)
  40. x2, x, a, b = root = tree('int_a^b x ^ 2 dx')
  41. self.assertEqual(solve_integral(root, F), indef(Fc, a, b))
  42. def test_solve_integral_skip_indef(self):
  43. root, x, C, l1 = tree('int_a^b y ^ x dy, x, C, 1')
  44. F = tree('1 / (x + 1)y ^ (x + 1)')
  45. y, a, b = root[1:4]
  46. Fx = lambda y: l1 / (x + 1) * y ** (x + 1) + C
  47. self.assertEqual(solve_integral(root, F), Fx(b) - Fx(a))
  48. def test_solve_indef(self):
  49. root, expect = tree('[x ^ 2]_a^b, b ^ 2 - a ^ 2')
  50. self.assertEqual(solve_indef(root, ()), expect)
  51. def test_match_integrate_variable_power(self):
  52. root = tree('int x ^ n')
  53. self.assertEqualPos(match_integrate_variable_power(root),
  54. [P(root, integrate_variable_root)])
  55. root = tree('int x ^ n')
  56. self.assertEqualPos(match_integrate_variable_power(root),
  57. [P(root, integrate_variable_root)])
  58. root = tree('int -x ^ n')
  59. self.assertEqualPos(match_integrate_variable_power(root), [])
  60. for root in tree('int g ^ x, int g ^ x'):
  61. self.assertEqualPos(match_integrate_variable_power(root),
  62. [P(root, integrate_variable_exponent)])
  63. def test_integrate_variable_root(self):
  64. root, expect = tree('int x ^ n, 1 / (n + 1) * x ^ (n + 1) + C')
  65. self.assertEqual(integrate_variable_root(root, ()), expect)
  66. def test_integrate_variable_exponent(self):
  67. root, expect = tree('int g ^ x, g ^ x / ln(g) + C')
  68. self.assertEqual(integrate_variable_exponent(root, ()), expect)
  69. def test_match_constant_integral(self):
  70. root = tree('int x dx')
  71. self.assertEqualPos(match_constant_integral(root),
  72. [P(root, single_variable_integral)])
  73. root = tree('int 2')
  74. self.assertEqualPos(match_constant_integral(root),
  75. [P(root, constant_integral)])
  76. root = tree('int c dx')
  77. self.assertEqualPos(match_constant_integral(root),
  78. [P(root, constant_integral)])
  79. def test_single_variable_integral(self):
  80. root, expect = tree('int x, int x ^ 1')
  81. self.assertEqual(single_variable_integral(root, ()), expect)
  82. def test_constant_integral(self):
  83. root, expect = tree('int 2, 2x + C')
  84. self.assertEqual(constant_integral(root, ()), expect)
  85. root, expect = tree('int_0^4 2, [2x + C]_0^4')
  86. self.assertEqual(constant_integral(root, ()), expect)
  87. def test_match_factor_out_constant(self):
  88. root, c, cx = tree('int cx dx, c, cx')
  89. self.assertEqualPos(match_factor_out_constant(root),
  90. [P(root, factor_out_constant, (Scope(cx), c))])
  91. root = tree('int -x2 dx')
  92. self.assertEqualPos(match_factor_out_constant(root),
  93. [P(root, factor_out_integral_negation)])
  94. def test_factor_out_integral_negation(self):
  95. root, expect = tree('int -x ^ 2 dx, -int x ^ 2 dx')
  96. self.assertEqual(factor_out_integral_negation(root, ()), expect)
  97. def test_factor_out_constant(self):
  98. root, expect = tree('int cx dx, c int x dx')
  99. c, x2 = cx2 = root[0]
  100. self.assertEqual(factor_out_constant(root, (Scope(cx2), c)), expect)
  101. def test_match_division_integral(self):
  102. root0, root1 = tree('int 1 / x, int 2 / x')
  103. self.assertEqualPos(match_division_integral(root0),
  104. [P(root0, division_integral)])
  105. self.assertEqualPos(match_division_integral(root1),
  106. [P(root1, extend_division_integral)])
  107. def test_division_integral(self):
  108. root, expect = tree('int 1 / x dx, ln|x| + C')
  109. self.assertEqual(division_integral(root, ()), expect)
  110. def test_extend_division_integral(self):
  111. root, expect = tree('int a / x dx, int a(1 / x) dx')
  112. self.assertEqual(extend_division_integral(root, ()), expect)
  113. def test_match_division_integral_chain(self):
  114. self.assertRewrite([
  115. 'int a / x',
  116. 'int a * 1 / x dx',
  117. 'aint 1 / x dx',
  118. 'a(ln|x| + C)',
  119. 'aln|x| + aC',
  120. # FIXME: 'aln|x| + C', # ac -> C
  121. ])
  122. def test_match_function_integral(self):
  123. root = tree('int ln x')
  124. self.assertEqualPos(match_function_integral(root),
  125. [P(root, logarithm_integral)])
  126. root = tree('int sin x')
  127. self.assertEqualPos(match_function_integral(root),
  128. [P(root, sinus_integral)])
  129. root = tree('int cos x')
  130. self.assertEqualPos(match_function_integral(root),
  131. [P(root, cosinus_integral)])
  132. root = tree('int sqrt x')
  133. self.assertEqualPos(match_function_integral(root), [])
  134. def test_logarithm_integral(self):
  135. root, expect = tree('int ln x, (xlnx - x) / ln e + C')
  136. self.assertEqual(logarithm_integral(root, ()), expect)
  137. def test_sinus_integral(self):
  138. root, expect = tree('int sin x, -cos x + C')
  139. self.assertEqual(sinus_integral(root, ()), expect)
  140. def test_cosinus_integral(self):
  141. root, expect = tree('int cos x, sin x + C')
  142. self.assertEqual(cosinus_integral(root, ()), expect)
  143. def test_match_sum_rule_integral(self):
  144. (f, g), x = root = tree('int 2x + 3x dx')
  145. self.assertEqualPos(match_sum_rule_integral(root),
  146. [P(root, sum_rule_integral, (Scope(root[0]), f))])
  147. ((f, g), h), x = root = tree('int 2x + 3x + 4x dx')
  148. self.assertEqualPos(match_sum_rule_integral(root),
  149. [P(root, sum_rule_integral, (Scope(root[0]), f)),
  150. P(root, sum_rule_integral, (Scope(root[0]), g)),
  151. P(root, sum_rule_integral, (Scope(root[0]), h))])
  152. def test_sum_rule_integral(self):
  153. ((f, g), h), x = root = tree('int 2x + 3x + 4x dx')
  154. self.assertEqual(sum_rule_integral(root, (Scope(root[0]), f)),
  155. tree('int 2x dx + int 3x + 4x dx'))
  156. self.assertEqual(sum_rule_integral(root, (Scope(root[0]), g)),
  157. tree('int 3x dx + int 2x + 4x dx'))
  158. self.assertEqual(sum_rule_integral(root, (Scope(root[0]), h)),
  159. tree('int 4x dx + int 2x + 3x dx'))
  160. def test_match_remove_indef_constant(self):
  161. Fx, a, b = root = tree('[2x + C]_a^b')
  162. self.assertEqualPos(match_remove_indef_constant(root),
  163. [P(root, remove_indef_constant, (Scope(Fx), Fx[1]))])
  164. Fx, a, b = root = tree('[2x + x]_a^b')
  165. self.assertEqualPos(match_remove_indef_constant(root), [])
  166. Fx, a, b = root = tree('[2x]_a^b')
  167. self.assertEqualPos(match_remove_indef_constant(root), [])
  168. def test_remove_indef_constant(self):
  169. root, e = tree('[2x + C]_a^b, [2x]_a^b')
  170. Fx = root[0]
  171. self.assertEqual(remove_indef_constant(root, (Scope(Fx), Fx[1])), e)