test_rules_integrals.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. from src.rules.logarithmic import ln
  5. #from .goniometry import sin, cos
  6. from src.possibilities import Possibility as P
  7. from tests.rulestestcase import RulesTestCase, tree
  8. class TestRulesIntegrals(RulesTestCase):
  9. def test_choose_constant(self):
  10. a, b, c = tree('a, b, c')
  11. self.assertEqual(choose_constant(tree('int x ^ n')), c)
  12. self.assertEqual(choose_constant(tree('int x ^ c')), a)
  13. self.assertEqual(choose_constant(tree('int a ^ c da')), b)
  14. def test_match_solve_indef(self):
  15. root = tree('[x ^ 2]_a^b')
  16. self.assertEqualPos(match_solve_indef(root), [P(root, solve_indef)])
  17. def test_solve_integral(self):
  18. root, F, Fc = tree('int x ^ 2 dx, 1 / 3 x ^ 3, 1 / 3 x ^ 3 + c')
  19. self.assertEqual(solve_integral(root, F), Fc)
  20. x2, x, a, b = root = tree('int_a^b x ^ 2 dx')
  21. self.assertEqual(solve_integral(root, F), indef(Fc, a, b))
  22. def test_solve_integral_skip_indef(self):
  23. root, x, c, l1 = tree('int_a^b y ^ x dy, x, c, 1')
  24. F = tree('1 / (x + 1)y ^ (x + 1)')
  25. y, a, b = root[1:4]
  26. Fx = lambda y: l1 / (x + 1) * y ** (x + 1) + c
  27. self.assertEqual(solve_integral(root, F), Fx(b) - Fx(a))
  28. def test_solve_indef(self):
  29. root, expect = tree('[x ^ 2]_a^b, b2 - a2')
  30. self.assertEqual(solve_indef(root, ()), expect)
  31. def test_match_integrate_variable_power(self):
  32. for root in tree('int x ^ n, int x ^ n'):
  33. self.assertEqualPos(match_integrate_variable_power(root),
  34. [P(root, integrate_variable_root)])
  35. for root in tree('int g ^ x, int g ^ x'):
  36. self.assertEqualPos(match_integrate_variable_power(root),
  37. [P(root, integrate_variable_exponent)])
  38. def test_integrate_variable_root(self):
  39. root, expect = tree('int x ^ n, x ^ (n + 1) / (n + 1) + c')
  40. self.assertEqual(integrate_variable_root(root, ()), expect)
  41. def test_integrate_variable_exponent(self):
  42. root, expect = tree('int g ^ x, g ^ x / ln(g) + c')
  43. self.assertEqual(integrate_variable_exponent(root, ()), expect)