test_rules_fractions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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
  23. from src.node import ExpressionNode as N, Scope, OP_MUL
  24. from src.possibilities import Possibility as P
  25. from tests.rulestestcase import RulesTestCase, tree
  26. class TestRulesFractions(RulesTestCase):
  27. def test_match_constant_division(self):
  28. a, zero = tree('a,0')
  29. root = a / zero
  30. with self.assertRaises(ZeroDivisionError) as cm:
  31. match_constant_division(root)
  32. self.assertEqual(cm.exception.message, 'Division by zero: a / 0.')
  33. root = a / 1
  34. possibilities = match_constant_division(root)
  35. self.assertEqualPos(possibilities, [P(root, division_by_one, (a,))])
  36. root = zero / a
  37. possibilities = match_constant_division(root)
  38. self.assertEqualPos(possibilities, [P(root, division_of_zero, (a,))])
  39. root = a / a
  40. possibilities = match_constant_division(root)
  41. self.assertEqualPos(possibilities, [P(root, division_by_self, (a,))])
  42. def test_division_by_one(self):
  43. a = tree('a')
  44. root = a / 1
  45. self.assertEqualNodes(division_by_one(root, (a,)), a)
  46. def test_division_of_zero(self):
  47. a, zero = tree('a,0')
  48. root = zero / a
  49. self.assertEqualNodes(division_of_zero(root, ()), zero)
  50. def test_division_by_self(self):
  51. a, one = tree('a,1')
  52. root = a / a
  53. self.assertEqualNodes(division_by_self(root, ()), one)
  54. def test_match_add_fractions(self):
  55. a, b, c, l1, l2, l3, l4 = tree('a,b,c,1,2,3,4')
  56. n0, n1 = root = l1 / l2 + l3 / l4
  57. possibilities = match_add_fractions(root)
  58. self.assertEqualPos(possibilities,
  59. [P(root, equalize_denominators, (Scope(root), n0, n1, 4)),
  60. P(root, equalize_denominators, (Scope(root), n0, n1, 8))])
  61. (((n0, n1), n2), n3), n4 = root = a + l1 / l2 + b + l3 / l4 + c
  62. possibilities = match_add_fractions(root)
  63. self.assertEqualPos(possibilities,
  64. [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
  65. P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
  66. n0, n1 = root = l2 / l4 + l3 / l4
  67. possibilities = match_add_fractions(root)
  68. self.assertEqualPos(possibilities,
  69. [P(root, add_nominators, (Scope(root), n0, n1))])
  70. (((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + l3 / l4 + c
  71. possibilities = match_add_fractions(root)
  72. self.assertEqualPos(possibilities,
  73. [P(root, add_nominators, (Scope(root), n1, n3))])
  74. def test_match_add_fractions_constant_to_fraction(self):
  75. l23, l1 = root = tree('2 / 3 + 1')
  76. self.assertEqualPos(match_add_fractions(root),
  77. [P(root, constant_to_fraction, (Scope(root), l23, l1))])
  78. def test_add_fractions_with_negation(self):
  79. a, b, c, l1, l2, l3, l4 = tree('a,b,c,1,2,3,4')
  80. (((n0, n1), n2), n3), n4 = root = a + l2 / l2 + b + (-l3 / l4) + c
  81. self.assertEqualPos(match_add_fractions(root),
  82. [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
  83. P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
  84. n0, n1 = root = l1 / l2 + l4 / l3
  85. self.assertEqualPos(match_add_fractions(root),
  86. [P(root, equalize_denominators, (Scope(root), n0, n1, 6))])
  87. (((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + (-l3 / l4) + c
  88. self.assertEqualPos(match_add_fractions(root),
  89. [P(root, add_nominators, (Scope(root), n1, n3))])
  90. def test_equalize_denominators(self):
  91. a, b, l1, l2, l3, l4 = tree('a,b,1,2,3,4')
  92. n0, n1 = root = l1 / l2 + l3 / l4
  93. self.assertEqualNodes(equalize_denominators(root,
  94. (Scope(root), n0, n1, 4)), l2 / l4 + l3 / l4)
  95. n0, n1 = root = a / l2 + b / l4
  96. self.assertEqualNodes(equalize_denominators(root,
  97. (Scope(root), n0, n1, 4)), (l2 * a) / l4 + b /
  98. l4)
  99. #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
  100. n0, n1 = root = l1 / l2 + (-l3 / l4)
  101. self.assertEqualNodes(equalize_denominators(root,
  102. (Scope(root), n0, n1, 4)), l2 / l4 + (-l3 / l4))
  103. #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
  104. n0, n1 = root = a / l2 + (-b / l4)
  105. self.assertEqualNodes(equalize_denominators(root,
  106. (Scope(root), n0, n1, 4)), (l2 * a) / l4 + (-b / l4))
  107. def test_add_nominators(self):
  108. a, b, c = tree('a,b,c')
  109. n0, n1 = root = a / b + c / b
  110. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  111. (a + c) / b)
  112. n0, n1 = root = a / b + -c / b
  113. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  114. (a + -c) / b)
  115. n0, n1 = root = a / b + -(c / b)
  116. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  117. (a + -c) / b)
  118. n0, n1 = root = a / -b + c / -b
  119. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  120. (a + c) / -b)
  121. n0, n1 = root = a / -b + -c / -b
  122. self.assertEqualNodes(add_nominators(root, (Scope(root), n0, n1)),
  123. (a + -c) / -b)
  124. def test_constant_to_fraction(self):
  125. root, e = tree('2 / 3 + 1, 2 / 3 + (3 * 1) / 3')
  126. l23, l1 = root
  127. self.assertEqual(constant_to_fraction(root, (Scope(root), l23, l1)), e)
  128. def test_match_multiply_fractions(self):
  129. (a, b), (c, d) = ab, cd = root = tree('a / b * (c / d)')
  130. self.assertEqualPos(match_multiply_fractions(root),
  131. [P(root, multiply_fractions, (Scope(root), ab, cd))])
  132. (ab, e), cd = root = tree('4 / b * 2 * (3 / d)')
  133. self.assertEqualPos(match_multiply_fractions(root),
  134. [P(root, multiply_fractions, (Scope(root), ab, cd)),
  135. P(root, multiply_with_fraction, (Scope(root), ab, e)),
  136. P(root, multiply_with_fraction, (Scope(root), cd, e))])
  137. ab, c = root = tree('1 / sqrt(3) * 2')
  138. self.assertEqualPos(match_multiply_fractions(root),
  139. [P(root, multiply_with_fraction, (Scope(root), ab, c))])
  140. def test_multiply_fractions(self):
  141. (a, b), (c, d) = ab, cd = root = tree('a / b * (c / d)')
  142. self.assertEqual(multiply_fractions(root, (Scope(root), ab, cd)),
  143. a * c / (b * d))
  144. (ab, e), cd = root = tree('a / b * e * (c / d)')
  145. self.assertEqual(multiply_fractions(root, (Scope(root), ab, cd)),
  146. a * c / (b * d) * e)
  147. def test_match_divide_fractions(self):
  148. (a, b), c = root = tree('a / b / c')
  149. self.assertEqualPos(match_divide_fractions(root),
  150. [P(root, divide_fraction, (a, b, c))])
  151. root = tree('a / (b / c)')
  152. self.assertEqualPos(match_divide_fractions(root),
  153. [P(root, divide_by_fraction, (a, b, c))])
  154. def test_divide_fraction(self):
  155. (a, b), c = root = tree('a / b / c')
  156. self.assertEqual(divide_fraction(root, (a, b, c)), a / (b * c))
  157. (a, b), c = root = tree('-a / b / c')
  158. self.assertEqual(divide_fraction(root, (a, b, c)), -(a / (b * c)))
  159. root = tree('a / b / -c')
  160. self.assertEqual(divide_fraction(root, (a, b, c)), a / (b * -c))
  161. def test_divide_by_fraction(self):
  162. a, (b, c) = root = tree('a / (b / c)')
  163. self.assertEqual(divide_by_fraction(root, (a, b, c)), a * c / b)
  164. a, (b, c) = root = tree('-a / (b / c)')
  165. self.assertEqual(divide_by_fraction(root, (a, b, c)), -(a * c / b))
  166. root = tree('a / -(b / c)')
  167. self.assertEqual(divide_by_fraction(root, (a, b, c)), -(a * c / b))
  168. def test_match_extract_fraction_terms(self):
  169. root, a, b, c = tree('(ab) / (ca), a, b, c')
  170. n, d = root
  171. self.assertEqualPos(match_extract_fraction_terms(root),
  172. [P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a))])
  173. lscp = lambda l: Scope(N(OP_MUL, l))
  174. n, d = root = tree('(ab) / a')
  175. self.assertEqualPos(match_extract_fraction_terms(root),
  176. [P(root, divide_fraction_by_term, (Scope(n), lscp(d), a, a))])
  177. n, d = root = tree('a / (ab)')
  178. self.assertEqualPos(match_extract_fraction_terms(root),
  179. [P(root, divide_fraction_by_term, (lscp(n), Scope(d), a, a))])
  180. n, d = root = tree('(abc) / (cba)')
  181. self.assertEqualPos(match_extract_fraction_terms(root),
  182. [P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a)),
  183. P(root, divide_fraction_by_term, (Scope(n), Scope(d), b, b)),
  184. P(root, divide_fraction_by_term, (Scope(n), Scope(d), c, c))])
  185. root = tree('a / a')
  186. self.assertEqualPos(match_extract_fraction_terms(root), [])
  187. (ap, b), aq = n, d = root = tree('(a ^ p * b) / a ^ q')
  188. self.assertEqualPos(match_extract_fraction_terms(root),
  189. [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, aq))])
  190. (a, b), aq = n, d = root = tree('(ab) / a ^ q')
  191. self.assertEqualPos(match_extract_fraction_terms(root),
  192. [P(root, extract_fraction_terms, (Scope(n), lscp(d), a, aq))])
  193. (ap, b), a = n, d = root = tree('(a ^ p * b) / a')
  194. self.assertEqualPos(match_extract_fraction_terms(root),
  195. [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, a))])
  196. (l2, a), l3 = n, d = root = tree('(2a) / 3')
  197. self.assertEqualPos(match_extract_fraction_terms(root),
  198. [P(root, extract_nominator_term, (2, a))])
  199. a, l3 = n, d = root = tree('a / 3')
  200. self.assertEqualPos(match_extract_fraction_terms(root),
  201. [P(root, extract_nominator_term, (1, a))])
  202. root = tree('(2 * 4) / 3')
  203. self.assertEqualPos(match_extract_fraction_terms(root), [])
  204. n, d = root = tree('(2a) / 2')
  205. self.assertEqualPos(match_extract_fraction_terms(root),
  206. [P(root, extract_nominator_term, (2, a)),
  207. P(root, divide_fraction_by_term, (Scope(n), lscp(d), 2, 2))])
  208. def test_extract_nominator_term(self):
  209. root, expect = tree('(2a) / 3, 2 / 3 * a')
  210. l2, a = root[0]
  211. self.assertEqual(extract_nominator_term(root, (l2, a)), expect)
  212. root, expect, l1 = tree('a / 3, 1 / 3 * a, 1')
  213. self.assertEqual(extract_nominator_term(root, (l1, root[0])), expect)
  214. def test_extract_fraction_terms_basic(self):
  215. root, expect = tree('(ab) / (ca), a / a * b / c')
  216. n, d = root
  217. self.assertEqual(extract_fraction_terms(root,
  218. (Scope(n), Scope(d), n[0], d[1])), expect)
  219. def test_extract_fraction_terms_leaf(self):
  220. root, expect = tree('(ba) / a, a / a * b / 1')
  221. n, d = root
  222. self.assertEqual(extract_fraction_terms(root,
  223. (Scope(n), Scope(N(OP_MUL, d)), n[1], d)), expect)
  224. root, expect = tree('a / (ab), a / a * 1 / b')
  225. n, d = root
  226. self.assertEqual(extract_fraction_terms(root,
  227. (Scope(N(OP_MUL, n)), Scope(d), n, d[0])), expect)
  228. def test_extract_fraction_terms_chain(self):
  229. self.assertRewrite([
  230. '(a ^ 3 * 4) / (a ^ 2 * 5)',
  231. 'a ^ 3 / a ^ 2 * 4 / 5',
  232. 'a ^ (3 - 2)4 / 5',
  233. 'a ^ 1 * 4 / 5',
  234. 'a * 4 / 5',
  235. # FIXME: '4 / 5 * a',
  236. ])
  237. def test_divide_fraction_by_term(self):
  238. (ab, a), expect = root = tree('(ab) / a, b')
  239. args = Scope(ab), Scope(N(OP_MUL, a)), ab[0], a
  240. self.assertEqual(divide_fraction_by_term(root, args), expect)
  241. def test_match_division_in_denominator(self):
  242. a, ((b, c), d) = root = tree('a / (b / c + d)')
  243. self.assertEqualPos(match_division_in_denominator(root),
  244. [P(root, multiply_with_term, (c,))])
  245. a, ((d, (b, c)), e) = root = tree('a / (d + b / c + e)')
  246. self.assertEqualPos(match_division_in_denominator(root),
  247. [P(root, multiply_with_term, (c,))])
  248. def test_multiply_with_term_chain(self):
  249. self.assertRewrite([
  250. '1 / (1 / b - 1 / a)',
  251. '(b * 1) / (b(1 / b - 1 / a))',
  252. 'b / (b(1 / b - 1 / a))',
  253. 'b / (b * 1 / b + b(-1 / a))',
  254. 'b / ((b * 1) / b + b(-1 / a))',
  255. 'b / (b / b + b(-1 / a))',
  256. 'b / (1 + b(-1 / a))',
  257. 'b / (1 - b * 1 / a)',
  258. 'b / (1 - (b * 1) / a)',
  259. 'b / (1 - b / a)',
  260. '(ab) / (a(1 - b / a))',
  261. '(ab) / (a * 1 + a(-b / a))',
  262. '(ab) / (a + a(-b / a))',
  263. '(ab) / (a - ab / a)',
  264. '(ab) / (a - (ab) / a)',
  265. '(ab) / (a - b)',
  266. ])