The key relation for functional differentiation is $$\frac{\delta}{\delta f(y)}f(x)=\delta(x-y), $$ where $\delta(x-y)$ is the Dirac delta function, and the usual properties of differentiation (e.g. linearity, chain rule) still hold.

Another example: $$\frac{\delta}{\delta f(y)}(f(x)f(x))=2 \delta(x-y) f(x).$$

A possible Mathematica version could look like this


How would one go about implementing this?


There's also the case of multiple arguments e.g. $$\frac{\delta}{\delta f(y)}(f(x_1)f(x_2))= \delta(x_1-y) f(x_2)+\delta(x_2-y) f(x_1).$$

  • 4
    $\begingroup$ short answer is yes; I did it in the past (with version 2.2. of mathematica too!) but more on a problem by problem basis than what you are asking. $\endgroup$
    – chris
    Commented Nov 13, 2012 at 18:00

3 Answers 3

FunctionalD[expr_, f_[y_]] := 
  With[{xs = DeleteDuplicates[Cases[expr, f[x_ /; FreeQ[x, _f]] :> x, {0, Infinity}]]},
    Total[D[expr, f[#]] DiracDelta[# - y] & /@ xs]

edit 1: the FreeQ is needed to get the chain rule correct for f[f[x]]

edit 2: the DeleteDuplicates is needed per Stephen Luttrell's comment. I still need to think about Chris' comment.

  • $\begingroup$ I think you need to use Cases[expr, f[x_] :> x, {0, Infinity}] // Union in order to retain only the distinct instances of f[x_]. For instance, FunctionalD[f[x] + g[f[x]], f[y]] gives twice the expected result doing it your way. $\endgroup$ Commented Nov 13, 2012 at 21:22
  • 1
    $\begingroup$ another issue may be: FunctionalD[Integrate[K[x - y] f[y], {y, 0, Infinity}], f[x]] returns the DiracDelta outside the integral. $\endgroup$
    – chris
    Commented Nov 13, 2012 at 21:23
  • $\begingroup$ The other thing I completely forgot -- it's been a while since I've had to functional differentiate -- is that one should have e.g. FunctionalD[f'[y],f[x]] = -DiracDelta'[y-x]. That could probably be fixed by supplementing the pattern in the Cases to include derivatives of f, and include an corresponding appropriate derivative of the DiracDelta in the Map. $\endgroup$ Commented Nov 14, 2012 at 2:03
  • 1
    $\begingroup$ I had to downvote this because it returns incorrect results as you notice yourself in the previous comment. $\endgroup$
    – Jens
    Commented Nov 15, 2012 at 4:45
  • $\begingroup$ @Jeremy: As far as I'm concerned the rule for the derivative is not overly important (in many cases one would simply get rid of the derivative through a Fourier transform). $\endgroup$
    – Emerson
    Commented Nov 15, 2012 at 21:46

Mathematica already knows quite a lot about functional derivatives. In particular, you can do variational derivatives. That is, you have to give it the functional and the function (I would strongly suspect that your problem can be written so as to use the VariationalD function). To get started, have a look at the tutorial for the Variational Methods package.

Edit: Originally I had a definition based on the Variational Methods package here, but the following method is more transparent, so I removed the original attempt (which also used a different syntax).

Edit 2

I just came across another approach to defining the functional derivative without recourse to the VariationalMethods package. It's directly from the documentation for DiracDelta (under "Applications"):

FunctionalD[functional_, f_[y_]] := 
 Assuming[ Element[y, Reals], 
   Limit[((functional /. f :>  Function[x, f[x] + 
                           ε DiracDelta[x - y]]) - 
      functional)/ε, ε -> 0]]

FunctionalD[f[x]^2, f[y]]

(* ==> 2 DiracDelta[x - y] f[x] *)

FunctionalD[f[x1] f[x2], f[y]]

(* ==> DiracDelta[x2 - y] f[x1] + DiracDelta[x1 - y] f[x2] *)

This satisfies all the requirements of the question without loading a package.

  • 1
    $\begingroup$ AFAICT this doesn't handle the case of multiple arguments (see edit in question). $\endgroup$
    – Emerson
    Commented Nov 15, 2012 at 21:42
  • $\begingroup$ @Emerson We can easily handle multiple arguments as soon as you understand what they are supposed to mean in terms of the variational approach. The new rule you added in your edit simply means you're varying f at y in a product of two functionals, one with x1 and the other with x2 as the integration variable. After applying the regular product rule to that, you're back to the FunctionalD in my answer. Of course one could try to automate the regular differentiation parts too - but the core of the calculation is always what I described already. $\endgroup$
    – Jens
    Commented Nov 16, 2012 at 4:30
  • 1
    $\begingroup$ FYI your generic definition fails on FunctionalD[G[f[x]^], f[y]]. $\endgroup$
    – chris
    Commented Aug 24, 2014 at 13:01
  • 1
    $\begingroup$ or FunctionalD[ Integrate[ K[z - Subscript[x, 1]] f[Subscript[x, 1]], {Subscript[x, 1], -Infinity, Infinity}], f[y]] … a pity :-) $\endgroup$
    – chris
    Commented Aug 24, 2014 at 13:04
  • $\begingroup$ @chris Sure, Mathematica probably can't do the limit in that case. But that's still better than getting a wrong result for that case, as you would with the accepted answer. Anyway, if you have an integral, are you sure you don't want the variational derivative instead of the functional derivative? $\endgroup$
    – Jens
    Commented Aug 24, 2014 at 16:51

When I did this in FeynCalc some 17 years ago I did not care about DeltaFunction, since it is not really needed, at least not for deriving Feynman rules. You can find a couple of examples here : http://www.feyncalc.org/FeynCalcBook/FunctionalD/


FunctionalD[QuantumField[f]^2, QuantumField[f]] 

results in 2 QuantumField[f] The code is free, so you can just download the package and look at FunctionalD.m and friends (like RightPartialD.m, ExpandPartialD.m , etc. )

It is all a bit rusty and should be rewritten, but that takes time.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.