Working with Linear Systems in Python with scipy.linalg

From learning to earning – Courses that prepare you for job - Enroll now

Linear algebra is a field of mathematics that finds applications, in domains. Python offers tools like scipy.linalg to simplify working with algebra problems. In this blog post, we will explore how to utilize the scipy.linalg module in Python to handle linear systems.

Working with Linear Systems in Python with scipy.linalg

Linear systems consist of a set of linear equations that are solved together to determine the values of variables. Scipy.linalg is a Python package that provides functions for dealing with linear algebra and linear systems. It builds upon the numpy library, which facilitates the manipulation of arrays and matrices.

Getting started with scipy.linalg:

Scipy.linalg is a Python library encompassing functions, for executing linear algebra operations. This library is built on top of numpy, ensuring the implementation of algebra routines. This blog post aims to introduce you to the capabilities of the scipy.linalg library and showcase how it can be leveraged for diverse linear algebra operations.

To begin, let’s install the library using pip:

pip install scipy

Once installed, we can readily incorporate this library into our Python code.

Before using its functions, we need to import the scipy.linalg package by including the code:

import scipy.linalg

The following are the subjects we shall discuss in this blog:

  • Solving linear systems
  • Computing determinants
  • Computing inverses
  • Eigenvalues and eigenvectors
  • Vectors and Matrices

Solving linear systems:

The scipy.linalg.solve() function can be used to solve linear systems. This method accepts two arguments, A and b, and returns the solution vector x.

The following code sample demonstrates how to use the solution() method to solve a linear system:

import numpy as np
from scipy.linalg import solve

A = np.array([[2, 1], [1, 2]])
b = np.array([3, 4])
x = solve(A, b)

print(x)

Output:

[0.66666667 1.66666667]

Determinants of computation:

A matrix’s determinant is a numerical value that can be used to determine the matrix’s many properties. One major application of determinant is determining whether or not a matrix is invertible, which is purely dependent on the determinant value being non-zero.

The determinant of a matrix can be computed using the scipy.linalg.det() function. The determinant is returned by the function, which takes a matrix as an argument.

The following code snippet demonstrates how to compute a matrix’s determinant:

import numpy as np
from scipy.linalg import det

A = np.array([[2, 1], [1, 2]])
det_A = det(A)

print(det_A)

Output:

3.0

Calculating inverses:

A matrix’s inverse is another matrix that, when multiplied by the original matrix, yields the identity matrix. To compute the inverse of a matrix, use the scipy.linalg.inv() function. The function accepts a matrix as input and returns its inverse.

The code below demonstrates how to compute the inverse of a matrix:

import numpy as np
from scipy.linalg import inv

A = np.array([[2, 1], [1, 2]])
inv_A = inv(A)

print(inv_A)

Output:

[[ 0.66666667 -0.33333333]
[-0.33333333 0.66666667]]

Eigenvalues and eigenvectors:

Eigenvalues and eigenvectors are key notions in linear algebra that are employed in a variety of applications. The scipy.linalg.eig() function can be used to compute the eigenvalues and eigenvectors of a matrix. The function accepts a matrix as input and outputs two arrays: eigenvalues and eigenvectors.

The following code sample explains how to determine a matrix’s eigenvalues and eigenvectors:

import numpy as np
from scipy.linalg import eig

A = np.array([[2, 1], [1, 2]])
eigvals, eigvecs = eig(A)

print("Eigenvalues:", eigvals)
print("Eigenvectors:", eigvecs)

Output:

Eigenvalues: [3.+0.j 1.+0.j]
Eigenvectors: [[ 0.70710678 -0.70710678]
[ 0.70710678 0.70710678]]

Matrices and vectors:

Vectors and matrices are essential mathematical concepts that are employed in a wide range of disciplines, including physics, engineering, and computer science. The NumPy module in Python provides capabilities for quickly dealing with vectors and matrices. NumPy is an open-source toolkit that supports massive, multi-dimensional arrays and matrices, as well as a wide range of mathematical functions that may be applied to these arrays.

To use NumPy, you must first install it with pip. Once installed, use the following command to import the library:

import numpy as np

This command imports the NumPy library and gives it the alias “np,” which is a common Python standard.

Making a Vector

A vector is represented as a 1-dimensional array in NumPy. You can make a vector by using the np.array() method, as demonstrated below:

a = np.array([1, 2, 3])
print(a)

Output:

[1 2 3]

We made a vector a with three elements in this example: 1, 2, and 3. The print() function is used to display the vector’s contents.

Making a Matrix

In NumPy, a matrix is a two-dimensional array. You can generate a matrix with a list of lists by using the np.array() method, where each nested list represents a row in the matrix. As an example:

a = np.array([[1, 2], [3, 4]])
print(a)

Output:

[[1 2]
[3 4]]

We made a matrix with two rows and two columns in this example.

Fundamental Operations

NumPy includes a number of mathematical functions for working with vectors and matrices. Here are some of the fundamental procedures you can perform:

1. Addition/Subtraction:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)

Output:

[5 7 9]

2. Multiplication:

Multiplication:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a * b
print(c)

Output:

[ 4 10 18]

3. Dot product:

Dot product:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.dot(a, b)
print(c)

Output:

32

In this example, we calculated the dot product of two vectors, a and b.

Scipy Commands Table

inv(a[, overwrite_a, check_finite]) Compute the inverse of a matrix.
solve(a, b[, sym_pos, lower, overwrite_a, …]) Solves the linear equation set a @ x == b for the unknown x for square a matrix.
solve_banded(l_and_u, ab, b[, overwrite_ab, …]) Solve the equation a x = b for x, assuming a is banded matrix.
solveh_banded(ab, b[, overwrite_ab, …]) Solve equation a x = b.
solve_circulant(c, b[, singular, tol, …]) Solve C x = b for x, where C is a circulant matrix.
solve_triangular(a, b[, trans, lower, …]) Solve the equation a x = b for x, assuming a is a triangular matrix.
solve_toeplitz(c_or_cr, b[, check_finite]) Solve a Toeplitz system using Levinson Recursion
matmul_toeplitz(c_or_cr, x[, check_finite, …]) Efficient Toeplitz Matrix-Matrix Multiplication using FFT
det(a[, overwrite_a, check_finite]) Compute the determinant of a matrix
norm(a[, ord, axis, keepdims, check_finite]) Matrix or vector norm.

Conclusion

We learned how to work with linear systems in Python using the scipy.linalg package in this tutorial. We showed how to use the solution function to solve a system of linear equations and the eig function to determine the eigenvalues and eigenvectors of a matrix. These tools have a wide range of applications in mathematics, science, and engineering.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google | Facebook


PythonGeeks Team

PythonGeeks Team is dedicated to creating beginner-friendly and advanced tutorials on Python programming, AI, ML, Data Science and more. From web development to machine learning, we help learners build strong foundations and excel in their Python journey.

1 Response

  1. RAMAVTAR lodhi says:

    Mujhe jaruri kam hai isiliye mujhe padhna hai

Leave a Reply

Your email address will not be published. Required fields are marked *