Given the gradient of a function, how do you find the function itself?
For a scalar-valued function $f : \mathbb{R}^n \to \mathbb{R}$, the gradient of $f$, denoted $\nabla f : \mathbb{R}^n \to \mathbb{R}^n$, is defined as
$\nabla f(x)_i := \frac{\delta f(x)}{\delta x_i}, \quad i=1,\ldots,n.$
For example, for $A \in \mathbb{R}^{n \times n}$ and $b,x \in \mathbb{R}^n$, the gradient of the function
$f(x) := \frac{1}{2} (Ax-b)^T(Ax-b)$
is
$\nabla f(x) = (Ax-b)^T A.$
Now suppose we are told that the gradient of a scalar-valued function $g$ is
$\nabla g(x) = (Ax-b)^T D$
for some diagonal matrix $D \in \mathbb{R}^{n \times n}$.
Is there a nice closed-form expression for $g(x)$?
These two threads (here, here) seem to use guess-and-check. We tried looking at function of the form
$g(x) := \frac{1}{2} (Ax-b)^T W (Ax-b)$
for various weighting matrices $W$, but no dice.


There is a nice closed form expression for $g(x)$ if it exists. Assuming it does, the path integration is quite straightforward. \begin{multline} g(x) = \int\nabla g(x)\cdot d\mathbf{x} = \int (\mathbf{A}\mathbf{x} - \mathbf{b})^T\mathbf{D}d\mathbf{x} = \int \mathbf{x}^T\mathbf{A}^T\mathbf{D}d\mathbf{x} - \mathbf{b}^T\mathbf{D}\int d\mathbf{x} \\?= \frac{1}{2}\mathbf{x}^T\mathbf{A}^T\mathbf{D}\mathbf{x} -\mathbf{b}^T\mathbf{D}\mathbf{x} = \left(\frac{1}{2}\mathbf{A}\mathbf{x} - \mathbf{b}\right)^T\mathbf{D}\mathbf{x} \end{multline}
However, the equality with the ? in front of it is not, in general, true. What condition on $\mathbf{A}$ and $\mathbf{D}$ is needed to make it true?