## Numpy.linalg.matrix_power Function

Numpy is a Python library used for scientific computing with an excellent collection of functions designed for mathematics, scientific, and engineering applications. One such function is `numpy.linalg.matrix_power`

.

This function is used to calculate the power of a matrix. In this article, we will explore the syntax and parameters of the `numpy.linalg.matrix_power`

function, its returns and raises, and compare it with the `numpy.power()`

function.

Also, we will provide examples of how to use `numpy.linalg.matrix_power`

in python.

### Syntax and Parameters

The `numpy.linalg.matrix_power`

function is used to calculate the power of a matrix. The syntax for this function is as follows:

`numpy.linalg.matrix_power(matrix, power)`

The function takes two parameters: `matrix`

and `power`

.

The `matrix`

parameter is the input matrix which we want to calculate the power, and the `power`

parameter represents an integer, used to specify the power of the matrix that we want to calculate.

### Returns and Raises

This function returns the result of raising the input matrix to the power specified by the user. The output is also a matrix of the same dimension as the input.

The `numpy.linalg.matrix_power`

function may raise two exceptions:

- The first exception is
`ValueError`

if the power is negative or floating-point number. - The second exception is
`LinAlgError`

, which is raised if the input matrix is singular or non-square.

### Comparison with `numpy.power()`

function

The `numpy.power()`

function computes the element-wise exponential of the input. However, it is not the same as `numpy.linalg.matrix_power`

because we cannot calculate the power of a matrix element-wise.

The `numpy.power()`

function takes two inputs, the base and the exponent. It returns the result of raising the base to the power specified by the exponent.

### Examples of `numpy.linalg.matrix_power`

#### Example 1: Calculating matrix power 2 and 3

In this example, we will calculate the power of a matrix. Consider the following matrix:

```
import numpy as np
matrix = np.array([[1, 2], [2, 3]])
```

We want to calculate the power of the matrix to the power of 2 and 3. The code for this is as follows:

```
result_2 = np.linalg.matrix_power(matrix, 2)
result_3 = np.linalg.matrix_power(matrix, 3)
```

#### The output for `result_2`

is as follows:

```
array([[ 5, 8],
[ 8, 13]])
```

#### The output for `result_3`

is as follows:

```
array([[ 21, 34],
[ 34, 55]])
```

#### Example 2: Using `numpy.linalg.power()`

with negative power

In this example, we will see the exception raised by the `numpy.linalg.matrix_power`

function for negative power.

Consider the following matrix:

`matrix = np.array([[1, 2], [2, 3]])`

We want to calculate the power of the matrix with power set to -2. The code for this is as follows:

`result = np.linalg.matrix_power(matrix, -2)`

This will raise a `ValueError`

exception because a negative power is not allowed.

#### Example 3: Using `numpy.linalg.matrix_power`

with 0

In this example, we will see what happens when the power is set to 0. Consider the following matrix:

`matrix = np.array([[1, 2], [2, 3]])`

We want to calculate the matrix to the power of 0.

The code for this is as follows:

`result = np.linalg.matrix_power(matrix, 0)`

The output for `result`

is the identity matrix of the same dimension as the input matrix.

### Conclusion

In this article, we explored the syntax and parameters of the `numpy.linalg.matrix_power`

function. We also discussed the returns and raises associated with this function.

We then looked at the comparison between `numpy.linalg.matrix_power`

and `numpy.power()`

function. Finally, we provided examples to demonstrate the use of the `numpy.linalg.matrix_power`

function in python.

Hopefully, this article has helped you to understand the `numpy.linalg.matrix_power`

function and its utility in scientific computing.