[New-bugs-announce] [issue1880] Generalize math.hypot function

David W. Lambert report at bugs.python.org
Mon Jan 21 03:58:52 CET 2008


New submission from David W. Lambert:

Please generalize math.hypot.  While I don't have a survey of python 
codes, it seems to me unlikely for this change to break existing 
programs.

import math

def hypot(*args):
    '''
        Return the Euclidean vector length.
        >>> from math import hypot, sqrt
        >>> hypot(5,12)    # traditional definition
        13.0
        >>> hypot()
        0.0
        >>> hypot(-6.25)
        6.25
        >>> hypot(1,1,1) == sqrt(3) # diagonal of unit box
        True
    '''
    return math.sqrt(sum(arg*arg for arg in args))


I propose this version as closest to:
>>> print sys.version
2.5.1 (r251:54863, Jan  4 2008, 17:15:14) 
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)]
>>> print math.hypot.__doc__
hypot(x,y)

Return the Euclidean distance, sqrt(x*x + y*y).

Thanks,
Dave.

PS.  I don't understand why python is so restrictive.  Although hypot 
is in the math library, it could be written in EAFP style as

def hypot(*args):
    return sum(arg*arg for arg in args)**0.5

Rather than review the entire python library for items to generalize, 
I'll accept that the resulting errors would confuse "the penguin on my 
tele".  "hypot" crosses me most often.  I've not yet needed a version 
in the complex domain, such as my second version.

I typically fill my need for length with scipy.sqrt(scipy.dot(v,v)), 
only to realize that for the short vectors I use, standard python 
constructs always perform faster than scipy

----------
components: Library (Lib)
messages: 61374
nosy: LambertDW
severity: minor
status: open
title: Generalize math.hypot function
type: rfe
versions: Python 3.0

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1880>
__________________________________


More information about the New-bugs-announce mailing list