Просмотр исходного кода

Added shortcut rules for derivatives with 'ln(e)', (more) corrseponding to the VWO function list.

Taddeus Kroes 14 лет назад
Родитель
Сommit
5445504d6c
3 измененных файлов с 18 добавлено и 13 удалено
  1. 2 3
      src/rules/__init__.py
  2. 13 8
      src/rules/derivatives.py
  3. 3 2
      src/rules/logarithmic.py

+ 2 - 3
src/rules/__init__.py

@@ -7,8 +7,7 @@ from .powers import match_add_exponents, match_subtract_exponents, \
         match_raised_fraction, match_remove_negative_exponent, \
         match_exponent_to_root, match_extend_exponent, match_constant_exponent
 from .numerics import match_add_numerics, match_divide_numerics, \
-        match_multiply_numerics, match_multiply_zero, match_multiply_one, \
-        match_raise_numerics
+        match_multiply_numerics, match_multiply_zero, match_raise_numerics
 from .fractions import match_constant_division, match_add_fractions, \
         match_multiply_fractions, match_divide_fractions, \
         match_extract_fraction_terms
@@ -36,7 +35,7 @@ RULES = {
                  match_combine_groups, match_add_quadrants,
                  match_add_logarithms],
         OP_MUL: [match_multiply_numerics, match_expand, match_add_exponents,
-                 match_multiply_zero, match_negated_factor, match_multiply_one,
+                 match_multiply_zero, match_negated_factor,
                  match_sort_multiplicants, match_multiply_fractions,
                  match_factor_in_multiplicant],
         OP_DIV: [match_subtract_exponents, match_divide_numerics,

+ 13 - 8
src/rules/derivatives.py

@@ -200,11 +200,14 @@ def variable_exponent(root, args):
     """
     der(g ^ x, x)  ->  g ^ x * ln(g)
 
-    Note that (in combination with logarithmic/constant rules):
-    der(e ^ x)  ->  e ^ x * ln(e)  ->  e ^ x * 1  ->  e ^ x
+    Shortcut rule (because of presence on formula list):
+    der(e ^ x, x)  ->  e ^ x
     """
     g, x = root[0]
 
+    if g == E:
+        return g ** x
+
     return g ** x * ln(g)
 
 
@@ -236,10 +239,16 @@ def match_logarithmic(node):
 
 def logarithmic(root, args):
     """
-    der(log(x, g), x)  ->  1 / (x * ln(g))
+    der(log(x, g), x)  ->  1 / (xln(g))
+
+    Shortcut function (because of presence on formula list):
+    der(ln(x), x)      ->  1 / x
     """
     x, g = root[0]
 
+    if g == E:
+        return L(1) / x
+
     return L(1) / (x * ln(g))
 
 
@@ -331,13 +340,9 @@ def match_sum_product_rule(node):
     if len(functions) < 2:
         return []
 
-    p = []
     handler = sum_rule if node[0].op == OP_ADD else product_rule
 
-    for f in functions:
-        p.append(P(node, handler, (scope, f)))
-
-    return p
+    return [P(node, handler, (scope, f)) for f in functions]
 
 
 def sum_rule(root, args):

+ 3 - 2
src/rules/logarithmic.py

@@ -179,7 +179,8 @@ def match_raised_base(node):
         logs, others = partition(is_matching_logarithm, scope)
 
         for other, log in product(others, logs):
-            # TODO: Give this function a high precedence
+            # Add this possibility so that a 'raised_base' possibility is
+            # generated in the following iteration
             p.append(P(node, factor_in_exponent_multiplicant,
                        (scope, other, log)))
 
@@ -250,7 +251,7 @@ MESSAGES[factor_out_exponent] = _('Factor out exponent {0[0][0]} from {0}.')
 
 def match_factor_in_multiplicant(node):
     """
-    Only bring a multiplicant inside a logarithms if both the multiplicant and
+    Only bring a multiplicant inside a logarithm if both the multiplicant and
     the logaritm's content are constants. This will yield a new simplification
     of constants inside the logarithm.
     2log(2)      ->  log(2 ^ 2)        # -> log(4)