# How to “undo” differentiation

Say I have an expression of the form:

$$\frac{-rf(r)f'(r)h'(r)}{h^2(r)}+f(r)\frac{rf''(r)+f'(r)}{h(r)}$$

Is there a Mathematica function, similar to Expand or Simplify that would take this expression and return:

$$f(r) \frac{d}{dr}\frac{rf'(r)}{h(r)}$$

For context, I am using Mathematica to help me calculate the Einstein field equations, but I want to solve the equations myself. I would like Mathematica to give them to me in the second, more digestible form.

• Well, Mathematica is able to recognize that the forms are identical, i.e. (-r f[r] f'[r] h'[r])/h[r]^2 + f[r] (r f''[r] + f'[r])/h[r] == f[r] D[r f'[r]/h[r], r] // FullSimplify, but I've generally had a pretty hard time getting one symbolic expression to be in an exact format. Since they're equivalent Mathematica is usually happy to just deal with whatever it considers to be the "simplest", in its terms, but not necessarily your terms. – user6014 Jan 30 '18 at 22:21
• Actually, things may be even worse than as described by @user6014. Consider D[f[r], r] g[r] + D[g[r], r] f[r], which has a LeafCount of 15, and D[f[r] g[r], r], which has a LeafCount of 8. However, even it FullSimplify could transform the first expression into the second, Mathematica automatically would transform it back to the first expression! – bbgodfrey Jan 31 '18 at 2:36
• People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful – Michael E2 Jan 31 '18 at 3:05
• @MichaelE2 I was wondering that. I am on mobile atm, but I'll change the formulas to code when I can. – Andrea Jan 31 '18 at 10:29

In some cases you can use DSolve to try and reduce your expression by guessing an appropriate form. For example, the form g[x] f'[x] + f[x] g'[x] can be integrated, so we just try the following:
DSolve[g[x] f'[x] + f[x] g'[x] == y'[x], y[x], x]

So by guessing that g[x] f'[x] + f[x] g'[x] can be written as the derivative of some other thing, DSolve found the integrated version. However, this only works if you can guess the correct integrated form and even then there's no guarantee that DSolve will find something nice.