precedences.py 2.4 KB

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