fractions.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. from itertools import combinations, product
  2. from .utils import least_common_multiple, partition
  3. from ..node import ExpressionLeaf as L, Scope, negate, OP_DIV, OP_ADD, \
  4. OP_MUL, nary_node, negate
  5. from ..possibilities import Possibility as P, MESSAGES
  6. from ..translate import _
  7. def match_constant_division(node):
  8. """
  9. a / 0 -> Division by zero
  10. a / 1 -> a
  11. 0 / a -> 0
  12. a / a -> 1
  13. """
  14. assert node.is_op(OP_DIV)
  15. p = []
  16. nominator, denominator = node
  17. # a / 0
  18. if denominator == 0:
  19. raise ZeroDivisionError('Division by zero: %s.' % node)
  20. # a / 1
  21. if denominator == 1:
  22. p.append(P(node, division_by_one, (nominator,)))
  23. # 0 / a
  24. if nominator == 0:
  25. p.append(P(node, division_of_zero, (denominator,)))
  26. # a / a
  27. if nominator == denominator:
  28. p.append(P(node, division_by_self, (nominator,)))
  29. return p
  30. def division_by_one(root, args):
  31. """
  32. a / 1 -> a
  33. """
  34. return args[0]
  35. MESSAGES[division_by_one] = _('Division of {1} by 1 reduces to {1}.')
  36. def division_of_zero(root, args):
  37. """
  38. 0 / a -> 0
  39. """
  40. return L(0)
  41. MESSAGES[division_of_zero] = _('Division of 0 by {1} reduces to 0.')
  42. def division_by_self(root, args):
  43. """
  44. a / a -> 1
  45. """
  46. return L(1)
  47. MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
  48. def match_add_constant_fractions(node):
  49. """
  50. 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
  51. 2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4
  52. 2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can
  53. # be added
  54. 2 / 4 - 3 / 4 -> -1 / 4
  55. 1 / 2 + 3 / 4 -> 4 / 8 + 6 / 8 # Equalize denominators by multiplying
  56. # them with eachother
  57. """
  58. assert node.is_op(OP_ADD)
  59. p = []
  60. scope = Scope(node)
  61. fractions = filter(lambda node: node.is_op(OP_DIV), scope)
  62. for a, b in combinations(fractions, 2):
  63. na, da = a
  64. nb, db = b
  65. if da == db:
  66. # Equal denominators, add nominators to create a single fraction
  67. p.append(P(node, add_nominators, (a, b)))
  68. elif da.is_numeric() and db.is_numeric():
  69. # Denominators are both numeric, rewrite both fractions to the
  70. # least common multiple of their denominators. Later, the
  71. # nominators will be added
  72. denom = least_common_multiple(da.value, db.value)
  73. p.append(P(node, equalize_denominators, (scope, a, b, denom)))
  74. # Also, add the (non-recommended) possibility to multiply the
  75. # denominators
  76. p.append(P(node, equalize_denominators, (scope, a, b,
  77. da.value * db.value)))
  78. return p
  79. def equalize_denominators(root, args):
  80. """
  81. 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4
  82. 1 / 2 - 3 / 4 -> 2 / 4 - 3 / 4
  83. a / 2 + b / 4 -> 2a / 4 + b / 4
  84. """
  85. scope, denom = args[::3]
  86. for fraction in args[1:3]:
  87. n, d = fraction
  88. mult = denom / d.value
  89. if mult != 1:
  90. if n.is_numeric():
  91. nom = L(n.value * mult)
  92. else:
  93. nom = L(mult) * n
  94. scope.replace(fraction, negate(nom / L(d.value * mult), n.negated))
  95. return scope.as_nary_node()
  96. MESSAGES[equalize_denominators] = _('Equalize the denominators of divisions'
  97. ' {2} and {3} to {4}.')
  98. def add_nominators(root, args):
  99. """
  100. a / b + c / b -> (a + c) / b
  101. a / b - c / b -> (a - c) / b
  102. -(a / b) + c / b -> -((a + c) / b)
  103. -(a / b) - c / b -> (c - a) / -b
  104. """
  105. # TODO: is 'add' Appropriate when rewriting to "(a + (-c)) / b"?
  106. ab, cb = args
  107. a, b = ab
  108. scope = Scope(root)
  109. # Replace the left node with the new expression
  110. scope.replace(ab, (a + cb[0].negate(cb.negated)) / b)
  111. # Remove the right node
  112. scope.remove(cb)
  113. return scope.as_nary_node()
  114. # TODO: convert this to a lambda. Example: 22 / 77 - 28 / 77. the "-" is above
  115. # the "28/77" division.
  116. MESSAGES[add_nominators] = _('Add the nominators of {1} and {2}.')
  117. def match_expand_and_add_fractions(node):
  118. """
  119. a * b / c + d * b / c -> (a + d) * (b / c)
  120. a * b / c + (- d * b / c) -> (a + (-d)) * (b / c)
  121. """
  122. # TODO: is 'add' Appropriate when rewriting to "(a + (-d)) / * (b / c)"?
  123. assert node.is_op(OP_MUL)
  124. p = []
  125. return p
  126. def match_multiply_fractions(node):
  127. """
  128. a / b * (c / d) -> ac / (bd)
  129. a * (b / c) -> ab / c
  130. """
  131. assert node.is_op(OP_MUL)
  132. p = []
  133. scope = Scope(node)
  134. fractions, others = partition(lambda n: n.is_op(OP_DIV), scope)
  135. for ab, cd in combinations(fractions, 2):
  136. p.append(P(node, multiply_fractions, (scope, ab, cd)))
  137. for a, bc in product(others, fractions):
  138. p.append(P(node, multiply_with_fraction, (scope, a, bc)))
  139. return p
  140. def multiply_fractions(root, args):
  141. """
  142. a / b * (c / d) -> ac / (bd)
  143. """
  144. scope, ab, cd = args
  145. a, b = ab
  146. c, d = cd
  147. scope.replace(ab, a * c / (b * d))
  148. scope.remove(cd)
  149. return scope.as_nary_node()
  150. MESSAGES[multiply_fractions] = _('Multiply fractions {2} and {3}.')
  151. def multiply_with_fraction(root, args):
  152. """
  153. a * (b / c) -> ab / c
  154. """
  155. scope, a, bc = args
  156. b, c = bc
  157. scope.replace(a, a * b / c)
  158. scope.remove(bc)
  159. return scope.as_nary_node()
  160. MESSAGES[multiply_with_fraction] = _('Multiply {2} with fraction {3}.')
  161. def match_equal_fraction_parts(node):
  162. """
  163. Divide nominator and denominator by the same part.
  164. Examples:
  165. ab / (ac) -> b / c
  166. ab / a -> b / 1
  167. a / (ab) -> 1 / b
  168. """
  169. assert node.is_op(OP_DIV)
  170. nominator, denominator = node
  171. if nominator.is_op(OP_MUL):
  172. n_scope = list(Scope(nominator))
  173. else:
  174. n_scope = [nominator]
  175. if denominator.is_op(OP_MUL):
  176. d_scope = list(Scope(denominator))
  177. else:
  178. d_scope = [denominator]
  179. p = []
  180. # Look for in scope
  181. for i, n in enumerate(n_scope):
  182. for j, d in enumerate(d_scope):
  183. if n.equals(d, ignore_negation=True):
  184. p.append(P(node, divide_fraction_parts,
  185. (negate(n, 0), n_scope, d_scope, i, j)))
  186. return p
  187. def divide_fraction_parts(root, args):
  188. """
  189. Divide nominator and denominator by the same part.
  190. Examples:
  191. ab / (ac) -> b / c
  192. ab / a -> b / 1
  193. a / (ab) -> 1 / b
  194. -ab / a -> -b / 1
  195. """
  196. a, n_scope, d_scope, i, j = args
  197. n, d = root
  198. a_n, a_d = n_scope[i], d_scope[j]
  199. del n_scope[i]
  200. del d_scope[j]
  201. if not n_scope:
  202. # Last element of nominator scope, replace by 1
  203. nom = L(1)
  204. elif len(n_scope) == 1:
  205. # Only one element left, no multiplication
  206. nom = n_scope[0]
  207. else:
  208. # Still a multiplication
  209. nom = nary_node('*', n_scope)
  210. if not d_scope:
  211. denom = L(1)
  212. elif len(n_scope) == 1:
  213. denom = d_scope[0]
  214. else:
  215. denom = nary_node('*', d_scope)
  216. # Move negation of removed part to nominator and denominator
  217. return nom.negate(n.negated + a_n.negated) \
  218. / denom.negate(d.negated + a_d.negated)
  219. MESSAGES[divide_fraction_parts] = \
  220. _('Divide nominator and denominator in {0} by {1}')
  221. def match_multiplied_power_division(node):
  222. """
  223. a ^ p * b / a ^ q -> a ^ p / a ^ q * b
  224. """
  225. assert node.is_op(OP_DIV)