SymPy: How to implement functionals and functional derivatives

Viewed 33

Dear StackOverflow community,

although the Python package SymPy provides functionality for various QM (e.g. wave functions and operators) and QFT (e.g. gamma matrices) operations, there is no support for functional derivatives.

I would like to implement the following analytically known functional derivative

D f(t) / D f(t') = delta(t - t') (delta distribution)

to compute more interesting results, e.g.,

D F[f] / D f(t)

where ordinary derivation rules apply until D f(t) / D f(t') has to be computed. I have included an example below. I am aware SymPy already supports taking derivatives with respect to functions, but that is not a functional derivative.

With best regards, XeLasar

Example:

F[f] := exp(integral integral f(x) G(x, y) f(y) dx dy)


D F[f] / D f(t) = (integral          f(x) * G(x, y) * delta (t - y) dx dy
                 + integral delta (t - x) * G(x, y) * f(y)          dx dy) * F[f]
                = (integral f(x) * G(x, t)        dx 
                 + integral        G(t, y) * f(y) dy) * F[f]
                = 2 * (integral G(t, t') * f(t') dt') * F[f]

Note: The integral over dt' collapses due to the delta that arises from D f(t) / D f(t')! G(t, t') is an arbitrary symmetric function. The result can be further simplified as integration variables can be renamed.

1 Answers

I found a solution to the problem and I want to share it so others don't have to waste as much time as I did.

Rewriting the Derivative() function in SymPy is unfortunately not an option as derivation rules are defined within expression classes. Although the SymPy source feels like Spaghetti, it is well tested and should not be touched.

It is best to explain with one or two examples what I came up with, but roughly speaking: I found a convenient substitution to perform the functional derivative using the ordinary Derivative() function of SymPy.

Example:

Z[f] = Integral(f(x) G(x,y) f(y), dx, dy) ,  let f(x) -> A(z), f(y) -> B(z)
     = Integral(A(z) G(x,y) B(z), dx, dy)

dZ/dz = Integral(dA/dz G(x,y) B(z) + A(z) G(x,y) dB/dz, x, y)

Re-substitute: A(z)  -> f(x)      , B(z)  -> f(y)
Insert:        dA/dz -> delta(t-x), dB/dz -> delta(t-y)

=> DZ[f]/Df(t) = Integral(delta(t-x) G(x,y) f(y) + f(x) G(x,y) delta(t-y), x, y)
               = Integral(G(t,y) f(y), y) + Integral(f(x) G(x,t), x)

Although this is not a general proof, this substitution works in my usecases. Since I am using the normal SymPy Derivative() routine, everything works flawlessly.

With best regards, XeLasar

Related