Просмотр исходного кода

Relative precedences in strategy now support more than 2 functions in the same list.

Taddeus Kroes 13 лет назад
Родитель
Сommit
7e9c999323
2 измененных файлов с 12 добавлено и 6 удалено
  1. 9 1
      src/rules/precedences.py
  2. 3 5
      src/strategy.py

+ 9 - 1
src/rules/precedences.py

@@ -7,12 +7,13 @@ from .logarithmic import factor_in_exponent_multiplicant, \
 from .derivatives import chain_rule
 from .derivatives import chain_rule
 from .negation import double_negation, negated_factor, negated_nominator, \
 from .negation import double_negation, negated_factor, negated_nominator, \
         negated_denominator, negated_zero
         negated_denominator, negated_zero
+from .factors import expand_double, expand_single
 from .fractions import multiply_with_fraction, extract_fraction_terms, \
 from .fractions import multiply_with_fraction, extract_fraction_terms, \
         add_nominators
         add_nominators
 from .integrals import factor_out_constant, integrate_variable_root
 from .integrals import factor_out_constant, integrate_variable_root
 from .powers import remove_power_of_one
 from .powers import remove_power_of_one
 from .sqrt import quadrant_sqrt, extract_sqrt_mult_priority
 from .sqrt import quadrant_sqrt, extract_sqrt_mult_priority
-from .lineq import substitute_variable, swap_sides
+from .lineq import substitute_variable, swap_sides, divide_term, multiply_term
 
 
 
 
 # Functions to move to the beginning of the possibilities list. Pairs of within
 # Functions to move to the beginning of the possibilities list. Pairs of within
@@ -32,6 +33,9 @@ HIGH = [
 LOW = [
 LOW = [
         factor_in_exponent_multiplicant,
         factor_in_exponent_multiplicant,
         reduce_fraction_constants,
         reduce_fraction_constants,
+
+        # Sorting expression terms has a low priority because it is assumed to
+        # be handled by the user
         move_constant,
         move_constant,
         ]
         ]
 
 
@@ -68,6 +72,10 @@ RELATIVE = [
 
 
         # Prevent useless swapping when solving multiple equations
         # Prevent useless swapping when solving multiple equations
         (substitute_variable, swap_sides),
         (substitute_variable, swap_sides),
+
+        # When solving of an equation with constants, expanding an equation has
+        # a lower priority
+        (divide_term, multiply_term, swap_sides, expand_double, expand_single),
         ]
         ]
 
 
 
 

+ 3 - 5
src/strategy.py

@@ -18,11 +18,9 @@ def compare_possibilities(a, b):
         return 0
         return 0
 
 
     # Check if A and B have a precedence relative to eachother
     # Check if A and B have a precedence relative to eachother
-    if (ha, hb) in RELATIVE:
-        return -1
-
-    if (hb, ha) in RELATIVE:
-        return 1
+    for rel in RELATIVE:
+        if ha in rel and hb in rel:
+            return cmp(rel.index(ha), rel.index(hb))
 
 
     # If A has a high priority, it might be moved to the start of the list
     # If A has a high priority, it might be moved to the start of the list
     if ha in HIGH:
     if ha in HIGH: