Best way to compute length of arbitrary dimension vector?

Robert Kern robert.kern at gmail.com
Fri Jun 3 19:12:05 EDT 2011


On 6/3/11 4:53 PM, Gabriel wrote:
>
>> The dimension is arbitrary, though, so:
>>
>> length = reduce(math.hypot, self._coords, 0)
>>
>
>
> Thanks, I was going to ask Algis that same question.
>
> But still, is this solution really faster or better than the one using
> list comprehension and the expression 'x*x'?
> It seems to me that the above solution (using hypot) involves repeated
> square roots (with subsequent squaring).

It also means that the floating point numbers stay roughly the same size, so you 
will lose less precision as the number of elements goes up. I don't expect the 
number of elements will be large enough to matter, though.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list