Chain rule and Subs

128 views
Skip to first unread message

Miguel Angel Salazar de Troya

unread,
May 1, 2015, 12:51:45 PM5/1/15
to sy...@googlegroups.com
Hello

I have this derivative of a function that has an argument which is also a function

# Declaration
x = symbols('x')
U, Theta = symbols('U Theta', cls=Function)

# Function to derive
obj_function = Theta(U(x),x)

# Derivative
gradient_obj_function = obj_function.diff(x)

And this is the output I obtain

Derivative(Theta(U(x), x), x) + Derivative(U(x), x)*Subs(Derivative(Theta(_xi_1, x), _xi_1), (_xi_1,), (U(x),))

How can I replace Subs(Derivative(Theta(_xi_1, x), _xi_1), (_xi_1,), (U(x),)) with Derivative(Theta(U(x), x), U(x)) ?
I found this post, https://groups.google.com/forum/#!searchin/sympy/"chain$20rule"/sympy/OfF5PklJtmY/t1FPWt3n8xIJ, but it was not clear to me how this has to be done. 

Thanks
Miguel

Isuru Fernando

unread,
May 1, 2015, 2:19:19 PM5/1/15
to sy...@googlegroups.com
Hi,

I'm not sure how to do the substitution, but why do you expect  obj_function.diff(x) to be 
Derivative(Theta(U(x), x), x) + Derivative(U(x), x)* Derivative(Theta(U(x), x), U(x)) ?

I think the answer you are getting from sympy is not correct.
obj_function.diff(x) should be the same as 
Derivative(Theta(U(x), x), x), but that is not what sympy is giving. 

CSymPy is giving, 
Derivative(U(x), x)*Subs(Derivative(Theta(_x, x), _x), (_x), (U(x))) + Subs(Derivative(Theta(U(x), _x), _x), (_x), (x)) 
which I think should be the correct answer. 

Here what  Subs(Derivative(Theta(U(x), _x), _x), (_x), (x)) means is partial differentiate Theta with respect to the second variable and replace the first variable with U(x) and the second with x. 
(D[2](Theta)(U(x), x) is the equivalent in Maple syntax or Derivative[0, 1][Theta][U[x], x] in Mathematica syntax)

Isuru Fernando


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/5d9dfd23-be1a-4a08-9289-43562d0062f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Miguel Angel Salazar de Troya

unread,
May 5, 2015, 1:32:44 PM5/5/15
to sy...@googlegroups.com
In the end, what I would like to have is this in Latex Code

\frac{\partial \Theta(U(x),x)}{\partial U(x)} \frac{U(x))}{\partial x} + \frac{\partial \Theta(U(x),x)}{\partial x}

and I want to obtain that just from the expression


# Declaration
x = symbols('x')
U, Theta = symbols('U Theta', cls=Function)

# Function to derive
obj_function = Theta(U(x),x)

and derive that w.r.t. "x" which is what mathematically makes sense to me.
Reply all
Reply to author
Forward
0 new messages