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.

Updated on: 01-Feb-2023

743 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements