VECTOR_MAGNITUDE
Calculates the magnitude of an n-dimensional vector. The magnitude of a vector is its length expressed as a positive scalar.
Calculates the magnitude of an n-dimensional vector. The magnitude of a vector is its length expressed as a positive scalar.
Behavior type
ImmutableSyntax
VECTOR_MAGNITUDE( vector_arr )
Arguments
vector_arr- 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 the vector magnitude for two input arrays using two function calls of VECTOR_MAGNITUDE():
SELECT id, vector_arr1, VECTOR_MAGNITUDE(vector_arr1) AS mag1, vector_arr2, VECTOR_MAGNITUDE(vector_arr2) AS mag2
FROM vectors_array;
id | vector_arr1 | mag1 | vector_arr2 | mag2
----+---------------+------------------+---------------+------------------
1 | [1.0,2.0,3.0] | 3.74165738677394 | [4.0,5.0,6.0] | 8.77496438739212
2 | [1.5,2.5,3.5] | 4.55521678957215 | [4.5,5.5,6.5] | 9.63068014212911
3 | [0.0,1.0,0.0] | 1 | [0.0,0.0,1.0] | 1
4 | [2.0,0.0,0.0] | 2 | [0.0,2.0,0.0] | 2
5 | [1.0,1.0,1.0] | 1.73205080756888 | [1.0,1.0,1.0] | 1.73205080756888
(5 rows)
The following example computes the vector magnitude for a 5D vector:
SELECT VECTOR_MAGNITUDE(Array[1.0, 2.0, 3.0, 4.0, 5.0]);
VECTOR_MAGNITUDE
------------------
7.41619848709566
(1 row)