test_rules_goniometry.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # vim: set fileencoding=utf-8 :
  2. # This file is part of TRS (http://math.kompiler.org)
  3. #
  4. # TRS is free software: you can redistribute it and/or modify it under the
  5. # terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  10. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  16. from src.rules.goniometry import match_add_quadrants, add_quadrants, \
  17. factor_out_quadrant_negation, match_negated_parameter, \
  18. negated_sinus_parameter, is_pi_frac, negated_cosinus_parameter, \
  19. match_standard_radian, standard_radian
  20. from src.node import PI, OP_SIN, OP_COS, OP_TAN, sin, cos, tan, Scope
  21. from src.possibilities import Possibility as P
  22. from tests.rulestestcase import RulesTestCase, tree
  23. from src.rules import goniometry
  24. import doctest
  25. class TestRulesGoniometry(RulesTestCase):
  26. def test_doctest(self):
  27. self.assertEqual(doctest.testmod(m=goniometry)[0], 0)
  28. def test_match_add_quadrants(self):
  29. s, c = root = tree('sin^2 t + cos^2 t')
  30. self.assertEqualPos(match_add_quadrants(root),
  31. [P(root, add_quadrants, (Scope(root), s, c))])
  32. c, s = root = tree('cos^2 t + sin^2 t')
  33. self.assertEqualPos(match_add_quadrants(root),
  34. [P(root, add_quadrants, (Scope(root), s, c))])
  35. (s, a), c = root = tree('sin^2 t + a + cos^2 t')
  36. self.assertEqualPos(match_add_quadrants(root),
  37. [P(root, add_quadrants, (Scope(root), s, c))])
  38. (s, c0), c1 = root = tree('sin^2 t + cos^2 t + cos^2 t')
  39. self.assertEqualPos(match_add_quadrants(root),
  40. [P(root, add_quadrants, (Scope(root), s, c0)),
  41. P(root, add_quadrants, (Scope(root), s, c1))])
  42. root = tree('sin^2 t + cos^2 y')
  43. self.assertEqualPos(match_add_quadrants(root), [])
  44. root = tree('sin^2 t - cos^2 t')
  45. self.assertEqualPos(match_add_quadrants(root), [])
  46. s, c = root = tree('-sin^2 t - cos^2 t')
  47. self.assertEqualPos(match_add_quadrants(root),
  48. [P(root, factor_out_quadrant_negation, (Scope(root), s, c))])
  49. def test_add_quadrants(self):
  50. s, c = root = tree('sin(t) ^ 2 + cos(t) ^ 2')
  51. self.assertEqual(add_quadrants(root, (Scope(root), s, c)), 1)
  52. root, expect = tree('cos(t) ^ 2 + a + sin(t) ^ 2, a + 1')
  53. (c, a), s = root
  54. self.assertEqual(add_quadrants(root, (Scope(root), s, c)), expect)
  55. def test_factor_out_quadrant_negation(self):
  56. r, e = tree('-sin(t) ^ 2 - cos(t) ^ 2, -(sin(t) ^ 2 + cos(t) ^ 2)')
  57. s, c = r
  58. self.assertEqual(factor_out_quadrant_negation(r, (Scope(r), s, c)), e)
  59. def test_match_negated_parameter(self):
  60. s, c = tree('sin -t, cos -t')
  61. t = s[0]
  62. self.assertEqualPos(match_negated_parameter(s), \
  63. [P(s, negated_sinus_parameter, (t,))])
  64. self.assertEqualPos(match_negated_parameter(c), \
  65. [P(c, negated_cosinus_parameter, (t,))])
  66. def test_negated_sinus_parameter(self):
  67. s = tree('sin -t')
  68. t = s[0]
  69. self.assertEqual(negated_sinus_parameter(s, (t,)), -sin(+t))
  70. def test_negated_cosinus_parameter(self):
  71. c = tree('cos -t')
  72. t = c[0]
  73. self.assertEqual(negated_cosinus_parameter(c, (t,)), cos(+t))
  74. def test_is_pi_frac(self):
  75. l1, pi = tree('1,' + PI)
  76. self.assertTrue(is_pi_frac(l1 / 2 * pi, 2))
  77. self.assertFalse(is_pi_frac(l1 / 2 * pi, 3))
  78. self.assertFalse(is_pi_frac(l1 * pi, 3))
  79. def test_match_standard_radian(self):
  80. s, c, t = tree('sin(1 / 6 * pi), cos(1 / 2 * pi), tan(0)')
  81. self.assertEqualPos(match_standard_radian(s), \
  82. [P(s, standard_radian, (OP_SIN, 1))])
  83. self.assertEqualPos(match_standard_radian(c), \
  84. [P(c, standard_radian, (OP_COS, 4))])
  85. self.assertEqualPos(match_standard_radian(t), \
  86. [P(t, standard_radian, (OP_TAN, 0))])
  87. root = tree('cos pi')
  88. self.assertEqualPos(match_standard_radian(root), \
  89. [P(root, standard_radian, (OP_COS, 5))])
  90. root = tree('cos x')
  91. self.assertEqualPos(match_standard_radian(root), [])
  92. def test_standard_radian(self):
  93. l0, l1, sq3, pi6, pi4, pi2, pi1 = tree('0,1,sqrt(3),1/6*pi,1/4*pi,1/2*pi,pi')
  94. self.assertEqual(standard_radian(sin(pi6), (OP_SIN, 1)), l1 / 2)
  95. self.assertEqual(standard_radian(sin(pi2), (OP_SIN, 4)), 1)
  96. self.assertEqual(standard_radian(cos(l0), (OP_COS, 0)), 1)
  97. self.assertEqual(standard_radian(tan(pi4), (OP_TAN, 3)), sq3)
  98. self.assertEqual(standard_radian(sin(pi1), (OP_SIN, 5)), 0)
  99. self.assertEqual(standard_radian(cos(pi1), (OP_COS, 5)), -1)
  100. self.assertEqual(standard_radian(-cos(pi1), (OP_COS, 5)), --l1)