Ver código fonte

Added rule for removing constants from indefinite integrals.

Taddeus Kroes 14 anos atrás
pai
commit
92f967d64c
3 arquivos alterados com 52 adições e 4 exclusões
  1. 2 2
      src/rules/__init__.py
  2. 32 1
      src/rules/integrals.py
  3. 18 1
      tests/test_rules_integrals.py

+ 2 - 2
src/rules/__init__.py

@@ -26,7 +26,7 @@ from .logarithmic import match_constant_logarithm, \
 from .integrals import match_solve_indef, match_constant_integral, \
         match_integrate_variable_power, match_factor_out_constant, \
         match_division_integral, match_function_integral, \
-        match_sum_rule_integral
+        match_sum_rule_integral, match_remove_indef_constant
 from .lineq import match_move_term
 from .absolute import match_factor_out_abs_term
 
@@ -62,7 +62,7 @@ RULES = {
         OP_INT: [match_integrate_variable_power, match_constant_integral,
                  match_factor_out_constant, match_division_integral,
                  match_function_integral, match_sum_rule_integral],
-        OP_INT_INDEF: [match_solve_indef],
+        OP_INT_INDEF: [match_remove_indef_constant, match_solve_indef],
         OP_EQ: [match_move_term],
         OP_ABS: [match_factor_out_abs_term],
         }

+ 32 - 1
src/rules/integrals.py

@@ -319,7 +319,6 @@ def match_sum_rule_integral(node):
     if not node[0].is_op(OP_ADD):
         return []
 
-    p = []
     scope = Scope(node[0])
 
     if len(scope) == 2:
@@ -341,3 +340,35 @@ def sum_rule_integral(root, args):
 
 
 MESSAGES[sum_rule_integral] = _('Apply the sum rule to {0}.')
+
+
+def match_remove_indef_constant(node):
+    """
+    [f(x) + c]_a^b  ->  [f(x)]_a^b
+    """
+    assert node.is_op(OP_INT_INDEF)
+
+    if not node[0].is_op(OP_ADD):
+        return []
+
+    scope = Scope(node[0])
+    x = find_variable(node[0])
+    constants = [n for n in scope if not n.contains(x)]
+
+    return [P(node, remove_indef_constant, (scope, c)) for c in constants]
+
+
+def remove_indef_constant(root, args):
+    """
+    [f(x) + c]_a^b  ->  [f(x)]_a^b
+    """
+    scope, c = args
+    scope.remove(c)
+    Fx = scope.as_nary_node()
+    a, b = root[1:]
+
+    return indef(Fx, a, b)
+
+
+MESSAGES[remove_indef_constant] = \
+        _('Remove constant {2} from indefinite integral.')

+ 18 - 1
tests/test_rules_integrals.py

@@ -6,7 +6,8 @@ from src.rules.integrals import indef, choose_constant, solve_integral, \
         factor_out_constant, match_division_integral, division_integral, \
         extend_division_integral, match_function_integral, \
         logarithm_integral, sinus_integral, cosinus_integral, \
-        match_sum_rule_integral, sum_rule_integral
+        match_sum_rule_integral, sum_rule_integral, \
+        match_remove_indef_constant, remove_indef_constant
 from src.node import Scope
 from src.possibilities import Possibility as P
 from tests.rulestestcase import RulesTestCase, tree
@@ -179,3 +180,19 @@ class TestRulesIntegrals(RulesTestCase):
                          tree('int 3x dx + int 2x + 4x dx'))
         self.assertEqual(sum_rule_integral(root, (Scope(root[0]), h)),
                          tree('int 4x dx + int 2x + 3x dx'))
+
+    def test_match_remove_indef_constant(self):
+        Fx, a, b = root = tree('[2x + c]_a^b')
+        self.assertEqualPos(match_remove_indef_constant(root),
+                [P(root, remove_indef_constant, (Scope(Fx), Fx[1]))])
+
+        Fx, a, b = root = tree('[2x + x]_a^b')
+        self.assertEqualPos(match_remove_indef_constant(root), [])
+
+        Fx, a, b = root = tree('[2x]_a^b')
+        self.assertEqualPos(match_remove_indef_constant(root), [])
+
+    def test_remove_indef_constant(self):
+        root, e = tree('[2x + c]_a^b, [2x]_a^b')
+        Fx = root[0]
+        self.assertEqual(remove_indef_constant(root, (Scope(Fx), Fx[1])), e)