test_leiden_oefenopgave.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 tests.rulestestcase import RulesTestCase as TestCase, rewrite
  16. class TestLeidenOefenopgave(TestCase):
  17. def test_1_1(self):
  18. self.assertRewrite([
  19. '-5(x ^ 2 - 3x + 6)',
  20. '-(5x ^ 2 + 5 * -3x + 5 * 6)',
  21. '-(5x ^ 2 + (-15)x + 5 * 6)',
  22. '-(5x ^ 2 + (-15)x + 30)',
  23. '-(5x ^ 2 - 15x + 30)',
  24. '-5x ^ 2 - -15x - 30',
  25. '-5x ^ 2 + 15x - 30',
  26. ])
  27. #for exp, solution in [
  28. # ('-5(x^2 - 3x + 6)', '-30 + 15x - 5x ^ 2'),
  29. # ('(x+1)^2', 'x ^ 2 + 2x + 1'),
  30. # ('(x-1)^2', 'x ^ 2 - 2x + 1'),
  31. # ('(2x+x)*x', '3x ^ 2'),
  32. # ('-2(6x-4)^2*x', '-72x ^ 3 + 96x ^ 2 + 32x'),
  33. # ('(4x + 5) * -(5 - 4x)', '16x^2 - 25'),
  34. # ]:
  35. # self.assertEqual(str(rewrite(exp)), solution)
  36. def test_1_2(self):
  37. self.assertRewrite([
  38. '(x + 1) ^ 3',
  39. '(x + 1)(x + 1) ^ 2',
  40. '(x + 1)(x + 1)(x + 1)',
  41. '(x * x + x * 1 + 1x + 1 * 1)(x + 1)',
  42. '(x * x + x + 1x + 1 * 1)(x + 1)',
  43. '(x * x + x + x + 1 * 1)(x + 1)',
  44. '(x * x + x + x + 1)(x + 1)',
  45. '(x * x + (1 + 1)x + 1)(x + 1)',
  46. '(x * x + 2x + 1)(x + 1)',
  47. '(x ^ (1 + 1) + 2x + 1)(x + 1)',
  48. '(x ^ 2 + 2x + 1)(x + 1)',
  49. 'x ^ 2 * x + x ^ 2 * 1 + 2x * x + 2x * 1 + 1x + 1 * 1',
  50. 'x ^ 2 * x + x ^ 2 + 2x * x + 2x * 1 + 1x + 1 * 1',
  51. 'x ^ 2 * x + x ^ 2 + 2x * x + 2x + 1x + 1 * 1',
  52. 'x ^ 2 * x + x ^ 2 + 2x * x + 2x + x + 1 * 1',
  53. 'x ^ 2 * x + x ^ 2 + 2x * x + 2x + x + 1',
  54. 'x ^ 2 * x + x ^ 2 + 2x * x + (2 + 1)x + 1',
  55. 'x ^ 2 * x + x ^ 2 + 2x * x + 3x + 1',
  56. 'x ^ (2 + 1) + x ^ 2 + 2x * x + 3x + 1',
  57. 'x ^ 3 + x ^ 2 + 2x * x + 3x + 1',
  58. 'x ^ 3 + x ^ 2 + 2x ^ (1 + 1) + 3x + 1',
  59. 'x ^ 3 + x ^ 2 + 2x ^ 2 + 3x + 1',
  60. 'x ^ 3 + (1 + 2)x ^ 2 + 3x + 1',
  61. 'x ^ 3 + 3x ^ 2 + 3x + 1',
  62. ])
  63. def test_1_3(self):
  64. self.assertRewrite([
  65. '(x + 1) ^ 2',
  66. '(x + 1)(x + 1)',
  67. 'x * x + x * 1 + 1x + 1 * 1',
  68. 'x * x + x + 1x + 1 * 1',
  69. 'x * x + x + x + 1 * 1',
  70. 'x * x + x + x + 1',
  71. 'x * x + (1 + 1)x + 1',
  72. 'x * x + 2x + 1',
  73. 'x ^ (1 + 1) + 2x + 1',
  74. 'x ^ 2 + 2x + 1',
  75. ])
  76. def test_1_4(self):
  77. self.assertRewrite([
  78. '(x - 1) ^ 2',
  79. '(x - 1)(x - 1)',
  80. 'x * x + x * -1 + (-1)x + (-1) * -1',
  81. 'x * x + x * -1 + (-1)x - -1',
  82. 'x * x + x * -1 + (-1)x + 1',
  83. 'x * x - x * 1 + (-1)x + 1',
  84. 'x * x - x + (-1)x + 1',
  85. 'x * x - x - 1x + 1',
  86. 'x * x - x - x + 1',
  87. 'x * x + (1 + 1) * -x + 1',
  88. 'x * x + 2 * -x + 1',
  89. 'x * x - 2x + 1',
  90. 'x ^ (1 + 1) - 2x + 1',
  91. 'x ^ 2 - 2x + 1',
  92. ])
  93. def test_1_4_1(self):
  94. self.assertRewrite([
  95. 'x * -1 + 1x',
  96. 'x * -1 + x',
  97. '-x * 1 + x',
  98. '-x + x',
  99. '(-1 + 1)x',
  100. '0x',
  101. '0',
  102. ])
  103. def test_1_4_2(self):
  104. self.assertRewrite([
  105. 'x * -1 - 1x',
  106. 'x * -1 - x',
  107. '-x * 1 - x',
  108. '-x - x',
  109. '(1 + 1) * -x',
  110. '2 * -x',
  111. '-2x',
  112. ])
  113. def test_1_4_3(self):
  114. self.assertRewrite([
  115. 'x * -1 + x * -1',
  116. '-x * 1 + x * -1',
  117. '-x + x * -1',
  118. '-x - x * 1',
  119. '-x - x',
  120. '(1 + 1) * -x',
  121. '2 * -x',
  122. '-2x',
  123. ])
  124. def test_1_5(self):
  125. self.assertRewrite([
  126. '(2x + x)x',
  127. '(2 + 1)x * x',
  128. '3x * x',
  129. '3x ^ (1 + 1)',
  130. '3x ^ 2',
  131. ])
  132. def test_1_7(self):
  133. self.assertRewrite([
  134. '(4x + 5) * -(5 - 4x)',
  135. '-(4x + 5)(5 - 4x)',
  136. '-(4x * 5 + 4x * -4x + 5 * 5 + 5 * -4x)',
  137. '-(20x + 4x * -4x + 5 * 5 + 5 * -4x)',
  138. '-(20x + (-16)x * x + 5 * 5 + 5 * -4x)',
  139. '-(20x + (-16)x * x + 25 + 5 * -4x)',
  140. '-(20x + (-16)x * x + 25 + (-20)x)',
  141. '-(20x - 16x * x + 25 + (-20)x)',
  142. '-(20x - 16x * x + 25 - 20x)',
  143. '-((1 - 1)20x - 16x * x + 25)',
  144. '-(0 * 20x - 16x * x + 25)',
  145. '-(0 - 16x * x + 25)',
  146. '-(-16x * x + 25)',
  147. '-(-16x ^ (1 + 1) + 25)',
  148. '-(-16x ^ 2 + 25)',
  149. '--16x ^ 2 - 25',
  150. '16x ^ 2 - 25',
  151. ])
  152. def test_2(self):
  153. pass
  154. def test_3(self):
  155. pass
  156. def test_4_1(self):
  157. self.assertRewrite([
  158. '2/15 + 1/4',
  159. '8 / 60 + 15 / 60',
  160. '(8 + 15) / 60',
  161. '23 / 60',
  162. ])
  163. def test_4_2(self):
  164. self.assertRewrite([
  165. '2 / 7 - 4 / 11',
  166. '22 / 77 - 28 / 77',
  167. '(22 - 28) / 77',
  168. '(-6) / 77',
  169. '-6 / 77',
  170. ])
  171. def test_4_3(self):
  172. self.assertRewrite([
  173. '(7/3)(3/5)',
  174. '(7 * 3) / (3 * 5)',
  175. '7 / 5',
  176. ])
  177. def test_4_4(self):
  178. self.assertRewrite([
  179. '(3/4) / (5/6)',
  180. '3 / (4 * 5 / 6)',
  181. '3 / ((4 * 5) / 6)',
  182. '3 / (20 / 6)',
  183. '(3 * 6) / 20',
  184. '18 / 20',
  185. '9 / 10'])
  186. def test_4_5(self):
  187. self.assertRewrite([
  188. '1/4 * 1/x',
  189. '(1 * 1) / (4x)',
  190. '1 / (4x)',
  191. ])
  192. def test_4_6(self):
  193. self.assertRewrite([
  194. '(3 / x^2) / (x / 7)',
  195. '3 / x ^ 2 / (1 / 7 * x)',
  196. '3 / (x ^ 2 * 1 / 7x)',
  197. '3 / (x ^ (2 + 1)1 / 7)',
  198. '3 / (x ^ 3 * 1 / 7)',
  199. '(7 * 3) / x ^ 3',
  200. '21 / x ^ 3',
  201. ])
  202. def test_4_7(self):
  203. self.assertEvaluates('1 / x + 2 / (x + 1)', '(3x + 1) / (x(x + 1))')