Commit 4e576a94 authored by Taddeus Kroes's avatar Taddeus Kroes

Added some unit tests.

parent 34e6d703
...@@ -108,7 +108,7 @@ MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.') ...@@ -108,7 +108,7 @@ MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.')
def match_const_deriv_multiplication(node): def match_const_deriv_multiplication(node):
""" """
[f(c * x)]' -> c * [f(x)]' der(c * f(x), x) -> c * der(f(x), x)
""" """
assert node.is_op(OP_DERIV) assert node.is_op(OP_DERIV)
...@@ -126,14 +126,13 @@ def match_const_deriv_multiplication(node): ...@@ -126,14 +126,13 @@ def match_const_deriv_multiplication(node):
def const_deriv_multiplication(root, args): def const_deriv_multiplication(root, args):
""" """
[f(c * x)]' -> c * [f(x)]' der(c * f(x), x) -> c * der(f(x), x)
""" """
scope, c = args scope, c = args
scope.remove(c) scope.remove(c)
x = L(get_derivation_variable(root)) x = L(get_derivation_variable(root))
# FIXME: is the explicit 'x' parameter necessary?
return c * der(scope.as_nary_node(), x) return c * der(scope.as_nary_node(), x)
...@@ -167,7 +166,7 @@ def match_variable_power(node): ...@@ -167,7 +166,7 @@ def match_variable_power(node):
if exponent.is_variable(): if exponent.is_variable():
return [P(node, variable_exponent)] return [P(node, variable_exponent)]
return [P(node, chain_rule, (root, variable_exponent, ()))] return [P(node, chain_rule, (exponent, variable_exponent, ()))]
return [] return []
...@@ -181,6 +180,10 @@ def variable_root(root, args): ...@@ -181,6 +180,10 @@ def variable_root(root, args):
return n * x ** (n - 1) return n * x ** (n - 1)
MESSAGES[variable_root] = \
_('Apply standard derivative d/dx x ^ n = n * x ^ (n - 1) on {0}.')
def variable_exponent(root, args): def variable_exponent(root, args):
""" """
der(g ^ x, x) -> g ^ x * ln(g) der(g ^ x, x) -> g ^ x * ln(g)
...@@ -192,3 +195,7 @@ def variable_exponent(root, args): ...@@ -192,3 +195,7 @@ def variable_exponent(root, args):
g, x = root[0] g, x = root[0]
return g ** x * ln(g) return g ** x * ln(g)
MESSAGES[variable_exponent] = \
_('Apply standard derivative d/dx g ^ x = g ^ x * ln g.')
from src.rules.derivatives import der, get_derivation_variable, \ from src.rules.derivatives import der, get_derivation_variable, \
match_zero_derivative, match_one_derivative, one_derivative, \ match_zero_derivative, match_one_derivative, one_derivative, \
zero_derivative, match_variable_power, variable_root, \ zero_derivative, match_variable_power, variable_root, \
match_const_deriv_multiplication, const_deriv_multiplication, \ variable_exponent, match_const_deriv_multiplication, \
chain_rule const_deriv_multiplication, chain_rule
from src.rules.logarithmic import ln
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
...@@ -70,11 +71,19 @@ class TestRulesDerivatives(RulesTestCase): ...@@ -70,11 +71,19 @@ class TestRulesDerivatives(RulesTestCase):
self.assertEqualPos(match_variable_power(root), self.assertEqualPos(match_variable_power(root),
[P(root, variable_root)]) [P(root, variable_root)])
root = tree('der(2 ^ x)')
self.assertEqualPos(match_variable_power(root),
[P(root, variable_exponent)])
def test_match_variable_power_chain_rule(self): def test_match_variable_power_chain_rule(self):
root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3') root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
self.assertEqualPos(match_variable_power(root), self.assertEqualPos(match_variable_power(root),
[P(root, chain_rule, (x3, variable_root, ()))]) [P(root, chain_rule, (x3, variable_root, ()))])
root = tree('der(2 ^ x ^ 3)')
self.assertEqualPos(match_variable_power(root),
[P(root, chain_rule, (x3, variable_exponent, ()))])
# Below is not mathematically underivable, it's just not within the # Below is not mathematically underivable, it's just not within the
# scope of our program # scope of our program
root, x = tree('der(x ^ x), x') root, x = tree('der(x ^ x), x')
......
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