
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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]