Let $X$ be a $n \times p$ matrix, $n>p$. I derived a (Hessian) matrix as follows:
$$H(\theta) = \sum_{i=1}^n g(x_i,\theta) x_i x_i^T$$
where $g(x_i,\theta)$ is a scalar function and $x_i$ is the $i$-th row of $X$. How can I vectorize this function? We may assume that $g(X,\theta)$ returns the $n \times 1$ vector where the $i$-th element is given by $g(x_i,\theta)$.
I found an answer. This problem is known as iterative reweighted least squares. Define:
$$H(\theta)= X^T W X$$
where
$$W_{ii} = g(x_i,\theta)$$
a diagonal matrix.