From aa1e1ae7049fafdb29212e3e08907112fb9dd59b Mon Sep 17 00:00:00 2001 From: Kausthub Kannan Date: Tue, 3 Oct 2023 17:00:11 +0000 Subject: [PATCH 1/3] Added Euclidean Distance --- Maths/EuclideanDistance.js | 13 +++++++++++++ Maths/test/EuclideanDistance.test.js | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Maths/EuclideanDistance.js create mode 100644 Maths/test/EuclideanDistance.test.js diff --git a/Maths/EuclideanDistance.js b/Maths/EuclideanDistance.js new file mode 100644 index 0000000000..adf65967da --- /dev/null +++ b/Maths/EuclideanDistance.js @@ -0,0 +1,13 @@ +// Wikipedia: https://en.wikipedia.org/wiki/Euclidean_distance + +const EuclideanDistance = (vector1, vector2) => { + let sumOfSquares = 0 + + for (let i = 0; i < vector1.length; i++) { + sumOfSquares += Math.pow(vector1[i] - vector2[i], 2) + } + + return Math.sqrt(sumOfSquares) +} + +export { EuclideanDistance } diff --git a/Maths/test/EuclideanDistance.test.js b/Maths/test/EuclideanDistance.test.js new file mode 100644 index 0000000000..d73bb03875 --- /dev/null +++ b/Maths/test/EuclideanDistance.test.js @@ -0,0 +1,19 @@ +import { EuclideanDistance } from '../EuclideanDistance.js' + +describe('EuclideanDistance', () => { + it('should calculate the distance correctly for 2D vectors', () => { + expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(2.8284271247461903, 10) + }) + + it('should calculate the distance correctly for 3D vectors', () => { + expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(3.4641016151377544, 10) + }) + + it('should calculate the distance correctly for 4D vectors', () => { + expect(EuclideanDistance([1, 2, 3, 4], [5, 6, 7, 8])).toBeCloseTo(8.0, 10) + }) + + it('should calculate the distance correctly for different 2D vectors', () => { + expect(EuclideanDistance([1, 2], [4, 6])).toBeCloseTo(5.0, 10) + }) +}) From d22c036c167b050bf3d9ddc0ff16abba2caa3355 Mon Sep 17 00:00:00 2001 From: Kausthub Kannan Date: Wed, 4 Oct 2023 12:45:37 +0000 Subject: [PATCH 2/3] Added documentation to params --- Maths/EuclideanDistance.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Maths/EuclideanDistance.js b/Maths/EuclideanDistance.js index adf65967da..714f2eb690 100644 --- a/Maths/EuclideanDistance.js +++ b/Maths/EuclideanDistance.js @@ -1,4 +1,10 @@ -// Wikipedia: https://en.wikipedia.org/wiki/Euclidean_distance +/** + * Wikipedia: https://en.wikipedia.org/wiki/Euclidean_distance + * Calculate the Euclidean distance between two vectors. + * @param {number[]} vector1 - The first vector. + * @param {number[]} vector2 - The second vector. + * @returns {number} The Euclidean distance between the two vectors. + */ const EuclideanDistance = (vector1, vector2) => { let sumOfSquares = 0 From 45b7683044fbdc29a0f5fbb7f11bd624ff1598be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:48:33 +0200 Subject: [PATCH 3/3] Use @see annotation --- Maths/EuclideanDistance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/EuclideanDistance.js b/Maths/EuclideanDistance.js index 714f2eb690..0cded84ebb 100644 --- a/Maths/EuclideanDistance.js +++ b/Maths/EuclideanDistance.js @@ -1,5 +1,5 @@ /** - * Wikipedia: https://en.wikipedia.org/wiki/Euclidean_distance + * @see [Wikipedia](https://en.wikipedia.org/wiki/Euclidean_distance) * Calculate the Euclidean distance between two vectors. * @param {number[]} vector1 - The first vector. * @param {number[]} vector2 - The second vector. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy