Scipy.linalg.solve_banded() Function



A banded matrix is a matrix in which non zero elements only on diagonal bands above and below the main diagonal. The upper bandwidth indicates the number of non-zero diagonals above the main diagonal, while the lower bandwidth indicates the number of non-zero diagonals below the main diagonal.

To solve the equation ax = b for x, assuming a is a banded matrix, the matrix a is sorted in ab using the matrix diagonal order form.

ab[u+i-j, j] == a[i,j]

Example of ab(shape of a is(6,6), u=1, l=2):

*    a01  a12  a23  a34  a45
a00  a11  a22  a33  a44  a55
a10  a21  a32  a43  a54   *
a20  a31  a42  a53   *    *

Syntax

Following is the syntax for the SciPy linalg.solve_banded() function.

scipy.linalg.solve_banded(l_and_u, ab, b, overwrite_ab=False, overwrite_b=False, debug=None, check_finite=True)

Parameters

The parameters for the scipy.linalg.solve_banded() function are listed below −

  • (l,u): This parameter requires two integers, l and u, which represents the number of non-zero elements on the lower and upper diagonals, respectively.

  • ab:(l+u+1,M): This parameter accepts an array that represents a banded matrix.

  • b: This parameter accepts an array as input , which represents the constants of the equation, i.e., the right-hand side of the equation.

  • overwrite_ab(): This parameter accepts a Boolean data type, which discards data in AB(input matrix) to improve performance. The default value is set to False.

  • overwrite_b(): This parameter accepts a Boolean data type that discards data in B to improve performance. The default value is False.

  • check_finite(): This parameter accepts a Boolean data type, which checks whether the input matrix contains only finite numbers. Displaying this parameter enhances performance, but if the input matrix includes infinities or NaNs,it can lead to problems such as crashes or non-termination. The default value is set to True.

Return Value

The linalg.solve_banded() function accepts the above parameters and returns the solution to the equations as an array. The shape of the returned array depends on the shape of B.

Example 1

In this example code, we pass an ab array representing a banded matrix. The code solves the linear equation using the linalg.solve_banded() function and prints the solution array

from scipy.linalg import solve_banded
import numpy as np

# Define the banded matrix in the specified form
ab = np.array([[0,  0, -1, -1, -1],
               [0,  2,  2,  2,  2],
               [5,  4,  3,  2,  1],
               [1,  1,  1,  1,  0]])

# Define the right-hand side of the equation
b = np.array([0, 1, 2, 2, 3])

# Solve the system using solve_banded
x = solve_banded((1, 2), ab, b)

# Print the solution
print(x)

Output

The result is generated as follows −

[-2.37288136  3.93220339 -4.  4.3559322  -1.3559322 ]

Example 2

Here is an example code where we pass the ab matrix and the check_finite parameter. When check_finite is set to True, the function checks for any NaNs or infinities in the matrix and returns an error if found. This code solves a linear equation using a banded matrix with possible NaNs, checks for finite numbers, and prints the solution.

# Import necessary libraries
from scipy.linalg import solve_banded
import numpy as np

# Define the banded matrix in the specified form
ab = np.array([[0,  0, -1, -1, -1],
               [0,  2,  np.nan,  2,  2],
               [5,  4,  3,  2,  1],
               [1,  1,  1,  1,  0]])

# Define the right-hand side of the equation
b = np.array([0, 1, 2, 2, 3])

# Solve the system using solve_banded
x = solve_banded((1, 2), ab, b, check_finite=True)

# Print the solution
print(x)

Output

The code is generated as follows −

Traceback (most recent call last):
  File "/home/cg/root/26555/main.py", line 15, in <module>
    x = solve_banded((1, 2), ab, b, check_finite=True)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/linalg/_basic.py", line 432, in solve_banded
    a1 = _asarray_validated(ab, check_finite=check_finite, as_inexact=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scipy/_lib/_util.py", line 240, in _asarray_validated
    a = toarray(a)
        ^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 630, in asarray_chkfinite
    raise ValueError(
ValueError: array must not contain infs or NaNs

Example 3

This code solves a linear equation with a banded matrix using solve_banded, bypassing checks for NaNs or infinities and prints the resulting solution array.

# Import necessary libraries
from scipy.linalg import solve_banded
import numpy as np

# Define the banded matrix in the specified form
ab = np.array([[0,  0, -1, -1, -1],
               [0,  2,  np.nan,  2,  2],
               [5,  4,  3,  2,  1],
               [1,  1,  1,  1,  0]])

# Define the right-hand side of the equation
b = np.array([0, 1, 2, 2, 3])

# Solve the system using solve_banded
x = solve_banded((1, 2), ab, b, check_finite=False)

# Print the solution
print(x)

Output

The output is obtained as follows −

[nan nan nan nan nan]
parent_file.htm
Advertisements