derivatives.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. from itertools import combinations
  2. from .utils import find_variables
  3. from .logarithmic import ln
  4. from .goniometry import sin, cos
  5. from ..node import ExpressionNode as N, ExpressionLeaf as L, Scope, OP_DER, \
  6. OP_MUL, OP_LOG, OP_SIN, OP_COS, OP_TAN, OP_ADD, OP_DIV
  7. from ..possibilities import Possibility as P, MESSAGES
  8. from ..translate import _
  9. def der(f, x=None):
  10. return N(OP_DER, f, x) if x else N(OP_DER, f)
  11. def second_arg(node):
  12. """
  13. Get the second child of a node if it exists, None otherwise.
  14. """
  15. return node[1] if len(node) > 1 else None
  16. def get_derivation_variable(node, variables=None):
  17. """
  18. Find the variable to derive over.
  19. >>> print get_derivation_variable(der(L('x')))
  20. 'x'
  21. """
  22. if len(node) > 1:
  23. assert node[1].is_identifier()
  24. return node[1].value
  25. if not variables:
  26. variables = find_variables(node)
  27. if len(variables) > 1:
  28. # FIXME: Use first variable, sorted alphabetically?
  29. #return sorted(variables)[0]
  30. raise ValueError('More than 1 variable in implicit derivative: '
  31. + ', '.join(variables))
  32. if not len(variables):
  33. return None
  34. return list(variables)[0]
  35. def chain_rule(root, args):
  36. """
  37. Apply the chain rule:
  38. [f(g(x)]' -> f'(g(x)) * g'(x)
  39. f'(g(x)) is not expressable in the current syntax, so calculate it directly
  40. using the application function in the arguments. g'(x) is simply expressed
  41. as der(g(x), x).
  42. """
  43. g, f_deriv, f_deriv_args = args
  44. x = root[1] if len(root) > 1 else None
  45. return f_deriv(root, f_deriv_args) * der(g, x)
  46. def match_zero_derivative(node):
  47. """
  48. der(x, y) -> 0
  49. der(n) -> 0
  50. """
  51. assert node.is_op(OP_DER)
  52. variables = find_variables(node[0])
  53. var = get_derivation_variable(node, variables)
  54. if not var or var not in variables:
  55. return [P(node, zero_derivative)]
  56. return []
  57. def zero_derivative(root, args):
  58. """
  59. der(x, y) -> 0
  60. der(n) -> 0
  61. """
  62. return L(0)
  63. MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.')
  64. def match_one_derivative(node):
  65. """
  66. der(x) -> 1 # Implicit x
  67. der(x, x) -> 1 # Explicit x
  68. """
  69. assert node.is_op(OP_DER)
  70. var = get_derivation_variable(node)
  71. if var and node[0] == L(var):
  72. return [P(node, one_derivative)]
  73. return []
  74. def one_derivative(root, args):
  75. """
  76. der(x) -> 1
  77. der(x, x) -> 1
  78. """
  79. return L(1)
  80. MESSAGES[one_derivative] = _('Variable {0[0]} has derivative 1.')
  81. def match_const_deriv_multiplication(node):
  82. """
  83. der(c * f(x), x) -> c * der(f(x), x)
  84. """
  85. assert node.is_op(OP_DER)
  86. p = []
  87. if node[0].is_op(OP_MUL):
  88. x = L(get_derivation_variable(node))
  89. scope = Scope(node[0])
  90. for n in scope:
  91. if not n.contains(x):
  92. p.append(P(node, const_deriv_multiplication, (scope, n, x)))
  93. return p
  94. def const_deriv_multiplication(root, args):
  95. """
  96. der(c * f(x), x) -> c * der(f(x), x)
  97. """
  98. scope, c, x = args
  99. scope.remove(c)
  100. return c * der(scope.as_nary_node(), x)
  101. MESSAGES[const_deriv_multiplication] = \
  102. _('Bring multiplication with {2} in derivative {0} to the outside.')
  103. def match_variable_power(node):
  104. """
  105. der(x ^ n) -> n * x ^ (n - 1)
  106. der(x ^ n, x) -> n * x ^ (n - 1)
  107. der(f(x) ^ n) -> n * f(x) ^ (n - 1) * der(f(x)) # Chain rule
  108. """
  109. assert node.is_op(OP_DER)
  110. if not node[0].is_power():
  111. return []
  112. root, exponent = node[0]
  113. rvars = find_variables(root)
  114. evars = find_variables(exponent)
  115. x = get_derivation_variable(node, rvars | evars)
  116. if x in rvars and x not in evars:
  117. if root.is_variable():
  118. return [P(node, variable_root)]
  119. return [P(node, chain_rule, (root, variable_root, ()))]
  120. elif not x in rvars and x in evars:
  121. if exponent.is_variable():
  122. return [P(node, variable_exponent)]
  123. return [P(node, chain_rule, (exponent, variable_exponent, ()))]
  124. return []
  125. def variable_root(root, args):
  126. """
  127. der(x ^ n, x) -> n * x ^ (n - 1)
  128. """
  129. x, n = root[0]
  130. return n * x ** (n - 1)
  131. MESSAGES[variable_root] = \
  132. _('Apply standard derivative d/dx x ^ n = n * x ^ (n - 1) on {0}.')
  133. def variable_exponent(root, args):
  134. """
  135. der(g ^ x, x) -> g ^ x * ln(g)
  136. Note that (in combination with logarithmic/constant rules):
  137. der(e ^ x) -> e ^ x * ln(e) -> e ^ x * 1 -> e ^ x
  138. """
  139. # TODO: Put above example 'der(e ^ x)' in unit test
  140. g, x = root[0]
  141. return g ** x * ln(g)
  142. MESSAGES[variable_exponent] = \
  143. _('Apply standard derivative d/dx g ^ x = g ^ x * ln g.')
  144. def match_logarithmic(node):
  145. """
  146. der(log(x, g), x) -> 1 / (x * ln(g))
  147. der(log(f(x), g), x) -> 1 / (f(x) * ln(g)) * der(f(x), x)
  148. """
  149. assert node.is_op(OP_DER)
  150. x = get_derivation_variable(node)
  151. if x and node[0].is_op(OP_LOG):
  152. f = node[0][0]
  153. x = L(x)
  154. if f == x:
  155. return [P(node, logarithmic, ())]
  156. if f.contains(x):
  157. return [P(node, chain_rule, (f, logarithmic, ()))]
  158. return []
  159. def logarithmic(root, args):
  160. """
  161. der(log(x, g), x) -> 1 / (x * ln(g))
  162. """
  163. x, g = root[0]
  164. return L(1) / (x * ln(g))
  165. MESSAGES[logarithmic] = \
  166. _('Apply standard derivative d/dx log(x, g) = 1 / (x * ln(g)).')
  167. def match_goniometric(node):
  168. """
  169. der(sin(x), x) -> cos(x)
  170. der(sin(f(x)), x) -> cos(f(x)) * der(f(x), x)
  171. der(cos(x), x) -> -sin(x)
  172. der(cos(f(x)), x) -> -sin(f(x)) * der(f(x), x)
  173. der(tan(x), x) -> der(sin(x) / cos(x), x)
  174. """
  175. assert node.is_op(OP_DER)
  176. x = get_derivation_variable(node)
  177. if x and not node[0].is_leaf:
  178. op = node[0].op
  179. if op in (OP_SIN, OP_COS):
  180. f = node[0][0]
  181. x = L(x)
  182. handler = sinus if op == OP_SIN else cosinus
  183. if f == x:
  184. return [P(node, handler)]
  185. if f.contains(x):
  186. return [P(node, chain_rule, (f, handler, ()))]
  187. if op == OP_TAN:
  188. return [P(node, tangens)]
  189. return []
  190. def sinus(root, args):
  191. """
  192. der(sin(x), x) -> cos(x)
  193. """
  194. return cos(root[0][0])
  195. MESSAGES[sinus] = _('Apply standard derivative d/dx sin(x) = cos(x).')
  196. def cosinus(root, args):
  197. """
  198. der(cos(x), x) -> -sin(x)
  199. """
  200. return -sin(root[0][0])
  201. MESSAGES[cosinus] = _('Apply standard derivative d/dx cos(x) = -sin(x).')
  202. def tangens(root, args):
  203. """
  204. der(tan(x), x) -> der(sin(x) / cos(x), x)
  205. """
  206. x = root[0][0]
  207. return der(sin(x) / cos(x), second_arg(root))
  208. MESSAGES[tangens] = \
  209. _('Convert the tanges to a division and apply the product rule.')
  210. def match_sum_product_rule(node):
  211. """
  212. [f(x) + g(x)]' -> f'(x) + g'(x)
  213. [f(x) * g(x)]' -> f'(x) * g(x) + f(x) * g'(x)
  214. """
  215. assert node.is_op(OP_DER)
  216. x = get_derivation_variable(node)
  217. if not x or node[0].is_leaf or node[0].op not in (OP_ADD, OP_MUL):
  218. return []
  219. scope = Scope(node[0])
  220. x = L(x)
  221. functions = [n for n in scope if n.contains(x)]
  222. if len(functions) < 2:
  223. return []
  224. p = []
  225. handler = sum_rule if node[0].op == OP_ADD else product_rule
  226. for f in functions:
  227. p.append(P(node, handler, (scope, f)))
  228. return p
  229. def sum_rule(root, args):
  230. """
  231. [f(x) + g(x)]' -> f'(x) + g'(x)
  232. """
  233. scope, f = args
  234. x = second_arg(root)
  235. scope.remove(f)
  236. return der(f, x) + der(scope.as_nary_node(), x)
  237. MESSAGES[sum_rule] = _('Apply the sum rule to {0}.')
  238. def product_rule(root, args):
  239. """
  240. [f(x) * g(x)]' -> f'(x) * g(x) + f(x) * g'(x)
  241. Note that implicitely:
  242. [f(x) * g(x) * h(x)]' -> f'(x) * (g(x) * h(x)) + f(x) * [g(x) * h(x)]'
  243. """
  244. scope, f = args
  245. x = second_arg(root)
  246. scope.remove(f)
  247. gh = scope.as_nary_node()
  248. return der(f, x) * gh + f * der(gh, x)
  249. MESSAGES[product_rule] = _('Apply the product rule to {0}.')
  250. def match_quotient_rule(node):
  251. """
  252. [f(x) / g(x)]' -> (f'(x) * g(x) - f(x) * g'(x)) / g(x) ^ 2
  253. """
  254. assert node.is_op(OP_DER)
  255. x = get_derivation_variable(node)
  256. if not x or not node[0].is_op(OP_DIV):
  257. return []
  258. f, g = node[0]
  259. x = L(x)
  260. if f.contains(x) and g.contains(x):
  261. return [P(node, quotient_rule)]
  262. return []
  263. def quotient_rule(root, args):
  264. """
  265. [f(x) / g(x)]' -> (f'(x) * g(x) - f(x) * g'(x)) / g(x) ^ 2
  266. """
  267. f, g = root[0]
  268. x = second_arg(root)
  269. return (der(f, x) * g - f * der(g, x)) / g ** 2
  270. MESSAGES[quotient_rule] = _('Apply the quotient rule to {0}.')