[Numpy-discussion] How to get rid of the loop?

josef.pktd at gmail.com josef.pktd at gmail.com
Sat Nov 7 13:57:14 EST 2009


On Sat, Nov 7, 2009 at 1:51 PM, Stas K <stanconn at gmail.com> wrote:
> Can I get rid of the loop in this example? And what is the fastest way
> to get v in the example?
>
> ar = array([1,2,3])
> for a in ar:
>    for b in ar:
>        v = a**2+b**2

>>> ar[:,None]**2 + ar**2
array([[ 2,  5, 10],
       [ 5,  8, 13],
       [10, 13, 18]])

I think, for this case there is also directly a function in numpy
hypot which should also work.

Josef
> _______________________________________________
> 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