cond

paddle.linalg. cond ( x, p=None, name=None ) [source]

Computes the condition number of a matrix or batches of matrices with respect to a matrix norm p.

Parameters
  • x (Tensor) – The input tensor could be tensor of shape (*, m, n) where * is zero or more batch dimensions for p in (2, -2), or of shape (*, n, n) where every matrix is invertible for any supported p. And the input data type could be float32 or float64.

  • p (float|string, optional) – Order of the norm. Supported values are fro, nuc, 1, -1, 2, -2, inf, -inf. Default value is None, meaning that the order of the norm is 2.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

computing results of condition number, its data type is the same as input Tensor x.

Return type

Tensor

Examples

>>> import paddle
>>> paddle.seed(2023)
>>> x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]])

>>> # compute conditional number when p is None
>>> out = paddle.linalg.cond(x)
>>> print(out)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.41421378)

>>> # compute conditional number when order of the norm is 'fro'
>>> out_fro = paddle.linalg.cond(x, p='fro')
>>> print(out_fro)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
3.16227770)

>>> # compute conditional number when order of the norm is 'nuc'
>>> out_nuc = paddle.linalg.cond(x, p='nuc')
>>> print(out_nuc)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
9.24264145)

>>> # compute conditional number when order of the norm is 1
>>> out_1 = paddle.linalg.cond(x, p=1)
>>> print(out_1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)

>>> # compute conditional number when order of the norm is -1
>>> out_minus_1 = paddle.linalg.cond(x, p=-1)
>>> print(out_minus_1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.)

>>> # compute conditional number when order of the norm is 2
>>> out_2 = paddle.linalg.cond(x, p=2)
>>> print(out_2)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.41421378)

>>> # compute conditional number when order of the norm is -1
>>> out_minus_2 = paddle.linalg.cond(x, p=-2)
>>> print(out_minus_2)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.70710671)

>>> # compute conditional number when order of the norm is inf
>>> out_inf = paddle.linalg.cond(x, p=float("inf"))
>>> print(out_inf)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
2.)

>>> # compute conditional number when order of the norm is -inf
>>> out_minus_inf = paddle.linalg.cond(x, p=-float("inf"))
>>> print(out_minus_inf)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.)

>>> a = paddle.randn([2, 4, 4])
>>> print(a)
Tensor(shape=[2, 4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.06132207,  1.11349595,  0.41906244, -0.24858207],
  [-1.85169315, -1.50370061,  1.73954511,  0.13331604],
  [ 1.66359663, -0.55764782, -0.59911072, -0.57773495],
  [-1.03176904, -0.33741450, -0.29695082, -1.50258386]],
 [[ 0.67233968, -1.07747352,  0.80170447, -0.06695852],
  [-1.85003340, -0.23008066,  0.65083790,  0.75387722],
  [ 0.61212337, -0.52664012,  0.19209868, -0.18707706],
  [-0.00711021,  0.35236868, -0.40404350,  1.28656745]]])

>>> a_cond_fro = paddle.linalg.cond(a, p='fro')
>>> print(a_cond_fro)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[6.37173700 , 35.15114594])

>>> b = paddle.randn([2, 3, 4])
>>> print(b)
Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[ 0.03306439,  0.70149767,  0.77064633, -0.55978841],
  [-0.84461296,  0.99335045, -1.23486686,  0.59551388],
  [-0.63035583, -0.98797107,  0.09410731,  0.47007179]],
 [[ 0.85850012, -0.98949534, -1.63086998,  1.07340240],
  [-0.05492965,  1.04750168, -2.33754158,  1.16518629],
  [ 0.66847134, -1.05326962, -0.05703246, -0.48190674]]])

>>> b_cond_2 = paddle.linalg.cond(b, p=2)
>>> print(b_cond_2)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.86566353, 6.85834455])