test_rules_derivatives.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. match_sum_product_rule, sum_rule, product_rule, match_quotient_rule, \
  8. quotient_rule
  9. from src.rules.logarithmic import ln
  10. from src.rules.goniometry import sin, cos
  11. from src.node import Scope
  12. from src.possibilities import Possibility as P
  13. from tests.rulestestcase import RulesTestCase, tree
  14. class TestRulesDerivatives(RulesTestCase):
  15. def test_get_derivation_variable(self):
  16. xy0, xy1, x, l1 = tree('der(xy, x), der(xy), der(x), der(1)')
  17. self.assertEqual(get_derivation_variable(xy0), 'x')
  18. self.assertEqual(get_derivation_variable(xy1), 'x')
  19. self.assertEqual(get_derivation_variable(x), 'x')
  20. self.assertIsNone(get_derivation_variable(l1))
  21. def test_match_zero_derivative(self):
  22. root = tree('der(x, y)')
  23. self.assertEqualPos(match_zero_derivative(root),
  24. [P(root, zero_derivative)])
  25. root = tree('der(2)')
  26. self.assertEqualPos(match_zero_derivative(root),
  27. [P(root, zero_derivative)])
  28. def test_zero_derivative(self):
  29. root = tree('der(1)')
  30. self.assertEqual(zero_derivative(root, ()), 0)
  31. def test_match_one_derivative(self):
  32. root = tree('der(x)')
  33. self.assertEqualPos(match_one_derivative(root),
  34. [P(root, one_derivative)])
  35. root = tree('der(x, x)')
  36. self.assertEqualPos(match_one_derivative(root),
  37. [P(root, one_derivative)])
  38. def test_one_derivative(self):
  39. root = tree('der(x)')
  40. self.assertEqual(one_derivative(root, ()), 1)
  41. def test_match_const_deriv_multiplication(self):
  42. root = tree('der(2x)')
  43. l2, x = root[0]
  44. self.assertEqualPos(match_const_deriv_multiplication(root),
  45. [P(root, const_deriv_multiplication, (Scope(root[0]), l2, x))])
  46. (x, y), x = root = tree('der(xy, x)')
  47. self.assertEqualPos(match_const_deriv_multiplication(root),
  48. [P(root, const_deriv_multiplication, (Scope(root[0]), y, x))])
  49. def test_match_const_deriv_multiplication_multiple_constants(self):
  50. root = tree('der(2x * 3)')
  51. (l2, x), l3 = root[0]
  52. scope = Scope(root[0])
  53. self.assertEqualPos(match_const_deriv_multiplication(root),
  54. [P(root, const_deriv_multiplication, (scope, l2, x)),
  55. P(root, const_deriv_multiplication, (scope, l3, x))])
  56. def test_const_deriv_multiplication(self):
  57. root = tree('der(2x)')
  58. l2, x = root[0]
  59. args = Scope(root[0]), l2, x
  60. self.assertEqual(const_deriv_multiplication(root, args),
  61. l2 * der(x, x))
  62. def test_match_variable_power(self):
  63. root, x, l2 = tree('der(x ^ 2), x, 2')
  64. self.assertEqualPos(match_variable_power(root),
  65. [P(root, variable_root)])
  66. root = tree('der(2 ^ x)')
  67. self.assertEqualPos(match_variable_power(root),
  68. [P(root, variable_exponent)])
  69. def test_match_variable_power_chain_rule(self):
  70. root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
  71. self.assertEqualPos(match_variable_power(root),
  72. [P(root, chain_rule, (x3, variable_root, ()))])
  73. root = tree('der(2 ^ x ^ 3)')
  74. self.assertEqualPos(match_variable_power(root),
  75. [P(root, chain_rule, (x3, variable_exponent, ()))])
  76. # Below is not mathematically underivable, it's just not within the
  77. # scope of our program
  78. root, x = tree('der(x ^ x), x')
  79. self.assertEqualPos(match_variable_power(root), [])
  80. def test_variable_root(self):
  81. root = tree('der(x ^ 2)')
  82. x, n = root[0]
  83. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  84. def test_variable_exponent(self):
  85. root = tree('der(2 ^ x)')
  86. g, x = root[0]
  87. self.assertEqual(variable_exponent(root, ()), g ** x * ln(g))
  88. def test_chain_rule(self):
  89. root = tree('der(2 ^ x ^ 3)')
  90. l2, x3 = root[0]
  91. x, l3 = x3
  92. self.assertEqual(chain_rule(root, (x3, variable_exponent, ())),
  93. l2 ** x3 * ln(l2) * der(x3))
  94. def test_match_logarithmic(self):
  95. root = tree('der(log(x))')
  96. self.assertEqualPos(match_logarithmic(root), [P(root, logarithmic)])
  97. def test_match_logarithmic_chain_rule(self):
  98. root, f = tree('der(log(x ^ 2)), x ^ 2')
  99. self.assertEqualPos(match_logarithmic(root),
  100. [P(root, chain_rule, (f, logarithmic, ()))])
  101. def test_logarithmic(self):
  102. root, x, l1, l10 = tree('der(log(x)), x, 1, 10')
  103. self.assertEqual(logarithmic(root, ()), l1 / (x * ln(l10)))
  104. def test_match_goniometric(self):
  105. root = tree('der(sin(x))')
  106. self.assertEqualPos(match_goniometric(root), [P(root, sinus)])
  107. root = tree('der(cos(x))')
  108. self.assertEqualPos(match_goniometric(root), [P(root, cosinus)])
  109. root = tree('der(tan(x))')
  110. self.assertEqualPos(match_goniometric(root), [P(root, tangens)])
  111. def test_match_goniometric_chain_rule(self):
  112. root, x2 = tree('der(sin(x ^ 2)), x ^ 2')
  113. self.assertEqualPos(match_goniometric(root),
  114. [P(root, chain_rule, (x2, sinus, ()))])
  115. root = tree('der(cos(x ^ 2))')
  116. self.assertEqualPos(match_goniometric(root),
  117. [P(root, chain_rule, (x2, cosinus, ()))])
  118. def test_sinus(self):
  119. root, x = tree('der(sin(x)), x')
  120. self.assertEqual(sinus(root, ()), cos(x))
  121. def test_cosinus(self):
  122. root, x = tree('der(cos(x)), x')
  123. self.assertEqual(cosinus(root, ()), -sin(x))
  124. def test_tangens(self):
  125. root, x = tree('der(tan(x), x), x')
  126. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x), x))
  127. root = tree('der(tan(x))')
  128. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x)))
  129. def test_match_sum_product_rule_sum(self):
  130. root = tree('der(x ^ 2 + x)')
  131. x2, x = f = root[0]
  132. self.assertEqualPos(match_sum_product_rule(root),
  133. [P(root, sum_rule, (Scope(f), x2)),
  134. P(root, sum_rule, (Scope(f), x))])
  135. root = tree('der(x ^ 2 + 3 + x)')
  136. self.assertEqualPos(match_sum_product_rule(root),
  137. [P(root, sum_rule, (Scope(root[0]), x2)),
  138. P(root, sum_rule, (Scope(root[0]), x))])
  139. def test_match_sum_product_rule_product(self):
  140. root = tree('der(x ^ 2 * x)')
  141. x2, x = f = root[0]
  142. self.assertEqualPos(match_sum_product_rule(root),
  143. [P(root, product_rule, (Scope(f), x2)),
  144. P(root, product_rule, (Scope(f), x))])
  145. def test_match_sum_product_rule_none(self):
  146. root = tree('der(x ^ 2 + 2)')
  147. self.assertEqualPos(match_sum_product_rule(root), [])
  148. root = tree('der(x ^ 2 * 2)')
  149. self.assertEqualPos(match_sum_product_rule(root), [])
  150. def test_sum_rule(self):
  151. root = tree('der(x ^ 2 + x)')
  152. x2, x = f = root[0]
  153. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(x))
  154. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2))
  155. root = tree('der(x ^ 2 + 3 + x)')
  156. (x2, l3), x = f = root[0]
  157. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(l3 + x))
  158. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2 + l3))
  159. def test_product_rule(self):
  160. root = tree('der(x ^ 2 * x)')
  161. x2, x = f = root[0]
  162. self.assertEqual(product_rule(root, (Scope(f), x2)),
  163. der(x2) * x + x2 * der(x))
  164. self.assertEqual(product_rule(root, (Scope(f), x)),
  165. der(x) * x2 + x * der(x2))
  166. root = tree('der(x ^ 2 * x * x ^ 3)')
  167. (x2, x), x3 = f = root[0]
  168. self.assertEqual(product_rule(root, (Scope(f), x2)),
  169. der(x2) * (x * x3) + x2 * der(x * x3))
  170. self.assertEqual(product_rule(root, (Scope(f), x)),
  171. der(x) * (x2 * x3) + x * der(x2 * x3))
  172. self.assertEqual(product_rule(root, (Scope(f), x3)),
  173. der(x3) * (x2 * x) + x3 * der(x2 * x))
  174. def test_match_quotient_rule(self):
  175. root = tree('der(x ^ 2 / x)')
  176. self.assertEqualPos(match_quotient_rule(root),
  177. [P(root, quotient_rule)])
  178. root = tree('der(x ^ 2 / 2)')
  179. self.assertEqualPos(match_quotient_rule(root), [])
  180. def test_quotient_rule(self):
  181. root = tree('der(x ^ 2 / x)')
  182. f, g = root[0]
  183. self.assertEqual(quotient_rule(root, ()),
  184. (der(f) * g - f * der(g)) / g ** 2)
  185. def test_natural_pase_chain(self):
  186. self.assertRewrite([
  187. 'der(e ^ x)',
  188. 'e ^ x * ln(e)',
  189. 'e ^ x * (log(e) / log(e))',
  190. 'e ^ x * 1',
  191. 'e ^ x',
  192. ])