Normalizing A Vector
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
>> it (scale all components by the same factor) so its magnitude is 1.
>> The usual way is something like this:
>> L = math.sqrt(V * V + V * V + V * V)
>> V = (V / L, V / L, V / L)
>> What I don’t like is having that intermediate variable L leftover after
> So, instead of fooling around with error-prone, hard-to-type constructions,
> just delete the damn thing!
> def L
> 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
More information about the Python-list