Jump to content

SciPy Linear Algebra


Linux Hint

Recommended Posts

In mathematics, linear algebra deals with the study of vector spaces, matrices, linear equations, determinants, eigenvalues, and others. The “Scipy” library in Python provides a collection of tools and algorithms for scientific computing, such as the “scipy.linalg” module, which offers a rich set of functions and classes for linear algebra operations.

This blog provides an in-depth tutorial on the Python “scipy.linalg” module using the below content:

What is the “scipy.linalg” in Python?

The “scipy.linalg” module of the Python “scipy” library contains various linear algebra functions to perform certain operations such as finding matrix determinants, determining the matrix inverse, calculating the matrices norm, etc. Both NumPy and SciPy libraries share the functionality of the “scipy.linalg” submodule.

Let’s start with a “solve()” function of the “scipy.linalg” module to solve the given linear equations:

Example 1: Solving Linear Equations Using the “scipy.linalg.solve()” Function

This example uses the “scipy.linalg.solve()” function of the “scipy.linalg” module to solve this linear equation.

5x + 10y + 7z = 28
4x + 5y + 9z = 10
10x + 6y + 3z = 22

Here is the code:

import scipy.linalg
import numpy
value1 = numpy.array([[5, 10, 7], [4, 5, 9], [10, 6, 3]])
value2 = numpy.array([28, 10, 22])
print(scipy.linalg.solve(value1, value2))

In the above code:

  • The “linalg” and the “Numpy” modules are imported.
  • The “array()” function takes the coefficient value of the right side of all the given equations as an argument and retrieves a Numpy array.
  • The “array()” function is used again to create a Numpy array by taking the coefficient value of the right-hand side of the equation.
  • The “linalg.solve()” function takes the input arrays representing the coefficient value of the right and left-hand side of the given linear equations and retrieves a solution array.

Output

scipy-linear-algebra-01.png

The linear equation has been solved.

Example 2: Determining the Matrix Inverse Using the “scipy.linalg.inv()”

In this example, the “scipy.linalg.inv()” function determines the matrix inverse:

import scipy.linalg
import numpy
value1 = numpy.array([[7, 2, 4], [4, 5, 12], [3, 4, 10]])
print(scipy.linalg.inv(value1))

In the above code:

  • The “linalg” and “numpy” modules are imported.
  • The “array()” function takes the equation value and initializes it as a Numpy array.
  • The “linalg.inv()” function takes the array value as an argument and retrieves an inverse array matrix.

Output

scipy-linear-algebra-02.png

The inverse of the input matrix has been determined.

Example 3: Determining the Determinant Matrix Using the “scipy.linalg.det()”

The “scipy.linalg.det()” function takes the matrix and retrieves the determinant (Scalar Value). Let’s overview this code:

import scipy.linalg
import numpy
value1 = numpy.array([[7, 2, 4], [4, 5, 12], [3, 4, 10]])
print(scipy.linalg.det(value1))

In the above code, the “numpy.array()” function takes the matrix value and creates a Numpy array. The “scipy.linalg.det()” function takes the array (matrix) as an argument and retrieves the scalar value.

Output

scipy-linear-algebra-03.png

The determinant of the input matrix has been determined successfully.

Note: You can check this official documentation for all Python “scipy.linalg” module functions.

Example 4: Determining the Norm of Matrices Using the “scipy.linalg.norm()” Function

This example determines the norm or convergences of the sequence of matrices using the “scipy.linalg.norm()” function:

import scipy.linalg
import numpy
x = numpy.array([16 , 5])
print(scipy.linalg.norm(x))
print(scipy.linalg.norm(x , 1))

In the above code:

  • The two modules “linalg” and “numpy” are imported.
  • The “array()” function creates a Numpy array by taking two elements as an argument.
  • The “linalg.norm()” function takes the array as an argument and returns the norm of the matrix, which is a measure of its length or size. By default, it uses the “2-norm”, calculated as the square root of the square sum of the elements.
  • The “linalg.norm()” function is used again to retrieve the norm of a matrix with a different value of the parameter “ord”, which specifies the type of norm to use. In this case, it uses the “1-norm”, calculated as the sum of the absolute values of the elements.

Output

scipy-linear-algebra-04.png

The norm value has been calculated successfully.

Conclusion

The “scipy.linalg” module of the “scipy” library provides various functions such as “solve()”, “det()”, “inv()”, “norm()”, and others to perform an operation on linear algebra equations. The  “scipy.linalg.solve()”, “scipy.linalg.det()”, and “scipy.linalg.inv()” functions are used to solve the linear algebraic equation, determine the determinant and inverse of the input matrix. This Python post presented a few “scipy.linalg” module functions using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...