Ver código fonte

Added some utilities for finding variables in a node.

Taddeus Kroes 14 anos atrás
pai
commit
d2cfb91dbd
2 arquivos alterados com 13 adições e 2 exclusões
  1. 3 1
      src/rules/__init__.py
  2. 10 1
      tests/test_rules_utils.py

+ 3 - 1
src/rules/__init__.py

@@ -1,5 +1,5 @@
 from ..node import OP_ADD, OP_MUL, OP_DIV, OP_POW, OP_NEG, OP_SIN, OP_COS, \
-        OP_TAN
+        OP_TAN, OP_DERIV
 from .groups import match_combine_groups
 from .factors import match_expand
 from .powers import match_add_exponents, match_subtract_exponents, \
@@ -17,6 +17,7 @@ from .negation import match_negated_factor, match_negate_polynome, \
 from .sort import match_sort_multiplicants
 from .goniometry import match_add_quadrants, match_negated_parameter, \
         match_half_pi_subtraction, match_standard_radian
+from src.rules.derivatives import match_constant_derivative
 
 RULES = {
         OP_ADD: [match_add_numerics, match_add_constant_fractions,
@@ -38,4 +39,5 @@ RULES = {
         OP_COS: [match_negated_parameter, match_half_pi_subtraction,
                  match_standard_radian],
         OP_TAN: [match_standard_radian],
+        OP_DERIV: [match_constant_derivative],
         }

+ 10 - 1
tests/test_rules_utils.py

@@ -1,6 +1,7 @@
 import unittest
 
-from src.rules.utils import least_common_multiple, is_fraction, partition
+from src.rules.utils import least_common_multiple, is_fraction, partition, \
+        find_variables
 from tests.rulestestcase import tree
 
 
@@ -22,3 +23,11 @@ class TestRulesUtils(unittest.TestCase):
     def test_partition(self):
         self.assertEqual(partition(lambda x: x & 1, range(6)),
                          ([1, 3, 5], [0, 2, 4]))
+
+    def test_find_variables(self):
+        x, l2, add, mul0, mul1 = tree('x, 2, x + 2, 2x, xy')
+        self.assertSetEqual(find_variables(x), set(['x']))
+        self.assertSetEqual(find_variables(l2), set())
+        self.assertSetEqual(find_variables(add), set(['x']))
+        self.assertSetEqual(find_variables(mul0), set(['x']))
+        self.assertSetEqual(find_variables(mul1), set(['x', 'y']))