Commit d2cfb91d authored by Taddeus Kroes's avatar Taddeus Kroes

Added some utilities for finding variables in a node.

parent adb38a1b
from ..node import OP_ADD, OP_MUL, OP_DIV, OP_POW, OP_NEG, OP_SIN, OP_COS, \ 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 .groups import match_combine_groups
from .factors import match_expand from .factors import match_expand
from .powers import match_add_exponents, match_subtract_exponents, \ from .powers import match_add_exponents, match_subtract_exponents, \
...@@ -17,6 +17,7 @@ from .negation import match_negated_factor, match_negate_polynome, \ ...@@ -17,6 +17,7 @@ from .negation import match_negated_factor, match_negate_polynome, \
from .sort import match_sort_multiplicants from .sort import match_sort_multiplicants
from .goniometry import match_add_quadrants, match_negated_parameter, \ from .goniometry import match_add_quadrants, match_negated_parameter, \
match_half_pi_subtraction, match_standard_radian match_half_pi_subtraction, match_standard_radian
from src.rules.derivatives import match_constant_derivative
RULES = { RULES = {
OP_ADD: [match_add_numerics, match_add_constant_fractions, OP_ADD: [match_add_numerics, match_add_constant_fractions,
...@@ -38,4 +39,5 @@ RULES = { ...@@ -38,4 +39,5 @@ RULES = {
OP_COS: [match_negated_parameter, match_half_pi_subtraction, OP_COS: [match_negated_parameter, match_half_pi_subtraction,
match_standard_radian], match_standard_radian],
OP_TAN: [match_standard_radian], OP_TAN: [match_standard_radian],
OP_DERIV: [match_constant_derivative],
} }
import unittest 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 from tests.rulestestcase import tree
...@@ -22,3 +23,11 @@ class TestRulesUtils(unittest.TestCase): ...@@ -22,3 +23,11 @@ class TestRulesUtils(unittest.TestCase):
def test_partition(self): def test_partition(self):
self.assertEqual(partition(lambda x: x & 1, range(6)), self.assertEqual(partition(lambda x: x & 1, range(6)),
([1, 3, 5], [0, 2, 4])) ([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']))
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