The ML.LP_NORM function

This document describes theML.LP_NORM scalar function, which lets youcompute theLp norm fora vector, wherep 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 type can beBIGNUMERIC,FLOAT64,INT64 orNUMERIC. For exampleARRAY<BIGNUMERIC>.

    Each element of the array denotes one dimension of the vector. An exampleof a four-dimensional vector is[0.0, 1.0, 1.0, 0.0].

    The function calculates thep degree norm of the numerical typevalues in all the values in the array.

  • degree: aFLOAT64 value that specifies the degree. This can be0.0,any value >=1.0, orCAST('INF' AS FLOAT64) to return the L_infinitynorm of the vector, which is the largest magnitude of the values inthe vector.

    Commonly used values are1.0 to calculate theManhattannormof the vector and2.0 to calculate theEuclideannorm ofthe vector.

Output

ML.LP_NORM returns aFLOAT64 value that represents the Lp normfor the vector. ReturnsNULL ifvector isNULL.

Example

The following example gets the Euclidean norm for vectors consisting ofARRAY<FLOAT64> values:

  1. Create the tablet1:

    CREATETABLEmydataset.t1(v1ARRAY<FLOAT64>,v2ARRAY<FLOAT64>)
  2. Populatet1:

    INSERTmydataset.t1(v1,v2)VALUES([4.1,0.5,1.0],[3.0,0.0,2.5])
  3. Calculate the Euclidean norm forv1 andv2:

    SELECTv1,ML.LP_NORM(v1,2.0)ASv1_norm,v2,ML.LP_NORM(v2,2.0)ASv2_normFROMmydataset.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

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.