Sfoglia il codice sorgente

Added some unit tests.

Taddeus Kroes 14 anni fa
parent
commit
4e576a94f1
2 ha cambiato i file con 22 aggiunte e 6 eliminazioni
  1. 11 4
      src/rules/derivatives.py
  2. 11 2
      tests/test_rules_derivatives.py

+ 11 - 4
src/rules/derivatives.py

@@ -108,7 +108,7 @@ MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.')
 
 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)
 
@@ -126,14 +126,13 @@ def match_const_deriv_multiplication(node):
 
 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.remove(c)
     x = L(get_derivation_variable(root))
 
-    # FIXME: is the explicit 'x' parameter necessary?
     return c * der(scope.as_nary_node(), x)
 
 
@@ -167,7 +166,7 @@ def match_variable_power(node):
         if exponent.is_variable():
             return [P(node, variable_exponent)]
 
-        return [P(node, chain_rule, (root, variable_exponent, ()))]
+        return [P(node, chain_rule, (exponent, variable_exponent, ()))]
 
     return []
 
@@ -181,6 +180,10 @@ def variable_root(root, args):
     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):
     """
     der(g ^ x, x)  ->  g ^ x * ln(g)
@@ -192,3 +195,7 @@ def variable_exponent(root, args):
     g, x = root[0]
 
     return g ** x * ln(g)
+
+
+MESSAGES[variable_exponent] = \
+        _('Apply standard derivative d/dx g ^ x = g ^ x * ln g.')

+ 11 - 2
tests/test_rules_derivatives.py

@@ -1,8 +1,9 @@
 from src.rules.derivatives import der, get_derivation_variable, \
         match_zero_derivative, match_one_derivative, one_derivative, \
         zero_derivative, match_variable_power, variable_root, \
-        match_const_deriv_multiplication, const_deriv_multiplication, \
-        chain_rule
+        variable_exponent, match_const_deriv_multiplication, \
+        const_deriv_multiplication, chain_rule
+from src.rules.logarithmic import ln
 from src.node import Scope
 from src.possibilities import Possibility as P
 from tests.rulestestcase import RulesTestCase, tree
@@ -70,11 +71,19 @@ class TestRulesDerivatives(RulesTestCase):
         self.assertEqualPos(match_variable_power(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):
         root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
         self.assertEqualPos(match_variable_power(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
         # scope of our program
         root, x = tree('der(x ^ x), x')