test_rules_logarithmic.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from src.rules.logarithmic import log, ln, match_constant_logarithm, \
  2. base_equals_raised, logarithm_of_one, divide_same_base, \
  3. match_add_logarithms, add_logarithms, expand_negations, \
  4. subtract_logarithms, match_raised_base, raised_base, \
  5. match_factor_out_exponent, split_negative_exponent, \
  6. factor_out_exponent, match_factor_in_multiplicant, \
  7. factor_in_multiplicant
  8. from src.node import Scope
  9. from src.possibilities import Possibility as P
  10. from tests.rulestestcase import RulesTestCase, tree
  11. class TestRulesLogarithmic(RulesTestCase):
  12. def test_match_constant_logarithm(self):
  13. self.assertRaises(ValueError, tree, 'log_1(a)')
  14. root = tree('log 1')
  15. self.assertEqualPos(match_constant_logarithm(root),
  16. [P(root, logarithm_of_one)])
  17. root = tree('log 10')
  18. self.assertEqualPos(match_constant_logarithm(root),
  19. [P(root, base_equals_raised),
  20. P(root, divide_same_base)])
  21. root = tree('log(a, a)')
  22. self.assertEqualPos(match_constant_logarithm(root),
  23. [P(root, base_equals_raised),
  24. P(root, divide_same_base)])
  25. def test_logarithm_of_one(self):
  26. root = tree('log 1')
  27. self.assertEqual(logarithm_of_one(root, ()), 0)
  28. def test_divide_same_base(self):
  29. root, l5, l6 = tree('log(5, 6), 5, 6')
  30. self.assertEqual(divide_same_base(root, ()), log(l5) / log(l6))
  31. def test_match_add_logarithms(self):
  32. root = tree('log a + ln b')
  33. self.assertEqualPos(match_add_logarithms(root), [])
  34. log_a, log_b = root = tree('log a + log b')
  35. self.assertEqualPos(match_add_logarithms(root),
  36. [P(root, add_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, expand_negations, (Scope(root), log_a, log_b))])
  40. log_a, log_b = root = tree('log a - log b')
  41. self.assertEqualPos(match_add_logarithms(root),
  42. [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])
  43. log_a, log_b = root = tree('-log a + log b')
  44. self.assertEqualPos(match_add_logarithms(root),
  45. [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
  46. def test_add_logarithms(self):
  47. root, expect = tree('log a + log b, log(ab)')
  48. log_a, log_b = root
  49. self.assertEqual(add_logarithms(root, (Scope(root), log_a, log_b)),
  50. expect)
  51. def test_expand_negations(self):
  52. root, expect = tree('-log(a) - log(b), -(log(a) + log(b))')
  53. log_a, log_b = root
  54. self.assertEqual(expand_negations(root, (Scope(root), log_a, log_b)),
  55. expect)
  56. def test_subtract_logarithms(self):
  57. root, expect = tree('log(a) - log(b), log(a / b)')
  58. loga, logb = root
  59. self.assertEqual(subtract_logarithms(root, (Scope(root), loga, logb)),
  60. expect)
  61. root, expect = tree('-log(a) + log(b), log(b / a)')
  62. loga, logb = root
  63. self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
  64. expect)
  65. def test_match_raised_base(self):
  66. root, a = tree('2 ^ log_2(a), a')
  67. self.assertEqualPos(match_raised_base(root),
  68. [P(root, raised_base, (a,))])
  69. root, a = tree('e ^ ln(a), a')
  70. self.assertEqualPos(match_raised_base(root),
  71. [P(root, raised_base, (a,))])
  72. root = tree('2 ^ log_3(a)')
  73. self.assertEqualPos(match_raised_base(root), [])
  74. def test_raised_base(self):
  75. root, a = tree('2 ^ log_2(a), a')
  76. self.assertEqual(raised_base(root, (root[1][0],)), a)
  77. def test_match_factor_out_exponent(self):
  78. for root in tree('log(a ^ 2), log(2 ^ a), log(a ^ a), log(2 ^ 2)'):
  79. self.assertEqualPos(match_factor_out_exponent(root),
  80. [P(root, factor_out_exponent)])
  81. root = tree('log(a ^ -b)')
  82. self.assertEqualPos(match_factor_out_exponent(root),
  83. [P(root, split_negative_exponent),
  84. P(root, factor_out_exponent)])
  85. def test_split_negative_exponent(self):
  86. root, expect = tree('log(a ^ -b), log((a ^ b) ^ -1)')
  87. self.assertEqual(split_negative_exponent(root, ()), expect)
  88. def test_factor_out_exponent(self):
  89. ((a, l2), l10) = root = tree('log(a ^ 2)')
  90. self.assertEqual(factor_out_exponent(root, ()), l2 * log(a))
  91. def test_match_factor_in_multiplicant(self):
  92. root, log_3 = tree('2log(3), log(3)')
  93. self.assertEqualPos(match_factor_in_multiplicant(root),
  94. [P(root, factor_in_multiplicant, (Scope(root), 2, log_3))])
  95. root = tree('2log(a)')
  96. self.assertEqualPos(match_factor_in_multiplicant(root), [])
  97. root = tree('alog(3)')
  98. self.assertEqualPos(match_factor_in_multiplicant(root), [])
  99. def test_factor_in_multiplicant(self):
  100. root, expect = tree('2log(3), log(3 ^ 2)')
  101. l2, log3 = root
  102. self.assertEqual(factor_in_multiplicant(root, (Scope(root), l2, log3)),
  103. expect)