test_rules_derivatives.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from src.rules.derivatives import der, get_derivation_variable, \
  2. match_zero_derivative, match_one_derivative, one_derivative, \
  3. zero_derivative, match_variable_power, variable_root, \
  4. variable_exponent, match_const_deriv_multiplication, \
  5. const_deriv_multiplication, chain_rule, match_logarithmic, \
  6. logarithmic, match_goniometric, sinus, cosinus, tangens
  7. from src.rules.logarithmic import ln
  8. from src.rules.goniometry import sin, cos
  9. from src.node import Scope
  10. from src.possibilities import Possibility as P
  11. from tests.rulestestcase import RulesTestCase, tree
  12. class TestRulesDerivatives(RulesTestCase):
  13. def test_get_derivation_variable(self):
  14. xy, x, l1 = tree('der(xy, x), der(x), der(1)')
  15. self.assertEqual(get_derivation_variable(xy), 'x')
  16. self.assertEqual(get_derivation_variable(x), 'x')
  17. self.assertIsNone(get_derivation_variable(l1))
  18. self.assertRaises(ValueError, tree, 'der(xy)')
  19. def test_match_zero_derivative(self):
  20. root = tree('der(x, y)')
  21. self.assertEqualPos(match_zero_derivative(root),
  22. [P(root, zero_derivative)])
  23. root = tree('der(2)')
  24. self.assertEqualPos(match_zero_derivative(root),
  25. [P(root, zero_derivative)])
  26. def test_zero_derivative(self):
  27. root = tree('der(1)')
  28. self.assertEqual(zero_derivative(root, ()), 0)
  29. def test_match_one_derivative(self):
  30. root = tree('der(x)')
  31. self.assertEqualPos(match_one_derivative(root),
  32. [P(root, one_derivative)])
  33. root = tree('der(x, x)')
  34. self.assertEqualPos(match_one_derivative(root),
  35. [P(root, one_derivative)])
  36. def test_one_derivative(self):
  37. root = tree('der(x)')
  38. self.assertEqual(one_derivative(root, ()), 1)
  39. def test_match_const_deriv_multiplication(self):
  40. root = tree('der(2x)')
  41. l2, x = root[0]
  42. self.assertEqualPos(match_const_deriv_multiplication(root),
  43. [P(root, const_deriv_multiplication, (Scope(root[0]), l2, x))])
  44. (x, y), x = root = tree('der(xy, x)')
  45. self.assertEqualPos(match_const_deriv_multiplication(root),
  46. [P(root, const_deriv_multiplication, (Scope(root[0]), y, x))])
  47. def test_match_const_deriv_multiplication_multiple_constants(self):
  48. root = tree('der(2x * 3)')
  49. (l2, x), l3 = root[0]
  50. scope = Scope(root[0])
  51. self.assertEqualPos(match_const_deriv_multiplication(root),
  52. [P(root, const_deriv_multiplication, (scope, l2, x)),
  53. P(root, const_deriv_multiplication, (scope, l3, x))])
  54. def test_const_deriv_multiplication(self):
  55. root = tree('der(2x)')
  56. l2, x = root[0]
  57. args = Scope(root[0]), l2, x
  58. self.assertEqual(const_deriv_multiplication(root, args),
  59. l2 * der(x, x))
  60. def test_match_variable_power(self):
  61. root, x, l2 = tree('der(x ^ 2), x, 2')
  62. self.assertEqualPos(match_variable_power(root),
  63. [P(root, variable_root)])
  64. root = tree('der(2 ^ x)')
  65. self.assertEqualPos(match_variable_power(root),
  66. [P(root, variable_exponent)])
  67. def test_match_variable_power_chain_rule(self):
  68. root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
  69. self.assertEqualPos(match_variable_power(root),
  70. [P(root, chain_rule, (x3, variable_root, ()))])
  71. root = tree('der(2 ^ x ^ 3)')
  72. self.assertEqualPos(match_variable_power(root),
  73. [P(root, chain_rule, (x3, variable_exponent, ()))])
  74. # Below is not mathematically underivable, it's just not within the
  75. # scope of our program
  76. root, x = tree('der(x ^ x), x')
  77. self.assertEqualPos(match_variable_power(root), [])
  78. def test_variable_root(self):
  79. root = tree('der(x ^ 2)')
  80. x, n = root[0]
  81. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  82. def test_variable_exponent(self):
  83. root = tree('der(2 ^ x)')
  84. g, x = root[0]
  85. self.assertEqual(variable_exponent(root, ()), g ** x * ln(g))
  86. def test_chain_rule(self):
  87. root = tree('der(2 ^ x ^ 3)')
  88. l2, x3 = root[0]
  89. x, l3 = x3
  90. self.assertEqual(chain_rule(root, (x3, variable_exponent, ())),
  91. l2 ** x3 * ln(l2) * der(x3))
  92. def test_match_logarithmic(self):
  93. root = tree('der(log(x))')
  94. self.assertEqualPos(match_logarithmic(root), [P(root, logarithmic)])
  95. def test_match_logarithmic_chain_rule(self):
  96. root, f = tree('der(log(x ^ 2)), x ^ 2')
  97. self.assertEqualPos(match_logarithmic(root),
  98. [P(root, chain_rule, (f, logarithmic, ()))])
  99. def test_logarithmic(self):
  100. root, x, l1, l10 = tree('der(log(x)), x, 1, 10')
  101. self.assertEqual(logarithmic(root, ()), l1 / (x * ln(l10)))
  102. def test_match_goniometric(self):
  103. root = tree('der(sin(x))')
  104. self.assertEqualPos(match_goniometric(root), [P(root, sinus)])
  105. root = tree('der(cos(x))')
  106. self.assertEqualPos(match_goniometric(root), [P(root, cosinus)])
  107. root = tree('der(tan(x))')
  108. self.assertEqualPos(match_goniometric(root), [P(root, tangens)])
  109. def test_match_goniometric_chain_rule(self):
  110. root, x2 = tree('der(sin(x ^ 2)), x ^ 2')
  111. self.assertEqualPos(match_goniometric(root),
  112. [P(root, chain_rule, (x2, sinus, ()))])
  113. root = tree('der(cos(x ^ 2))')
  114. self.assertEqualPos(match_goniometric(root),
  115. [P(root, chain_rule, (x2, cosinus, ()))])
  116. def test_sinus(self):
  117. root, x = tree('der(sin(x)), x')
  118. self.assertEqual(sinus(root, ()), cos(x))
  119. def test_cosinus(self):
  120. root, x = tree('der(cos(x)), x')
  121. self.assertEqual(cosinus(root, ()), -sin(x))
  122. def test_tangens(self):
  123. root, x = tree('der(tan(x), x), x')
  124. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x), x))
  125. root = tree('der(tan(x))')
  126. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x)))