Skip to content
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

Euclidean distance #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts)
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts)
* [Double Factorial Iterative](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/double_factorial_iterative.ts)
* [Euclidean Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/euclidean_distance.ts)
* [Euler Totient](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/euler_totient.ts)
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
Expand Down
25 changes: 25 additions & 0 deletions maths/euclidean_distance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @description Calculate the Euclidean distance between two vectors.
* @param {number[]} vectorA - The first vector.
* @param {number[]} vectorB - The second vector.
* @return {number} - The Euclidean distance between the two vectors.
* @throws Will throw an error if the vectors do not have the same dimensions.
* @see https://en.wikipedia.org/wiki/Euclidean_distance
* @example euclideanDistance([0, 0], [3, 4]) = 5
* @example euclideanDistance([1, 2, 3], [4, 5, 6]) = 5.196
*/
export const euclideanDistance = (
vectorA: number[],
vectorB: number[]
): number => {
if (vectorA.length !== vectorB.length) {
throw new Error('Vectors must have the same dimensions')
}

let sum: number = 0
for (let index: number = 0; index < vectorA.length; index++) {
sum += (vectorA[index] - vectorB[index]) ** 2
}

return Math.sqrt(sum)
}
43 changes: 43 additions & 0 deletions maths/test/euclidean_distance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { euclideanDistance } from '../euclidean_distance'

describe('Euclidean distance', () => {
test.each([
[[0, 0], [0, 0], 0],
[[1, 0], [0, 0], 1],
[[0, 1], [0, 0], 1],
[[3, 4], [0, 0], 5],
[[1, 2, 3], [4, 5, 6], 5.196152422706632],
[[-1, -1], [1, 1], Math.sqrt(8)],
[[2, 3], [2, 3], 0]
])('between %p and %p should be %f', (point1, point2, expected) => {
expect(euclideanDistance(point1, point2)).toBeCloseTo(expected)
})

test.each([
[[0, 0], [0]],
[[1], [1, 2]],
[
[1, 2, 3],
[4, 5]
]
])(
'should throw an error for mismatched dimensions %p and %p',
(point1, point2) => {
expect(() => euclideanDistance(point1, point2)).toThrowError(
'Vectors must have the same dimensions'
)
}
)

test('should handle empty vectors', () => {
expect(euclideanDistance([], [])).toBe(0)
})

test('should handle large vectors', () => {
const largeVector1 = Array(1000).fill(1)
const largeVector2 = Array(1000).fill(2)
expect(euclideanDistance(largeVector1, largeVector2)).toBeCloseTo(
Math.sqrt(1000)
)
})
})