[Numpy-discussion] Distance Formula on an Array

josef.pktd@gmai... josef.pktd@gmai...
Sat Apr 25 17:01:21 CDT 2009


On Sat, Apr 25, 2009 at 4:38 PM, Ian Mallett <geometrian@gmail.com> wrote:
> Oops, one more thing.  In reference to:
> vec = array([[0,0,0],[0,1,0],[0,0,3]])
> pos = array([0,4,0])
> sqrt(((vec - pos)**2).sum(1)) -> array([ 4.,  3.,  5.])
>
> Can I make "vec" an array of class instances?  I tried:
> class c:
>     def __init__(self):
>         self.position = [0,0,0]
> vec = array([c(),c(),c()])
> pos = array([0,4,0])
> sqrt(((vec.position - pos)**2).sum(1))
>
> Which doesn't work.  I'm not familiar with class objects in arrays--how
> should they be referenced?


I never work with object arrays. I think they will kill your
performance if you need fast calculation for many positions.

list comprehension works:
print [sqrt(((v.position - pos)**2).sum()) for v in vec]

I didn't find any other way either.

Josef


More information about the Numpy-discussion mailing list