Skip to content
fn

matrix_power_op

→TensorImplPtr
int matrix_power_op(const int & a, int n)

Python wrappers

Public Python APIs implemented by this engine symbol.

Compute AnA^n for a square float matrix and integer exponent.

Validates that a is at least 2-D, square in the trailing two axes, and float-typed; dispatches to the backend. The output shape always matches a's shape (matrix powers preserve shape). Autograd is not wired — the result is a leaf in the gradient graph.

Math

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

Repeated squaring decomposes n=∑ibi2in = \sum_i b_i 2^i (its binary representation) and accumulates the result in O(log⁡∣n∣)O(\log |n|) matrix multiplies.

Shape

  • a: (..., N, N).
  • Output: same shape as a.

References

Knuth, The Art of Computer Programming, Vol. 2 §4.6.3 (right-to-left binary exponentiation). Higham, Functions of Matrices (2008), §1.2.

See Also

inv_op : Used internally when n < 0. matmul_op : Single-step underlying primitive.

Parameters

aTensorImplPtr
Input matrix of shape (..., N, N).
nint
Integer exponent. Named n for parity with the reference framework's matrix_power(a, n) signature; the algorithm refers to it as pp internally. May be zero, positive, or negative; negative values require AA to be invertible.

Returns

TensorImplPtr

Tensor of shape (..., N, N) holding AnA^n (and the identity matrix if n == 0).

Raises

ValueError
If a is not square, fewer than 2-D, or non-float.
LinAlgError
(At backend level, when n < 0.) If AA is singular.

Notes

  • No backward is registered; matrix_power outputs are leaves.
  • For graph neural networks AA is often an adjacency or normalised Laplacian — large n is fine because cost grows only logarithmically.