test_rules_logarithmic.py 5.4 KB

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