All Products
Search
Document Center

Simple Log Service:Vector calculation functions

Last Updated:May 06, 2024

You can save text, audio, images, and videos in vector databases as vectors to facilitate search and query. Simple Log Service supports vector calculation functions. This topic describes the syntax of vector calculation functions. This topic also provides examples on how to use the functions.

Vector calculation functions

Important

The vector elements that are used in the following functions are displayed as strings in a specific format. A string is a set of numbers that are enclosed in brackets [] or parentheses () and are separated with commas (,).

Function

Syntax

Description

Supported in SQL

Supported in SPL

cosine_similarity function

  • cosine_similarity(array(double) vector1, array(double) vector2)

  • cosine_similarity(varchar vector1, varchar vector2)

Calculates the cosine similarity of two vectors. Valid values of the cosine similarity: [-1, 1].

  • If the value of the cosine similarity between two vectors is 1, the vectors are in the same direction.

  • If the value of the cosine similarity between two vectors is -1, the vectors are in the opposite directions.

  • If the value of the cosine similarity between two vectors is 0, one vector is vertical to the other vector.

×

inner_product or dot_product function

  • inner_product(array(double) vector1, array(double) vector2)

  • inner_product(varchar vector1, varchar vector2)

  • dot_product(array(double) vector1, array(double) vector2)

Calculates the inner product of two vectors.

×

l2_distance or euclidean_distance function

  • euclidean_distance(array(double) vector1, array(double) vector2)

  • euclidean_distance(varchar vector1, varchar vector2)

  • l2_distance(array(double) vector1, array(double) vector2)

  • l2_distance(varchar vector1, varchar vector2)

Calculates the Euclidean distance between two vectors.

×

vector_norm function

  • vector_norm(array(double) vector1)

  • vector_norm(array(double) vector1, double p)

  • vector_norm(varchar vector1)

  • vector_norm(varchar vector1, double p)

Calculates the norm of a vector. The norm of a vector is equivalent to the magnitude of the vector.

×

vector_sum function

  • vector_sum(array(double) vector1)

  • vector_sum(varchar vector1)

Calculates the sum of all elements of a vector.

×

vector_mean function

  • vector_mean(array(double) vector1)

  • vector_mean(varchar vector1)

Calculates the mean of a vector.

×

vector_variance function

  • vector_variance(array(double) vector1)

  • vector_variance(varchar vector1)

Calculates the variance of a vector. Variance measures the dispersion of the elements of a vector. Variance is the average of the squared distances from each element of the vector to the mean.

×

vector_std_dev function

  • vector_std_dev(array(double) vector1)

  • vector_std_dev(varchar vector1)

Calculates the standard deviation of a vector. The standard deviation of a vector is the positive square root of its variance. Variance measures how far each data point in a dataset is from the mean. A high variance value indicates that the data points are significantly spread out from the mean. A low variance value indicates that the data points are close to the mean.

×

vector_add function

  • vector_add(array(double) vector1, array(double) vector2)

  • vector_add(varchar vector1, varchar vector2)

Calculates the sum of two vectors.

You can perform arbitrary linear transformation based on vector addition, vector subtraction, and vector scaling functions.

×

vector_subtract function

  • vector_subtract(array(double) vector1, array(double) vector2)

  • vector_subtract(varchar vector1, varchar vector2)

Calculates the difference between two vectors.

You can perform arbitrary linear transformation based on vector addition, vector subtraction, and vector scaling functions.

×

vector_scale function

  • vector_scale(array(double) vector1, double alpha)

  • vector_scale(varchar vector1, double alpha)

Calculates the result of vector scaling. You can perform arbitrary linear transformation based on vector addition, vector subtraction, and vector scaling functions.

×

cosine_similarity function

The cosine_similarity function calculates the cosine similarity of two vectors.

Examples

  • Query statement

    * | SELECT cosine_similarity(array[1, 2, 3, 4], array[10, 20, 30, 40])

    Or

    * | SELECT cosine_similarity('[1, 2, 3, 4]', '[10, 20, 30, 40]')
  • Query and analysis results

    1

Return value type

Same as the data type of the parameter value.

inner_product or dot_product function

The inner_product or dot_product function calculates the inner product of two vectors.

Examples

  • Query statement

    * | SELECT inner_product(array[1, 2, 3, 4], array[10, 20, 30, 40])

    Or

    * | SELECT inner_product('[1, 2, 3, 4]', '[10, 20, 30, 40]')
  • Query and analysis results

    300.0

Return value type

Same as the data type of the parameter value.

l2_distance or euclidean_distance function

Note

l2_distance starts with the lowercase letter l.

The l2_distance or euclidean_distance function calculates the Euclidean distance between two vectors.

Examples

  • Query statement

    * | SELECT l2_distance(array[1, 3], array[4, 2])

    Or

    * | SELECT l2_distance('[1, 3]', '[4, 2]')
  • Query and analysis results

    3.16

Return value type

Same as the data type of the parameter value.

vector_norm function

The vector_norm function calculates the norm of a vector based on the type of the norm. The type of a norm is specified by p.

Examples

  • Query statement

    * | SELECT vector_norm(array[1, 3], 1)
    * | SELECT vector_norm(array[1, 3], 2)
    * | SELECT vector_norm(array[1, 3], 3)
  • Query and analysis results

    The function calculates the norm of the [1,3] vector when the norm type is 1, 2, and 3. When the norm type is 1, the function calculates the sum for the absolute values of vector elements. The result is 4. When the norm type is 2, the function calculates the square root of the sum for squares of vector elements. The result is 3.16. When the norm type is 3, the function calculates the cube root of the sum for cubes of vector elements. The result is 3.04.

Return value type

Same as the data type of the parameter value.

vector_sum function

The vector_sum function calculates the sum of all elements of a vector.

Examples

  • Query statement

    * | SELECT vector_sum(array[1, -3])

    Or

    * | SELECT vector_sum('[1, -3]')
  • Query and analysis results

    -2

Return value type

Same as the data type of the parameter value.

vector_mean function

The vector_mean function calculates the mean of a vector.

Examples

  • Query statement

    * | SELECT vector_mean(array[1, -3])

    Or

    * | SELECT vector_mean('[1, -3]')
  • Query and analysis results

    -1

Return value type

Same as the data type of the parameter value.

vector_variance function

The vector_variance function calculates the variance of a vector. Variance measures the dispersion of the elements of a vector.

Examples

  • Query statement

    * | SELECT vector_variance(array[1, -3, 8])

    Or

    * | SELECT vector_variance('[1, -3, 8]')
  • Query and analysis results

    The mean of the vector is 2. The variance is calculated by using the following formula: (12 + 52 + 62 = 62/3) = 20.67.

Return value type

Same as the data type of the parameter value.

vector_std_dev function

The vector_std_dev function calculates the standard deviation of a vector. The standard deviation of a vector is the positive square root of its variance.

Examples

  • Query statement

    * | SELECT vector_std_dev(array[1, -3, 8])

    Or

    * | SELECT vector_std_dev('[1, -3, 8]')
  • Query and analysis results

    The mean of the vector is 2. The standard deviation is calculated by using the following formula: (12 + 52 + 62 = 62/3)0.5 = 20.670.5 = 4.54.

Return value type

Same as the data type of the parameter value.

vector_add function

The vector_add function calculates the sum of two vectors.

Examples

  • Query statement

    * | SELECT vector_add(array[1, 2, 3, 4], array[2, 3, 1, 1.5])

    Or

    * | SELECT vector_add("[1, 2, 3, 4]", "[2, 3, 1, 1.5]")
  • Query and analysis results

    [3.0,5.0,4.0,5.5]

Return value type

Same as the data type of the parameter value.

vector_subtract function

The vector_subtract function calculates the difference between two vectors.

Examples

  • Query statement

    * | SELECT vector_subtract(array[1, 2, 3, 4], array[2, 3, 1, 1.5])

    Or

    * | SELECT vector_subtract(array[1, 2, 3, 4], array[2, 3, 1, 1.5])
  • Query and analysis results

    [-1.0,-1.0,2.0,2.5]

Return value type

Same as the data type of the parameter value.

vector_scale function

The vector_scale function calculates the result of vector scaling.

Examples

  • Query statement

    * | SELECT vector_scale(array[1, 2, 3, 4], 5)

    Or

    * | SELECT vector_scale('[1, 2, 3, 4]', 5)
  • Query and analysis results

    [5.0,10.0,15.0,20.0]

Return value type

Same as the data type of the parameter value.