test_rules_derivatives.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. from src.rules.derivatives import get_derivation_variable, \
  16. match_zero_derivative, match_one_derivative, one_derivative, \
  17. zero_derivative, match_variable_power, variable_root, \
  18. variable_exponent, match_const_deriv_multiplication, \
  19. const_deriv_multiplication, chain_rule, match_logarithmic, \
  20. logarithmic, match_goniometric, sinus, cosinus, tangens, \
  21. match_sum_product_rule, sum_rule, product_rule, match_quotient_rule, \
  22. quotient_rule, power_rule
  23. from src.node import Scope, sin, cos, ln, der
  24. from src.possibilities import Possibility as P
  25. from tests.rulestestcase import RulesTestCase, tree
  26. class TestRulesDerivatives(RulesTestCase):
  27. def test_get_derivation_variable(self):
  28. xy0, xy1, x, l1 = tree('der(xy, x), der(xy), der(x), der(1)')
  29. self.assertEqual(get_derivation_variable(xy0), 'x')
  30. self.assertEqual(get_derivation_variable(xy1), 'x')
  31. self.assertEqual(get_derivation_variable(x), 'x')
  32. self.assertIsNone(get_derivation_variable(l1))
  33. def test_match_zero_derivative(self):
  34. root = tree('der(x, y)')
  35. self.assertEqualPos(match_zero_derivative(root),
  36. [P(root, zero_derivative)])
  37. root = tree('der(2)')
  38. self.assertEqualPos(match_zero_derivative(root),
  39. [P(root, zero_derivative)])
  40. def test_zero_derivative(self):
  41. root = tree('der(1)')
  42. self.assertEqual(zero_derivative(root, ()), 0)
  43. def test_match_one_derivative(self):
  44. root = tree('der(x)')
  45. self.assertEqualPos(match_one_derivative(root),
  46. [P(root, one_derivative)])
  47. root = tree('der(x, x)')
  48. self.assertEqualPos(match_one_derivative(root),
  49. [P(root, one_derivative)])
  50. def test_one_derivative(self):
  51. root = tree('der(x)')
  52. self.assertEqual(one_derivative(root, ()), 1)
  53. def test_match_const_deriv_multiplication(self):
  54. root = tree('der(2x)')
  55. l2, x = root[0]
  56. self.assertEqualPos(match_const_deriv_multiplication(root),
  57. [P(root, const_deriv_multiplication, (Scope(root[0]), l2, x))])
  58. (x, y), x = root = tree('der(xy, x)')
  59. self.assertEqualPos(match_const_deriv_multiplication(root),
  60. [P(root, const_deriv_multiplication, (Scope(root[0]), y, x))])
  61. def test_match_const_deriv_multiplication_multiple_constants(self):
  62. root = tree('der(2x * 3)')
  63. (l2, x), l3 = root[0]
  64. scope = Scope(root[0])
  65. self.assertEqualPos(match_const_deriv_multiplication(root),
  66. [P(root, const_deriv_multiplication, (scope, l2, x)),
  67. P(root, const_deriv_multiplication, (scope, l3, x))])
  68. def test_const_deriv_multiplication(self):
  69. root = tree('der(2x)')
  70. l2, x = root[0]
  71. args = Scope(root[0]), l2, x
  72. self.assertEqual(const_deriv_multiplication(root, args),
  73. l2 * der(x, x))
  74. def test_match_variable_power(self):
  75. root, x, l2 = tree('der(x ^ 2), x, 2')
  76. self.assertEqualPos(match_variable_power(root),
  77. [P(root, variable_root)])
  78. root = tree('der(2 ^ x)')
  79. self.assertEqualPos(match_variable_power(root),
  80. [P(root, variable_exponent)])
  81. def test_match_variable_power_chain_rule(self):
  82. root, x, l2, x3 = tree('der((x ^ 3) ^ 2), x, 2, x ^ 3')
  83. self.assertEqualPos(match_variable_power(root),
  84. [P(root, chain_rule, (x3, variable_root, ()))])
  85. root = tree('der(2 ^ x ^ 3)')
  86. self.assertEqualPos(match_variable_power(root),
  87. [P(root, chain_rule, (x3, variable_exponent, ()))])
  88. # Below is not mathematically underivable, it's just not within the
  89. # scope of our program
  90. root, x = tree('der(x ^ x), x')
  91. self.assertEqualPos(match_variable_power(root),
  92. [P(root, power_rule)])
  93. def test_power_rule(self):
  94. root, expect = tree("[x ^ x]', [e ^ ln(x ^ x)]'")
  95. self.assertEqual(power_rule(root, ()), expect)
  96. def test_power_rule_chain(self):
  97. self.assertRewrite([
  98. "[x ^ x]'",
  99. "[e ^ ln(x ^ x)]'",
  100. "e ^ ln(x ^ x)[ln(x ^ x)]'",
  101. "x ^ x * [ln(x ^ x)]'",
  102. "x ^ x * [xln(x)]'",
  103. "x ^ x * ([x]' * ln(x) + x[ln(x)]')",
  104. "x ^ x * (1ln(x) + x[ln(x)]')",
  105. "x ^ x * (ln(x) + x[ln(x)]')",
  106. "x ^ x * (ln(x) + x * 1 / x)",
  107. "x ^ x * (ln(x) + (x * 1) / x)",
  108. "x ^ x * (ln(x) + x / x)",
  109. "x ^ x * (ln(x) + 1)",
  110. "x ^ x * ln(x) + x ^ x * 1",
  111. "x ^ x * ln(x) + x ^ x",
  112. ])
  113. def test_variable_root(self):
  114. root = tree('der(x ^ 2)')
  115. x, n = root[0]
  116. self.assertEqual(variable_root(root, ()), n * x ** (n - 1))
  117. def test_variable_exponent(self):
  118. root = tree('der(2 ^ x)')
  119. g, x = root[0]
  120. self.assertEqual(variable_exponent(root, ()), g ** x * ln(g))
  121. root = tree('der(e ^ x)')
  122. e, x = root[0]
  123. self.assertEqual(variable_exponent(root, ()), e ** x)
  124. def test_chain_rule(self):
  125. root = tree('der(2 ^ x ^ 3)')
  126. l2, x3 = root[0]
  127. x, l3 = x3
  128. self.assertEqual(chain_rule(root, (x3, variable_exponent, ())),
  129. l2 ** x3 * ln(l2) * der(x3))
  130. def test_match_logarithmic(self):
  131. root = tree('der(log(x))')
  132. self.assertEqualPos(match_logarithmic(root), [P(root, logarithmic)])
  133. def test_match_logarithmic_chain_rule(self):
  134. root, f = tree('der(log(x ^ 2)), x ^ 2')
  135. self.assertEqualPos(match_logarithmic(root),
  136. [P(root, chain_rule, (f, logarithmic, ()))])
  137. def test_logarithmic(self):
  138. root, x, l1, l10 = tree('der(log(x)), x, 1, 10')
  139. self.assertEqual(logarithmic(root, ()), l1 / (x * ln(l10)))
  140. root, x, l1, l10 = tree('der(ln(x)), x, 1, 10')
  141. self.assertEqual(logarithmic(root, ()), l1 / x)
  142. def test_match_goniometric(self):
  143. root = tree('der(sin(x))')
  144. self.assertEqualPos(match_goniometric(root), [P(root, sinus)])
  145. root = tree('der(cos(x))')
  146. self.assertEqualPos(match_goniometric(root), [P(root, cosinus)])
  147. root = tree('der(tan(x))')
  148. self.assertEqualPos(match_goniometric(root), [P(root, tangens)])
  149. def test_match_goniometric_chain_rule(self):
  150. root, x2 = tree('der(sin(x ^ 2)), x ^ 2')
  151. self.assertEqualPos(match_goniometric(root),
  152. [P(root, chain_rule, (x2, sinus, ()))])
  153. root = tree('der(cos(x ^ 2))')
  154. self.assertEqualPos(match_goniometric(root),
  155. [P(root, chain_rule, (x2, cosinus, ()))])
  156. def test_sinus(self):
  157. root, x = tree('der(sin(x)), x')
  158. self.assertEqual(sinus(root, ()), cos(x))
  159. def test_cosinus(self):
  160. root, x = tree('der(cos(x)), x')
  161. self.assertEqual(cosinus(root, ()), -sin(x))
  162. def test_tangens(self):
  163. root, x = tree('der(tan(x), x), x')
  164. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x), x))
  165. root = tree('der(tan(x))')
  166. self.assertEqual(tangens(root, ()), der(sin(x) / cos(x)))
  167. def test_match_sum_product_rule_sum(self):
  168. root = tree('der(x ^ 2 + x)')
  169. x2, x = f = root[0]
  170. self.assertEqualPos(match_sum_product_rule(root),
  171. [P(root, sum_rule, (Scope(f), x2)),
  172. P(root, sum_rule, (Scope(f), x))])
  173. root = tree('der(x ^ 2 + 3 + x)')
  174. self.assertEqualPos(match_sum_product_rule(root),
  175. [P(root, sum_rule, (Scope(root[0]), x2)),
  176. P(root, sum_rule, (Scope(root[0]), x))])
  177. def test_match_sum_product_rule_product(self):
  178. root = tree('der(x ^ 2 * x)')
  179. x2, x = f = root[0]
  180. self.assertEqualPos(match_sum_product_rule(root),
  181. [P(root, product_rule, (Scope(f), x2)),
  182. P(root, product_rule, (Scope(f), x))])
  183. def test_match_sum_product_rule_none(self):
  184. root = tree('der(2 + 2)')
  185. self.assertEqualPos(match_sum_product_rule(root), [])
  186. root = tree('der(x ^ 2 * 2)')
  187. self.assertEqualPos(match_sum_product_rule(root), [])
  188. def test_sum_rule(self):
  189. root = tree('der(x ^ 2 + x)')
  190. x2, x = f = root[0]
  191. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(x))
  192. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2))
  193. root = tree('der(x ^ 2 + 3 + x)')
  194. (x2, l3), x = f = root[0]
  195. self.assertEqual(sum_rule(root, (Scope(f), x2)), der(x2) + der(l3 + x))
  196. self.assertEqual(sum_rule(root, (Scope(f), x)), der(x) + der(x2 + l3))
  197. def test_product_rule(self):
  198. root = tree('der(x ^ 2 * x)')
  199. x2, x = f = root[0]
  200. self.assertEqual(product_rule(root, (Scope(f), x2)),
  201. der(x2) * x + x2 * der(x))
  202. self.assertEqual(product_rule(root, (Scope(f), x)),
  203. der(x) * x2 + x * der(x2))
  204. root = tree('der(x ^ 2 * x * x ^ 3)')
  205. (x2, x), x3 = f = root[0]
  206. self.assertEqual(product_rule(root, (Scope(f), x2)),
  207. der(x2) * (x * x3) + x2 * der(x * x3))
  208. self.assertEqual(product_rule(root, (Scope(f), x)),
  209. der(x) * (x2 * x3) + x * der(x2 * x3))
  210. self.assertEqual(product_rule(root, (Scope(f), x3)),
  211. der(x3) * (x2 * x) + x3 * der(x2 * x))
  212. def test_match_quotient_rule(self):
  213. root = tree('der(x ^ 2 / x)')
  214. self.assertEqualPos(match_quotient_rule(root),
  215. [P(root, quotient_rule)])
  216. root = tree('der(x ^ 2 / 2)')
  217. self.assertEqualPos(match_quotient_rule(root), [])
  218. def test_quotient_rule(self):
  219. root = tree('der(x ^ 2 / x)')
  220. f, g = root[0]
  221. self.assertEqual(quotient_rule(root, ()),
  222. (der(f) * g - f * der(g)) / g ** 2)
  223. #def test_natural_pase_chain(self):
  224. # self.assertRewrite([
  225. # 'der(e ^ x)',
  226. # 'e ^ x * ln(e)',
  227. # 'e ^ x * 1',
  228. # 'e ^ x',
  229. # ])