[Numpy-discussion] non-linear array manipulation

Anne Archibald peridot.faceted at gmail.com
Tue Aug 12 16:28:00 EDT 2008


2008/8/12 Nadav Horesh <nadavh at visionsense.com>:
> from numpy import *
>
> a = sqrt(maximum(0, a**2-repeat(b*c, columns).reshape(columns, rows)))

better:

import numpy as np

a = np.sqrt(np.maximum(0.0, a**2-(b*c)[np.newaxis,:]))

This doesn't have to make a temporary the size of a; instead
broadcasting rules are used to treat b*c as if it were the same shape
as a.

Also, "from numpy import *" is a good way to shoot yourself in the
foot. At least "from numpy import sqrt, maximum, repeat" would have
been safer.

Anne



More information about the NumPy-Discussion mailing list