Skip to contents

If A is complex valued, it computes the norm of A$abs() Support input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices: the norm will be computed over the dimensions specified by the 2-tuple dim and the other dimensions will be treated as batch dimensions. The output will have the same batch dimensions.

Usage

linalg_matrix_norm(
  A,
  ord = "fro",
  dim = c(-2, -1),
  keepdim = FALSE,
  dtype = NULL
)

Arguments

A

(Tensor): tensor with two or more dimensions. By default its shape is interpreted as (*, m, n) where * is zero or more batch dimensions, but this behavior can be controlled using dim.

ord

(int, inf, -inf, 'fro', 'nuc', optional): order of norm. Default: 'fro'

dim

(int, Tupleint, optional): dimensions over which to compute the vector or matrix norm. See above for the behavior when dim=NULL. Default: NULL

keepdim

(bool, optional): If set to TRUE, the reduced dimensions are retained in the result as dimensions with size one. Default: FALSE

dtype

dtype (torch_dtype, optional): If specified, the input tensor is cast to dtype before performing the operation, and the returned tensor's type will be dtype. Default: NULL

Details

ord defines the norm that is computed. The following norms are supported:

ordnorm for matricesnorm for vectors
NULL (default)Frobenius norm2-norm (see below)
"fro"Frobenius norm– not supported –
"nuc"nuclear norm– not supported –
Infmax(sum(abs(x), dim=2))max(abs(x))
-Infmin(sum(abs(x), dim=2))min(abs(x))
0– not supported –sum(x != 0)
1max(sum(abs(x), dim=1))as below
-1min(sum(abs(x), dim=1))as below
2largest singular valueas below
-2smallest singular valueas below
other int or float– not supported –sum(abs(x)^{ord})^{(1 / ord)}

Examples

if (torch_is_installed()) {
a <- torch_arange(0, 8, dtype = torch_float())$reshape(c(3, 3))
linalg_matrix_norm(a)
linalg_matrix_norm(a, ord = -1)
b <- a$expand(c(2, -1, -1))
linalg_matrix_norm(b)
linalg_matrix_norm(b, dim = c(1, 3))
}
#> torch_tensor
#>   3.1623
#>  10.0000
#>  17.2627
#> [ CPUFloatType{3} ]