Normalizing A Vector
Matteo Landi
landimatte at gmail.com
Mon Aug 2 01:56:14 CEST 2010
On Mon, Aug 2, 2010 at 1:50 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 7/30/2010 7:46 AM, Lawrence D'Oliveiro 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.
>
> So, instead of fooling around with error-prone, hard-to-type constructions,
> just delete the damn thing!
>
> def L
del L
:P
>
> Compare those foolproof 5 chars to what at first did not work right and even
> what did. And, as other said, in most real applications, you will normalize
> in more than one place. In fact, you may well want a vlen function.
>
> def vlen(seq): return math.sqrt(sum(x*x for x in seq))
>
> --
> Terry Jan Reedy
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Matteo Landi
http://www.matteolandi.net/
More information about the Python-list
mailing list