test_rules_sqrt.py 4.0 KB

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