fractions.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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, OP_POW, 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 by 1 yields the nominator.')
  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 itself 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_divide_fractions(node):
  162. """
  163. Reduce divisions of fractions to a single fraction.
  164. Examples:
  165. a / b / c -> a / (bc)
  166. a / (b / c) -> ac / b
  167. """
  168. assert node.is_op(OP_DIV)
  169. nom, denom = node
  170. p = []
  171. if nom.is_op(OP_DIV):
  172. p.append(P(node, divide_fraction, tuple(nom) + (denom,)))
  173. if denom.is_op(OP_DIV):
  174. p.append(P(node, divide_by_fraction, (nom,) + tuple(denom)))
  175. return p
  176. def divide_fraction(root, args):
  177. """
  178. a / b / c -> a / (bc)
  179. """
  180. a, b, c = args
  181. return a / (b * c)
  182. MESSAGES[divide_fraction] = _('Move {3} to denominator of fraction {1} / {2}.')
  183. def divide_by_fraction(root, args):
  184. """
  185. a / (b / c) -> ac / b
  186. """
  187. a, b, c = args
  188. return a * c / b
  189. MESSAGES[divide_by_fraction] = \
  190. _('Move {3} to nominator of fraction {1} / {2}.')
  191. #def match_extract_divided_fractions(node):
  192. # """
  193. # Reduce divisions of fractions to a single fraction.
  194. #
  195. # Examples:
  196. # a / b / c -> a / bc
  197. # a / (b / c) -> ac / b
  198. # # IMPLICIT: a / b / (c / d) ->* ad / bd -> validation test!
  199. # """
  200. # assert node.is_op(OP_DIV)
  201. #
  202. # nom, denom = node
  203. # n_scope, d_scope = fraction_scopes(node)
  204. # is_division = lambda n: n.is_op(OP_DIV)
  205. # n_fractions, n_others = partition(is_division, n_scope)
  206. # d_fractions, d_others = partition(is_division, d_scope)
  207. #
  208. #
  209. # return []
  210. def fraction_scopes(node):
  211. """
  212. Get the multiplication scopes of the nominator and denominator of a
  213. fraction.
  214. """
  215. assert node.is_op(OP_DIV)
  216. nominator, denominator = node
  217. if nominator.is_op(OP_MUL):
  218. n_scope = list(Scope(nominator))
  219. else:
  220. n_scope = [nominator]
  221. if denominator.is_op(OP_MUL):
  222. d_scope = list(Scope(denominator))
  223. else:
  224. d_scope = [denominator]
  225. return n_scope, d_scope
  226. def match_equal_fraction_parts(node):
  227. """
  228. Divide nominator and denominator by the same part.
  229. Examples:
  230. ab / (ac) -> b / c
  231. ab / a -> b / 1
  232. a / (ab) -> 1 / b
  233. If the same root appears in both nominator and denominator, extrct it so
  234. that it can be reduced to a single power by power division rules.
  235. a ^ p * b / a ^ q -> a ^ p / a ^ q * b / 1
  236. a ^ p * b / a -> a ^ p / a * b / 1
  237. a * b / a ^ q -> a / a ^ q * b / 1
  238. """
  239. assert node.is_op(OP_DIV)
  240. nominator, denominator = node
  241. n_scope, d_scope = fraction_scopes(node)
  242. p = []
  243. # Look for matching parts in scopes
  244. for i, n in enumerate(n_scope):
  245. for j, d in enumerate(d_scope):
  246. if n.equals(d, ignore_negation=True):
  247. p.append(P(node, divide_fraction_parts,
  248. (negate(n, 0), n_scope, d_scope, i, j)))
  249. if n.is_op(OP_POW):
  250. a = n[0]
  251. if d == a or (d.is_op(OP_POW) and d[0] == a):
  252. # a ^ p * b / a -> a ^ p / a * b
  253. p.append(P(node, extract_divided_roots,
  254. (a, n_scope, d_scope, i, j)))
  255. elif d.is_op(OP_POW) and n == d[0]:
  256. # a * b / a ^ q -> a / a ^ q * b
  257. p.append(P(node, extract_divided_roots,
  258. (d[0], n_scope, d_scope, i, j)))
  259. return p
  260. def remove_from_scopes(n_scope, d_scope, i, j):
  261. a_n, a_d = n_scope[i], d_scope[j]
  262. del n_scope[i]
  263. del d_scope[j]
  264. if not n_scope:
  265. # Last element of nominator scope, replace by 1
  266. nom = L(1)
  267. elif len(n_scope) == 1:
  268. # Only one element left, no multiplication
  269. nom = n_scope[0]
  270. else:
  271. # Still a multiplication
  272. nom = nary_node('*', n_scope)
  273. if not d_scope:
  274. denom = L(1)
  275. elif len(n_scope) == 1:
  276. denom = d_scope[0]
  277. else:
  278. denom = nary_node('*', d_scope)
  279. return a_n, a_d, nom, denom
  280. def divide_fraction_parts(root, args):
  281. """
  282. Divide nominator and denominator by the same part.
  283. Examples:
  284. ab / (ac) -> b / c
  285. ab / a -> b / 1
  286. a / (ab) -> 1 / b
  287. -ab / a -> -b / 1
  288. """
  289. a, n_scope, d_scope, i, j = args
  290. n, d = root
  291. a_n, a_d, nom, denom = remove_from_scopes(n_scope, d_scope, i, j)
  292. # Move negation of removed part to nominator and denominator
  293. return nom.negate(n.negated + a_n.negated) \
  294. / denom.negate(d.negated + a_d.negated)
  295. MESSAGES[divide_fraction_parts] = \
  296. _('Divide nominator and denominator in {0} by {1}.')
  297. def extract_divided_roots(root, args):
  298. """
  299. a ^ p * b / a ^ q -> a ^ p / a ^ q * b / 1
  300. a ^ p * b / a -> a ^ p / a * b / 1
  301. a * b / a ^ q -> a / a ^ q * b / 1
  302. """
  303. a, n_scope, d_scope, i, j = args
  304. n, d = root
  305. ap, aq, nom, denom = remove_from_scopes(n_scope, d_scope, i, j)
  306. return ap / aq * nom.negate(n.negated) / denom.negate(d.negated)
  307. MESSAGES[extract_divided_roots] = \
  308. _('Extract the root {1} from nominator and denominator in {0}.')