# Identifying total derivatives

I would like to identify which terms in my expression are a total derivative and which ones are not.

For expressions in which all terms are a total derivative, one can simply use integrate to find out. For instance, the following simple example nicely illustrates this:

Integrate[f[x] f'[x] + g[x] h'[x] + g'[x] h[x], x]

which yields

f[x]^2/2 + g[x] h[x]


However, for expressions that contain terms that are not total derivatives, this does not work. For instance

Integrate[f[x] f'[x] + g[x] h'[x] + g'[x] h[x]+ g[x] k[x], x]


would not work. Mathematica just returns the same expression with an integral sign. I would like it to tell me that the first three terms are a total derivative and the last one is not.

• I do not understand what you would like to do. Could you provide an example of one expression where the Integrate approach does not work, together with the output you seek in that case?\ Mar 19, 2019 at 21:05
• Sure, for instance Integrate[f[x] f'[x] + g[x] h'[x] + g'[x] h[x]+ g[x] k[x], x] would not work. Mathematica just returns the same expression with an integral sign. I would like it to tell me that the first three terms are a total derivative and the last one is not. Mar 19, 2019 at 21:14
• Got it. I have a proposed approach in an answer below. See if that is what you meant. Mar 19, 2019 at 22:03

Let's generate all possible partial sums of elements from your expression expr in the comments, then let us attempt to calculate the indefinite integral. Among these, we select only those that do not return an Integrate expression, i.e. those for which Integrate found an antiderivative:

expr = f[x] f'[x] + g[x] h'[x] + g'[x] h[x] + g[x] k[x];
res = Select[Subsets[expr] /. 0 -> Nothing, Head[Integrate[#, x]] =!= Integrate &]


The /. 0 -> Nothing part removes the spurious results caused by a subset containing no elements of the original expression (i.e. Plus[]).

If you want the largest subexpression among those, you can take the one with the largest Length:

MaximalBy[Length][res]


More examples might be needed to develop robustness, but this works:

Integrate[f[x] f'[x] + g[x] h'[x] + g'[x] h[x] + g[x] k[x], x] //.
Integrate[Plus[b__, c__], x_] :>
With[{i1 = Integrate[+b, x]}, i1 + Integrate[+c, x] /; FreeQ[i1, Integrate]]
(*  f[x]^2/2 + g[x]*h[x] + Integrate[g[x]*k[x], x]  *)