I am trying to obtain the mixed derivative formula for fun with SymPy.
I am looking for a better replacement for a function f by partial derivative of f respect to y.
from sympy import symbols, Symbol, Function, Eq, solve
from sympy import Derivative as D
x, y = symbols("x y")
dx = Symbol(r"\Delta x")
dy = Symbol(r"\Delta y")
f = Function("f")
eq1 = Eq(f(x + dx, y), f(x + dx, y).series(x=dx, x0=0, n=2).removeO().simplify())
expr1 = solve(eq1, D(f(x, y), x))[0]
Eq1 = Eq(D(f(x, y), x), expr1)
Eq1.rhs.subs({f(x, y): D(f(x, y), y), f(x + dx, y): D(f(x + dx, y), y)})
In the cell [11], I would like replace f by other function like f_y and works fine, no matter what argument have the function to be replaced. For instance, if f --> f**2, then we will have any expression of f to square.
You may want to look into using Wild
in pattern matching. Here is some code to get you started:
from sympy import symbols, Symbol, Function, Eq, solve, Wild
from sympy import Derivative as D
x, y = symbols("x y")
dx = Symbol(r"\Delta x")
dy = Symbol(r"\Delta y")
f = Function("f")
eq1 = Eq(f(x + dx, y), f(x + dx, y).series(x=dx, x0=0, n=2).removeO().simplify())
expr1 = solve(eq1, D(f(x, y), x))[0]
Eq1 = Eq(D(f(x, y), x), expr1)
arg1, arg2 = Wild("arg1"), Wild("arg2")
Eq1.rhs.replace(f(arg1, arg2), D(f(arg1, arg2), arg2))
For more information I always refer to the sympy.core.basic.Basic API.