fractions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from itertools import combinations
  2. from .utils import least_common_multiple
  3. from ..node import ExpressionLeaf as L, Scope, OP_DIV, OP_ADD, OP_MUL
  4. from ..possibilities import Possibility as P, MESSAGES
  5. from ..translate import _
  6. def match_constant_division(node):
  7. """
  8. a / 0 -> Division by zero
  9. a / 1 -> a
  10. 0 / a -> 0
  11. a / a -> 1
  12. """
  13. assert node.is_op(OP_DIV)
  14. p = []
  15. nominator, denominator = node
  16. # a / 0
  17. if denominator == 0:
  18. raise ZeroDivisionError('Division by zero: %s.' % node)
  19. # a / 1
  20. if denominator == 1:
  21. p.append(P(node, division_by_one, (nominator,)))
  22. # 0 / a
  23. if nominator == 0:
  24. p.append(P(node, division_of_zero, (denominator,)))
  25. # a / a
  26. if nominator == denominator:
  27. p.append(P(node, division_by_self, (nominator,)))
  28. return p
  29. def division_by_one(root, args):
  30. """
  31. a / 1 -> a
  32. """
  33. return args[0]
  34. MESSAGES[division_by_one] = _('Division of {1} by 1 reduces to {1}.')
  35. def division_of_zero(root, args):
  36. """
  37. 0 / a -> 0
  38. """
  39. return L(0)
  40. MESSAGES[division_of_zero] = _('Division of 0 by {1} reduces to 0.')
  41. def division_by_self(root, args):
  42. """
  43. a / a -> 1
  44. """
  45. return L(1)
  46. MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
  47. def match_add_constant_fractions(node):
  48. """
  49. 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
  50. 2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can
  51. # be added
  52. 2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
  53. 2 / 4 - 3 / 4 -> -1 / 4 # Equal denominators, so nominators can
  54. # be subtracted
  55. """
  56. assert node.is_op(OP_ADD)
  57. p = []
  58. fractions = filter(lambda node: node.is_op(OP_DIV), Scope(node))
  59. for a, b in combinations(fractions, 2):
  60. na, da = a
  61. nb, db = b
  62. if da == db:
  63. # Equal denominators, add nominators to create a single fraction
  64. p.append(P(node, add_nominators, (a, b)))
  65. elif da.is_numeric() and db.is_numeric():
  66. # Denominators are both numeric, rewrite both fractions to the
  67. # least common multiple of their denominators. Later, the
  68. # nominators will be added
  69. denom = least_common_multiple(da.value, db.value)
  70. p.append(P(node, equalize_denominators, (a, b, denom)))
  71. return p
  72. def equalize_denominators(root, args):
  73. """
  74. 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4
  75. a / 2 + b / 4 -> 2a / 4 + b / 4
  76. """
  77. denom = args[2]
  78. scope = Scope(root)
  79. for fraction in args[:2]:
  80. n, d = fraction
  81. mult = denom / d.value
  82. if mult != 1:
  83. n = L(n.value * mult) if n.is_numeric() else L(mult) * n
  84. scope.remove(fraction, negate(n / L(d.value * mult),
  85. fraction.negated))
  86. return scope.as_nary_node()
  87. MESSAGES[equalize_denominators] = _('Equalize the denominators of division'
  88. ' of {1} by {2}.')
  89. def add_nominators(root, args):
  90. """
  91. a / b + c / b -> (a + c) / b
  92. a / b - c / b -> (a - c) / b
  93. -(a / b) + c / b -> -((a + c) / b)
  94. -(a / b) - c / b -> (c - a) / -b
  95. """
  96. # TODO: is 'add' Appropriate when rewriting to "(a + (-c)) / b"?
  97. ab, cb = args
  98. a, b = ab
  99. scope = Scope(root)
  100. # Replace the left node with the new expression
  101. scope.remove(ab, (a + negate(cb[0], cb.negated)) / b)
  102. # Remove the right node
  103. scope.remove(cb)
  104. return scope.as_nary_node()
  105. # TODO: convert this to a lambda. Example: 22 / 77 - 28 / 77. the "-" is above
  106. # the "28/77" division.
  107. MESSAGES[add_nominators] = _('Add the nominators of {1} and {2}.')
  108. def match_expand_and_add_fractions(node):
  109. """
  110. a * b / c + d * b / c -> (a + d) * (b / c)
  111. a * b / c + (- d * b / c) -> (a + (-d)) * (b / c)
  112. """
  113. # TODO: is 'add' Appropriate when rewriting to "(a + (-d)) / * (b / c)"?
  114. assert node.is_op(OP_MUL)
  115. p = []
  116. return p