I have been trying to simplify integrand expressions using integration by parts, which schematically is given by $\theta(x)\nabla\phi(x)=-\phi(x)\nabla\theta(x)$. I think this can be done as a replacement rule, but I don't know the correct syntax. Here is an attempt

IntegrationByParts = # D[\[Phi][x], x] -> \[Phi][x] D[#, x]

Testing the rule with the expression $\phi^2(x)\nabla\phi(x)$:

\[Phi][x]^2 D[\[Phi][x], x] /. IntegrationByParts

simply returns the same expression. The expected result is $\phi^2(x)\nabla\phi(x)=-2\phi^2(x)\nabla\phi(x)$.

  • $\begingroup$ Note: # is short for Slot[1], and only makes sense in function syntax (&) to specify a formal parameter. Rules, however, use patterns, like _, which is short for Blank[]. To keep track of the pattern name it, e.g. a : patt, in this case a_ (which is short for a : _—which is actually short for an even longer expression :) ), and to be safe with keeping track of named patterns, use delayed rules :> instead of -> (so as to avoid evaluating the right hand side until the rule is applied). $\endgroup$
    – thorimur
    Commented Aug 9, 2021 at 7:13
  • $\begingroup$ likewise, x and \[Phi] are also used as patterns, but as trivial ones: they only match the literal symbols x and \[Phi]. I'm not sure if this is intentional or if you want more generality—how generally do you hope to implement integration by parts? note that given an integrand there are many ways to divide it up into $\theta$ and $\phi'$, and you also need to take into consideration what happens on the boundary. so if you have unevaluated symbols like \[Phi] in your integrand it might be simpler, but if you have arbitrary expressions it will be more difficult. $\endgroup$
    – thorimur
    Commented Aug 9, 2021 at 7:16
  • $\begingroup$ Ultimately I'd love to have something that works in all generality that replace (something * \nabla (another thing)) to (-(another thing) * \nabla(something)), while neglecting any boundary terms. However, I understand that there are more than one ways to express the integrand and it is not clear which way is "simpler". So for the current purpose, I'd be satisfied with something more restricted, that replaces (something function of \phi(x)) * \nabla(\phi(x)) with literally the symbol \[Phi] and x. $\endgroup$ Commented Aug 9, 2021 at 8:26
  • 2
    $\begingroup$ oh, ok. the highly specific case is very doable! try IntegrationByParts = (f_ D[\[Phi][x], x] :> -\[Phi][x] D[f, x]) $\endgroup$
    – thorimur
    Commented Aug 9, 2021 at 8:30
  • 1
    $\begingroup$ That worked! Thank you! $\endgroup$ Commented Aug 10, 2021 at 9:39

1 Answer 1


For the sake of answer:

oh, ok. the highly specific case is very doable! try IntegrationByParts = (f_ D[\[Phi][x], x] :> -\[Phi][x] D[f, x]) – thorimur Aug 9, 2021 at 8:30


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.