test_rules_logarithmic.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. match_raised_base, raised_base
  5. from src.node import Scope
  6. from src.possibilities import Possibility as P
  7. from tests.rulestestcase import RulesTestCase, tree
  8. class TestRulesLogarithmic(RulesTestCase):
  9. def test_match_constant_logarithm(self):
  10. self.assertRaises(ValueError, tree, 'log_1(a)')
  11. root = tree('log 1')
  12. self.assertEqualPos(match_constant_logarithm(root),
  13. [P(root, logarithm_of_one)])
  14. root = tree('log 10')
  15. self.assertEqualPos(match_constant_logarithm(root),
  16. [P(root, divide_same_base)])
  17. root = tree('log(a, a)')
  18. self.assertEqualPos(match_constant_logarithm(root),
  19. [P(root, divide_same_base)])
  20. def test_logarithm_of_one(self):
  21. root = tree('log 1')
  22. self.assertEqual(logarithm_of_one(root, ()), 0)
  23. def test_divide_same_base(self):
  24. root, l5, l6 = tree('log(5, 6), 5, 6')
  25. self.assertEqual(divide_same_base(root, ()), log(l5) / log(l6))
  26. def test_match_add_logarithms(self):
  27. root = tree('log a + ln b')
  28. self.assertEqualPos(match_add_logarithms(root), [])
  29. log_a, log_b = root = tree('log a + log b')
  30. self.assertEqualPos(match_add_logarithms(root),
  31. [P(root, add_logarithms, (Scope(root), log_a, log_b))])
  32. log_a, log_b = root = tree('-log a - log b')
  33. self.assertEqualPos(match_add_logarithms(root),
  34. [P(root, expand_negations, (Scope(root), log_a, log_b))])
  35. log_a, log_b = root = tree('log a - log b')
  36. self.assertEqualPos(match_add_logarithms(root),
  37. [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])
  38. log_a, log_b = root = tree('-log a + log b')
  39. self.assertEqualPos(match_add_logarithms(root),
  40. [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
  41. def test_add_logarithms(self):
  42. root, expect = tree('log a + log b, log(ab)')
  43. log_a, log_b = root
  44. self.assertEqual(add_logarithms(root, (Scope(root), log_a, log_b)),
  45. expect)
  46. def test_expand_negations(self):
  47. root, expect = tree('-log(a) - log(b), -(log(a) + log(b))')
  48. log_a, log_b = root
  49. self.assertEqual(expand_negations(root, (Scope(root), log_a, log_b)),
  50. expect)
  51. def test_subtract_logarithms(self):
  52. root, expect = tree('log(a) - log(b), log(a / b)')
  53. loga, logb = root
  54. self.assertEqual(subtract_logarithms(root, (Scope(root), loga, logb)),
  55. expect)
  56. root, expect = tree('-log(a) + log(b), log(b / a)')
  57. loga, logb = root
  58. self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
  59. expect)
  60. def test_match_raised_base(self):
  61. root, a = tree('2 ^ log_2(a), a')
  62. self.assertEqualPos(match_raised_base(root),
  63. [P(root, raised_base, (a,))])
  64. root, a = tree('e ^ ln(a), a')
  65. self.assertEqualPos(match_raised_base(root),
  66. [P(root, raised_base, (a,))])
  67. root = tree('2 ^ log_3(a)')
  68. self.assertEqualPos(match_raised_base(root), [])
  69. def test_raised_base(self):
  70. root, a = tree('2 ^ log_2(a), a')
  71. self.assertEqual(raised_base(root, (root[1][0],)), a)