Skip to main content
Skip to main content

Distance functions

L1Distance

Introduced in: v21.11

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)

Arguments

Returned value

Returns the 1-norm distance. UInt32 or Float64

Examples

Basic usage

SELECT L1Distance((1, 2), (2, 3))
┌─L1Distance((1, 2), (2, 3))─┐
│                          2 │
└────────────────────────────┘

L1Norm

Introduced in: v21.11

Calculates the sum of absolute values of a vector.

Syntax

L1Norm(vector)

Arguments

Returned value

Returns the L1-norm or taxicab geometry distance. UInt* or Float* or Decimal

Examples

Basic usage

SELECT L1Norm((1, 2))
┌─L1Norm((1, 2))─┐
│              3 │
└────────────────┘

L1Normalize

Introduced in: v21.11

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

Syntax

L1Normalize(tuple)

Arguments

  • tuple — A tuple of numeric values. Tuple(T)

Returned value

Returns the unit vector. Tuple(Float64)

Examples

Basic usage

SELECT L1Normalize((1, 2))
┌─L1Normalize((1, 2))─────────────────────┐
│ (0.3333333333333333,0.6666666666666666) │
└─────────────────────────────────────────┘

L2Distance

Introduced in: v21.11

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

Syntax

L2Distance(vector1, vector2)

Arguments

Returned value

Returns the 2-norm distance. Float64

Examples

Basic usage

SELECT L2Distance((1, 2), (2, 3))
┌─L2Distance((1, 2), (2, 3))─┐
│         1.4142135623730951 │
└────────────────────────────┘

L2Norm

Introduced in: v21.11

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

Syntax

L2Norm(vector)

Arguments

Returned value

Returns the L2-norm or Euclidean distance. UInt* or Float*

Examples

Basic usage

SELECT L2Norm((1, 2))
┌───L2Norm((1, 2))─┐
│ 2.23606797749979 │
└──────────────────┘

L2Normalize

Introduced in: v21.11

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)

Arguments

  • tuple — A tuple of numeric values. Tuple(T)

Returned value

Returns the unit vector. Tuple(Float64)

Examples

Basic usage

SELECT L2Normalize((3, 4))
┌─L2Normalize((3, 4))─┐
│ (0.6,0.8)           │
└─────────────────────┘

L2SquaredDistance

Introduced in: v22.7

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

Syntax

L2SquaredDistance(vector1, vector2)

Arguments

Returned value

Returns the sum of the squares of the difference between the corresponding elements of two vectors. Float64

Examples

Basic usage

SELECT L2SquaredDistance([1, 2, 3], [0, 0, 0])
┌─L2SquaredDis⋯ [0, 0, 0])─┐
│                       14 │
└──────────────────────────┘

L2SquaredNorm

Introduced in: v22.7

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

Syntax

L2SquaredNorm(vector)

Arguments

Returned value

Returns the L2-norm squared. UInt* or Float* or Decimal

Examples

Basic usage

SELECT L2SquaredNorm((1, 2))
┌─L2SquaredNorm((1, 2))─┐
│                     5 │
└───────────────────────┘

LinfDistance

Introduced in: v21.11

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

Syntax

LinfDistance(vector1, vector2)

Arguments

Returned value

Returns the Infinity-norm distance. Float64

Examples

Basic usage

SELECT LinfDistance((1, 2), (2, 3))
┌─LinfDistance((1, 2), (2, 3))─┐
│                            1 │
└──────────────────────────────┘

LinfNorm

Introduced in: v21.11

Calculates the maximum of absolute values of a vector.

Syntax

LinfNorm(vector)

Arguments

Returned value

Returns the Linf-norm or the maximum absolute value. Float64

Examples

Basic usage

SELECT LinfNorm((1, -2))
┌─LinfNorm((1, -2))─┐
│                 2 │
└───────────────────┘

LinfNormalize

Introduced in: v21.11

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)

Arguments

  • tuple — A tuple of numeric values. Tuple(T)

Returned value

Returns the unit vector. Tuple(Float64)

Examples

Basic usage

SELECT LinfNormalize((3, 4))
┌─LinfNormalize((3, 4))─┐
│ (0.75,1)              │
└───────────────────────┘

LpDistance

Introduced in: v21.11

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)

Arguments

Returned value

Returns the p-norm distance. Float64

Examples

Basic usage

SELECT LpDistance((1, 2), (2, 3), 3)
┌─LpDistance((1, 2), (2, 3), 3)─┐
│            1.2599210498948732 │
└───────────────────────────────┘

LpNorm

Introduced in: v21.11

Calculates the p-norm of a vector, which is the p-th root of the sum of the p-th powers of the absolute values of its elements.

Special cases:

  • When p=1, it's equivalent to L1Norm (Manhattan distance).
  • When p=2, it's equivalent to L2Norm (Euclidean distance).
  • When p=∞, it's equivalent to LinfNorm (maximum norm).

Syntax

LpNorm(vector, p)

Arguments

  • vector — Vector or tuple of numeric values. Tuple(T) or Array(T)
  • p — The power. Possible values are real numbers in the range [1; inf). UInt* or Float*

Returned value

Returns the Lp-norm. Float64

Examples

Basic usage

SELECT LpNorm((1, -2), 2)
┌─LpNorm((1, -2), 2)─┐
│   2.23606797749979 │
└────────────────────┘

LpNormalize

Introduced in: v21.11

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)

Arguments

  • tuple — A tuple of numeric values. Tuple(T)
  • p — The power. Possible values are any number in the range range from [1; inf). UInt* or Float*

Returned value

Returns the unit vector. Tuple(Float64)

Examples

Basic usage

SELECT LpNormalize((3, 4), 5)
┌─LpNormalize((3, 4), 5)──────────────────┐
│ (0.7187302630182624,0.9583070173576831) │
└─────────────────────────────────────────┘

cosineDistance

Introduced in: v1.1

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

Returns the cosine of the angle between two vectors subtracted from one. Float64

Examples

Basic usage

SELECT cosineDistance((1, 2), (2, 3));
┌─cosineDistance((1, 2), (2, 3))─┐
│           0.007722123286332261 │
└────────────────────────────────┘