Commit d7329b8d authored by Taddeus Kroes's avatar Taddeus Kroes

Added last logarithmic rule.

parent 3cfa07da
from itertools import combinations from itertools import combinations
from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_LOG, E, \ 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 ..possibilities import Possibility as P, MESSAGES
from ..translate import _ from ..translate import _
...@@ -65,7 +65,7 @@ def divide_same_base(root, args): ...@@ -65,7 +65,7 @@ def divide_same_base(root, args):
return log(raised) / log(base) 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): def match_add_logarithms(node):
...@@ -154,3 +154,24 @@ def subtract_logarithms(root, args): ...@@ -154,3 +154,24 @@ def subtract_logarithms(root, args):
MESSAGES[subtract_logarithms] = _('Apply log(a) - log(b) = log(a / b).') 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]
from src.rules.logarithmic import log, ln, match_constant_logarithm, \ from src.rules.logarithmic import log, ln, match_constant_logarithm, \
logarithm_of_one, divide_same_base, match_add_logarithms, \ 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.node import Scope
from src.possibilities import Possibility as P from src.possibilities import Possibility as P
from tests.rulestestcase import RulesTestCase, tree from tests.rulestestcase import RulesTestCase, tree
...@@ -73,3 +74,15 @@ class TestRulesLogarithmic(RulesTestCase): ...@@ -73,3 +74,15 @@ class TestRulesLogarithmic(RulesTestCase):
loga, logb = root loga, logb = root
self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)), self.assertEqual(subtract_logarithms(root, (Scope(root), logb, loga)),
expect) 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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment