test_leiden_oefenopgave.py 6.8 KB

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