DOT_PRODUCT
Returns the dot product of two n-dimensional vectors, represented as two equal length ARRAYs. Dot products may sometimes be referred to as scalar products.
Returns the dot product of two n-dimensional vectors, represented as two equal length ARRAYs. Dot products may sometimes be referred to as scalar products.
Behavior type
ImmutableSyntax
DOT_PRODUCT( vector_arr1, vector_arr2 )
Arguments
vector_arr1- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
vector_arr2- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
Note
NULL and NaN values are treated equivalently. If any element in the input array is NULL or NaN, the function returns NULL.Returns
FLOAT
Examples
The following example computes dot products for two vector arrays:
SELECT id, vector_arr1, vector_arr2, DOT_PRODUCT(vector_arr1, vector_arr2) AS dot_product FROM vectors_array;
id | vector_arr1 | vector_arr2 | dot_product
----+---------------+---------------+-------------
1 | [1.0,2.0,3.0] | [4.0,5.0,6.0] | 32
2 | [1.5,2.5,3.5] | [4.5,5.5,6.5] | 43.25
3 | [0.0,1.0,0.0] | [0.0,0.0,1.0] | 0
4 | [2.0,0.0,0.0] | [0.0,2.0,0.0] | 0
5 | [1.0,1.0,1.0] | [1.0,1.0,1.0] | 3
(5 rows)
The following example computes the dot product for a 5D vector:
SELECT DOT_PRODUCT(Array[1.0, 2.0, 3.0, 4.0, 5.0], Array[5.0, 4.0, 3.0, 2.0, 1.0]);
DOT_PRODUCT
------------
35