Przeglądaj źródła

Added shortcut constructor for equality operator.

Taddeus Kroes 14 lat temu
rodzic
commit
d32ba0623a
3 zmienionych plików z 15 dodań i 2 usunięć
  1. 7 0
      src/node.py
  2. 3 1
      src/rules/__init__.py
  3. 5 1
      tests/test_node.py

+ 7 - 0
src/node.py

@@ -691,3 +691,10 @@ def indef(*args):
     Create an indefinite integral node.
     """
     return ExpressionNode(OP_INT_INDEF, *args)
+
+
+def eq(left, right):
+    """
+    Create an equality operator node.
+    """
+    return ExpressionNode(OP_EQ, left, right)

+ 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_DER, OP_LOG, OP_INT, OP_INT_INDEF
+        OP_TAN, OP_DER, OP_LOG, OP_INT, OP_INT_INDEF, OP_EQ
 from .groups import match_combine_groups
 from .factors import match_expand
 from .powers import match_add_exponents, match_subtract_exponents, \
@@ -27,6 +27,7 @@ from src.rules.logarithmic import match_constant_logarithm, \
 from src.rules.integrals import match_solve_indef, match_constant_integral, \
         match_integrate_variable_power, match_factor_out_constant, \
         match_division_integral, match_function_integral
+from src.rules.lineq import match_subtract_addition_term
 
 RULES = {
         OP_ADD: [match_add_numerics, match_add_constant_fractions,
@@ -60,4 +61,5 @@ RULES = {
                  match_factor_out_constant, match_division_integral,
                  match_function_integral],
         OP_INT_INDEF: [match_solve_indef],
+        OP_EQ: [match_subtract_addition_term],
         }

+ 5 - 1
tests/test_node.py

@@ -1,6 +1,6 @@
 from src.node import ExpressionNode as N, ExpressionLeaf as L, Scope, \
         nary_node, get_scope, OP_ADD, infinity, absolute, sin, cos, tan, log, \
-        ln, der, integral, indef
+        ln, der, integral, indef, eq
 from tests.rulestestcase import RulesTestCase, tree
 
 
@@ -291,3 +291,7 @@ class TestNode(RulesTestCase):
     def test_indef(self):
         x2, a, b, expect = tree('x ^ 2, a, b, [x ^ 2]_a^b')
         self.assertEqual(indef(x2, a, b), expect)
+
+    def test_eq(self):
+        x, a, b, expect = tree('x, a, b, x + a = b')
+        self.assertEqual(eq(x + a, b), expect)