test_rules_fractions.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. from src.rules.fractions import match_constant_division, division_by_one, \
  16. division_of_zero, division_by_self, match_add_fractions, \
  17. equalize_denominators, add_nominators, match_multiply_fractions, \
  18. multiply_fractions, multiply_with_fraction, match_divide_fractions, \
  19. divide_fraction, divide_by_fraction, match_extract_fraction_terms, \
  20. constant_to_fraction, extract_nominator_term, extract_fraction_terms, \
  21. match_division_in_denominator, multiply_with_term, \
  22. divide_fraction_by_term, match_combine_fractions, combine_fractions, \
  23. match_remove_division_negation, remove_division_negation
  24. from src.node import ExpressionNode as N, Scope, OP_MUL
  25. from src.possibilities import Possibility as P
  26. from tests.rulestestcase import RulesTestCase, tree
  27. class TestRulesFractions(RulesTestCase):
  28. def test_match_constant_division(self):
  29. a, zero = tree('a,0')
  30. root = a / zero
  31. with self.assertRaises(ZeroDivisionError) as cm:
  32. match_constant_division(root)
  33. self.assertEqual(cm.exception.message, 'Division by zero: a / 0.')
  34. root = a / 1
  35. possibilities = match_constant_division(root)
  36. self.assertEqualPos(possibilities, [P(root, division_by_one, (a,))])
  37. root = zero / a
  38. possibilities = match_constant_division(root)
  39. self.assertEqualPos(possibilities, [P(root, division_of_zero, (a,))])
  40. root = a / a
  41. possibilities = match_constant_division(root)
  42. self.assertEqualPos(possibilities, [P(root, division_by_self, (a,))])
  43. def test_division_by_one(self):
  44. a = tree('a')
  45. root = a / 1
  46. self.assertEqualNodes(division_by_one(root, (a,)), a)
  47. def test_division_of_zero(self):
  48. a, zero = tree('a,0')
  49. root = zero / a
  50. self.assertEqualNodes(division_of_zero(root, ()), zero)
  51. def test_division_by_self(self):
  52. a, one = tree('a,1')
  53. root = a / a
  54. self.assertEqualNodes(division_by_self(root, ()), one)
  55. def test_match_add_fractions(self):
  56. a, b, c, l1, l2, l3, l4 = tree('a,b,c,1,2,3,4')
  57. n0, n1 = root = l1 / l2 + l3 / l4
  58. possibilities = match_add_fractions(root)
  59. self.assertEqualPos(possibilities,
  60. [P(root, equalize_denominators, (Scope(root), n0, n1, 4)),
  61. P(root, equalize_denominators, (Scope(root), n0, n1, 8))])
  62. (((n0, n1), n2), n3), n4 = root = a + l1 / l2 + b + l3 / l4 + c
  63. possibilities = match_add_fractions(root)
  64. self.assertEqualPos(possibilities,
  65. [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
  66. P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
  67. n0, n1 = root = l2 / l4 + l3 / l4
  68. possibilities = match_add_fractions(root)
  69. self.assertEqualPos(possibilities,
  70. [P(root, add_nominators, (Scope(root), n0, n1))])
  71. (((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + l3 / l4 + c
  72. possibilities = match_add_fractions(root)
  73. self.assertEqualPos(possibilities,
  74. [P(root, add_nominators, (Scope(root), n1, n3))])
  75. def test_match_add_fractions_constant_to_fraction(self):
  76. l23, l1 = root = tree('2 / 3 + 1')
  77. self.assertEqualPos(match_add_fractions(root),
  78. [P(root, constant_to_fraction, (Scope(root), l23, l1))])
  79. def test_add_fractions_with_negation(self):
  80. a, b, c, l1, l2, l3, l4 = tree('a,b,c,1,2,3,4')
  81. (((n0, n1), n2), n3), n4 = root = a + l2 / l2 + b + (-l3 / l4) + c
  82. self.assertEqualPos(match_add_fractions(root),
  83. [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
  84. P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
  85. n0, n1 = root = l1 / l2 + l4 / l3
  86. self.assertEqualPos(match_add_fractions(root),
  87. [P(root, equalize_denominators, (Scope(root), n0, n1, 6))])
  88. (((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + (-l3 / l4) + c
  89. self.assertEqualPos(match_add_fractions(root),
  90. [P(root, add_nominators, (Scope(root), n1, n3))])
  91. def test_equalize_denominators(self):
  92. a, b, l1, l2, l3, l4 = tree('a,b,1,2,3,4')
  93. n0, n1 = root = l1 / l2 + l3 / l4
  94. self.assertEqualNodes(equalize_denominators(root,
  95. (Scope(root), n0, n1, 4)), l2 / l4 + l3 / l4)
  96. n0, n1 = root = a / l2 + b / l4
  97. self.assertEqualNodes(equalize_denominators(root,
  98. (Scope(root), n0, n1, 4)), (l2 * a) / l4 + b /
  99. l4)
  100. #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
  101. n0, n1 = root = l1 / l2 + (-l3 / l4)
  102. self.assertEqualNodes(equalize_denominators(root,
  103. (Scope(root), n0, n1, 4)), l2 / l4 + (-l3 / l4))
  104. #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
  105. n0, n1 = root = a / l2 + (-b / l4)
  106. self.assertEqualNodes(equalize_denominators(root,
  107. (Scope(root), n0, n1, 4)), (l2 * a) / l4 + (-b / l4))
  108. def test_add_nominators(self):
  109. a, b, c = tree('a,b,c')
  110. n0, n1 = root = a / b + c / b
  111. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  112. (a + c) / b)
  113. n0, n1 = root = a / b + -c / b
  114. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  115. (a + -c) / b)
  116. n0, n1 = root = a / b + -(c / b)
  117. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  118. (a + -c) / b)
  119. n0, n1 = root = a / -b + c / -b
  120. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  121. (a + c) / -b)
  122. n0, n1 = root = a / -b + -c / -b
  123. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  124. (a + -c) / -b)
  125. def test_constant_to_fraction(self):
  126. root, e = tree('2 / 3 + 1, 2 / 3 + (3 * 1) / 3')
  127. l23, l1 = root
  128. self.assertEqual(constant_to_fraction(root, (Scope(root), l23, l1)), e)
  129. def test_match_multiply_fractions(self):
  130. (a, b), (c, d) = ab, cd = root = tree('a / b * (c / d)')
  131. self.assertEqualPos(match_multiply_fractions(root),
  132. [P(root, multiply_fractions, (Scope(root), ab, cd))])
  133. (ab, e), cd = root = tree('4 / b * 2 * (3 / d)')
  134. self.assertEqualPos(match_multiply_fractions(root),
  135. [P(root, multiply_fractions, (Scope(root), ab, cd)),
  136. P(root, multiply_with_fraction, (Scope(root), ab, e)),
  137. P(root, multiply_with_fraction, (Scope(root), cd, e))])
  138. ab, c = root = tree('1 / sqrt(3) * 2')
  139. self.assertEqualPos(match_multiply_fractions(root),
  140. [P(root, multiply_with_fraction, (Scope(root), ab, c))])
  141. def test_multiply_fractions(self):
  142. (a, b), (c, d) = ab, cd = root = tree('a / b * (c / d)')
  143. self.assertEqual(multiply_fractions(root, (Scope(root), ab, cd)),
  144. a * c / (b * d))
  145. (ab, e), cd = root = tree('a / b * e * (c / d)')
  146. self.assertEqual(multiply_fractions(root, (Scope(root), ab, cd)),
  147. a * c / (b * d) * e)
  148. def test_match_divide_fractions(self):
  149. (a, b), c = root = tree('a / b / c')
  150. self.assertEqualPos(match_divide_fractions(root),
  151. [P(root, divide_fraction, (a, b, c))])
  152. root = tree('a / (b / c)')
  153. self.assertEqualPos(match_divide_fractions(root),
  154. [P(root, divide_by_fraction, (a, b, c))])
  155. def test_divide_fraction(self):
  156. (a, b), c = root = tree('a / b / c')
  157. self.assertEqual(divide_fraction(root, (a, b, c)), a / (b * c))
  158. (a, b), c = root = tree('-a / b / c')
  159. self.assertEqual(divide_fraction(root, (a, b, c)), -(a / (b * c)))
  160. root = tree('a / b / -c')
  161. self.assertEqual(divide_fraction(root, (a, b, c)), a / (b * -c))
  162. def test_divide_by_fraction(self):
  163. a, (b, c) = root = tree('a / (b / c)')
  164. self.assertEqual(divide_by_fraction(root, (a, b, c)), a * c / b)
  165. a, (b, c) = root = tree('-a / (b / c)')
  166. self.assertEqual(divide_by_fraction(root, (a, b, c)), -(a * c / b))
  167. root = tree('a / -(b / c)')
  168. self.assertEqual(divide_by_fraction(root, (a, b, c)), -(a * c / b))
  169. def test_match_extract_fraction_terms(self):
  170. root, a, b, c = tree('(ab) / (ca), a, b, c')
  171. n, d = root
  172. self.assertEqualPos(match_extract_fraction_terms(root),
  173. [P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a))])
  174. lscp = lambda l: Scope(N(OP_MUL, l))
  175. n, d = root = tree('(ab) / a')
  176. self.assertEqualPos(match_extract_fraction_terms(root),
  177. [P(root, divide_fraction_by_term, (Scope(n), lscp(d), a, a))])
  178. n, d = root = tree('a / (ab)')
  179. self.assertEqualPos(match_extract_fraction_terms(root),
  180. [P(root, divide_fraction_by_term, (lscp(n), Scope(d), a, a))])
  181. n, d = root = tree('(abc) / (cba)')
  182. self.assertEqualPos(match_extract_fraction_terms(root),
  183. [P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a)),
  184. P(root, divide_fraction_by_term, (Scope(n), Scope(d), b, b)),
  185. P(root, divide_fraction_by_term, (Scope(n), Scope(d), c, c))])
  186. root = tree('a / a')
  187. self.assertEqualPos(match_extract_fraction_terms(root), [])
  188. (ap, b), aq = n, d = root = tree('(a ^ p * b) / a ^ q')
  189. self.assertEqualPos(match_extract_fraction_terms(root),
  190. [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, aq))])
  191. (a, b), aq = n, d = root = tree('(ab) / a ^ q')
  192. self.assertEqualPos(match_extract_fraction_terms(root),
  193. [P(root, extract_fraction_terms, (Scope(n), lscp(d), a, aq))])
  194. (ap, b), a = n, d = root = tree('(a ^ p * b) / a')
  195. self.assertEqualPos(match_extract_fraction_terms(root),
  196. [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, a))])
  197. (l2, a), l3 = n, d = root = tree('(2a) / 3')
  198. self.assertEqualPos(match_extract_fraction_terms(root),
  199. [P(root, extract_nominator_term, (2, a))])
  200. a, l3 = n, d = root = tree('a / 3')
  201. self.assertEqualPos(match_extract_fraction_terms(root),
  202. [P(root, extract_nominator_term, (1, a))])
  203. root = tree('(2 * 4) / 3')
  204. self.assertEqualPos(match_extract_fraction_terms(root), [])
  205. n, d = root = tree('(2a) / 2')
  206. self.assertEqualPos(match_extract_fraction_terms(root),
  207. [P(root, extract_nominator_term, (2, a)),
  208. P(root, divide_fraction_by_term, (Scope(n), lscp(d), 2, 2))])
  209. def test_extract_nominator_term(self):
  210. root, expect = tree('(2a) / 3, 2 / 3 * a')
  211. l2, a = root[0]
  212. self.assertEqual(extract_nominator_term(root, (l2, a)), expect)
  213. root, expect, l1 = tree('a / 3, 1 / 3 * a, 1')
  214. self.assertEqual(extract_nominator_term(root, (l1, root[0])), expect)
  215. def test_extract_fraction_terms_basic(self):
  216. root, expect = tree('(ab) / (ca), a / a * b / c')
  217. n, d = root
  218. self.assertEqual(extract_fraction_terms(root,
  219. (Scope(n), Scope(d), n[0], d[1])), expect)
  220. def test_extract_fraction_terms_leaf(self):
  221. root, expect = tree('(ba) / a, a / a * b / 1')
  222. n, d = root
  223. self.assertEqual(extract_fraction_terms(root,
  224. (Scope(n), Scope(N(OP_MUL, d)), n[1], d)), expect)
  225. root, expect = tree('a / (ab), a / a * 1 / b')
  226. n, d = root
  227. self.assertEqual(extract_fraction_terms(root,
  228. (Scope(N(OP_MUL, n)), Scope(d), n, d[0])), expect)
  229. def test_extract_fraction_terms_chain(self):
  230. self.assertRewrite([
  231. '(a ^ 3 * 4) / (a ^ 2 * 5)',
  232. 'a ^ 3 / a ^ 2 * 4 / 5',
  233. 'a ^ (3 - 2)4 / 5',
  234. 'a ^ 1 * 4 / 5',
  235. 'a * 4 / 5',
  236. # FIXME: '4 / 5 * a',
  237. ])
  238. def test_divide_fraction_by_term(self):
  239. (ab, a), expect = root = tree('(ab) / a, b')
  240. args = Scope(ab), Scope(N(OP_MUL, a)), ab[0], a
  241. self.assertEqual(divide_fraction_by_term(root, args), expect)
  242. def test_match_division_in_denominator(self):
  243. a, ((b, c), d) = root = tree('a / (b / c + d)')
  244. self.assertEqualPos(match_division_in_denominator(root),
  245. [P(root, multiply_with_term, (c,))])
  246. a, ((d, (b, c)), e) = root = tree('a / (d + b / c + e)')
  247. self.assertEqualPos(match_division_in_denominator(root),
  248. [P(root, multiply_with_term, (c,))])
  249. def test_multiply_with_term_chain(self):
  250. self.assertRewrite([
  251. '1 / (1 / b - 1 / a)',
  252. '1 / ((1 * -a) / (b * -a) + (b * 1) / (b * -a))',
  253. '1 / ((1 * -a + b * 1) / (b * -a))',
  254. '1 / ((-a + b * 1) / (b * -a))',
  255. '1 / ((-a + b) / (b * -a))',
  256. '1 / ((-a + b) / (-ba))',
  257. '1 / (-(-a + b) / (ba))',
  258. '1 / ((--a - b) / (ba))',
  259. '1 / ((a - b) / (ba))',
  260. '(1ba) / (a - b)',
  261. '(ba) / (a - b)',
  262. '(ab) / (a - b)',
  263. ])
  264. def test_match_combine_fractions(self):
  265. ab, cd = root = tree('a / b + c / d')
  266. self.assertEqualPos(match_combine_fractions(root),
  267. [P(root, combine_fractions, (Scope(root), ab, cd))])
  268. def test_combine_fractions(self):
  269. (a, b), (c, d) = ab, cd = root = tree('a / b + c / d')
  270. self.assertEqual(combine_fractions(root, (Scope(root), ab, cd)),
  271. a * d / (b * d) + b * c / (b * d))
  272. def test_match_remove_division_negation(self):
  273. root = tree('-(-a + b) / c')
  274. self.assertEqualPos(match_remove_division_negation(root),
  275. [P(root, remove_division_negation, (True, root[0]))])
  276. root = tree('-a / (-b + c)')
  277. self.assertEqualPos(match_remove_division_negation(root),
  278. [P(root, remove_division_negation, (False, root[1]))])
  279. def test_remove_division_negation(self):
  280. (a, b), c = root = tree('-(-a + b) / c')
  281. self.assertEqual(remove_division_negation(root, (True, root[0])),
  282. (-a - b) / c)
  283. a, (b, c) = root = tree('-a / (-b + c)')
  284. self.assertEqual(remove_division_negation(root, (False, root[1])),
  285. +a / (-b - c))