python - Sum of Gaussians into fast Numpy? -
here problem:
i have 2 sets of 3d points. lets call them "gausspoints" , "xyz". define function sum of gaussians in every gaussian centered @ 1 of gausspoints. want evaluate function on xyz points: approach working fine rather slow. idea how speed exploiting numpy little better?
def sumgaus(r): t=r-gausspoints t=map(np.linalg.norm,t) t = -np.power(t,2.0) t=np.exp(t) res=np.sum(t) return res result=map(sumgaus,xyz)
thanks help
edit: shape of xyz n*3 , gausspoints m*3 m, n being different integers
edit2: want apply following function on each item in xyz
the tricky part how vectorize computation of differences between points without explicit python looping or mapping. can roll out own implementation using broadcasting doing like:
dist2 = xyz[:, np.newaxis, :] - gausspoints dist2 *= dist dist2 = np.sum(dist, axis=-1)
and if xyz
has shape (n, 3)
, gausspoints
has shape (m, 3)
, dist
have shape (n, m)
, dist[i, j]
being distance between points xyz[i]
, gausspoints[j]
.
it may easier understand using scipy.spatial.distance.cdist
:
from scipy.spatial.distance import cdist dist2 = cdist(xyz, gausspoints) dist2 *= dist2
but once have array of squared distances, it's child's play:
f = np.sum(np.exp(-dist2), axis=1)
Comments
Post a Comment