Bladeren bron

Added last logarithmic rule.

Taddeus Kroes 14 jaren geleden
bovenliggende
commit
d7329b8da3
2 gewijzigde bestanden met toevoegingen van 37 en 3 verwijderingen
  1. 23 2
      src/rules/logarithmic.py
  2. 14 1
      tests/test_rules_logarithmic.py

+ 23 - 2
src/rules/logarithmic.py

@@ -1,7 +1,7 @@
 from itertools import combinations
 
 from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_LOG, E, \
-        OP_ADD, OP_MUL, Scope
+        OP_ADD, OP_MUL, OP_POW, Scope
 from ..possibilities import Possibility as P, MESSAGES
 from ..translate import _
 
@@ -65,7 +65,7 @@ def divide_same_base(root, args):
     return log(raised) / log(base)
 
 
-MESSAGES[divide_same_base] = _('Apply log_b(a)  ->  log(a) / log(b) on {0}.')
+MESSAGES[divide_same_base] = _('Apply log_b(a) = log(a) / log(b) on {0}.')
 
 
 def match_add_logarithms(node):
@@ -154,3 +154,24 @@ def subtract_logarithms(root, args):
 
 
 MESSAGES[subtract_logarithms] = _('Apply log(a) - log(b) = log(a / b).')
+
+
+def match_raised_base(node):
+    """
+    g ^ log_g(a)  ->  a
+    """
+    assert node.is_op(OP_POW)
+
+    root, exponent = node
+
+    if exponent.is_op(OP_LOG) and exponent[1] == root:
+        return [P(node, raised_base, (exponent[0],))]
+
+    return []
+
+
+def raised_base(root, args):
+    """
+    g ^ log_g(a)  ->  a
+    """
+    return args[0]

+ 14 - 1
tests/test_rules_logarithmic.py

@@ -1,6 +1,7 @@
 from src.rules.logarithmic import log, ln, match_constant_logarithm, \
         logarithm_of_one, divide_same_base, match_add_logarithms, \
-        add_logarithms, expand_negations, subtract_logarithms
+        add_logarithms, expand_negations, subtract_logarithms, \
+        match_raised_base, raised_base
 from src.node import Scope
 from src.possibilities import Possibility as P
 from tests.rulestestcase import RulesTestCase, tree
@@ -73,3 +74,15 @@ class TestRulesLogarithmic(RulesTestCase):
         loga, logb = root
         self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
                          expect)
+
+    def test_match_raised_base(self):
+        root, a = tree('2 ^ log_2(a), a')
+        self.assertEqualPos(match_raised_base(root),
+                [P(root, raised_base, (a,))])
+
+        root = tree('2 ^ log_3(a)')
+        self.assertEqualPos(match_raised_base(root), [])
+
+    def test_raised_base(self):
+        root, a = tree('2 ^ log_2(a), a')
+        self.assertEqual(raised_base(root, (root[1][0],)), a)