
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Raise a square matrix to the power n in Linear Algebra in Python
To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() in Python For positive integers n, the power is computed by repeated matrix squarings and matrix multiplications. If n == 0, the identity matrix of the same shape as M is returned. If n < 0, the inverse is computed and then raised to the abs(n).
The return value is the same shape and type as M; if the exponent is positive or zero then the type of the elements is the same as those of M. If the exponent is negative the elements are floating-point. The 1st parameter, a is a matrix to be “powered”. The 2nd parameter, n is the exponent that can be any integer or long integer, positive, negative, or zero.
Steps
At first, import the required libraries −
import numpy as np from numpy.linalg import matrix_power
Create a 2D array, matrix equivalent of the imaginary unit −
arr = np.array([[0, 1], [-1, 0]])
Display the array −
print("Our Array...\n",arr)
Check the Dimensions −
print("\nDimensions of our Array...\n",arr.ndim)
Get the Datatype −
print("\nDatatype of our Array object...\n",arr.dtype)
Get the Shape −
print("\nShape of our Array object...\n",arr.shape)
To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() in Python. For positive integers n, the power is computed by repeated matrix squarings and matrix multiplications. If n == 0, the identity matrix of the same shape as M is returned. If n < 0, the inverse is computed and then raised to the abs(n) −
print("\nResult...\n",matrix_power(arr, 0))
Example
import numpy as np from numpy.linalg import matrix_power # Create a 2D array, matrix equivalent of the imaginary unit arr = np.array([[0, 1], [-1, 0]]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To raise a square matrix to the power n in Linear Algebra, use the numpy.linalg.matrix_power() in Python print("\nResult...\n",matrix_power(arr, 0))
Output
Our Array... [[ 0 1] [-1 0]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [[1 0] [0 1]]
- Related Articles
- Matrix and linear Algebra calculations in Python
- Compute the condition number of a matrix in linear algebra in Python
- Return the infinity Norm of the matrix in Linear Algebra in Python
- Return the Frobenius Norm of the matrix in Linear Algebra in Python
- Return the Nuclear Norm of the matrix in Linear Algebra in Python
- Return the negative infinity Norm of the matrix in Linear Algebra in Python
- Return the Norm of the matrix or vector in Linear Algebra in Python
- Return the Norm of the matrix over axis in Linear Algebra in Python
- Raise a polynomial to a power in Python
- Compute the condition number of a matrix in linear algebra using Frobenius norm in Python
- Compute the condition number of a matrix in linear algebra using 2 norm in Python
- Compute the condition number of a matrix in linear algebra using Infinity norm in Python
- Raise a Laguerre series to a power in Python
- Raise a Hermite series to a power in Python
- Raise a Legendre series to a power in Python
