New submission from Raymond Hettinger <raymond.hettin...@gmail.com>:

A need for a distance-between-two-points function arises frequently enough to 
warrant consideration for inclusion in the math module.   It shows-up 
throughout mathematics -- everywhere from simple homework problems for kids to 
machine learning and computer vision.

In the latter cases, the function is called frequently and would benefit from a 
fast C implementation that includes good error checking and is algorithmically 
smart about numerical issues such as overflow and loss-of-precision.

A simple implementation would be something like this:

    def dist(p, q):
        'Multi-dimensional Euclidean distance'
        # XXX needs error checking:  len(p) == len(q)
        return sqrt(sum((x0 - x1) ** 2 for x0, x1 in zip(p, q)))

The implementation could also include value added features such as hypot() 
style scaling to mitigate overflow during the squaring step:

    def dist2(p, q):
        # https://en.wikipedia.org/wiki/Hypot#Implementation
        diffs = [x0 - x1 for x0, x1 in zip(p, q)]
        scale = max(diffs, key=abs)
        return abs(scale) * sqrt(fsum((d/scale) ** 2 for d in diffs))

----------
components: Library (Lib)
messages: 313967
nosy: mark.dickinson, rhettinger, skrah, steven.daprano, tim.peters
priority: normal
severity: normal
status: open
title: Add multi-dimensional Euclidean distance function to the math module
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33089>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to