bundles / scipy 1.18.0 / scipy / sparse / linalg / _norm / norm
function
scipy.sparse.linalg._norm:norm
source: /scipy/sparse/linalg/_norm.py:20
Signature
def norm(x, ord = None, axis = None)Summary
Norm of a sparse matrix.
Extended Summary
This function is able to return one of seven different matrix norms, depending on the value of the ord parameter.
Parameters
x: a sparse arrayInput sparse array.
ord: {non-zero int, inf, -inf, 'fro'}, optionalOrder of the norm (see table under
Notes). inf means numpy'sinfobject.axis: {int, 2-tuple of ints, None}, optionalIf
axisis an integer, it specifies the axis ofxalong which to compute the vector norms. Ifaxisis a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. Ifaxisis None then either a vector norm (whenxis 1-D) or a matrix norm (whenxis 2-D) is returned.
Returns
n: float or ndarrayThe selected norm of
x.
Notes
Some of the ord are not implemented because some associated functions like, _multi_svd_norm, are not yet available for sparse array.
This docstring is modified based on numpy.linalg.norm. https://github.com/numpy/numpy/blob/main/numpy/linalg/linalg.py
The following norms can be calculated:
===== ============================ ord norm for sparse arrays ===== ============================ None Frobenius norm 'fro' Frobenius norm inf max(sum(abs(x), axis=1)) -inf min(sum(abs(x), axis=1)) 0 abs(x).sum(axis=axis) 1 max(sum(abs(x), axis=0)) -1 min(sum(abs(x), axis=0)) 2 Spectral norm (the largest singular value) -2 Not implemented other Not implemented ===== ============================
The Frobenius norm is given by [1]:
Examples
from scipy.sparse import csr_array, diags_array import numpy as np from scipy.sparse.linalg import norm a = np.arange(9) - 4✓
a
✗array([-4, -3, -2, -1, 0, 1, 2, 3, 4])
b = a.reshape((3, 3))
✓b
✗array([[-4, -3, -2],
[-1, 0, 1],
[ 2, 3, 4]])
b = csr_array(b)
✓norm(b) norm(b, 'fro') norm(b, np.inf) norm(b, -np.inf) norm(b, 1) norm(b, -1)✗
np.float64(6.0)The matrix 2-norm or the spectral norm is the largest singular value, computed approximately and with limitations.
b = diags_array([-1, 1], offsets=[0, 1], shape=(9, 10))
✓norm(b, 2)
✗np.float64(1.9753766811902758)
Aliases
scipy.sparse.linalg.norm