On 30 Jul, 13:46, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:

> Say a vector V is a tuple of 3 numbers, not all zero. You want to normalize
> it (scale all components by the same factor) so its magnitude is 1.
>
> The usual way is something like this:
>
>     L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2])
>     V = (V[0] / L, V[1] / L, V[2] / L)
>
> What I don’t like is having that intermediate variable L leftover after the
> computation.

    L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2])
    V = (V[0] / L, V[1] / L, V[2] / L)
    del L

But this is the kind of programming tasks where NumPy is nice:

    V[:] = V / np.sqrt((V**2).sum())


Sturla


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to