vector_norm#
- ivy.vector_norm(x, /, *, axis=None, keepdims=False, ord=2, dtype=None, out=None)[source]#
Compute the vector norm of a vector (or batch of vectors)
x.- Parameters:
x (
Union[Array,NativeArray]) – input array. Should have a floating-point data type.axis (
Optional[Union[int,Sequence[int]]], default:None) – If an integer,axisspecifies the axis (dimension) along which to compute vector norms. If an n-tuple,axisspecifies the axes (dimensions) along which to compute batched vector norms. IfNone, the vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array). Negative indices are also supported. Default:None.keepdims (
bool, default:False) – IfTrue, the axes (dimensions) specified byaxismust be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, ifFalse, the axes (dimensions) specified byaxismust not be included in the result. Default:False.ord (
Union[int,float,Literal[inf,-inf]], default:2) –order of the norm. The following mathematical norms are supported:
ord
description
1
L1-norm (Manhattan)
2
L2-norm (Euclidean)
inf
infinity norm
(int,float >= 1)
p-norm
The following non-mathematical “norms” are also supported:
ord
description
0
sum(a != 0)
-inf
min(abs(a))
(int,float < 1)
sum(abs(a)**ord)**(1./ord)
Default:
2.dtype (
Optional[Union[Dtype,NativeDtype]], default:None) – data type that may be used to perform the computation more precisely. The input arrayxgets cast todtypebefore the function’s computations.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – an array containing the vector norms. If
axisisNone, the returned array must be a zero-dimensional array containing a vector norm. Ifaxisis a scalar value (intorfloat), the returned array must have a rank which is one less than the rank ofx. Ifaxisis an-tuple, the returned array must have a rank which isnless than the rank ofx. The returned array must have a floating-point data type determined by type-promotion. Ifxhas a complex-valued data type, the returned array must have a real-valued floating-point data type whose precision matches the precision ofx(e.g., ifxiscomplex128, then the returned array must have afloat64data type).
This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.
Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts
ivy.Containerinstances in place of any of the arguments.Examples
>>> x = ivy.array([1., 2., 3.]) >>> y = ivy.vector_norm(x) >>> print(y) ivy.array([3.7416575])
>>> x = ivy.array([[1, 2, 3], [1.3, 2.4, -1.2]]) >>> y = ivy.vector_norm(x, axis = 1, ord = 1, dtype = ivy.float32) >>> print(y) ivy.array([6., 4.9000001])
>>> x = ivy.array([[1, 2, 3], [1.3, 2.4, -1.2]]) >>> y = ivy.vector_norm(x, axis = 0, keepdims = True, ord = float("inf")) >>> print(y)
ivy.array([[1.3, 2.4, 3.]])
>>> x = ivy.native_array([1, 2, 3, 4], dtype = ivy.float32) >>> y = ivy.vector_norm(x, ord = 3.) >>> print(y)
ivy.array([4.64158917])
>>> x = ivy.array([1.,2.,3.,4.], dtype = ivy.float16) >>> z = ivy.empty(shape = 1, dtype=ivy.float16) >>> y = ivy.vector_norm(x, ord = 0, out = z) >>> print(y) ivy.array(4.)
>>> x = ivy.arange(8, dtype=ivy.float32).reshape((2,2,2)) >>> y = ivy.vector_norm(x, axis = (0,1), ord = float("-inf")) >>> print(y) ivy.array([0, 1])
>>> x = ivy.Container(a = [-1., 1., -2., 2.], b = [0., 1.2, 2.3, -3.1]) >>> y = ivy.vector_norm(x, ord = -1) >>> print(y) { a: ivy.array([0.33333334]), b: ivy.array([0.]) }
- Array.vector_norm(self, /, *, axis=None, keepdims=False, ord=2, dtype=None, out=None)[source]#
ivy.Array instance method variant of ivy.vector_norm. This method computes the vector norm of a vector (or batch of vectors).
- Parameters:
self (
Array) – Input array. Should have a floating-point data type.axis (
Optional[Union[int,Sequence[int]]], default:None) – If an integer,axisspecifies the axis (dimension) along which to compute vector norms. If an n-tuple,axisspecifies the axes (dimensions) along which to compute batched vector norms. IfNone, the vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array). Negative indices are also supported. Default:None.keepdims (
bool, default:False) – IfTrue, the axes (dimensions) specified byaxismust be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting). Otherwise, ifFalse, the axes (dimensions) specified byaxismust not be included in the result. Default:False.ord (
Union[int,float,Literal[inf,-inf]], default:2) –order of the norm. The following mathematical norms are supported:
ord
description
1
L1-norm (Manhattan)
2
L2-norm (Euclidean)
inf
infinity norm
(int,float >= 1)
p-norm
The following non-mathematical “norms” are also supported:
ord
description
0
sum(a != 0)
-inf
min(abs(a))
(int,float < 1)
sum(abs(a)**ord)**(1./ord)
Default:
2.dtype (
Optional[Union[Dtype,NativeDtype]], default:None) – data type that may be used to perform the computation more precisely. The input arrayselfgets cast todtypebefore the function’s computations.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array- Returns:
ret – an array containing the vector norms. If
axisisNone, the returned array must be a zero-dimensional array containing a vector norm. Ifaxisis a scalar value (intorfloat), the returned array must have a rank which is one less than the rank ofself. Ifaxisis an-tuple, the returned array must have a rank which isnless than the rank ofself. The returned array must have a floating-point data type determined by type-promotion.
Examples
>>> x = ivy.array([1., 2., 3.]) >>> y = x.vector_norm() >>> print(y) ivy.array([3.7416575])
- Container.vector_norm(self, /, *, axis=None, keepdims=False, ord=2, dtype=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.vector_norm. This method simply wraps the function, and so the docstring for ivy.vector_norm also applies to this method with minimal changes.
- Parameters:
self (
Container) – input array. Should have a floating-point data type.axis (
Optional[Union[int,Sequence[int],Container]], default:None) – If an integer,axisspecifies the axis (dimension) along which to compute vector norms. If an n-tuple,axisspecifies the axes (dimensions) along which to compute batched vector norms. IfNone, the vector norm must be computed over all array values (i.e., equivalent to computing the vector norm of a flattened array). Negative indices must be supported. Default:None.keepdims (
Union[bool,Container], default:False) – IfTrue, the axes (dimensions) specified byaxismust be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see broadcasting).Otherwise, ifFalse, the axes (dimensions) specified byaxismust not be included in the result. Default:False.ord (
Union[int,float,Literal[inf,-inf],Container], default:2) –order of the norm. The following mathematical norms must be supported:
ord
description
1
L1-norm (Manhattan)
2
L2-norm (Euclidean)
inf
infinity norm
(int,float >= 1)
p-norm
The following non-mathematical “norms” must be supported:
Default:
2.dtype (
Optional[Union[Dtype,NativeDtype,Container]], default:None) – data type that may be used to perform the computation more precisely. The input arrayxgets cast todtypebefore the function’s computations.out (
Optional[Container], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container- Returns:
ret – an array containing the vector norms. If
axisisNone, the returned array must be a zero-dimensional array containing a vector norm. Ifaxisis a scalar value (intorfloat), the returned array must have a rank which is one less than the rank ofx. Ifaxisis an-tuple, the returned array must have a rank which isnless than the rank ofx. The returned array must have a floating-point data type determined by type-promotion.
Examples
>>> x = ivy.Container(a = [1., 2., 3.], b = [-2., 0., 3.2]) >>> y = x.vector_norm() >>> print(y) { a: ivy.array([3.7416575]), b: ivy.array([3.77359247]) }