I have a function that can be written as follows: $$f(\vec{x},\vec{y}) = f_x(\vec{x}) + f_y(\vec{y}) + k\bigg(f_x(\vec{x})-f_y(\vec{y})\bigg)^2$$
I have to take the first derivative (gradient) and second derivative (hessian) of this function with respect to the combined $\vec{x}$ and $\vec{y}$. By combined, I mean a vector which the concatenation of both $\vec{x}$ and $\vec{y}$ like this: $$\vec{z} = (x_1,x_2,x_3,...,y_1,y_2,y_3,...)$$
So far I have been able to derive the analytic expression for the gradient $\nabla f$, which is simple because $f_x$ depends only on $x$ terms, and $f_y$ depends only on y terms. So, I can simply expand the square expression, collect the gradient terms, and concatenate. However, I am struggling to derive an expression for the Hessian matrix ($\nabla^2 f$) because of the cross-terms.
Note: The gradient and Hessian of each of the $f_x(\vec{x})$ and $f_y(\vec{y})$ with respect to their own vectors are possible to calculate (available). So, I have to write the gradient and Hessian of the composite function in those terms, as I need those for a program I am coding. Both $f_x$ and $f_y$ are functions that produce scalar numbers as ouptut. $k$ is a scalar constant.
I am a chemist, so I am not very familiar with linear algebra. If the Hessian (or gradient) can be calculated quickly with a matrix operation then it would be easier to code for. Any help is appreciated.
Let $h(x,y) = f_x(\vec{x})+f_y(\vec{y})+k\Big(f_x(\vec{x})-f_y(\vec{y})\Big)^2$. Then
$$\nabla_{\vec{x},\vec{y}}h(\vec{x},\vec{y})=\begin{bmatrix}\nabla_\vec{x}\\\nabla_\vec{y}\end{bmatrix}h(\vec{x},\vec{y})\\ = \begin{bmatrix}\nabla_\vec{x}f_x(\vec{x}) + k\nabla_\vec{x}f_x(\vec{x})^2-2kf_y(\vec{y})\nabla_\vec{x}f_x(\vec{x}) \\ \nabla_\vec{y}f_y(\vec{y}) + k\nabla_\vec{y}f_y(\vec{y})^2-2kf_x(\vec{x})\nabla_\vec{y}f_y(\vec{y})\end{bmatrix}$$ and $$H_{\vec{x},\vec{y}}h(\vec{x},\vec{y})=\begin{bmatrix}\partial^2_{\vec{x},\vec{x}} & \partial^2_{\vec{x},\vec{y}} \\ \partial^2_{\vec{y},\vec{x}} & \partial^2_{\vec{y},\vec{y}} \end{bmatrix}h(\vec{x},\vec{y})$$ $$=\begin{bmatrix} H_\vec{x}f_x(\vec{x}) + kH_\vec{x}f_x(\vec{x})^2-2kf_y(\vec{y})H_\vec{x}f_x(\vec{x}) & -2k\nabla f_x^T \nabla f_y \\ -2k\nabla f_y^T \nabla f_x & H_\vec{y}f_y(\vec{y}) + kH_\vec{y}f_y(\vec{y})^2-2kf_x(\vec{x})H_\vec{y}f_y(\vec{y}) \end{bmatrix}$$
I've written these in block matrix notation. If you can compute the regular gradients and hessians of $f_i$ and $f_i^2$, then these formulae should get you to the gradient and hessian of $h$