# Raise a matrix to a fractional power in R

• Last Updated : 21 Apr, 2021

Matrices can be raised to integral as well as non-integral powers in R Programming Language. The computation of power of a matrix involves simultaneous multiplication of matrix as many times as the power integer is specified. However, in the case of fractional powers, we need to use the in-built R functions to simulate this operation.

Method 1: Using expm package in R

The expm package can be used to perform various types of matrix computations, like logarithm, exponential, and others. This function computes the exponential of a square matrix, which is non-diagonalizable. The expm package includes logm and sqrtm methods, respectively. However, it cannot be used to work with negative fractional powers. The expm package can be installed in R directory using the following command :

`install.packages("expm)`

The package is included in the script and then the expm method can be used which has the following syntax :

`expm(x), where x is the matrix`

Code:

## R

 `require``(expm)``library``(``"expm"``)`` ` `mat <- ``matrix``(``c``(1, 1, 1, 1), 2, 2)``print ``(``"Original Matrix"``)``print ``(mat)``modified_matrix <- 1.1*``logm``(mat)`` ` `# computing power matrix ``powmat <- ``expm``(modified_matrix)`` ` `# printing the power matrix ``print ``(``"Power Matrix"``)``print ``(powmat)`

Output

```[1] "Original Matrix"
[,1]   [,2]
[1,]   1      1
[2,]   1      1
[1] "Power Matrix"
[,1]      [,2]
[1,] 1.071773  1.071773
[2,] 1.071773  1.071773```

Method 2: Using powerplus package in R

Matrices can be raised to negative or positive non-integral powers using the powerplus package in R. It is used to raise a valid matrix to any power (even complex numbers are allowed). The matrices that can be used are either square matrices or diagonalisable matrices with positive eigenvalues. The powerplus package can be installed into the R directory by using the following command :

`install.packages("powerplus")`

The package can then be included in the script and then the Matpow() method can be used, which can be used to exponential power computation of a square matrix.

Syntax: Matpow(M, power_number)

Arguments :

• M – A diagnosable square matrix
• power_number – The non-integral power factor of the matrix

Returns :  A matrix raised to the power_number factor.

Example 1: The following examples illustrate the Matpow() method in R.

## R

 `# requiring the necessary package``require``(``"powerplus"``)`` ` `# declaring a non diagonizable matrix``mat <- ``matrix``(``c``(2, 0, 1, 1), ncol = 2) ``print ``(``"Original Matrix"``)``print ``(mat)`` ` `# raising the matrix to a fractional ``# power 0.5``print ``(``"Power Matrix"``)``Matpow``(mat, 0.5)`
```[1] "Original Matrix"
[,1] [,2]
[1,]    1    1
[2,]    0    1
[1] "Power Matrix"
[,1] [,2]
[1,]    1  0.5
[2,]    0  1.0```

Example 2: Negative integral powers.

Matpow() methods can be used to work out with negative integral powers also. The output is the negative matrix in case the result is odd, otherwise even.

## R

 `# requiring the necessary package``require``(``"powerplus"``)`` ` `# declaring a non diagonizable matrix``mat <- ``matrix``(``c``(2, 0, 1, 1), ncol = 2) ``print ``(``"Original Matrix"``)``print ``(mat)`` ` `# raising the matrix to a fractional power 0.5``print ``(``"Power Matrix"``)``Matpow``(mat, -2.1)`

Output

```[1] "Original Matrix"
[,1]     [,2]
[1,]    2    1
[2,]    0    1
[1] "Power Matrix"
[,1]       [,2]
[1,] 0.2332582 -0.7667418
[2,] 0.0000000  1.0000000```

My Personal Notes arrow_drop_up