Skip to content
fn

matrix_power

→Tensor
matrix_power(input: Tensor, n: int)

Implementing kernel

C++ engine symbols that back this Python API.

Raise a square matrix to an integer power.

Computes AnA^n for an integer exponent nn:

An = {AA⋯A⏟n timesn>0In=0(A−1)∣n∣n<0A^n \,=\, \begin{cases} \underbrace{A A \cdots A}_{n\ \text{times}} & n > 0 \\ I & n = 0 \\ (A^{-1})^{|n|} & n < 0 \end{cases}

Parameters

inputTensor
Square matrix of shape (*, m, m).
nint
Integer exponent. Negative values require AA to be invertible.

Returns

Tensor

AnA^n, shape (*, m, m).

Notes

Uses binary exponentiation (repeated squaring) so the cost is O(log⁡∣n∣)O(\log |n|) matrix multiplies rather than ∣n∣|n|. Implemented as a Python composite over matmul and inv so autograd flows naturally — the engine matrix_power_op is not differentiable.

The result is always a new tensor on input's device: n = 0 gives an identity per matrix (with a zero gradient) and n = 1 a copy of input, never input itself.

Examples

>>> import lucid
>>> from lucid.linalg import matrix_power
>>> A = lucid.tensor([[1.0, 1.0], [0.0, 1.0]])
>>> matrix_power(A, 5)
tensor([[1., 5.], [0., 1.]])