How to integrate-by-parts a quadratic expression of first derivatives? I'm trying to re-express the following integral in terms of the second derivative $g''(x)$.
$$\int g(x) f'(x)^2 \, dx$$
where $f$ and $g$ are general functions.  My approach is to think of $f(x)$ as a vector, with each element representing the value of the function at some point on a grid with finite separations (eventually taking the limit to zero separation).  It is acted upon by a differential operator matrix $\overset\rightarrow \partial$, and a diagonal matrix representing $g(x)$.  Then the integral looks like an inner product:
$$ (\overset\rightarrow\partial \mathbf f)^T \mathbf g (\overset\rightarrow\partial \mathbf f) $$
Instead of acting with differential operator matrix on $\mathbf f$, we first consider the matrix product $\overset\rightarrow\partial^T \mathbf g \overset\rightarrow\partial$.  Doing the matrix multiplication with derivatives as the limit of finite differences, what I get for the non-boundary part is
$$ \int \left( \frac14 g''(x) f(x)^2 + \frac12 g(x) f'(x)^2 \right) dx $$
which would mean that $\tfrac12 g''(x) f(x)^2$ could be the whole integrand (since the second term is just half of what we started with).  Happy to show my steps here if anyone is interested.
Is this a right expression for the non-boundary part after integration by parts? Thanks!
 A: The solution I was looking for is
$$ \int_{x_0}^{x_1} g(x) f'(x)^2 \, \mathrm dx = \int_{x_0}^{x_1} \left[ \frac14 g''(x) f(x)^2 +\frac12 g(x) \left( \lim_{h\to0} \frac{f(x)^2 - f(x-h)f(x+h)}{h^2} \right) \right] \, \mathrm dx + \text{boundary}$$
where the boundary terms are
$$ \left[ \frac12 g(x) f(x) f'(x) - \frac14 g'(x) f(x)^2 \right]_{x_0}^{x_1} $$
This works numerically for a number of arbitrary-looking functions $f(x)$ and $g(x)$ that I have tried, and I believe can be derived from considering finite differences.
The original integrand can be expressed before the $h\to0$ limit as:
$$ g(x) \left[ \frac{f(x+h) - f(x-h)}{2h} \right]^2 $$
Expanding the square, we get
$$ \frac{g(x)}{4h^2} \left[ f(x+h)^2 + f(x-h)^2 - 2f(x+h)f(x-h) \right] $$
Next we mix terms from adjacent points at which the above quantity is evaluated in the summation, and add and subtract the term $2g(x)f(x)^2/4h^2$
$$ \frac{1}{4h^2} \left[ g(x-h) f(x)^2 - 2 g(x) f(x)^2 + g(x+h) f(x)^2 + 2 g(x) f(x)^2 - 2 g(x) f(x+h)f(x-h) \right] $$
Rearranging terms isolates $g(x)$ evaluated at different points.
$$ \frac14 \frac{g(x-h) - 2 g(x) + g(x+h)}{h^2} f(x)^2 + \frac12 g(x) \frac{ f(x)^2 - f(x+h)f(x-h)}{h^2} $$
Finally, we take the limit to recover the second derivative $g''(x)$.
$$ \frac14 g''(x) f(x)^2 +\frac12 g(x) \left( \lim_{h\to0} \frac{f(x)^2 - f(x-h)f(x+h)}{h^2} \right) $$
The quantity in parentheses seems to be well defined --- I'm not sure if it can be expressed in terms of ordinary derivatives.  I obtained the boundary terms from trial and error, but I'm sure they can be found from the finite differences method.
