test_leiden_oefenopgave.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. '(xx + x * 1 + 1x + 1 * 1)(x + 1)',
  42. '(xx + x + 1x + 1 * 1)(x + 1)',
  43. '(xx + x + x + 1 * 1)(x + 1)',
  44. '(xx + x + x + 1)(x + 1)',
  45. '(xx + (1 + 1)x + 1)(x + 1)',
  46. '(xx + 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 + 2xx + 2x * 1 + 1x + 1 * 1',
  50. 'x ^ 2 * x + x ^ 2 + 2xx + 2x * 1 + 1x + 1 * 1',
  51. 'x ^ 2 * x + x ^ 2 + 2xx + 2x + 1x + 1 * 1',
  52. 'x ^ 2 * x + x ^ 2 + 2xx + 2x + x + 1 * 1',
  53. 'x ^ 2 * x + x ^ 2 + 2xx + 2x + x + 1',
  54. 'x ^ 2 * x + x ^ 2 + 2xx + (2 + 1)x + 1',
  55. 'x ^ 2 * x + x ^ 2 + 2xx + 3x + 1',
  56. 'x ^ (2 + 1) + x ^ 2 + 2xx + 3x + 1',
  57. 'x ^ 3 + x ^ 2 + 2xx + 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. # (x+1)^2 -> x^2 + 2x + 1
  65. self.assertRewrite([
  66. '(x + 1) ^ 2',
  67. '(x + 1)(x + 1)',
  68. 'xx + x * 1 + 1x + 1 * 1',
  69. 'xx + x + 1x + 1 * 1',
  70. 'xx + x + x + 1 * 1',
  71. 'xx + x + x + 1',
  72. 'xx + (1 + 1)x + 1',
  73. 'xx + 2x + 1',
  74. 'x ^ (1 + 1) + 2x + 1',
  75. 'x ^ 2 + 2x + 1',
  76. ])
  77. def test_1_4(self):
  78. # (x-1)^2 -> x^2 - 2x + 1
  79. self.assertRewrite([
  80. '(x - 1) ^ 2',
  81. '(x - 1)(x - 1)',
  82. 'xx + x * -1 + (-1)x + (-1) * -1',
  83. 'xx + x * -1 + (-1)x - -1',
  84. 'xx + x * -1 + (-1)x + 1',
  85. 'xx - x * 1 + (-1)x + 1',
  86. 'xx - x + (-1)x + 1',
  87. 'xx - x - 1x + 1',
  88. 'xx - x - x + 1',
  89. 'xx + (1 + 1) * -x + 1',
  90. 'xx + 2 * -x + 1',
  91. 'xx - 2x + 1',
  92. 'x ^ (1 + 1) - 2x + 1',
  93. 'x ^ 2 - 2x + 1',
  94. ])
  95. def test_1_4_1(self):
  96. self.assertRewrite([
  97. 'x * -1 + 1x',
  98. 'x * -1 + x',
  99. '-x * 1 + x',
  100. '-x + x',
  101. '(-1 + 1)x',
  102. '0x',
  103. '0',
  104. ])
  105. def test_1_4_2(self):
  106. self.assertRewrite([
  107. 'x * -1 - 1x',
  108. 'x * -1 - x',
  109. '-x * 1 - x',
  110. '-x - x',
  111. '(1 + 1) * -x',
  112. '2 * -x',
  113. '-2x',
  114. ])
  115. def test_1_4_3(self):
  116. self.assertRewrite([
  117. 'x * -1 + x * -1',
  118. '-x * 1 + x * -1',
  119. '-x + x * -1',
  120. '-x - x * 1',
  121. '-x - x',
  122. '(1 + 1) * -x',
  123. '2 * -x',
  124. '-2x',
  125. ])
  126. def test_1_5(self):
  127. self.assertRewrite([
  128. '(2x + x)x',
  129. '(2 + 1)xx',
  130. '3xx',
  131. '3x ^ (1 + 1)',
  132. '3x ^ 2',
  133. ])
  134. def test_1_7(self):
  135. self.assertRewrite([
  136. '(4x + 5) * -(5 - 4x)',
  137. '-(4x + 5)(5 - 4x)',
  138. '-(4x * 5 + 4x * -4x + 5 * 5 + 5 * -4x)',
  139. '-(20x + 4x * -4x + 5 * 5 + 5 * -4x)',
  140. '-(20x + (-16)xx + 5 * 5 + 5 * -4x)',
  141. '-(20x + (-16)xx + 25 + 5 * -4x)',
  142. '-(20x + (-16)xx + 25 + (-20)x)',
  143. '-(20x - 16xx + 25 + (-20)x)',
  144. '-(20x - 16xx + 25 - 20x)',
  145. '-((1 - 1)20x - 16xx + 25)',
  146. '-(0 * 20x - 16xx + 25)',
  147. '-(0 - 16xx + 25)',
  148. '-(-16xx + 25)',
  149. '-(-16x ^ (1 + 1) + 25)',
  150. '-(-16x ^ 2 + 25)',
  151. '--16x ^ 2 - 25',
  152. '16x ^ 2 - 25',
  153. ])
  154. def test_2(self):
  155. pass
  156. def test_3(self):
  157. pass
  158. def test_4_1(self):
  159. self.assertRewrite([
  160. '2/15 + 1/4',
  161. '8 / 60 + 15 / 60',
  162. '(8 + 15) / 60',
  163. '23 / 60',
  164. ])
  165. def test_4_2(self):
  166. self.assertRewrite([
  167. '2 / 7 - 4 / 11',
  168. '22 / 77 - 28 / 77',
  169. '(22 - 28) / 77',
  170. '(-6) / 77',
  171. '-6 / 77',
  172. ])
  173. def test_4_3(self):
  174. self.assertRewrite([
  175. '(7/3)(3/5)',
  176. '(7 * 3) / (3 * 5)',
  177. '7 / 5',
  178. ])
  179. def test_4_4(self):
  180. self.assertRewrite([
  181. '(3/4) / (5/6)',
  182. '3 / (4 * 5 / 6)',
  183. '3 / ((4 * 5) / 6)',
  184. '3 / (20 / 6)',
  185. '(3 * 6) / 20',
  186. '18 / 20',
  187. '9 / 10'])
  188. def test_4_5(self):
  189. self.assertRewrite([
  190. '1/4 * 1/x',
  191. '(1 * 1) / (4x)',
  192. '1 / (4x)',
  193. ])
  194. #def test_4_6(self):
  195. # self.assertRewrite([
  196. # '(3 / x^2) / (x / 7)',
  197. # '3 / x ^ 2 / (1 / 7 * x)',
  198. # '3 / (x ^ 2 * 1 / 7 * x)',
  199. # '3 / (x ^ (2 + 1)1 / 7)',
  200. # '3 / (x ^ 3 * 1 / 7)',
  201. # '3 / (1 / 7 * x ^ 3)',
  202. # '21 / x^3',
  203. # ])
  204. #def test_4_7(self):
  205. # self.assertRewrite([
  206. # '1 / x + 2 / (x + 1)',
  207. # '(3x + 1) / (x * (x + 1))',
  208. # ])