
- 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
Matrix and linear Algebra calculations in Python
In this article, we will learn Matrix and linear Algebra calculations in Python such as matrix multiplication, finding determinants, solving linear equations, etc.
A matrix object from the NumPy library can be used for this. When it comes to calculation, matrices are relatively comparable to the array objects
Linear Algebra is a huge topic that is outside of this article.
However, NumPy is an excellent library to start if you need to manipulate matrices and vectors.
Methods Used
Finding Transpose of a Matrix Using Numpy
Finding Inverse of a Matrix Using Numpy
Multiplying Matrix with a Vector
Getting the Determinant of Matrix using numpy.linalg subpackage
Finding Eigenvalues using numpy.linalg
Solving equations using numpy.linalg
Method 1: Finding Transpose of a Matrix Using Numpy
numpy.matrix.T attribute − Returns the transpose of the given matrix.
Example
The following program returns the transpose of a matrix using the numpy.matrix.T attribute −
# importing NumPy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # printing the transpose of an input matrix # by applying the .T attribute of the NumPy matrix of the numpy Module print("Transpose of an input matrix\n", inputMatrix.T)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Transpose of an input matrix [[6 2 1] [1 0 4] [5 8 3]]
Method 2: Finding Inverse of a Matrix Using Numpy
numpy.matrix.I attribute − Returns the Inverse of the given matrix.
Example
The following program returns the Inverse of a matrix using the numpy.matrix.I attribute −
# importing NumPy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # printing the inverse of an input matrix # by applying the .I attribute of the NumPy matrix of the numpy Module print("Inverse of an input matrix:\n", inputMatrix.I)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Inverse of an input matrix: [[ 0.21333333 -0.11333333 -0.05333333] [-0.01333333 -0.08666667 0.25333333] [-0.05333333 0.15333333 0.01333333]]
Method 3: Multiplying Matrix with a Vector
Example
The following program returns the multiplication of the input matrix and vector using the * operator −
# importing numpy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # creating a vector using numpy.matrix() function inputVector = np.matrix([[1],[3],[5]]) # printing the multiplication of the input matrix and vector print("Multiplication of input matrix and vector:\n", inputMatrix*inputVector)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Multiplication of input matrix and vector: [[34] [42] [28]]
Method 4: Getting the Determinant of Matrix using numpy.linalg subpackage
numpy.linalg.det() function − Calculates the determinant of a square matrix.
Example
The following program returns the determinant of a matrix using numpy.linalg.det() function −
# importing numpy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # getting the determinant of an input matrix outputDet = np.linalg.det(inputMatrix) # printing the determinant of an input matrix print("Determinant of an input matrix:\n", outputDet)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Determinant of an input matrix: -149.99999999999997
Method 5: Finding Eigenvalues using numpy.linalg
numpy.linalg.eigvals() function − calculates the eigenvalues and right eigenvectors of a specified square array/matrix.
Example
The following program returns the Eigenvalues of an input matrix using the numpy.linalg.eigvals() function −
# importing NumPy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # getting Eigenvalues of an input matrix eigenValues = np.linalg.eigvals(inputMatrix) # printing Eigenvalues of an input matrix print("Eigenvalues of an input matrix:\n", eigenValues)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Eigenvalues of an input matrix: [ 9.55480959 3.69447805 -4.24928765]
Method 6: Solving equations using numpy.linalg
We can solve problems like Finding the value of X of A*X = B,
Where A is the matrix and B is the vector.
Example
Below is the program that returns the Value of x using solve() function −
# importing NumPy module import numpy as np # input matrix inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # printing the input matrix print("Input Matrix:\n", inputMatrix) # creating a vector using np.matrix() function inputVector = np.matrix([[1],[3],[5]]) # getting the value of x in an equation inputMatrix * x = inputVector x_value = np.linalg.solve(inputMatrix, inputVector) # printing x value print("x value:\n", x_value) # multiplying input matrix with x values print("Multiplication of input matrix with x values:\n", inputMatrix * x_value)
Output
On executing, the above program will generate the following output −
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] x value: [[-0.39333333] [ 0.99333333] [ 0.47333333]] Multiplication of input matrix with x values: [[1.] [3.] [5.]]
Conclusion
In this article, we learned how to use the NumPy module in Python to execute matrix and linear algebra operations. We learned how to compute the matrix's transpose, inverse, and determinant. We also learned how to perform some computations in linear algebra, such as solving equations and determining eigenvalues.
- Related Articles
- Return the infinity Norm of the matrix in Linear Algebra in Python
- Compute the condition number of a 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 Norm of the matrix or vector in Linear Algebra in Python
- Raise a square matrix to the power n in Linear Algebra in Python
- Return the negative infinity Norm of the matrix in Linear Algebra in Python
- Return the Norm of the matrix over axis in Linear Algebra 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
- Return the Norm of the matrix or vector in Linear Algebra and also set the order in Python
- Compute the condition number of a matrix in linear algebra using negative 2 norm in Python
- Compute the condition number of a matrix in linear algebra using Negative Infinity norm in Python
- Return the Cholesky decomposition in Linear Algebra in Python
