-
Notifications
You must be signed in to change notification settings - Fork 1.3k
add Minkowski Distance #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| (function (exports) { | ||
| 'use strict'; | ||
|
|
||
| var minkowskiDistance = (function () { | ||
|
|
||
| function chebyshevDistance (x, y, lx, p, mathfn) { | ||
| var ret = -p; | ||
| var i; | ||
|
|
||
| for (i = 0; i < lx; i += 1) { | ||
| ret = mathfn(ret, Math.abs(x[i] - y[i])); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| function minkowskiDistance (x, lx, y, ly, p) { | ||
| var d; | ||
| var i; | ||
|
|
||
| if (lx !== ly) { | ||
| throw 'Both vectors should have same dimension'; | ||
| } | ||
|
|
||
| if (isNaN(p)) { | ||
| throw 'The order "p" must be a number'; | ||
| } | ||
|
|
||
| if (p === Number.POSITIVE_INFINITY) { | ||
| return chebyshevDistance(x, y, lx, p, Math.max); | ||
| } else if (p === Number.NEGATIVE_INFINITY) { | ||
| return chebyshevDistance(x, y, lx, p, Math.min); | ||
| } else if (p < 1) { | ||
| throw 'Order less than 1 will violate the triangle inequality'; | ||
| } else { | ||
| d = 0; | ||
|
|
||
| for (i = 0; i < lx; i += 1) { | ||
| d += Math.pow(Math.abs(x[i] - y[i]), p); | ||
| } | ||
|
|
||
| return isNaN(d) | ||
| ? 0 | ||
| : Math.pow(d, 1 / p); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * The Minkowski distance between two points gets generalized | ||
| * metric distance | ||
| * when p === 1, this becomes same as Manhattan Distance | ||
| * when p === 2, this becomes same as Euclidean Distance | ||
| * when p === Positive or Negative Infinity, | ||
| * this becomes chebyshev distance | ||
| * | ||
| * @public | ||
| * @module others/minkowski-distance | ||
| * | ||
| * @example | ||
| * var dist = require('path-to-algorithms/src/others/' + | ||
| * 'minkowski-distance').minkowskiDistance; | ||
| * console.log(dist([0, 1], [1, 1], 2)); // 1 | ||
| * | ||
| * @param {Array} x source point | ||
| * @param {Array} y target point | ||
| * @param {Number} p order of Minkowski distance | ||
| * @returns {Number} distance between two points, if distance | ||
| * is NaN, then this returns 0 | ||
| */ | ||
| return function (x, y, p) { | ||
| return minkowskiDistance (x, x.length, y, y.length, p); | ||
| }; | ||
| }()); | ||
|
|
||
| exports.minkowskiDistance = minkowskiDistance; | ||
|
|
||
| }(typeof exports === 'undefined' ? window : exports)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 'use strict'; | ||
|
|
||
| var mod = require('../../src/others/minkowski-distance.js'); | ||
| var minkowskiDistance = mod.minkowskiDistance; | ||
|
|
||
| describe('Minkowski Distance', function () { | ||
| it('should return 1 with points (0, 1), (1, 1) in order 1.', function () { | ||
| expect(minkowskiDistance([0, 1], [1, 1], 1)).toBe(1); | ||
| }); | ||
| it('should return 2 with points (0, 1), (1, 1) in order 2.', function () { | ||
| expect(minkowskiDistance([0, 1], [1, 1], 2)).toBe(1); | ||
| }); | ||
| it('should return 2 with points (0, 1, 4), (1, 1, 6) in order Positive Infinity.', function () { | ||
| expect(minkowskiDistance([0, 1, 4], [1, 1, 6], Number.POSITIVE_INFINITY)).toBe(2); | ||
| }); | ||
| it('should return 0 with points (0, 1, 4), (1, 1, 6) in order Negative Infinity.', function () { | ||
| expect(minkowskiDistance([0, 1, 4], [1, 1, 6], Number.NEGATIVE_INFINITY)).toBe(0); | ||
| }); | ||
| it('should return 8.372966759705923 with points (0, 3, 4, 5), (7, 6, 3, -1) in order 3.', function () { | ||
| expect(minkowskiDistance([0, 3, 4, 5], [7, 6, 3, -1], 3)).toBe(8.372966759705923); | ||
| }); | ||
| it('should throw when both vectors don\'t have same dimension', function () { | ||
| expect(function () { | ||
| minkowskiDistance([1, 2], [1], 1) | ||
| }).toThrow('Both vectors should have same dimension'); | ||
| }); | ||
| it('should throw when p is not defined', function () { | ||
| expect(function () { | ||
| minkowskiDistance([1, 2], [1, 2]) | ||
| }).toThrow('The order "p" must be a number'); | ||
| }); | ||
| it('should throw when p is not a number', function () { | ||
| expect(function () { | ||
| minkowskiDistance([1, 2], [1, 2], NaN) | ||
| }).toThrow('The order "p" must be a number'); | ||
| }); | ||
| it('should throw when p is less than 1', function () { | ||
| expect(function () { | ||
| minkowskiDistance([1, 2], [1, 2], 0) | ||
| }).toThrow('Order less than 1 will violate the triangle inequality'); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description doesn't match the content.