test_rules_numerics.py 5.6 KB

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