test_rules_sqrt.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.sqrt import is_eliminateable_sqrt, match_reduce_sqrt, \
  16. quadrant_sqrt, constant_sqrt, split_dividers, \
  17. extract_sqrt_multiplicant, extract_sqrt_mult_priority
  18. from src.node import Scope
  19. from src.possibilities import Possibility as P
  20. from tests.rulestestcase import RulesTestCase, tree
  21. class TestRulesSqrt(RulesTestCase):
  22. def test_is_eliminateable_sqrt(self):
  23. self.assertFalse(is_eliminateable_sqrt(3))
  24. self.assertTrue(is_eliminateable_sqrt(4))
  25. self.assertTrue(is_eliminateable_sqrt(9))
  26. self.assertTrue(is_eliminateable_sqrt(tree('9')))
  27. self.assertFalse(is_eliminateable_sqrt(tree('-9')))
  28. self.assertFalse(is_eliminateable_sqrt(tree('5')))
  29. self.assertTrue(is_eliminateable_sqrt(tree('a ^ 2')))
  30. self.assertFalse(is_eliminateable_sqrt(tree('a ^ 3')))
  31. self.assertFalse(is_eliminateable_sqrt(tree('a')))
  32. def test_match_reduce_sqrt_none(self):
  33. root = tree('sqrt(a)')
  34. self.assertEqualPos(match_reduce_sqrt(root), [])
  35. root = tree('sqrt(-4)')
  36. self.assertEqualPos(match_reduce_sqrt(root), [])
  37. def test_match_reduce_sqrt_quadrant(self):
  38. root = tree('sqrt(a ^ 2)')
  39. self.assertEqualPos(match_reduce_sqrt(root), [P(root, quadrant_sqrt)])
  40. def test_match_reduce_sqrt_constant(self):
  41. root = tree('sqrt(4)')
  42. self.assertEqualPos(match_reduce_sqrt(root),
  43. [P(root, constant_sqrt, (2,))])
  44. def test_match_reduce_sqrt_dividers(self):
  45. root = tree('sqrt(8)')
  46. self.assertEqualPos(match_reduce_sqrt(root),
  47. [P(root, split_dividers, (4, 2))])
  48. root = tree('sqrt(27)')
  49. self.assertEqualPos(match_reduce_sqrt(root),
  50. [P(root, split_dividers, (9, 3))])
  51. def test_match_reduce_sqrt_mult_priority(self):
  52. root = tree('sqrt(9 * 3)')
  53. self.assertEqualPos(match_reduce_sqrt(root),
  54. [P(root, extract_sqrt_mult_priority, (Scope(root[0]), 9)),
  55. P(root, extract_sqrt_multiplicant, (Scope(root[0]), 3))])
  56. def test_match_reduce_sqrt_mult(self):
  57. ((l2, x),) = root = tree('sqrt(2x)')
  58. self.assertEqualPos(match_reduce_sqrt(root),
  59. [P(root, extract_sqrt_multiplicant, (Scope(root[0]), l2)),
  60. P(root, extract_sqrt_multiplicant, (Scope(root[0]), x))])
  61. (((l2, x), y),) = root = tree('sqrt(2xy)')
  62. self.assertEqualPos(match_reduce_sqrt(root),
  63. [P(root, extract_sqrt_multiplicant, (Scope(root[0]), l2)),
  64. P(root, extract_sqrt_multiplicant, (Scope(root[0]), x)),
  65. P(root, extract_sqrt_multiplicant, (Scope(root[0]), y))])
  66. def test_quadrant_sqrt(self):
  67. root, expect = tree('sqrt(a ^ 2), a')
  68. self.assertEqual(quadrant_sqrt(root, ()), expect)
  69. root, expect = tree('-sqrt(a ^ 2), -a')
  70. self.assertEqual(quadrant_sqrt(root, ()), expect)
  71. def test_constant_sqrt(self):
  72. root = tree('sqrt(4)')
  73. self.assertEqual(constant_sqrt(root, (2,)), 2)
  74. def test_split_dividers(self):
  75. root, expect = tree('sqrt(27), sqrt(9 * 3)')
  76. self.assertEqual(split_dividers(root, (9, 3)), expect)
  77. def test_extract_sqrt_multiplicant(self):
  78. root, expect = tree('sqrt(2x), sqrt(2)sqrt(x)')
  79. l2, x = mul = root[0]
  80. self.assertEqual(extract_sqrt_multiplicant(root, (Scope(mul), l2,)),
  81. expect)
  82. root, expect = tree('-sqrt(2x), -sqrt(2)sqrt(x)')
  83. l2, x = mul = root[0]
  84. self.assertEqual(extract_sqrt_multiplicant(root, (Scope(mul), l2,)),
  85. expect)
  86. root, expect = tree('sqrt(2xy), sqrt(x)sqrt(2y)')
  87. (l2, x), y = mul = root[0]
  88. self.assertEqual(extract_sqrt_multiplicant(root, (Scope(mul), x,)),
  89. expect)
  90. def test_extract_sqrt_mult_priority(self):
  91. root, expect = tree('sqrt(9 * 3), sqrt(9)sqrt(3)')
  92. l9, l3 = mul = root[0]
  93. self.assertEqual(extract_sqrt_mult_priority(root, (Scope(mul), l9,)),
  94. expect)