# Gradient of a multivariate function numpy

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))

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?

• Apply the Chain Rule.
– whuber
Commented Apr 10, 2022 at 12:16

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

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

when used denominator layout.

• Thanks. w is an array which consists of w0 and w1, for example w = np.array([0.5,0.5]). In this case np.sum(w^2) is equivalent to w0^2 + w1^2, so I considered the derivation (2 * w0 + 2 * w1) which is equivalent to np.sum(w*2). Commented Apr 11, 2022 at 5:01
• Why do you sum the derivatives? Commented Apr 11, 2022 at 5:15
• Because in the function there is np.sum(w^2) part which is equivalent to w0^2 + w1^2. The parameter w is a 2-element array, the square roots of w0 and w1 added in the function, so I think the derivative of (w0^2 + w1^2) part should be np.sum(2*w). Commented Apr 11, 2022 at 5:38
• No, derivative of w0^2 + w1^2 wrt w0 is 2w0, and wrt w1 is 2w1. You do not sum the derivatives. Commented Apr 11, 2022 at 6:49
• That is correct, in none of them it’s summed. Commented Apr 11, 2022 at 18:19