Эх сурвалжийг харах

Added left-or-right check to application that brings an expression into a fraction nominator.

Taddeus Kroes 14 жил өмнө
parent
commit
a455b76bfc

+ 0 - 2
TODO

@@ -60,8 +60,6 @@ Division of 0 by 1 reduces to 0.
 
  - Parser: 'apia' -> 'aa'
 
- - 'x(1 / x)'  ->  '(x * 1) / x'  (currently no left/right check: '1x / x')
-
  - Unit tests for strategy.
 
  - MESSAGES needs to be expanded.

+ 7 - 2
src/rules/fractions.py

@@ -217,12 +217,17 @@ MESSAGES[multiply_fractions] = _('Multiply fractions {2} and {3}.')
 
 def multiply_with_fraction(root, args):
     """
-    a / b * c and a, c in Z or a == 1  ->  ac / b
+    a / b * c and (eval(c) in Z or eval(a / b) not in Z)  ->  (ac) / b
     """
     scope, ab, c = args
     a, b = ab
 
-    scope.replace(ab, (a * c / b).negate(ab.negated))
+    if scope.index(ab) - scope.index(c) < 0:
+        replacement = a * c / b
+    else:
+        replacement = c * a / b
+
+    scope.replace(ab, replacement.negate(ab.negated))
     scope.remove(c)
 
     return scope.as_nary_node()

+ 3 - 3
tests/test_leiden_oefenopgave_v12.py

@@ -57,10 +57,10 @@ class TestLeidenOefenopgaveV12(TestCase):
 
     def test_2_a(self):
         self.assertRewrite([
-            '(a^2b^-1)^3(ab^2)',
+            '(a ^ 2 * b ^ -1) ^ 3(ab ^ 2)',
             '(a ^ 2 * 1 / b ^ 1) ^ 3 * ab ^ 2',
             '(a ^ 2 * 1 / b) ^ 3 * ab ^ 2',
-            '((1a ^ 2) / b) ^ 3 * ab ^ 2',
+            '((a ^ 2 * 1) / b) ^ 3 * ab ^ 2',
             '(a ^ 2 / b) ^ 3 * ab ^ 2',
             '(a ^ 2) ^ 3 / b ^ 3 * ab ^ 2',
             'a ^ (2 * 3) / b ^ 3 * ab ^ 2',
@@ -103,7 +103,7 @@ class TestLeidenOefenopgaveV12(TestCase):
         self.assertRewrite([
             '4b^-2',
             '4 * 1 / b ^ 2',
-            '(1 * 4) / b ^ 2',
+            '(4 * 1) / b ^ 2',
             '4 / b ^ 2',
         ])
 

+ 1 - 1
tests/test_rules_derivatives.py

@@ -110,7 +110,7 @@ class TestRulesDerivatives(RulesTestCase):
             "x ^ x * (1ln(x) + x[ln(x)]')",
             "x ^ x * (ln(x) + x[ln(x)]')",
             "x ^ x * (ln(x) + x * 1 / x)",
-            "x ^ x * (ln(x) + (1x) / x)",
+            "x ^ x * (ln(x) + (x * 1) / x)",
             "x ^ x * (ln(x) + x / x)",
             "x ^ x * (ln(x) + 1)",
             "x ^ x * ln(x) + x ^ x * 1",