SciPy linalg.solve() function (4 examples)

Updated: March 7, 2024 By: Guest Contributor Post a comment

The SciPy library is a cornerstone for scientific computing in Python, providing a wide array of high-level mathematical functions. Among its numerous capabilities, the linalg.solve() function is a pivotal tool for solving linear equations. This tutorial will explore the linalg.solve() function through four progressive examples, diving into how you can solve systems of linear equations effectively with SciPy.

The Use of linalg.solve()

Before diving into examples, it’s crucial to understand what linalg.solve() does. This function solves a system of linear equations of the form Ax = B, where A is a matrix and x and B are vectors or matrices. The function returns the solution as a vector or matrix x such that the equation holds true.

Example 1: Solving Simple Linear Equations

Let’s start with a simple example:

import scipy.linalg as la
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([1, 4])

x = la.solve(A, B)
print(x)

Output:

[ 2.  -0.5]

This solves the system of equations:

1x + 2y = 1
3x + 4y = 4

where A is the coefficient matrix, and B is the results vector. The solution vector x indicates that x = -3 and y = 4 satisfy the system of equations.

Example 2: Dealing with Singular Matrices

What happens if A is a singular matrix? Let’s see an example:

import scipy.linalg as la
import numpy as np

A = np.array([[1, 2], [2, 4]])
B = np.array([4, 8])

try:
    x = la.solve(A, B)
    print(x)
except la.LinAlgError as e:
    print(str(e))

Output:

Matrix is singular.

In this case, A is singular, meaning it does not have an inverse. The function correctly identifies this and raises a LinAlgError, preventing us from solving the equation with this method.

Example 3: Solving Systems with Multiple Right-hand Sides

Now, let’s tackle a more complex scenario where we have multiple right-hand sides.

import scipy.linalg as la
import numpy as np

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

B = np.array([[2, 1], [1, 0]])

x = la.solve(A, B)
print(x)

Output:

[[-1. -3.]
 [ 1.5  2.]]

In this case, B is a matrix instead of a vector. The solution x is also a matrix, showing the solutions for each corresponding right-hand side presented in B.

Example 4: Parameterized Solutions using sympy

For the fourth example, let’s explore a scenario outside the direct use of linalg.solve(), incorporating sympy for systems without unique solutions:

from sympy import symbols, Eq, solve

x, y = symbols('x y')

equation1 = Eq(2*x + 3*y, 5)
equation2 = Eq(4*x + 6*y, 10)

solution = solve((equation1, equation2), (x, y))
print(solution)

Output:

{x: 5/2 - 3*y/2}

This showcases how to handle situations where the system of equations does not yield a straightforward numerical solution but instead parameterized or infinite solutions. sympy allows for algebraic manipulation and solutions where scipy.linalg.solve() may not directly apply due to the system’s nature.

Conclusion

The linalg.solve() function in SciPy offers a powerful and efficient means of solving systems of linear equations. Through the examples shown, we’ve seen its application in straightforward scenarios, how it gracefully handles errors with singular matrices, manages multiple right-hand sides, and integrates with other libraries like sympy for complex cases. Understanding and leveraging linalg.solve() can significantly enhance your scientific computing capabilities in Python.