test_rules_poly.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from src.rules.poly import match_combine_polynomes, combine_polynomes
  2. from src.rules.numerics import add_numerics
  3. from src.node import Scope
  4. from src.possibilities import Possibility as P
  5. from tests.rulestestcase import RulesTestCase, tree
  6. class TestRulesPoly(RulesTestCase):
  7. def test_identifiers_basic(self):
  8. a1, a2 = root = tree('a+a')
  9. possibilities = match_combine_polynomes(root)
  10. self.assertEqualPos(possibilities,
  11. [P(root, combine_polynomes, (Scope(root), a1, a2,
  12. 1, 1, 'a', 1))])
  13. def test_identifiers_normal(self):
  14. a1, a2 = root = tree('a+2a')
  15. possibilities = match_combine_polynomes(root)
  16. self.assertEqualPos(possibilities,
  17. [P(root, combine_polynomes, (Scope(root), a1, a2,
  18. 1, 2, 'a', 1))])
  19. def test_identifiers_reverse(self):
  20. a1, a2 = root = tree('a+a*2')
  21. possibilities = match_combine_polynomes(root)
  22. self.assertEqualPos(possibilities,
  23. [P(root, combine_polynomes, (Scope(root), a1, a2,
  24. 1, 2, a1, 1))])
  25. def test_identifiers_exponent(self):
  26. a1, a2 = root = tree('a2+a2')
  27. possibilities = match_combine_polynomes(root)
  28. self.assertEqualPos(possibilities,
  29. [P(root, combine_polynomes, (Scope(root), a1, a2,
  30. 1, 1, 'a', 2))])
  31. def test_identifiers_coeff_exponent_left(self):
  32. a1, a2 = root = tree('2a3+a3')
  33. possibilities = match_combine_polynomes(root)
  34. self.assertEqualPos(possibilities,
  35. [P(root, combine_polynomes, (Scope(root), a1, a2,
  36. 2, 1, 'a', 3))])
  37. def test_identifiers_coeff_exponent_both(self):
  38. a1, a2 = root = tree('2a3+2a3')
  39. possibilities = match_combine_polynomes(root)
  40. self.assertEqualPos(possibilities,
  41. [P(root, combine_polynomes, (Scope(root), a1, a2,
  42. 2, 2, 'a', 3))])
  43. def test_basic_subexpressions(self):
  44. a_b, c, d = tree('a+b,c,d')
  45. left, right = root = tree('(a+b)^d + (a+b)^d')
  46. self.assertEqual(left, right)
  47. possibilities = match_combine_polynomes(root)
  48. self.assertEqualPos(possibilities,
  49. [P(root, combine_polynomes, (Scope(root), left, right,
  50. 1, 1, a_b, d))])
  51. left, right = root = tree('5(a+b)^d + 7(a+b)^d')
  52. possibilities = match_combine_polynomes(root)
  53. self.assertEqualPos(possibilities,
  54. [P(root, combine_polynomes, (Scope(root), left, right,
  55. 5, 7, a_b, d))])
  56. # TODO: Move to other strategy
  57. #left, right = root = tree('c(a+b)^d + c(a+b)^d')
  58. #self.assertEqual(left, right)
  59. #possibilities = match_combine_polynomes(root)
  60. #self.assertEqualPos(possibilities,
  61. # [P(root, combine_polynomes, (Scope(root), left, right,
  62. # c, c, a_b, d))])
  63. def test_match_add_numerics(self):
  64. l0, l1, l2 = tree('0,1,2')
  65. root = l0 + l1 + l2
  66. possibilities = match_combine_polynomes(root)
  67. self.assertEqualPos(possibilities,
  68. [P(root, add_numerics, (Scope(root), l0, l1, l0, l1)),
  69. P(root, add_numerics, (Scope(root), l0, l2, l0, l2)),
  70. P(root, add_numerics, (Scope(root), l1, l2, l1, l2))])
  71. def test_match_add_numerics_explicit_powers(self):
  72. l0, l1, l2 = tree('0^1,1*1,1*2^1')
  73. root = l0 + l1 + l2
  74. possibilities = match_combine_polynomes(root)
  75. self.assertEqualPos(possibilities,
  76. [P(root, add_numerics, (Scope(root), l0, l1, l0[0], l1[1])),
  77. P(root, add_numerics, (Scope(root), l0, l2, l0[0], l2[1][0])),
  78. P(root, add_numerics, (Scope(root), l1, l2,
  79. l1[1], l2[1][0]))])
  80. def test_combine_polynomes(self):
  81. # 2a + 3a -> (2 + 3) * a
  82. l0, a, l1, l2 = tree('2,a,3,1')
  83. root = l0 * a + l1 * a
  84. left, right = root
  85. replacement = combine_polynomes(root, (Scope(root), left, right,
  86. l0, l1, a, 1))
  87. self.assertEqualNodes(replacement, (l0 + l1) * a)
  88. # a + 3a -> (1 + 3) * a
  89. root = a + l1 * a
  90. left, right = root
  91. replacement = combine_polynomes(root, (Scope(root), left, right,
  92. l2, l1, a, 1))
  93. self.assertEqualNodes(replacement, (l2 + l1) * a)
  94. # 2a + a -> (2 + 1) * a
  95. root = l0 * a + a
  96. left, right = root
  97. replacement = combine_polynomes(root, (Scope(root), left, right,
  98. l0, l2, a, 1))
  99. self.assertEqualNodes(replacement, (l0 + 1) * a)
  100. # a + a -> (1 + 1) * a
  101. root = a + a
  102. left, right = root
  103. replacement = combine_polynomes(root, (Scope(root), left, right, l2, l2, a, 1))
  104. self.assertEqualNodes(replacement, (l2 + 1) * a)