The ML.LP_NORM function
This document describes the ML.LP_NORM scalar function, which lets you
compute the Lp norm for
a vector, where p is the degree.
Syntax
ML.LP_NORM(vector, degree)
Arguments
ML.LP_NORM has the following arguments:
vector: anARRAY<Numerical type>value that represents a vector, whereNumerical typecan beBIGNUMERIC,FLOAT64,INT64orNUMERIC. For exampleARRAY<BIGNUMERIC>.Each element of the array denotes one dimension of the vector. An example of a four-dimensional vector is
[0.0, 1.0, 1.0, 0.0].The function calculates the p degree norm of the numerical type values in all the values in the array.
degree: aFLOAT64value that specifies the degree. This can be0.0, any value >=1.0, orCAST('INF' AS FLOAT64)to return the L_infinity norm of the vector, which is the largest magnitude of the values in the vector.Commonly used values are
1.0to calculate the Manhattan norm of the vector and2.0to calculate the Euclidean norm of the vector.
Output
ML.LP_NORM returns a FLOAT64 value that represents the Lp norm
for the vector. Returns NULL if vector is NULL.
Example
The following example gets the Euclidean norm for vectors consisting of
ARRAY<FLOAT64> values:
Create the table
t1:CREATE TABLE mydataset.t1 ( v1 ARRAY<FLOAT64>, v2 ARRAY<FLOAT64> )
Populate
t1:INSERT mydataset.t1 (v1,v2) VALUES ([4.1,0.5,1.0], [3.0,0.0,2.5])
Calculate the Euclidean norm for
v1andv2:SELECT v1, ML.LP_NORM(v1, 2.0) AS v1_norm, v2, ML.LP_NORM(v2, 2.0) AS v2_norm FROM mydataset.t1;
This query produces the following output:
+---------------------------+-----+-------------------+ | v1 | v1_norm | v2 | v2_norm | +---------------------------+-----+-------------------+ | 4.1 | 4.2497058721751557 | 3.0 | 3.905124837953327 | +-----| |-----| | | 0.5 | | 0.0 | | +-----| |-----+ | | 1.0 | | 2.5 | | +---------------------------+-----+-------------------+
What's next
- For information about the supported SQL statements and functions for each model type, see End-to-end user journey for each model.