test_rules_integrals.py 5.3 KB

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