Added the Minkowski distance function#10143
Added the Minkowski distance function#10143tianyizheng02 merged 3 commits intoTheAlgorithms:masterfrom
Conversation
maths/minkowski_distance.py
Outdated
| raise Exception("Both points must have the same dimension.") | ||
|
|
||
| return float( | ||
| sum(abs(a - b) ** order for a, b in zip(point_a, point_b)) ** (1 / order) |
There was a problem hiding this comment.
I'm concerned that the ** (1 / order) could produce inaccuracies in the output. For example, 125 ** (1/3) returns 4.999... instead of 5 because of floating-point errors. I don't know of any better alternative, but I think it's worth looking into.
There was a problem hiding this comment.
Yeah, I can't find a good solution for this either. I've tried using Decimal, pow, numpy but I'm getting the same result as you. I think adding in some sort of approximation scheme like Newton's method would unnecessarily complicate and detract from the main thrust of this algorithm. What do you think?
There was a problem hiding this comment.
In order to not overcomplicate the code, I think it should be fine to keep it as is. We should leave a note in the comments acknowledging this issue so that future contributors know about it, however. For doctests, we could use np.isclose() to compare doctest outputs to the true, mathematical values.
Describe your change:
Checklist: