Skip to main content
Skip to main content

Distance Functions

L1Norm​

Calculates the sum of absolute values of a vector.

Syntax

L1Norm(vector)

Alias: normL1.

Arguments

Returned value

Examples

Query:

SELECT L1Norm((1, 2));

Result:

β”Œβ”€L1Norm((1, 2))─┐
β”‚ 3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L2Norm​

Calculates the square root of the sum of the squares of the vector values.

Syntax

L2Norm(vector)

Alias: normL2.

Arguments

Returned value

Example

Query:

SELECT L2Norm((1, 2));

Result:

β”Œβ”€β”€β”€L2Norm((1, 2))─┐
β”‚ 2.23606797749979 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L2SquaredNorm​

Calculates the square root of the sum of the squares of the vector values (the L2Norm) squared.

Syntax

L2SquaredNorm(vector)

Alias: normL2Squared.

*Arguments

Returned value

  • L2-norm squared. Float.

Example

Query:

SELECT L2SquaredNorm((1, 2));

Result:

β”Œβ”€L2SquaredNorm((1, 2))─┐
β”‚ 5 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LinfNorm​

Calculates the maximum of absolute values of a vector.

Syntax

LinfNorm(vector)

Alias: normLinf.

Arguments

Returned value

  • Linf-norm or the maximum absolute value. Float.

Example

Query:

SELECT LinfNorm((1, -2));

Result:

β”Œβ”€LinfNorm((1, -2))─┐
β”‚ 2 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LpNorm​

Calculates the root of p-th power of the sum of the absolute values of a vector in the power of p.

Syntax

LpNorm(vector, p)

Alias: normLp.

Arguments

  • vector β€” Tuple or Array.
  • p β€” The power. Possible values: real number in [1; inf). UInt or Float.

Returned value

Example

Query:

SELECT LpNorm((1, -2), 2);

Result:

β”Œβ”€LpNorm((1, -2), 2)─┐
β”‚ 2.23606797749979 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L1Distance​

Calculates the distance between two points (the values of the vectors are the coordinates) in L1 space (1-norm (taxicab geometry distance)).

Syntax

L1Distance(vector1, vector2)

Alias: distanceL1.

Arguments

Returned value

  • 1-norm distance. Float.

Example

Query:

SELECT L1Distance((1, 2), (2, 3));

Result:

β”Œβ”€L1Distance((1, 2), (2, 3))─┐
β”‚ 2 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L2Distance​

Calculates the distance between two points (the values of the vectors are the coordinates) in Euclidean space (Euclidean distance).

Syntax

L2Distance(vector1, vector2)

Alias: distanceL2.

Arguments

Returned value

  • 2-norm distance. Float.

Example

Query:

SELECT L2Distance((1, 2), (2, 3));

Result:

β”Œβ”€L2Distance((1, 2), (2, 3))─┐
β”‚ 1.4142135623730951 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L2SquaredDistance​

Calculates the sum of the squares of the difference between the corresponding elements of two vectors.

Syntax

L2SquaredDistance(vector1, vector2)

Alias: distanceL2Squared.

Arguments

Returned value

  • Sum of the squares of the difference between the corresponding elements of two vectors. Float.

Example

Query:

SELECT L2SquaredDistance([1, 2, 3], [0, 0, 0])

Result:

β”Œβ”€L2SquaredDistance([1, 2, 3], [0, 0, 0])─┐
β”‚ 14 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LinfDistance​

Calculates the distance between two points (the values of the vectors are the coordinates) in L_{inf} space (maximum norm).

Syntax

LinfDistance(vector1, vector2)

Alias: distanceLinf.

Arguments

Returned value

  • Infinity-norm distance. Float.

Example

Query:

SELECT LinfDistance((1, 2), (2, 3));

Result:

β”Œβ”€LinfDistance((1, 2), (2, 3))─┐
β”‚ 1 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LpDistance​

Calculates the distance between two points (the values of the vectors are the coordinates) in Lp space (p-norm distance).

Syntax

LpDistance(vector1, vector2, p)

Alias: distanceLp.

Arguments

  • vector1 β€” First vector. Tuple or Array.
  • vector2 β€” Second vector. Tuple or Array.
  • p β€” The power. Possible values: real number from [1; inf). UInt or Float.

Returned value

  • p-norm distance. Float.

Example

Query:

SELECT LpDistance((1, 2), (2, 3), 3);

Result:

β”Œβ”€LpDistance((1, 2), (2, 3), 3)─┐
β”‚ 1.2599210498948732 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L1Normalize​

Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in L1 space (taxicab geometry).

Syntax

L1Normalize(tuple)

Alias: normalizeL1.

Arguments

Returned value

Example

Query:

SELECT L1Normalize((1, 2));

Result:

β”Œβ”€L1Normalize((1, 2))─────────────────────┐
β”‚ (0.3333333333333333,0.6666666666666666) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

L2Normalize​

Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in Euclidean space (using Euclidean distance).

Syntax

L2Normalize(tuple)

Alias: normalizeL1.

Arguments

Returned value

Example

Query:

SELECT L2Normalize((3, 4));

Result:

β”Œβ”€L2Normalize((3, 4))─┐
β”‚ (0.6,0.8) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LinfNormalize​

Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in L_{inf} space (using maximum norm).

Syntax

LinfNormalize(tuple)

Alias: normalizeLinf .

Arguments

Returned value

Example

Query:

SELECT LinfNormalize((3, 4));

Result:

β”Œβ”€LinfNormalize((3, 4))─┐
β”‚ (0.75,1) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LpNormalize​

Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in Lp space (using p-norm).

Syntax

LpNormalize(tuple, p)

Alias: normalizeLp .

Arguments

  • tuple β€” Tuple.
  • p β€” The power. Possible values: any number from [1;inf). UInt or Float.

Returned value

Example

Query:

SELECT LpNormalize((3, 4),5);

Result:

β”Œβ”€LpNormalize((3, 4), 5)──────────────────┐
β”‚ (0.7187302630182624,0.9583070173576831) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

cosineDistance​

Calculates the cosine distance between two vectors (the values of the tuples are the coordinates). The smaller the returned value is, the more similar are the vectors.

Syntax

cosineDistance(vector1, vector2)

Arguments

Returned value

  • Cosine of the angle between two vectors subtracted from one. Float.

Examples

Query:

SELECT cosineDistance((1, 2), (2, 3));

Result:

β”Œβ”€cosineDistance((1, 2), (2, 3))─┐
β”‚ 0.007722123286332261 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜