I have a problem with the symbolic Python package Sympy. Perhaps I am missing some obvious tools in Sympy but I have read through the documentation and not come across a solution.
My question is can the derivative of g(f(y)) = g(f(x))|_(x=y)
with respect to x
be obtained in the form Derivative(f(x), x)(y)
in Sympy? And if so how?
Here is a simple example: I have a symbolic function of one variable e.g. f(x)
.
I have another expression g
, which calls this function with input arguments
represented by other symbols, i.e. g(f(y))
where y = Symbol('y')
.
I implement this with the following:
x = Symbol('x')
f = Function('f')(x)
y = Symbol('y')
g = Function('g')(f).subs('x','y')
My goal is then to evaluate the derivative of g wrt x at x=y, something like
Derivative(f(x), x)(y) * Derivative(g(f), f)(f(y))
This can be roughly achieved with
diff(g, y) = Derivative(f(y), y)*Derivative(g(f(y)), f(y))
.
The problem here is that the term Derivative(f(y), y)
in this case is the derivative evaluated at y.
I have expressions for the derivatives of functions wrt all input variables, i.e. Derivative(f(x), x)
in this case, and not wrt y since it is not a variable of f. Hence, I would like the derivative expressed in the form Derivative(f(x), x)(y)
to avoid repeating the same expression for all the possible variables used.
(For completeness, the term diff(g, x)=0
since any instances of x have been replaced by y.)
To enforce the notion that g is a function of x evaluated at y, replacing the method subs
of the fourth line above with the evalf
method could be the right path. But it seems that expression can only be evaluated at particular values and not other symbolic variables.
Any help and advice is most appreciated.