Stay organized with collectionsSave and categorize content based on your preferences.
The ML.LP_NORM function
This document describes theML.LP_NORMscalar function, which lets you
compute theLpnorm for
a vector, wherepis the degree.
Syntax
ML.LP_NORM(vector, degree)
Arguments
ML.LP_NORMhas 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 thepdegree 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 are1.0to calculate theManhattan
normof the vector and2.0to calculate theEuclidean
normof
the vector.
Output
ML.LP_NORMreturns aFLOAT64value that represents the Lpnorm
for the vector. ReturnsNULLifvectorisNULL.
Example
The following example gets the Euclidean norm for vectors consisting ofARRAY<FLOAT64>values:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[[["\u003cp\u003e\u003ccode\u003eML.LP_NORM\u003c/code\u003e is a scalar function that computes the L^p^ norm of a numerical vector, where ^p^ represents the degree.\u003c/p\u003e\n"],["\u003cp\u003eThe function accepts two arguments: a \u003ccode\u003evector\u003c/code\u003e of numerical type (e.g., \u003ccode\u003eBIGNUMERIC\u003c/code\u003e, \u003ccode\u003eFLOAT64\u003c/code\u003e, \u003ccode\u003eINT64\u003c/code\u003e, \u003ccode\u003eNUMERIC\u003c/code\u003e) and a \u003ccode\u003edegree\u003c/code\u003e (\u003ccode\u003eFLOAT64\u003c/code\u003e) to specify the norm's degree.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003edegree\u003c/code\u003e can be \u003ccode\u003e0.0\u003c/code\u003e, any value greater than or equal to \u003ccode\u003e1.0\u003c/code\u003e, or \u003ccode\u003eCAST('INF' AS FLOAT64)\u003c/code\u003e to calculate the L_infinity norm.\u003c/p\u003e\n"],["\u003cp\u003eCommon values for the \u003ccode\u003edegree\u003c/code\u003e are \u003ccode\u003e1.0\u003c/code\u003e for the Manhattan norm and \u003ccode\u003e2.0\u003c/code\u003e for the Euclidean norm, and the function returns a \u003ccode\u003eFLOAT64\u003c/code\u003e value representing the calculated L^p^ norm.\u003c/p\u003e\n"],["\u003cp\u003eIf \u003ccode\u003evector\u003c/code\u003e is null, the function returns \u003ccode\u003eNULL\u003c/code\u003e as the output.\u003c/p\u003e\n"]]],[],null,["# The ML.LP_NORM function\n=======================\n\nThis document describes the `ML.LP_NORM` scalar function, which lets you\ncompute the [L^p^](https://en.wikipedia.org/wiki/Lp_space) norm for\na vector, where ^p^ is the degree.\n\nSyntax\n------\n\n```sql\nML.LP_NORM(vector, degree)\n```\n\n### Arguments\n\n`ML.LP_NORM` has the following arguments:\n\n- `vector`: an `ARRAY\u003cNumerical type\u003e` value that represents a vector,\n where `Numerical type` can be `BIGNUMERIC`, `FLOAT64`,\n `INT64` or `NUMERIC`. For example `ARRAY\u003cBIGNUMERIC\u003e`.\n\n Each element of the array denotes one dimension of the vector. An example\n of a four-dimensional vector is `[0.0, 1.0, 1.0, 0.0]`.\n\n The function calculates the ^p^ degree norm of the numerical type\n values in all the values in the array.\n- `degree`: a `FLOAT64` value that specifies the degree. This can be `0.0`,\n any value \\\u003e= `1.0`, or `CAST('INF' AS FLOAT64)` to return the L_infinity\n norm of the vector, which is the largest magnitude of the values in\n the vector.\n\n Commonly used values are `1.0` to calculate the [Manhattan\n norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Taxicab_norm_or_Manhattan_norm)\n of the vector and `2.0` to calculate the [Euclidean\n norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm) of\n the vector.\n\nOutput\n------\n\n`ML.LP_NORM` returns a `FLOAT64` value that represents the L^p^ norm\nfor the vector. Returns `NULL` if `vector` is `NULL`.\n\nExample\n-------\n\nThe following example gets the Euclidean norm for vectors consisting of\n`ARRAY\u003cFLOAT64\u003e` values:\n\n1. Create the table `t1`:\n\n ```sql\n CREATE TABLE mydataset.t1\n (\n v1 ARRAY\u003cFLOAT64\u003e,\n v2 ARRAY\u003cFLOAT64\u003e\n )\n ```\n2. Populate `t1`:\n\n ```sql\n INSERT mydataset.t1 (v1,v2)\n VALUES ([4.1,0.5,1.0], [3.0,0.0,2.5])\n ```\n3. Calculate the Euclidean norm for `v1` and `v2`:\n\n ```sql\n SELECT v1, ML.LP_NORM(v1, 2.0) AS v1_norm, v2, ML.LP_NORM(v2, 2.0) AS v2_norm\n FROM mydataset.t1;\n ```\n\n This query produces the following output: \n\n +---------------------------+-----+-------------------+\n | v1 | v1_norm | v2 | v2_norm |\n +---------------------------+-----+-------------------+\n | 4.1 | 4.2497058721751557 | 3.0 | 3.905124837953327 |\n +-----| |-----| |\n | 0.5 | | 0.0 | |\n +-----| |-----+ |\n | 1.0 | | 2.5 | |\n +---------------------------+-----+-------------------+\n\nWhat's next\n-----------\n\n- For information about the supported SQL statements and functions for each model type, see [End-to-end user journey for each model](/bigquery/docs/e2e-journey)."]]