test_rules_powers.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. from src.rules.powers import match_add_exponents, add_exponents, \
  2. match_subtract_exponents, subtract_exponents, \
  3. match_multiply_exponents, multiply_exponents, \
  4. match_duplicate_exponent, duplicate_exponent, \
  5. match_remove_negative_exponent, remove_negative_exponent, \
  6. match_exponent_to_root, exponent_to_root, \
  7. match_constant_exponent, remove_power_of_zero, remove_power_of_one
  8. from src.node import Scope, ExpressionNode as N
  9. from src.possibilities import Possibility as P
  10. from tests.rulestestcase import RulesTestCase, tree
  11. class TestRulesPowers(RulesTestCase):
  12. def test_match_add_exponents_binary(self):
  13. a, p, q = tree('a,p,q')
  14. n0, n1 = root = a ** p * a ** q
  15. possibilities = match_add_exponents(root)
  16. self.assertEqualPos(possibilities,
  17. [P(root, add_exponents, (Scope(root), n0, n1, a, p, q))])
  18. def test_match_add_exponents_ternary(self):
  19. a, p, q, r = tree('a,p,q,r')
  20. (n0, n1), n2 = root = a ** p * a ** q * a ** r
  21. possibilities = match_add_exponents(root)
  22. self.assertEqualPos(possibilities,
  23. [P(root, add_exponents, (Scope(root), n0, n1, a, p, q)),
  24. P(root, add_exponents, (Scope(root), n0, n2, a, p, r)),
  25. P(root, add_exponents, (Scope(root), n1, n2, a, q, r))])
  26. def test_match_add_exponents_multiple_identifiers(self):
  27. a, b, p, q = tree('a,b,p,q')
  28. ((a0, b0), a1), b1 = root = a ** p * b ** p * a ** q * b ** q
  29. possibilities = match_add_exponents(root)
  30. self.assertEqualPos(possibilities,
  31. [P(root, add_exponents, (Scope(root), a0, a1, a, p, q)),
  32. P(root, add_exponents, (Scope(root), b0, b1, b, p, q))])
  33. def test_match_add_exponents_nary_multiplication(self):
  34. a, p, q = tree('a,p,q')
  35. (n0, l1), n1 = root = a ** p * 2 * a ** q
  36. possibilities = match_add_exponents(root)
  37. self.assertEqualPos(possibilities,
  38. [P(root, add_exponents, (Scope(root), n0, n1, a, p, q))])
  39. def test_match_add_exponents_negated(self):
  40. a, q = tree('a,q')
  41. n0, n1 = root = (-a) * a ** q
  42. possibilities = match_add_exponents(root)
  43. self.assertEqualPos(possibilities,
  44. [P(root, add_exponents, (Scope(root), n0, n1, a, 1, q))])
  45. def test_match_subtract_exponents_powers(self):
  46. a, p, q = tree('a,p,q')
  47. root = a ** p / a ** q
  48. possibilities = match_subtract_exponents(root)
  49. self.assertEqualPos(possibilities,
  50. [P(root, subtract_exponents, (a, p, q))])
  51. def test_match_subtract_power_id(self):
  52. a, p = tree('a,p')
  53. root = a ** p / a
  54. possibilities = match_subtract_exponents(root)
  55. self.assertEqualPos(possibilities,
  56. [P(root, subtract_exponents, (a, p, 1))])
  57. def test_match_subtract_id_power(self):
  58. a, q = tree('a,q')
  59. root = a / a ** q
  60. possibilities = match_subtract_exponents(root)
  61. self.assertEqualPos(possibilities,
  62. [P(root, subtract_exponents, (a, 1, q))])
  63. def test_match_multiply_exponents(self):
  64. a, p, q = tree('a,p,q')
  65. root = (a ** p) ** q
  66. possibilities = match_multiply_exponents(root)
  67. self.assertEqualPos(possibilities,
  68. [P(root, multiply_exponents, (a, p, q))])
  69. def test_match_duplicate_exponent(self):
  70. a, b, p = tree('a,b,p')
  71. root = (a * b) ** p
  72. possibilities = match_duplicate_exponent(root)
  73. self.assertEqualPos(possibilities,
  74. [P(root, duplicate_exponent, ([a, b], p))])
  75. def test_match_remove_negative_exponent(self):
  76. a, p = tree('a,p')
  77. root = a ** -p
  78. possibilities = match_remove_negative_exponent(root)
  79. self.assertEqualPos(possibilities,
  80. [P(root, remove_negative_exponent, (a, -p))])
  81. def test_match_exponent_to_root(self):
  82. a, n, m, l1 = tree('a,n,m,1')
  83. root = a ** (n / m)
  84. possibilities = match_exponent_to_root(root)
  85. self.assertEqualPos(possibilities,
  86. [P(root, exponent_to_root, (a, n, m))])
  87. root = a ** (l1 / m)
  88. possibilities = match_exponent_to_root(root)
  89. self.assertEqualPos(possibilities,
  90. [P(root, exponent_to_root, (a, 1, m))])
  91. def test_add_exponents(self):
  92. a, p, q = tree('a,p,q')
  93. n0, n1 = root = a ** p * a ** q
  94. self.assertEqualNodes(add_exponents(root,
  95. (Scope(root), n0, n1, a, p, q)), a ** (p + q))
  96. def test_subtract_exponents(self):
  97. a, p, q = tree('a,p,q')
  98. root = a ** p / a ** q
  99. self.assertEqualNodes(subtract_exponents(root, (a, p, q)),
  100. a ** (p - q))
  101. def test_multiply_exponents(self):
  102. a, p, q = tree('a,p,q')
  103. root = (a ** p) ** q
  104. self.assertEqualNodes(multiply_exponents(root, (a, p, q)),
  105. a ** (p * q))
  106. def test_duplicate_exponent(self):
  107. a, b, c, p = tree('a,b,c,p')
  108. root = (a * b) ** p
  109. self.assertEqualNodes(duplicate_exponent(root, ([a, b], p)),
  110. a ** p * b ** p)
  111. root = (a * b * c) ** p
  112. self.assertEqualNodes(duplicate_exponent(root, ([a, b, c], p)),
  113. a ** p * b ** p * c ** p)
  114. def test_remove_negative_exponent(self):
  115. a, p, l1 = tree('a,-p,1')
  116. root = a ** p
  117. self.assertEqualNodes(remove_negative_exponent(root, (a, p)),
  118. l1 / a ** +p)
  119. def test_exponent_to_root(self):
  120. a, n, m, l1 = tree('a,n,m,1')
  121. root = a ** (n / m)
  122. self.assertEqualNodes(exponent_to_root(root, (a, n, m)),
  123. N('sqrt', a ** n, m))
  124. self.assertEqualNodes(exponent_to_root(root, (a, l1, m)),
  125. N('sqrt', a, m))
  126. def test_match_constant_exponent(self):
  127. a0, a1, a2 = tree('a0,a1,a2')
  128. self.assertEqualPos(match_constant_exponent(a0),
  129. [P(a0, remove_power_of_zero, ())])
  130. self.assertEqualPos(match_constant_exponent(a1),
  131. [P(a1, remove_power_of_one, ())])
  132. self.assertEqualPos(match_constant_exponent(a2), [])
  133. def test_remove_power_of_zero(self):
  134. self.assertEqual(remove_power_of_zero(tree('a0'), ()), 1)
  135. def test_remove_power_of_one(self):
  136. a1 = tree('a1')
  137. self.assertEqual(remove_power_of_one(a1, ()), a1[0])