precedences.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from .factors import expand_double, expand_single
  2. from .sort import move_constant
  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, negated_factor
  10. from .fractions import multiply_with_fraction
  11. from .integrals import factor_out_constant, integrate_variable_root
  12. from .powers import remove_power_of_one
  13. from .sqrt import quadrant_sqrt, extract_sqrt_mult_priority
  14. # Functions to move to the beginning of the possibilities list. Pairs of within
  15. # the list itself are compared by their position in the list: lower in the list
  16. # means lower priority
  17. HIGH = [
  18. raised_base,
  19. ]
  20. # Functions to move to the end of the possibilities list. Pairs of within the
  21. # list itself are compared by their position in the list: lower in the list
  22. # means lower priority
  23. LOW = [
  24. factor_in_exponent_multiplicant,
  25. reduce_fraction_constants,
  26. move_constant,
  27. ]
  28. # Fucntion precedences relative to eachother. Tuple (A, B) means that A has a
  29. # higer priority than B. This list ignores occurences in the HIGH or LOW lists
  30. # above
  31. RELATIVE = [
  32. # Precedences needed for 'power rule'
  33. (chain_rule, raised_base),
  34. (raised_base, factor_out_exponent),
  35. # Expand 'single' before 'double' to avoid unnessecary complexity
  36. (expand_single, expand_double),
  37. (factor_out_exponent_important, raise_numerics),
  38. (factor_out_constant, multiply_with_fraction),
  39. # int x dx -> int x ^ 1 dx # do not remove power of one that has
  40. # # deliberately been inserted
  41. (integrate_variable_root, remove_power_of_one),
  42. # When simplifying square roots, bring numeric quadrants out of the
  43. # root first
  44. (extract_sqrt_mult_priority, multiply_numerics),
  45. # sqrt(2 ^ 2) -> 2 # not sqrt 4
  46. (quadrant_sqrt, raise_numerics),
  47. ]
  48. # Convert to dictionaries for efficient lookup
  49. HIGH = dict([(h, i) for i, h in enumerate(HIGH)])
  50. LOW = dict([(h, i) for i, h in enumerate(LOW)])
  51. # List of implicit rules. Implicit rules are considered trivial and are
  52. # therefore not printed in verbose rewrite_all mode
  53. IMPLICIT_RULES = [
  54. negated_factor,
  55. double_negation,
  56. negated_nominator,
  57. negated_denominator,
  58. multiply_one,
  59. multiply_zero,
  60. negated_zero,
  61. remove_zero,
  62. remove_power_of_one,
  63. negated_factor,
  64. multiply_numerics,
  65. add_numerics,
  66. ]