precedences.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from .factors import expand_double, expand_single
  2. from .sort import swap_factors
  3. from .numerics import multiply_one, multiply_zero, reduce_fraction_constants, \
  4. raise_numerics, remove_zero, multiply_numerics, add_numerics
  5. from .logarithmic import factor_in_exponent_multiplicant, \
  6. factor_out_exponent, raised_base, factor_out_exponent_important
  7. from .derivatives import chain_rule
  8. from .negation import double_negation, negated_factor, negated_nominator, \
  9. negated_denominator, negated_zero
  10. from .factors import expand_double, expand_single
  11. from .fractions import multiply_with_fraction, divide_fraction_by_term, \
  12. add_nominators
  13. from .integrals import factor_out_constant, integrate_variable_root
  14. from .powers import remove_power_of_one
  15. from .sqrt import quadrant_sqrt, extract_sqrt_mult_priority
  16. from .lineq import substitute_variable, swap_sides, divide_term, multiply_term
  17. # Functions to move to the beginning of the possibilities list. Pairs of within
  18. # the list itself are compared by their position in the list: lower in the list
  19. # means lower priority
  20. HIGH = [
  21. raised_base,
  22. # 4 / 4 + 1 / 4 -> 5 / 4 instead of 1 + 1/4
  23. add_nominators,
  24. ]
  25. # Functions to move to the end of the possibilities list. Pairs of within the
  26. # list itself are compared by their position in the list: lower in the list
  27. # means lower priority
  28. LOW = [
  29. factor_in_exponent_multiplicant,
  30. reduce_fraction_constants,
  31. # Sorting expression terms has a low priority because it is assumed to
  32. # be handled by the user
  33. swap_factors,
  34. ]
  35. # Fucntion precedences relative to eachother. Tuple (A, B) means that A has a
  36. # higher priority than B. This list ignores occurences in the HIGH or LOW lists
  37. # above
  38. RELATIVE = [
  39. # Precedences needed for 'power rule' (derivative of an exponentiation)
  40. (chain_rule, raised_base),
  41. (raised_base, factor_out_exponent),
  42. # Expand 'single' before 'double' to avoid unnessecary complexity
  43. (expand_single, expand_double),
  44. (factor_out_exponent_important, raise_numerics),
  45. (factor_out_constant, multiply_with_fraction),
  46. # int x dx -> int x ^ 1 dx # do not remove power of one that has
  47. # # deliberately been inserted
  48. (integrate_variable_root, remove_power_of_one),
  49. # When simplifying square roots, bring numeric quadrants out of the
  50. # root first
  51. (extract_sqrt_mult_priority, multiply_numerics),
  52. # sqrt(2 ^ 2) -> 2 # rather than sqrt(4)
  53. (quadrant_sqrt, raise_numerics),
  54. # Prevent cycles that are caused by multiplication reductions when
  55. # splitting up fractions
  56. (divide_fraction_by_term, multiply_numerics),
  57. # Prevent useless swapping when solving multiple equations
  58. (substitute_variable, swap_sides),
  59. # When solving of an equation with constants, expanding an equation has
  60. # a lower priority
  61. (divide_term, multiply_term, swap_sides, expand_double, expand_single),
  62. ]
  63. # Convert to dictionaries for efficient lookup
  64. HIGH = dict([(h, i) for i, h in enumerate(HIGH)])
  65. LOW = dict([(h, i) for i, h in enumerate(LOW)])
  66. # List of implicit rules. Implicit rules are considered trivial and are
  67. # therefore not printed in verbose rewrite_all mode
  68. IMPLICIT_RULES = [
  69. negated_factor,
  70. double_negation,
  71. negated_nominator,
  72. negated_denominator,
  73. multiply_one,
  74. multiply_zero,
  75. negated_zero,
  76. remove_zero,
  77. remove_power_of_one,
  78. negated_factor,
  79. add_numerics,
  80. swap_factors,
  81. ]