[Numpy-discussion] Distance Matrix speed

Alan G Isaac aisaac at american.edu
Sun Jun 18 21:05:51 CDT 2006


On Sun, 18 Jun 2006, Sebastian Beca apparently wrote: 
> def dist():
> d = zeros([N, C], dtype=float)
> if N < C: for i in range(N):
>  xy = A[i] - B d[i,:] = sqrt(sum(xy**2, axis=1))
>  return d
> else:
>  for j in range(C):
>  xy = A - B[j] d[:,j] = sqrt(sum(xy**2, axis=1))
> return d 


But that is 50% slower than Johannes's version:

def dist_loehner1():
        d = A[:, newaxis, :] - B[newaxis, :, :]
        d = sqrt((d**2).sum(axis=2))
	return d

Cheers,
Alan Isaac







More information about the Numpy-discussion mailing list