matrix_exp

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

Computes the matrix exponential of square matrices.

\[exp(A) = \sum_{n=0}^\infty A^n/n!\]

The input tensor x should be of square matrices with shape like \((*, M, M)\), and the exponential output is computed by Pade approximation of the scaling and squaring method.

[1] Nicholas J. Higham, The scaling and squaring method for the matrix exponential revisited.

Parameters
  • x (Tensor) – A tensor with shape \((*, M, M)\) where \(*\) is zero or more batch dimensions. The data type should be one of float32, float64.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

Tensor, the shape and dtype are same as input tensor.

Examples

>>> import paddle

>>> mat_a = paddle.empty((2, 2, 2))
>>> mat_a[0, :, :] = paddle.eye(2, 2)
>>> mat_a[1, :, :] = 2 * paddle.eye(2, 2)
>>> print(mat_a)
Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 0.],
  [0., 1.]],
 [[2., 0.],
  [0., 2.]]])

>>> out = paddle.linalg.matrix_exp(mat_a)
>>> print(out)
Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[2.71828198, 0.        ],
  [0.        , 2.71828198]],
 [[7.38905621, 0.        ],
  [0.        , 7.38905621]]])

>>> import math
>>> mat_a = paddle.to_tensor([[0, math.pi/3], [-math.pi/3, 0]])
>>> out = paddle.linalg.matrix_exp(mat_a)
>>> print(out)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.49999994,  0.86602545],
 [-0.86602551,  0.50000000]])