test_rules_logarithmic.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from src.rules.logarithmic import log, ln, match_constant_logarithm, \
  2. logarithm_of_one, divide_same_base, match_add_logarithms, \
  3. add_logarithms, expand_negations, subtract_logarithms
  4. from src.node import Scope
  5. from src.possibilities import Possibility as P
  6. from tests.rulestestcase import RulesTestCase, tree
  7. class TestRulesLogarithmic(RulesTestCase):
  8. def test_match_constant_logarithm(self):
  9. self.assertRaises(ValueError, tree, 'log_1(a)')
  10. root = tree('log 1')
  11. self.assertEqualPos(match_constant_logarithm(root),
  12. [P(root, logarithm_of_one)])
  13. root = tree('log 10')
  14. self.assertEqualPos(match_constant_logarithm(root),
  15. [P(root, divide_same_base)])
  16. root = tree('log(a, a)')
  17. self.assertEqualPos(match_constant_logarithm(root),
  18. [P(root, divide_same_base)])
  19. def test_match_add_logarithms(self):
  20. log_a, log_b = root = tree('log a + log b')
  21. self.assertEqualPos(match_add_logarithms(root),
  22. [P(root, add_logarithms, (Scope(root), log_a, log_b))])
  23. log_a, log_b = root = tree('-log a - log b')
  24. self.assertEqualPos(match_add_logarithms(root),
  25. [P(root, expand_negations, (Scope(root), log_a, log_b))])
  26. log_a, log_b = root = tree('log a - log b')
  27. self.assertEqualPos(match_add_logarithms(root),
  28. [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])
  29. log_a, log_b = root = tree('-log a + log b')
  30. self.assertEqualPos(match_add_logarithms(root),
  31. [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
  32. def test_add_logarithms(self):
  33. root, a, b = tree('log a + log b, a, b')
  34. log_a, log_b = root
  35. self.assertEqual(add_logarithms(root, (Scope(root), log_a, log_b)),
  36. log(a * b))