[Numpy-discussion] speed of numpy.ndarray compared to Numeric.array
Pascal
pascal22p at parois.net
Mon Jan 10 10:25:33 EST 2011
Hi,
On 01/10/2011 09:09 AM, EMMEL Thomas wrote:
>
> No I didn't, due to the fact that these values are coordinates in 3D (x,y,z).
> In fact I work with a list/array/tuple of arrays with 100000 to 1M of elements or more.
> What I need to do is to calculate the distance of each of these elements (coordinates)
> to a given coordinate and filter for the nearest.
> The brute force method would look like this:
>
>
> #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> def bruteForceSearch(points, point):
>
> minpt = min([(vec2Norm(pt, point), pt, i)
> for i, pt in enumerate(points)], key=itemgetter(0))
> return sqrt(minpt[0]), minpt[1], minpt[2]
>
> #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> def vec2Norm(pt1,pt2):
> xDis = pt1[0]-pt2[0]
> yDis = pt1[1]-pt2[1]
> zDis = pt1[2]-pt2[2]
> return xDis*xDis+yDis*yDis+zDis*zDis
>
I am not sure I understood the problem properly but here what I would
use to calculate a distance from horizontally stacked vectors (big):
ref=numpy.array([0.1,0.2,0.3])
big=numpy.random.randn(1000000, 3)
big=numpy.add(big,-ref)
distsquared=numpy.sum(big**2, axis=1)
Pascal
More information about the NumPy-Discussion
mailing list