4
$\begingroup$

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.

Any other practical ways to go about this?

$\endgroup$
  • 1
    $\begingroup$ 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?\ $\endgroup$ – MarcoB Mar 19 at 21:05
  • $\begingroup$ 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. $\endgroup$ – user25477 Mar 19 at 21:14
  • $\begingroup$ Got it. I have a proposed approach in an answer below. See if that is what you meant. $\endgroup$ – MarcoB Mar 19 at 22:03
3
$\begingroup$

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 &]

Mathematica graphics

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]

only max

$\endgroup$
3
$\begingroup$

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]  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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