DEFINITIONS
$ \beta =$ scalar scaling parameter
$ \alpha = $ scalar shape parameter
$ X = N\times 3$ matrix of triples, each a point in 3D space ( so the $i^{\text{th}}$ row of $X$ is $ x_i , y_i , z_i $ )
$ d_{ij} = $ distance between $ (x_i , y_i , z_i) $ and $ (x_j , y_j , z_j) $, $j \ge i$
$r_{ij} = (1 \times N)$ vector with $i^{\text{th}}$ entry equal to $1$, $j^{\text{th}}$ entry equal to $-1$, and $0$ everywhere else. $j \ge i$
$c_{ij} =$ constants, $j \ge i$
PROBLEM
I would like to find the $1^{\text{st}}$ and $2^{\text{nd}}$ derivatives of the following equation w.r.t. the matrix $X$. Please note the the function, $L(X,\alpha,\beta | c)$ is a scalar.
The purpose of this is to implement a Newton-Raphson type algorithm for $X$ which is an $N\times 3$ matrix
$$ L(X,\alpha,\beta | c) = \frac{\alpha}{2} \sum_{ij} c_{ij} \log{(r_{ij}^{'}XX^{'}r_{ij})} + \log{\beta}\sum_{ij} c_{ij} - \beta \sum_{ij} (r_{ij}^{'}XX^{'}r_{ij})^{\frac{\alpha}{2}} $$
I have taken the first derivative (please feel free to check my work)
\begin{align*}\frac{d}{dX'}L(X,\alpha,\beta | c) &= \alpha \sum_{ij} \frac{ c_{ij} }{ r_{ij}^{'}XX^{'}r_{ij} } (X^{'}r_{ij}r_{ij}^{'}) - \alpha\beta \sum_{ij} (r_{ij}^{'}XX'r_{ij})^{ \frac{\alpha-2}{2} }(X^{'}r_{ij}r_{ij}^{'})\\ &=\alpha X' \left[\sum_{ij} \frac{ r_{ij}r_{ij}^{'} }{ r_{ij}^{'}XX^{'}r_{ij} } \left[c_{ij} - \beta (r_{ij}^{'}XX'r_{ij})^{ \frac{\alpha}{2} }\right] \right] \end{align*}
which is a $3 \times N$ matrix, so it will conform with my original $X$.
PLEASE HELP WITH THE FOLLOWING
How to I take the second derivative? I've tried taking the derivative of the above expression, but I get an $N\times N$ matrix added to a $3\times N$ matrix. It ought to be a square, $3\times 3$ matrix (I believe).
Many thanks for any insight you can offer.