test_rules_logarithmic.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. from src.rules.logarithmic import log, 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, match_expand_terms, \
  8. expand_multiplication_terms, expand_division_terms, \
  9. factor_in_exponent_multiplicant, factor_out_exponent_important, \
  10. make_raised_base
  11. from src.node import Scope
  12. from src.possibilities import Possibility as P
  13. from tests.rulestestcase import RulesTestCase, tree
  14. class TestRulesLogarithmic(RulesTestCase):
  15. def test_match_constant_logarithm(self):
  16. self.assertRaises(ValueError, match_constant_logarithm,
  17. tree('log_1(a)'))
  18. root = tree('log 1')
  19. self.assertEqualPos(match_constant_logarithm(root),
  20. [P(root, logarithm_of_one)])
  21. root = tree('log 10')
  22. self.assertEqualPos(match_constant_logarithm(root),
  23. [P(root, base_equals_raised),
  24. P(root, divide_same_base)])
  25. root = tree('log(a, a)')
  26. self.assertEqualPos(match_constant_logarithm(root),
  27. [P(root, base_equals_raised),
  28. P(root, divide_same_base)])
  29. root = tree('log(a, b)')
  30. self.assertEqualPos(match_constant_logarithm(root),
  31. [P(root, divide_same_base)])
  32. def test_logarithm_of_one(self):
  33. root = tree('log 1')
  34. self.assertEqual(logarithm_of_one(root, ()), 0)
  35. def test_base_equals_raised(self):
  36. root, expect = tree('log(a, a), 1')
  37. self.assertEqual(base_equals_raised(root, ()), expect)
  38. root, expect = tree('-log(a, a), -1')
  39. self.assertEqual(base_equals_raised(root, ()), expect)
  40. def test_divide_same_base(self):
  41. root, l5, l6 = tree('log(5, 6), 5, 6')
  42. self.assertEqual(divide_same_base(root, ()), log(l5) / log(l6))
  43. def test_match_add_logarithms(self):
  44. root = tree('log a + ln b')
  45. self.assertEqualPos(match_add_logarithms(root), [])
  46. # log(ab) is not desired if ab is not reduceable
  47. root = tree('log a + log b')
  48. self.assertEqualPos(match_add_logarithms(root), [])
  49. log_a, log_b = root = tree('log 2 + log 3')
  50. self.assertEqualPos(match_add_logarithms(root),
  51. [P(root, add_logarithms, (Scope(root), log_a, log_b))])
  52. log_a, log_b = root = tree('-log 2 - log 3')
  53. self.assertEqualPos(match_add_logarithms(root),
  54. [P(root, expand_negations, (Scope(root), log_a, log_b))])
  55. # log(2 / 3) is not desired because 2 / 3 cannot be reduced
  56. log_a, log_b = root = tree('log 2 - log 3')
  57. self.assertEqualPos(match_add_logarithms(root), [])
  58. log_a, log_b = root = tree('log 4 - log 2')
  59. self.assertEqualPos(match_add_logarithms(root),
  60. [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])
  61. log_a, log_b = root = tree('-log 2 + log 4')
  62. self.assertEqualPos(match_add_logarithms(root),
  63. [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
  64. def test_add_logarithms(self):
  65. root, expect = tree('log a + log b, log(ab)')
  66. log_a, log_b = root
  67. self.assertEqual(add_logarithms(root, (Scope(root), log_a, log_b)),
  68. expect)
  69. def test_expand_negations(self):
  70. root, expect = tree('-log(a) - log(b), -(log(a) + log(b))')
  71. log_a, log_b = root
  72. self.assertEqual(expand_negations(root, (Scope(root), log_a, log_b)),
  73. expect)
  74. def test_subtract_logarithms(self):
  75. root, expect = tree('log(a) - log(b), log(a / b)')
  76. loga, logb = root
  77. self.assertEqual(subtract_logarithms(root, (Scope(root), loga, logb)),
  78. expect)
  79. root, expect = tree('-log(a) + log(b), log(b / a)')
  80. loga, logb = root
  81. self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
  82. expect)
  83. def test_match_raised_base(self):
  84. root, a = tree('2 ^ log_2(a), a')
  85. self.assertEqualPos(match_raised_base(root),
  86. [P(root, raised_base, (a,))])
  87. root, a = tree('e ^ ln(a), a')
  88. self.assertEqualPos(match_raised_base(root),
  89. [P(root, raised_base, (a,))])
  90. root = tree('2 ^ log_3(a)')
  91. self.assertEqualPos(match_raised_base(root), [])
  92. root = tree('e ^ (2ln x)')
  93. l2, lnx = mul = root[1]
  94. self.assertEqualPos(match_raised_base(root),
  95. [P(root, factor_in_exponent_multiplicant,
  96. (Scope(mul), l2, lnx))])
  97. def test_factor_in_exponent_multiplicant(self):
  98. root, expect = tree('e ^ (2ln x), e ^ ln x ^ 2')
  99. l2, lnx = mul = root[1]
  100. self.assertEqual(factor_in_exponent_multiplicant(root,
  101. (Scope(mul), l2, lnx)), expect)
  102. def test_raised_base(self):
  103. root, a = tree('2 ^ log_2(a), a')
  104. self.assertEqual(raised_base(root, (root[1][0],)), a)
  105. def test_match_factor_out_exponent(self):
  106. for root in tree('log(a ^ 2), log(2 ^ a), log(a ^ a), log(2 ^ 2)'):
  107. self.assertEqualPos(match_factor_out_exponent(root),
  108. [P(root, factor_out_exponent)])
  109. root = tree('log(a ^ -b)')
  110. self.assertEqualPos(match_factor_out_exponent(root),
  111. [P(root, split_negative_exponent),
  112. P(root, factor_out_exponent)])
  113. def test_match_factor_out_exponent_important(self):
  114. root = tree('log(10 ^ 2)')
  115. self.assertEqualPos(match_factor_out_exponent(root),
  116. [P(root, factor_out_exponent_important)])
  117. def test_match_factor_out_exponent_make_raised_base(self):
  118. root = tree('log(100)')
  119. self.assertEqualPos(match_factor_out_exponent(root),
  120. [P(root, make_raised_base, (2,))])
  121. root = tree('log(1000)')
  122. self.assertEqualPos(match_factor_out_exponent(root),
  123. [P(root, make_raised_base, (3,))])
  124. root = tree('log_2(16)')
  125. self.assertEqualPos(match_factor_out_exponent(root),
  126. [P(root, make_raised_base, (4,))])
  127. root = tree('log(99)')
  128. self.assertEqualPos(match_factor_out_exponent(root), [])
  129. def test_split_negative_exponent(self):
  130. root, expect = tree('log(a ^ -b), log((a ^ b) ^ -1)')
  131. self.assertEqual(split_negative_exponent(root, ()), expect)
  132. def test_factor_out_exponent(self):
  133. ((a, l2), l10) = root = tree('log(a ^ 2)')
  134. self.assertEqual(factor_out_exponent(root, ()), l2 * log(a))
  135. def test_make_raised_base(self):
  136. root, expect = tree('log(1000), log(10 ^ 3)')
  137. self.assertEqual(make_raised_base(root, (3,)), expect)
  138. root, expect = tree('log_2(64), log_2(2 ^ 4)')
  139. self.assertEqual(make_raised_base(root, (4,)), expect)
  140. def test_factor_out_exponent_important(self):
  141. ((a, l2), l10) = root = tree('log(10 ^ 2)')
  142. self.assertEqual(factor_out_exponent_important(root, ()), l2 * log(a))
  143. def test_match_factor_in_multiplicant(self):
  144. (l2, log_3) = root = tree('2log(3)')
  145. self.assertEqualPos(match_factor_in_multiplicant(root),
  146. [P(root, factor_in_multiplicant, (Scope(root), l2, log_3))])
  147. (l2, log_3), l4 = root = tree('2log(3)4')
  148. self.assertEqualPos(match_factor_in_multiplicant(root),
  149. [P(root, factor_in_multiplicant, (Scope(root), l2, log_3)),
  150. P(root, factor_in_multiplicant, (Scope(root), l4, log_3))])
  151. root = tree('2log(a)')
  152. self.assertEqualPos(match_factor_in_multiplicant(root), [])
  153. root = tree('alog(3)')
  154. self.assertEqualPos(match_factor_in_multiplicant(root), [])
  155. def test_factor_in_multiplicant(self):
  156. root, expect = tree('2log(3), log(3 ^ 2)')
  157. l2, log3 = root
  158. self.assertEqual(factor_in_multiplicant(root, (Scope(root), l2, log3)),
  159. expect)
  160. def test_match_expand_terms(self):
  161. ab, base = root = tree('log(2x)')
  162. a, b = ab
  163. self.assertEqualPos(match_expand_terms(root),
  164. [P(root, expand_multiplication_terms, (Scope(ab), b))])
  165. root = tree('log(2 * 3)')
  166. self.assertEqualPos(match_expand_terms(root), [])
  167. root = tree('log(2 / a)')
  168. self.assertEqualPos(match_expand_terms(root),
  169. [P(root, expand_division_terms)])
  170. def test_expand_multiplication_terms(self):
  171. root, expect = tree('log(2x), log x + log 2')
  172. self.assertEqual(expand_multiplication_terms(root,
  173. (Scope(root[0]), root[0][1])), expect)
  174. def test_expand_division_terms(self):
  175. root, expect = tree('log(2 / x), log 2 - log x')
  176. self.assertEqual(expand_division_terms(root, ()), expect)