matrix_power

paddle.linalg. matrix_power ( x, n, name=None ) [source]

Computes the n-th power of a square matrix or a batch of square matrices.

Let \(X\) be a sqaure matrix or a batch of square matrices, \(n\) be an exponent, the equation should be:

\[Out = X ^ {n}\]

Specifically,

  • If n > 0, it returns the matrix or a batch of matrices raised to the power of n.

  • If n = 0, it returns the identity matrix or a batch of identity matrices.

  • If n < 0, it returns the inverse of each matrix (if invertible) raised to the power of abs(n).

Parameters
  • x (Tensor) – A square matrix or a batch of square matrices to be raised to power n. Its shape should be [*, M, M], where * is zero or more batch dimensions. Its data type should be float32 or float64.

  • n (int) – The exponent. It can be any positive, negative integer or zero.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

  • Tensor, The n-th power of the matrix (or the batch of matrices) x. Its data type should be the same as that of x.

Examples

>>> import paddle

>>> x = paddle.to_tensor([[1, 2, 3],
...                       [1, 4, 9],
...                       [1, 8, 27]], dtype='float64')
>>> print(paddle.linalg.matrix_power(x, 2))
Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
[[6.  , 34. , 102.],
 [14. , 90. , 282.],
 [36. , 250., 804.]])

>>> print(paddle.linalg.matrix_power(x, 0))
Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
[[1., 0., 0.],
 [0., 1., 0.],
 [0., 0., 1.]])

>>> print(paddle.linalg.matrix_power(x, -2))
Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
[[ 12.91666667, -12.75000000,  2.83333333 ],
 [-7.66666667 ,  8.         , -1.83333333 ],
 [ 1.80555556 , -1.91666667 ,  0.44444444 ]])