## Matrix power in R

matrix exponential in r

matrix square in r

r matrix inverse

raise to power in r

matrix power calculator

how to exponentiate a matrix in r

matrix algebra in r

Trying to compute the power of a matrix in R, I found that package `expm`

implements the operator %^%.

So x %^% k computes the k-th power of a matrix.

> A<-matrix(c(1,3,0,2,8,4,1,1,1),nrow=3) > A %^% 5 [,1] [,2] [,3] [1,] 6469 18038 2929 [2,] 21837 60902 9889 [3,] 10440 29116 4729

but, to my surprise:

> A [,1] [,2] [,3] [1,] 691 1926 312 [2,] 2331 6502 1056 [3,] 1116 3108 505

somehow the initial matrix A has changed to A %^% 4 !!!

How do you perform the matrix power operation?

I have fixed that bug in the R-forge sources (of "expm" package), svn rev. 53. --> expm R-forge page For some reason the web page still shows rev.52, so the following may not yet solve your problem (but should within 24 hours):

install.packages("expm", repos="http://R-Forge.R-project.org")

Otherwise, get the svn version directly, and install yourself:

svn checkout svn://svn.r-forge.r-project.org/svnroot/expm

Thanks to "gd047" who alerted me to the problem by e-mail. Note that R-forge also has its own bug tracking facilities. Martint

**Compute the power of a matrix in R,** Package expm provides the matrix %^% number operator notation for its function matpow(): > library(expm) > mat <- matrix(1:9, nrow=3) > mat %^% 2 [,1] [,2] [,3]� The matrix power is computed by successive matrix multiplications. If the exponent is zero, the order n identity matrix is returned. If the exponent is negative, the inverse of the matrix is raised to the given power.

**Matrix power in R,** I have fixed that bug in the R-forge sources (of "expm" package), svn rev. 53. --> expm R-forge page For some reason the web page still shows� To the best of my knowledge, it currently is the most comprehensive R package that exists to deal with matrix exponentiation. Version 3.0 extends capabilities to (some) non-diagonalizable matrices too.

Although the source-code is not visible in the package since it is packed in a `.dll file`

, I believe the algorithm used by the package is the fast exponentiation algorithm, which you can study by looking at the function called `matpowfast`

instead.

You need two variables :

`result`

, in order to store the output,`mat`

, as an intermediate variable.

To compute `A^6`

, since `6 = 110`

(binary writing), in the end, `result = A^6`

and `mat = A^4`

. This is the same for `A^5`

.

You could easily check if `mat = A^8`

when you try to compute `A^n`

for any `8<n<16`

. If so, you have your explanation.

The package function uses the initial variable `A`

as the intermediate variable `mat`

.

**matrix.power: Matrix Raised to a Power in matrixcalc: Collection of ,** Bellman, R. (1987). Matrix Analysis, Second edition, Classics in Applied Mathematics, Society for Industrial and Applied Mathematics. Examples� Invert a matrix in R. Contrary to your intuition, inverting a matrix is not done by raising it to the power of –1, R normally applies the arithmetic operators element-wise on the matrix. So, the command first.matrix^(-1) doesn’t give you the inverse of

Very quick solution **without using any package** is using recursivity:
if your matrix is a

powA = function(n) { if (n==1) return (a) if (n==2) return (a%*%a) if (n>2) return ( a%*%powA(n-1)) }

HTH

**Power of a Matrix,** Power of a Matrix. Description. %^% calculates x^p for the square matrix, x , by spectral decomposition. Usage. x%^%p. Arguments. x. A square matrix. p. Value. %^% returns the power of a matrix. Author(s) J.K. Lindsey. Examples. x <- matrix(c(0.4,0.6,0.6,0.4),nrow=2) x%^%2 x%^%10 x%^%20. [Package rmutil version 1.0 The matrix power is computed by successive matrix multiplications. If the exponent is zero, the order n identity matrix is returned. If the exponent is negative, the inverse of the matrix is raised to the given power. Value. An order n matrix. Author(s) Frederick Novomestky fnovomes@poly.edu. References. Bellman, R. (1987).

An inefficient version (since it's more efficient to first diagonalize your matrix) in `base`

without much effort is:

pow = function(x, n) Reduce(`%*%`, replicate(n, x, simplify = FALSE))

I know this question is specifically about an old bug in `expm`

, but it's one of the first results for "matrix power R" at the moment, so hopefully this little shorthand can be useful for someone else who ends up here just looking for a quick way to run matrix powers without installing any packages.

**[PDF] Package 'powerplus',** R topics documented: sured in radians, which is the default method built-in in R . Value Raises a valid Matrix to any power (even complex). Matrix Power Calculator Here you can raise a matrix to a power with complex numbers online for free. You can examine multiplication apart that was used to get the current power on every step.

**Matrix Exponentiation in R – The Research Kitchen,** A short while ago, I needed to do some matrix exponentiation in R (raising a matrix to a power). By default, the exponentiation operator ^ in R,� The multiplication of matrix A by matrix B is a 1 × 1 matrix defined by: Example 1 Matrices A and B are defined by Find the matrix A B. Solution Multiplication of Matrices We now apply the idea of multiplying a row by a column to multiplying more general matrices. Let A be an m × p matrix and B be an p × n matrix. Let R 1, R 2,

**[R] A function for raising a matrix to a power?,** Ron E. VanNimwegen wrote: > > I was looking for a similar operator, because R uses scalar products > when raising a matrix to a power with� Paul, Your solution based on SVD does not work even for the matrix in your example (the reason it worked for e=3, was that it is an odd power and since P is a permutation matrix.

**Raising a matrix to a power in R,** A surprising deficiency in R is the absence of an operator or built-in function for matrix exponentiation. I had to deal with this today when porting� A matrix makes it easier to display data meaningfully across multiple dimensions -- it supports a stepped layout. The matrix automatically aggregates the data and enables drill down. You can create matrix visuals in Power BI Desktop reports and cross-highlight elements within the matrix with other visuals on that report page. For example, you

##### Comments

- It doesn't have side effects in the global environment - C code is passed a reference to the R objects, so can modify an object in place. This is necessary for certain optimisations, but should never be exposed to the R user.
- @hadley I understand that. But if there is a single reference for two objects (as it seems to be the case above, probably for efficiency) and you let the C code modify the object in place, you are (I think) having side effects in the global environment, right?
- Your explanation is basically correct, but you are using suboptimal terminology. It doesn't make sense to talk about modifying the global environment here, because the object might not be in the global environment.
- this is not terribly useful, as the original bug was fixed more than two years ago ...
- plus this is a terrible way to perform matrix exponentiation for large exponents
- Calculating A%^%6 also leaves A as (initial A)%^%4. Assigning the result to a new variable, doesn't prevent my initial matrix from being changed.
- sounds like you just have to take the unusual step of assigning the matrix to a new variable first.