TITLE: Did I just discover this integration formula?
QUESTION [39 upvotes]: One night, I discovered an integration relationship. That relationship allows to quickly integrate squares of functions (and even more, but I will talk about this at the end).
I was wondering if anyone has found a formula like this before. So I researched on the internet, but couldn't find anything like this.

The formula:
$$\int f(x)^2\,dx\;=\;xf(x)^2\;-\;2f(x)\cdot F^{-1}_{(1)}(f(x))\;+\;2\,\cdot F^{-1}_{(2)}(f(x))\;+\;C$$
Where $F^{-1}_{(n)}$ denotes the $n$th anti-derivative of the inverse function of $f$.
The formula looks rather complicated, but it's really not, on further inspection. Note: the formula doesn't work on the function $f(x)=x$ for a reason that I wasn't able to determine yet. Edit: it actually works.

An example in action:
Let's compute $\int \ln^2x\,dx$. We have then:
$f(x)=\ln x$
$f^{-1}(x)=e^x$
$F^{-1}_{(1)}(x)=e^x$
$F^{-1}_{(2)}(x)=e^x$
Applying the formula, the integral becomes:
$$\int\ln^2x\,dx=x\ln^2x-2\ln x\cdot e^{\ln x}+2\cdot e^{\ln x}+C$$
$$=x\ln^2x-2x\ln x+2x+C$$

Derivation (for the curious):
I derived this formula by substituting for inverse functions and doing repeated integration by parts.
First, substitute $x=f^{-1}(u)$. Then, we have $dx=df^{-1}(u)$. This changes the original integral to:
$$\int f(x)^2\,dx=\int f(f^{-1}(u))^2\,df^{-1}(u)=\int u^2\,df^{-1}(u)$$
At this point, I did integration by parts (probably the funkiest integration by parts you have ever seen). Note, that I am using the "DI table" trick for integration by parts, where in one column, derivatives of one function are specified, and integrals of another are put into the other column. Terms are multiplied diagonally left-down.
$$\begin{array}{ l | c | r }
\pm & D & I \\
\hline
+ & u^2 & df^{-1}(u) \\
- & 2u & f^{-1}(u) \\
+ & 2 & F^{-1}_{(1)}(u) \\
- & 0 & F^{-1}_{(2)}(u) \\
\end{array}$$
$$\int u^2\,df^{-1}(u)\;=\;u^2f^{-1}(u)-2uF^{-1}_{(1)}(u)+2F^{-1}_{(2)}(u)+C$$
Now, simply subsitute back $u=f(x)$ and we arrive at the formula.

Generalization to composition of functions:
It didn't take long for me to realize that this method can be extended to compositions of functions. Just for the curious folks, this is my integral of composition formula:
$$\int f(g(x))\,dx\;=\;\sum^{\infty}_{k=0}(-1)^k\cdot D_k(g(x))\cdot A_k(g(x)) + C$$
Where $D_k={d^k f\over dx^k}$ and $A_k={d^{-k}g^{-1}\over dx^{-k}}$.
Interesting, isn't it?

Back to the question:
Have I discovered this formula? If I didn't can someone point me to a further reading on this topic? I really can't find anything myself, probably because I don't know how to search.

REPLY [10 votes]: My first thought was that if this were true, I'd have seen it already in conjunction with the tables of integrals in one of the standard reference books.
But it seems it is true.
The reason the formula isn't more prominent may be because it requires $f(x)$ to have an inverse, and then it requires that inverse to be integrated twice.
Those requirements put a lot of functions out of reach of this formula.
For example, consider $f(x) = xe^{x^2/2},$ which is sometimes given as an example of why there (supposedly) is no general formula to integrate $f(x).$
(For example, see
how to calculate integral of square of a function
and Integral of Derivative squared).
Then $f^{-1}(x) = \sqrt{\mathrm W(x^2)},$ where W is the Lambert W function, but
(according to Wolfram Alpha) $\sqrt{\mathrm W(x^2)}$ has no integral expressible in elementary functions.
Out of curiosity, I worked this out for a few examples.
All of these are confirmed by other methods (which in these examples are
easier than the formula).

\begin{align}
f(x) &= x \\
f^{-1}(x) &= x \\
F^{-1}_{(1)}(x) &= \tfrac12 x^2   \\
F^{-1}_{(2)}(x) &= \tfrac16 x^3 \\
F^{-1}_{(1)}(f(x)) &= \tfrac12 x^2 \\
F^{-1}_{(2)}(f(x)) &= \tfrac16 x^3
\end{align}
\begin{align}
\int x^2\,dx 
&= x \cdot x^2 - 2x \cdot \tfrac12 x^2 + 2 \cdot \tfrac16 x^3 + C \\
&= x^3 - x^3 + \tfrac13 x^3 + C \\
&= \tfrac13 x^3 + C.
\end{align}

\begin{align}
f(x) &= \sin x \\
f^{-1}(x) &= \arcsin x \\
F^{-1}_{(1)}(x) &= \sqrt{1 - x^2} + x \arcsin x   \\
F^{-1}_{(2)}(x) &= \tfrac14 (3x \sqrt{1 - x^2} + 2 x^2 \arcsin x + \arcsin x) \\
F^{-1}_{(1)}(f(x)) &= \cos x + x\sin x \\
F^{-1}_{(2)}(f(x)) &= \tfrac14 (3\sin x \cos x + 2 x\sin^2 x + x)
\end{align}
\begin{align}
\int \sin^2 x\,dx 
&= x \sin^2 x - 2\sin x (\cos x + x\sin x)
    + 2 \cdot \tfrac14 (3\sin x \cos x + 2 x\sin^2 x + x) + C \\
&= x \sin^2 x - 2\sin x \cos x - 2x\sin^2 x
    + \tfrac32\sin x \cos x + x\sin^2 x + \tfrac12x + C \\
&= (x - 2x +x)\sin^2 x + \left(- 2 + \tfrac32\right)\sin x \cos x + \tfrac12x + C \\
&= -\tfrac12 \sin x \cos x + \tfrac12x + C. \\
\end{align}

\begin{align}
f(x) &= e^x \\
f^{-1}(x) &= \ln x \\
F^{-1}_{(1)}(x) &= x (\ln x - 1) \\
F^{-1}_{(2)}(x) &= \tfrac14 x^2 (2 \ln x - 3) \\
F^{-1}_{(1)}(f(x)) &= e^x (x - 1) \\
F^{-1}_{(2)}(f(x)) &= \tfrac14 e^{2x} (2 x - 3)
\end{align}
\begin{align}
\int (e^x)^2\,dx 
&= x e^{2x} - 2e^x \cdot e^x (x - 1) + 2 \cdot \tfrac14 e^{2x} (2 x - 3) + C \\
&= x e^{2x} - 2(x - 1)e^{2x} + \left(x - \tfrac32\right) e^{2x} + C \\
&= \left(x - 2x + 2 + x - \tfrac32\right)e^{2x} + C \\
&= \tfrac12 e^{2x} + C. \\
\end{align}

\begin{align}
f(x) &= e^{x^2} \\
f^{-1}(x) &= \sqrt{\ln x} \\
F^{-1}_{(1)}(x) &= x\sqrt{\ln x} - \tfrac12\sqrt\pi\,\mathrm{erfi}(\sqrt{\ln x}) \\
F^{-1}_{(2)}(x)
&= -\tfrac12 \sqrt\pi x \, \mathrm{erfi}(\sqrt{\ln x}) 
  + \tfrac18\sqrt{2\pi}\,\mathrm{erfi}(\sqrt{2\ln x}) + \tfrac12 x^2 \sqrt{\ln x} \\
x f(x)^2 &= x e^{2x^2}, \\
F^{-1}_{(1)}(f(x)) &= x e^{x^2} - \tfrac12\sqrt\pi\,\mathrm{erfi}(x) \\
2f(x)F^{-1}_{(1)}(f(x)) &= 2x e^{2x^2} - \sqrt\pi\,e^{x^2}\mathrm{erfi}(x),\\
2F^{-1}_{(2)}(f(x)) &= -\sqrt\pi e^{x^2} \mathrm{erfi}(x) 
    + \tfrac14\sqrt{2\pi}\, \mathrm{erfi}(\sqrt2 x) + xe^{2x^2} \\
\end{align}
\begin{align}
\int \left(e^{x^2}\right)^2\,dx 
&= x e^{2x^2} - \left(2x e^{2x^2} - \sqrt\pi\,e^{x^2}\mathrm{erfi}(x)\right)
   - \sqrt\pi e^{x^2} \mathrm{erfi}(x) 
    + \tfrac14\sqrt{2\pi}\, \mathrm{erfi}(\sqrt2 x) + xe^{2x^2} + C\\
&= \tfrac14\sqrt{2\pi}\, \mathrm{erfi}(\sqrt2 x) + C.
\end{align}
This agrees with $\int e^{u^2}\,du = \tfrac12\sqrt\pi\,\mathrm{erfi}(u) + C$
with the substitution $u = \sqrt2 x.$