Integral of exponential with linear term $$x \in \mathbb{R^n},$$ M is a positive symmetrical nonsingular nxn Matrix and j is an arbitrary vector in $$\mathbb{R}^n.$$ The following has to be calculated:
$$Z(j) = \int_\mathbb{R^n} e^{-x^TMx+2j^Tx} d^{n}x.$$
Further, calculate  
$\dfrac{\partial {\log(Z)}}{\partial {M}} $ at $j=0.$
I m not really sure how I should approach the problem, however, it should somehow be solved with the help of completing the square, but am not sure, how exactly. Any help would be appreciated.
 A: To answer your question about completing the full square. First, note that you can write $j = M y$, where $y := M^{-1} j$. Then, you get:
$$
\exp\left\{-x^\top M x + 2 j^\top x\right\} = \exp\left\{-\left(x^\top M x - 2 y^\top M x + y^\top M y\right) + y^\top M y\right\} = \exp\left\{-(x - y)^\top M (x - y) + y^\top M y\right\}
$$
The expression for $Z(j)$ will turn into
$$
Z(j) = e^{y^\top M y} \int_{\mathbb{R}^n} e^{-\frac{1}{2}(x - y)^\top (2M) (x - y)} d^n x
$$
Note that now the integral resembles PDF of a multivariate normal distribution with precision matrix $2M$. The only difference is that there is no normalizing constant. Therefore, integration will give you precisely the following:
$$
\int_{\mathbb{R}^n} e^{-\frac{1}{2}(x - y)^\top (2M) (x - y)} d^n x = \frac{(2\pi)^{n/2}}{\det (2M)^{1/2}} = \sqrt{\frac{\pi^n}{\det M}}
$$
We can plug in $y = M^{-1}j$ and get the final expression for $Z(j)$:
$$
Z(j) = \sqrt{\frac{\pi^n}{\det M}} \exp\left\{j^\top M^{-1} j\right\}
$$
To answer the second part of the question, you need to compute the gradient of the scalar function $Z$ with a matrix argument. Here is how you can do that.
$$
\frac{\partial}{\partial M}\ln Z = -\frac{1}{2} \frac{\partial}{\partial M}\ln \det M + \frac{\partial}{\partial M} j^T M^{-1} j.
$$
For these two derivatives, you can look the standard expressions in the Matrix Cookbook or consult the first section of a nice tutorial on matrix algebra. You get the following:
$$
\frac{\partial}{\partial M} \ln \det M = M^{-1}
$$
and
$$
\frac{\partial}{\partial M} j^T M^{-1} j = \frac{\partial}{\partial M} \mathrm{Tr}\left(j^T M^{-1} j\right) = \frac{\partial}{\partial M} \mathrm{Tr}\left(j j^T M^{-1}\right) = - M^{-1} j j^T M^{-1}
$$
Finally:
$$
\frac{\partial}{\partial M}\ln Z = -\frac{1}{2} M^{-1} - M^{-1} j j^T M^{-1}
$$
and you have $\partial\ln Z / \partial M = -M^{-1}/2$ when $j = 0$.
