I'm trying to calculate the gradient of multivariate function g using NumPy.

`g = lambda w: -np.sin(np.pi*np.sum(w**2)) + np.log(np.sum(w**2))`

`gradient = lambda w: ...`

the parameter w is a vector, for example, w = np.array([0.5,0.5]). I calculated the gradient like this;

`gradient = lambda w: np.pi*np.sum(2*w)*-np.cos(np.pi*np.sum(w**2)) + np.sum(2*w)*(1/np.sum(w**2))`

It does not give meaningful results. Is this formula correct or not?

## Best Answer

If we write it more explicitly, $$f(w)=-\sin\left(\pi\sum w_i^2\right)+\ln \left(\sum w_i^2\right)$$

$${\partial f \over \partial w_i}=-\cos\left(\pi\sum w_i^2\right)\pi2w_i+\frac{2w_i}{\sum w_i^2}$$

So, the derivative is a vector; which means your expression will look like

when used denominator layout.