test_rules_derivatives.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, power_rule
  9. from src.node import Scope, sin, cos, ln
  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. xy0, xy1, x, l1 = tree('der(xy, x), der(xy), der(x), der(1)')
  15. self.assertEqual(get_derivation_variable(xy0), 'x')
  16. self.assertEqual(get_derivation_variable(xy1), 'x')
  17. self.assertEqual(get_derivation_variable(x), 'x')
  18. self.assertIsNone(get_derivation_variable(l1))
  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. [P(root, power_rule)])
  79. def test_power_rule(self):
  80. root, expect = tree("[x ^ x]', [e ^ ln(x ^ x)]'")
  81. self.assertEqual(power_rule(root, ()), expect)
  82. def test_power_rule_chain(self):
  83. self.assertRewrite([
  84. "[x ^ x]'",
  85. "[e ^ ln(x ^ x)]'",
  86. "[e ^ (xln(x))]'",
  87. "e ^ (xln(x))ln(e)[xln(x)]'",
  88. "e ^ (xln(x))1[xln(x)]'",
  89. "e ^ (xln(x))[xln(x)]'",
  90. "e ^ (xln(x))([x]' * ln(x) + x[ln(x)]')",
  91. "e ^ (xln(x))(1ln(x) + x[ln(x)]')",
  92. "e ^ (xln(x))(ln(x) + x[ln(x)]')",
  93. "e ^ (xln(x))(ln(x) + x(1 / (xln(e))))",
  94. "e ^ (xln(x))(ln(x) + x(1 / (x * 1)))",
  95. "e ^ (xln(x))(ln(x) + x(1 / x))",
  96. "e ^ (xln(x))(ln(x) + 1x / x)",
  97. "e ^ (xln(x))(ln(x) + x / x)",
  98. "e ^ (xln(x))(ln(x) + 1)",
  99. "e ^ ln(x ^ x)(ln(x) + 1)",
  100. # FIXME: "x ^ x(ln(x) + 1)", -> needs strategy
  101. ])
  102. def test_variable_root(self):
  103. root = tree('der(x ^ 2)')
  104. x, n = root[0]
  105. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  106. def test_variable_exponent(self):
  107. root = tree('der(2 ^ x)')
  108. g, x = root[0]
  109. self.assertEqual(variable_exponent(root, ()), g ** x * ln(g))
  110. def test_chain_rule(self):
  111. root = tree('der(2 ^ x ^ 3)')
  112. l2, x3 = root[0]
  113. x, l3 = x3
  114. self.assertEqual(chain_rule(root, (x3, variable_exponent, ())),
  115. l2 ** x3 * ln(l2) * der(x3))
  116. def test_match_logarithmic(self):
  117. root = tree('der(log(x))')
  118. self.assertEqualPos(match_logarithmic(root), [P(root, logarithmic)])
  119. def test_match_logarithmic_chain_rule(self):
  120. root, f = tree('der(log(x ^ 2)), x ^ 2')
  121. self.assertEqualPos(match_logarithmic(root),
  122. [P(root, chain_rule, (f, logarithmic, ()))])
  123. def test_logarithmic(self):
  124. root, x, l1, l10 = tree('der(log(x)), x, 1, 10')
  125. self.assertEqual(logarithmic(root, ()), l1 / (x * ln(l10)))
  126. def test_match_goniometric(self):
  127. root = tree('der(sin(x))')
  128. self.assertEqualPos(match_goniometric(root), [P(root, sinus)])
  129. root = tree('der(cos(x))')
  130. self.assertEqualPos(match_goniometric(root), [P(root, cosinus)])
  131. root = tree('der(tan(x))')
  132. self.assertEqualPos(match_goniometric(root), [P(root, tangens)])
  133. def test_match_goniometric_chain_rule(self):
  134. root, x2 = tree('der(sin(x ^ 2)), x ^ 2')
  135. self.assertEqualPos(match_goniometric(root),
  136. [P(root, chain_rule, (x2, sinus, ()))])
  137. root = tree('der(cos(x ^ 2))')
  138. self.assertEqualPos(match_goniometric(root),
  139. [P(root, chain_rule, (x2, cosinus, ()))])
  140. def test_sinus(self):
  141. root, x = tree('der(sin(x)), x')
  142. self.assertEqual(sinus(root, ()), cos(x))
  143. def test_cosinus(self):
  144. root, x = tree('der(cos(x)), x')
  145. self.assertEqual(cosinus(root, ()), -sin(x))
  146. def test_tangens(self):
  147. root, x = tree('der(tan(x), x), x')
  148. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x), x))
  149. root = tree('der(tan(x))')
  150. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x)))
  151. def test_match_sum_product_rule_sum(self):
  152. root = tree('der(x ^ 2 + x)')
  153. x2, x = f = root[0]
  154. self.assertEqualPos(match_sum_product_rule(root),
  155. [P(root, sum_rule, (Scope(f), x2)),
  156. P(root, sum_rule, (Scope(f), x))])
  157. root = tree('der(x ^ 2 + 3 + x)')
  158. self.assertEqualPos(match_sum_product_rule(root),
  159. [P(root, sum_rule, (Scope(root[0]), x2)),
  160. P(root, sum_rule, (Scope(root[0]), x))])
  161. def test_match_sum_product_rule_product(self):
  162. root = tree('der(x ^ 2 * x)')
  163. x2, x = f = root[0]
  164. self.assertEqualPos(match_sum_product_rule(root),
  165. [P(root, product_rule, (Scope(f), x2)),
  166. P(root, product_rule, (Scope(f), x))])
  167. def test_match_sum_product_rule_none(self):
  168. root = tree('der(x ^ 2 + 2)')
  169. self.assertEqualPos(match_sum_product_rule(root), [])
  170. root = tree('der(x ^ 2 * 2)')
  171. self.assertEqualPos(match_sum_product_rule(root), [])
  172. def test_sum_rule(self):
  173. root = tree('der(x ^ 2 + x)')
  174. x2, x = f = root[0]
  175. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(x))
  176. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2))
  177. root = tree('der(x ^ 2 + 3 + x)')
  178. (x2, l3), x = f = root[0]
  179. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(l3 + x))
  180. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2 + l3))
  181. def test_product_rule(self):
  182. root = tree('der(x ^ 2 * x)')
  183. x2, x = f = root[0]
  184. self.assertEqual(product_rule(root, (Scope(f), x2)),
  185. der(x2) * x + x2 * der(x))
  186. self.assertEqual(product_rule(root, (Scope(f), x)),
  187. der(x) * x2 + x * der(x2))
  188. root = tree('der(x ^ 2 * x * x ^ 3)')
  189. (x2, x), x3 = f = root[0]
  190. self.assertEqual(product_rule(root, (Scope(f), x2)),
  191. der(x2) * (x * x3) + x2 * der(x * x3))
  192. self.assertEqual(product_rule(root, (Scope(f), x)),
  193. der(x) * (x2 * x3) + x * der(x2 * x3))
  194. self.assertEqual(product_rule(root, (Scope(f), x3)),
  195. der(x3) * (x2 * x) + x3 * der(x2 * x))
  196. def test_match_quotient_rule(self):
  197. root = tree('der(x ^ 2 / x)')
  198. self.assertEqualPos(match_quotient_rule(root),
  199. [P(root, quotient_rule)])
  200. root = tree('der(x ^ 2 / 2)')
  201. self.assertEqualPos(match_quotient_rule(root), [])
  202. def test_quotient_rule(self):
  203. root = tree('der(x ^ 2 / x)')
  204. f, g = root[0]
  205. self.assertEqual(quotient_rule(root, ()),
  206. (der(f) * g - f * der(g)) / g ** 2)
  207. def test_natural_pase_chain(self):
  208. self.assertRewrite([
  209. 'der(e ^ x)',
  210. 'e ^ x * ln(e)',
  211. 'e ^ x * 1',
  212. 'e ^ x',
  213. ])