Skip to contents

A simple function to demonstrate calculating the power of a square symmetric matrix in terms of its eigenvalues and eigenvectors.

Usage

mpower(A, p, tol = sqrt(.Machine$double.eps))

Arguments

A

a square symmetric matrix

p

matrix power, not necessarily a positive integer

tol

tolerance for determining if the matrix is symmetric

Value

A raised to the power p: A^p

Details

The matrix power p can be a fraction or other non-integer. For example, p=1/2 and p=1/3 give a square-root and cube-root of the matrix.

Negative powers are also allowed. For example, p=-1 gives the inverse and p=-1/2 gives the inverse square-root.

See also

The {%^%} operator in the expm package is far more efficient

Examples

C <- matrix(c(1,2,3,2,5,6,3,6,10), 3, 3) # nonsingular, symmetric
C
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    2    5    6
#> [3,]    3    6   10
mpower(C, 2)
#>      [,1] [,2] [,3]
#> [1,]   14   30   45
#> [2,]   30   65   96
#> [3,]   45   96  145
zapsmall(mpower(C, -1))
#>      [,1] [,2] [,3]
#> [1,]   14   -2   -3
#> [2,]   -2    1    0
#> [3,]   -3    0    1
solve(C)    # check
#>      [,1] [,2] [,3]
#> [1,]   14   -2   -3
#> [2,]   -2    1    0
#> [3,]   -3    0    1