[Numpy-discussion] speed of numpy.ndarray compared to Numeric.array

René Dudfield renesd at gmail.com
Mon Jan 10 11:23:05 EST 2011


Hi,

Spatial hashes are the common solution.

Another common optimization is using the distance squared for
collision detection.  Since you do not need the expensive sqrt for
this calc.

cu.



On Mon, Jan 10, 2011 at 3:25 PM, Pascal <pascal22p at parois.net> wrote:
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list