test_rules_integrals.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. from src.rules.logarithmic import ln
  7. #from .goniometry import sin, cos
  8. from src.node import Scope
  9. from src.possibilities import Possibility as P
  10. from tests.rulestestcase import RulesTestCase, tree
  11. class TestRulesIntegrals(RulesTestCase):
  12. def test_choose_constant(self):
  13. a, b, c = tree('a, b, c')
  14. self.assertEqual(choose_constant(tree('int x ^ n')), c)
  15. self.assertEqual(choose_constant(tree('int x ^ c')), a)
  16. self.assertEqual(choose_constant(tree('int a ^ c da')), b)
  17. def test_match_solve_indef(self):
  18. root = tree('[x ^ 2]_a^b')
  19. self.assertEqualPos(match_solve_indef(root), [P(root, solve_indef)])
  20. def test_solve_integral(self):
  21. root, F, Fc = tree('int x ^ 2 dx, 1 / 3 x ^ 3, 1 / 3 x ^ 3 + c')
  22. self.assertEqual(solve_integral(root, F), Fc)
  23. x2, x, a, b = root = tree('int_a^b x ^ 2 dx')
  24. self.assertEqual(solve_integral(root, F), indef(Fc, a, b))
  25. def test_solve_integral_skip_indef(self):
  26. root, x, c, l1 = tree('int_a^b y ^ x dy, x, c, 1')
  27. F = tree('1 / (x + 1)y ^ (x + 1)')
  28. y, a, b = root[1:4]
  29. Fx = lambda y: l1 / (x + 1) * y ** (x + 1) + c
  30. self.assertEqual(solve_integral(root, F), Fx(b) - Fx(a))
  31. def test_solve_indef(self):
  32. root, expect = tree('[x ^ 2]_a^b, b2 - a2')
  33. self.assertEqual(solve_indef(root, ()), expect)
  34. def test_match_integrate_variable_power(self):
  35. for root in tree('int x ^ n, int x ^ n'):
  36. self.assertEqualPos(match_integrate_variable_power(root),
  37. [P(root, integrate_variable_root)])
  38. for root in tree('int g ^ x, int g ^ x'):
  39. self.assertEqualPos(match_integrate_variable_power(root),
  40. [P(root, integrate_variable_exponent)])
  41. def test_integrate_variable_root(self):
  42. root, expect = tree('int x ^ n, x ^ (n + 1) / (n + 1) + c')
  43. self.assertEqual(integrate_variable_root(root, ()), expect)
  44. def test_integrate_variable_exponent(self):
  45. root, expect = tree('int g ^ x, g ^ x / ln(g) + c')
  46. self.assertEqual(integrate_variable_exponent(root, ()), expect)
  47. def test_match_constant_integral(self):
  48. root0, root1 = tree('int 2, int c dx')
  49. self.assertEqualPos(match_constant_integral(root0),
  50. [P(root0, constant_integral)])
  51. self.assertEqualPos(match_constant_integral(root1),
  52. [P(root1, constant_integral)])
  53. def test_constant_integral(self):
  54. root, expect = tree('int 2, 2x + c')
  55. self.assertEqual(constant_integral(root, ()), expect)
  56. root, expect = tree('int_0^4 2, [2x + c]_0^4')
  57. self.assertEqual(constant_integral(root, ()), expect)
  58. def test_match_factor_out_constant(self):
  59. root, c, cx = tree('int cx dx, c, cx')
  60. self.assertEqualPos(match_factor_out_constant(root),
  61. [P(root, factor_out_constant, (Scope(cx), c))])
  62. def test_factor_out_constant(self):
  63. root, expect = tree('int cx2 dx, c int x2 dx')
  64. c, x2 = cx2 = root[0]
  65. self.assertEqual(factor_out_constant(root, (Scope(cx2), c)), expect)