Skip to contents

Calculates the n-th power of a square matrix, where n can be a positive or negative integer or a fractional power.

Usage

mpower(A, n)

A %^% n

Arguments

A

A square matrix. Must also be symmetric for non-integer powers.

n

matrix power

Value

Returns the matrix \(A^n\)

Details

If n<0, the method is applied to \(A^{-1}\). When n is an integer, the function uses the Russian peasant method, or repeated squaring for efficiency. Otherwise, it uses the spectral decomposition of A, \(\mathbf{A}^n = \mathbf{V} \mathbf{D}^n \mathbf{V}^{T}\) requiring a symmetric matrix.

See also

Packages corpcor and expm define similar functions.

Author

Michael Friendly

Examples


M <- matrix(sample(1:9), 3,3)
mpower(M,2)
#>      [,1] [,2] [,3]
#> [1,]   50   70   78
#> [2,]   31   83   57
#> [3,]   82  113  138
mpower(M,4)
#>       [,1]  [,2]  [,3]
#> [1,] 11066 18124 18654
#> [2,]  8797 15500 15015
#> [3,] 18919 30713 31881

# make a symmetric matrix
MM <- crossprod(M)
mpower(MM, -1)
#>         [,1]    [,2]    [,3]
#> [1,]  0.2209  0.0399 -0.1662
#> [2,]  0.0399  0.0347 -0.0503
#> [3,] -0.1662 -0.0503  0.1480
Mhalf <- mpower(MM, 1/2)
all.equal(MM, Mhalf %*% Mhalf)
#> [1] TRUE