0

I'm interested on calculating a derivative using diff from numpy. For this, I use two arrays a and b with shape (N,) try to compute x

N=int(1e4)
a=np.ones(N)
b=9*np.ones(N)

x=np.zeros((N,1)) #shape (10000, 1)
for j in range(N):
    x[j]=a[j]/b[j]
y=3*x - 1

doty=np.diff(y)/np.diff(b)

I find a ValueError: operands could not be broadcast together with shapes (10000,0) (9999,).

Why does this happens? How can I reshape b to match x?

nuwe
  • 177
  • 9
  • 1
    Try `doty=np.diff(y.reshape(-1))/np.diff(b)` –  Dec 06 '21 at 01:29
  • Thanks @user17242583 , I didn't think the solution was that simple. Should I close this question? ; I also think this [this question](https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape) might be useful for someone having the same issue. – nuwe Dec 06 '21 at 01:36

1 Answers1

1

Try this:

doty = np.diff(y.reshape(-1)) / np.diff(b)