Distancia v0.1.3 Distancia View Source

Distancia is a module which provides functions that calculate distances between two points.

It allows to perform calulations in various metrics:

Link to this section Summary

Types

Point can be represented as a list, a tuple, a number or a string

Functions

Calculates the Chebyshev distance between points p and q

Calculates the Euclidean distance between points p and q

Calculates the Hamming distance between strings p and q

Calculates the Manhattan distance between points p and q

Link to this section Types

Link to this type point() View Source
point() :: list() | tuple() | number() | String.t()

Point can be represented as a list, a tuple, a number or a string.

Link to this section Functions

Link to this function chebyshev(p, q) View Source
chebyshev(point(), point()) :: number()

Calculates the Chebyshev distance between points p and q.

Simply speaking, the Chebyshev distance is equal to the minimum number of moves needed by a king to go from one point to another in the game of chess.

Examples

iex> Distancia.chebyshev(1, 2)
1
iex> Distancia.chebyshev([2, -1, 6], [8, 5, -4])
10
iex> Distancia.chebyshev({1.34, 2}, {-3.13, 5.3})
4.47
Link to this function euclidean(p, q) View Source
euclidean(point(), point()) :: number()

Calculates the Euclidean distance between points p and q.

The Euclidean distance is the most common understanding of distance - a straight line between two points in the Cartesian plane.

Examples

iex> Distancia.euclidean(1, 2)
1.0
iex> Distancia.euclidean([2, -1, 6], [8, 5, -4])
13.114877048604
iex> Distancia.euclidean({1.34, 2}, {-3.13, 5.3})
5.556158745032398
Link to this function hamming(p, q) View Source
hamming(point(), point()) :: number()

Calculates the Hamming distance between strings p and q.

The Hamming distance is the amount of elements which differ between two strings.

Examples

iex> Distancia.hamming(1, 2)
1
iex> Distancia.hamming("This is Sparta!", "Thiz iz Sparta!")
2
iex> Distancia.hamming("2173896", "2233796")
3
Link to this function manhattan(p, q) View Source
manhattan(point(), point()) :: number()

Calculates the Manhattan distance between points p and q.

The Manhattan distance is a sum of all horizontal and vertical segments between two points in a grid.

Examples

iex> Distancia.manhattan(1, 2)
1
iex> Distancia.manhattan([2, -1, 6], [8, 5, -4])
22
iex> Distancia.manhattan({1.34, 2}, {-3.13, 5.3})
7.77