A scalar function of a matrix argument is a function that takes a matrix as input and produces a scalar as output. The derivative of a scalar function with respect to a matrix is a matrix of the derivatives of the function with respect to each element of the matrix. For a scalar function $f(\mathbf{X})$ where $\mathbf{X}$ is a matrix, the derivative is given by:
$$\nabla f(\mathbf{X}) = \begin{bmatrix} \frac{\partial f}{\partial X_{11}} & \cdots & \frac{\partial f}{\partial X_{1n}} \ \vdots & \ddots & \vdots \ \frac{\partial f}{\partial X_{m1}} & \cdots & \frac{\partial f}{\partial X_{mn}} \end{bmatrix}$$
def scalar_function(M):
return np.sum(M)
M = np.array([[1, 2], [3, 4]])
print(f"f({M}) = {scalar_function(M)}")