test_rules_logarithmic.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_logarithm_of_one(self):
  20. root = tree('log 1')
  21. self.assertEqual(logarithm_of_one(root, ()), 0)
  22. def test_divide_same_base(self):
  23. root, l5, l6 = tree('log(5, 6), 5, 6')
  24. self.assertEqual(divide_same_base(root, ()), log(l5) / log(l6))
  25. def test_match_add_logarithms(self):
  26. root = tree('log a + ln b')
  27. self.assertEqualPos(match_add_logarithms(root), [])
  28. log_a, log_b = root = tree('log a + log b')
  29. self.assertEqualPos(match_add_logarithms(root),
  30. [P(root, add_logarithms, (Scope(root), log_a, log_b))])
  31. log_a, log_b = root = tree('-log a - log b')
  32. self.assertEqualPos(match_add_logarithms(root),
  33. [P(root, expand_negations, (Scope(root), log_a, log_b))])
  34. log_a, log_b = root = tree('log a - log b')
  35. self.assertEqualPos(match_add_logarithms(root),
  36. [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])
  37. log_a, log_b = root = tree('-log a + log b')
  38. self.assertEqualPos(match_add_logarithms(root),
  39. [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
  40. def test_add_logarithms(self):
  41. root, expect = tree('log a + log b, log(ab)')
  42. log_a, log_b = root
  43. self.assertEqual(add_logarithms(root, (Scope(root), log_a, log_b)),
  44. expect)
  45. def test_expand_negations(self):
  46. root, expect = tree('-log(a) - log(b), -(log(a) + log(b))')
  47. log_a, log_b = root
  48. self.assertEqual(expand_negations(root, (Scope(root), log_a, log_b)),
  49. expect)
  50. def test_subtract_logarithms(self):
  51. root, expect = tree('log(a) - log(b), log(a / b)')
  52. loga, logb = root
  53. self.assertEqual(subtract_logarithms(root, (Scope(root), loga, logb)),
  54. expect)
  55. root, expect = tree('-log(a) + log(b), log(b / a)')
  56. loga, logb = root
  57. self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
  58. expect)