test_rules_numerics.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.numerics import match_add_numerics, add_numerics, \
  16. match_divide_numerics, divide_numerics, reduce_fraction_constants, \
  17. match_multiply_numerics, multiply_numerics, multiply_zero, \
  18. multiply_one, raise_numerics
  19. from src.node import ExpressionLeaf as L, Scope
  20. from src.possibilities import Possibility as P
  21. from tests.rulestestcase import RulesTestCase, tree
  22. class TestRulesNumerics(RulesTestCase):
  23. def test_match_add_numerics(self):
  24. l1, l2 = root = tree('1 + 2')
  25. possibilities = match_add_numerics(root)
  26. self.assertEqualPos(possibilities,
  27. [P(root, add_numerics, (Scope(root), l1, l2))])
  28. (l1, b), l2 = root = tree('1 + b + 2')
  29. possibilities = match_add_numerics(root)
  30. self.assertEqualPos(possibilities,
  31. [P(root, add_numerics, (Scope(root), l1, l2))])
  32. def test_add_numerics(self):
  33. l1, l2 = root = tree('1 + 2')
  34. self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), 3)
  35. (l1, a), l2 = root = tree('1 + a + 2')
  36. self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), L(3) + a)
  37. def test_add_numerics_negations(self):
  38. ml1, l2 = root = tree('-1 + 2')
  39. self.assertEqual(add_numerics(root, (Scope(root), ml1, l2)), 1)
  40. l1, ml2 = root = tree('1 - 2')
  41. self.assertEqual(add_numerics(root, (Scope(root), l1, ml2)), -1)
  42. def test_match_divide_numerics(self):
  43. a, b, i2, i3, i4, i6, f1, f2, f3 = tree('a,b,2,3,4,6,1.0,2.0,3.0')
  44. root = i6 / i2
  45. possibilities = match_divide_numerics(root)
  46. self.assertEqualPos(possibilities, [P(root, divide_numerics)])
  47. root = -i6 / i2
  48. self.assertEqualPos(match_divide_numerics(root), [])
  49. root = i6 / -i2
  50. self.assertEqualPos(match_divide_numerics(root), [])
  51. root = i2 / i4
  52. self.assertEqualPos(match_divide_numerics(root),
  53. [P(root, reduce_fraction_constants, (2,))])
  54. root = f3 / i2
  55. self.assertEqualPos(match_divide_numerics(root),
  56. [P(root, divide_numerics)])
  57. root = i3 / f2
  58. self.assertEqualPos(match_divide_numerics(root),
  59. [P(root, divide_numerics)])
  60. root = f3 / f2
  61. self.assertEqualPos(match_divide_numerics(root),
  62. [P(root, divide_numerics)])
  63. root = i3 / f1
  64. self.assertEqualPos(match_divide_numerics(root),
  65. [P(root, divide_numerics)])
  66. root = a / b
  67. self.assertEqualPos(match_divide_numerics(root), [])
  68. def test_divide_numerics(self):
  69. i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
  70. self.assertEqual(divide_numerics(i6 / i2, ()), 3)
  71. self.assertEqual(divide_numerics(f3 / i2, ()), 1.5)
  72. self.assertEqual(divide_numerics(i3 / f2, ()), 1.5)
  73. self.assertEqual(divide_numerics(f3 / f2, ()), 1.5)
  74. self.assertEqual(divide_numerics(-(i6 / i2), ()), -i3)
  75. def test_reduce_fraction_constants(self):
  76. l1, l2 = tree('1,2')
  77. self.assertEqual(reduce_fraction_constants(l2 / 4, (2,)), l1 / l2)
  78. #def test_fraction_to_int_fraction(self):
  79. # l1, l4 = tree('1,4')
  80. # self.assertEqual(fraction_to_int_fraction(l4 / 3, (1, 1, 3)),
  81. # l1 + l1 / 3)
  82. def test_match_multiply_numerics(self):
  83. i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
  84. root = i3 * i2
  85. self.assertEqual(match_multiply_numerics(root),
  86. [P(root, multiply_numerics, (Scope(root), i3, i2))])
  87. root = f3 * i2
  88. self.assertEqual(match_multiply_numerics(root),
  89. [P(root, multiply_numerics, (Scope(root), f3, i2))])
  90. root = i3 * f2
  91. self.assertEqual(match_multiply_numerics(root),
  92. [P(root, multiply_numerics, (Scope(root), i3, f2))])
  93. root = f3 * f2
  94. self.assertEqual(match_multiply_numerics(root),
  95. [P(root, multiply_numerics, (Scope(root), f3, f2))])
  96. def test_match_multiply_zero(self):
  97. l0, x = root = tree('0x')
  98. self.assertEqual(match_multiply_numerics(root),
  99. [P(root, multiply_zero, (l0,))])
  100. def test_match_multiply_one(self):
  101. l1, x = root = tree('1x')
  102. self.assertEqual(match_multiply_numerics(root),
  103. [P(root, multiply_one, (Scope(root), l1))])
  104. (x, l1), x = root = tree('x * 1x')
  105. self.assertEqual(match_multiply_numerics(root),
  106. [P(root, multiply_one, (Scope(root), l1))])
  107. def test_multiply_numerics(self):
  108. a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
  109. i3, i2 = root = tree('3 * 2')
  110. self.assertEqual(multiply_numerics(root, (Scope(root), i3, i2)), 6)
  111. f3, i2 = root = tree('3.0 * 2')
  112. self.assertEqual(multiply_numerics(root, (Scope(root), f3, i2)), 6.0)
  113. i3, f2 = root = tree('3 * 2.0')
  114. self.assertEqual(multiply_numerics(root, (Scope(root), i3, f2)), 6.0)
  115. f3, f2 = root = tree('3.0 * 2.0')
  116. self.assertEqual(multiply_numerics(root, (Scope(root), f3, f2)), 6.0)
  117. ((a, i3), i2), b = root = tree('a * 3 * 2 * b')
  118. self.assertEqualNodes(multiply_numerics(root,
  119. (Scope(root), i3, i2)), a * 6 * b)
  120. def test_multiply_numerics_negation(self):
  121. l1_neg, l2 = root = tree('-1 * 2')
  122. self.assertEqualNodes(multiply_numerics(root, (Scope(root), l1_neg,
  123. l2)), -l2)
  124. root, l6 = tree('1 + -2 * 3,6')
  125. l1, mul = root
  126. l2_neg, l3 = mul
  127. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  128. l2_neg, l3)), -l6)
  129. root, l30 = tree('-5 * x ^ 2 - -15x - 5 * 6,30')
  130. rest, mul = root
  131. l5_neg, l6 = mul
  132. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  133. l5_neg, l6)), -l30)
  134. def test_raise_numerics(self):
  135. l1, l2 = root = tree('2 ^ 3')
  136. self.assertEqualNodes(raise_numerics(root, (l1, l2, 0)), L(8))
  137. l1_neg, l2 = root = tree('(-2) ^ 2')
  138. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), --L(4))
  139. l1_neg, l2 = root = tree('(-2) ^ 3')
  140. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), ---L(8))
  141. l1_neg, l2 = root = tree('-((-2) ^ 3)')
  142. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), ----L(8))
  143. l1_neg, l2 = root = tree('-(2 ^ 3)')
  144. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), -L(8))