[Numpy-discussion] distance matrix speed

David Douard david.douard at logilab.fr
Fri Jun 16 03:53:37 EDT 2006


Hi,

On Fri, Jun 16, 2006 at 08:28:18AM +0200, Johannes Loehnert wrote:
> Hi,
> 
> def dtest():
>     A = random( [4,2])
>     B = random( [1000,2])
> 
>     # drawback: memory usage temporarily doubled
>     # solution see below
>     d = A[:, newaxis, :] - B[newaxis, :, :]

Unless I'm wrong, one can simplify a (very) little bit this line: 
      d = A[:, newaxis, :] - B

>     # written as 3 expressions for more clarity
>     d = sqrt((d**2).sum(axis=2))
>     return d
> 



-- 
David Douard                             LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian : http://www.logilab.fr/formations
Développement logiciel sur mesure :      http://www.logilab.fr/services
Informatique scientifique :              http://www.logilab.fr/science
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060616/ea6ff71e/attachment-0001.sig>


More information about the NumPy-Discussion mailing list