
- 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 - Solving Linear Systems
In linear algebra solving linear systems refers to finding the solution(s) to a system of linear equations. A linear system can be represented as follows −
A . x = b
Where −
- A is a matrix of coefficients).
- x is the vector of variables (unknowns)
- b is the vector of constants.
In SciPy solving linear systems is done using several methods depending on the type and properties of the matrix A. SciPy provides highly optimized functions to solve linear systems directly or via matrix decompositions such as LU, Cholesky, QR and others.
SciPy Function to Solve Linear Systems
The primary function in SciPy to solve a linear system is scipy.linalg.solve(). This function is used to compute the solution x of the system Ax=b where A is the matrix of coefficients and b is the vector of constants.
Syntax
Below is the syntax of the function scipy.linalg.solve() which is used to Solve Linear systems −
scipy.linalg.solve( a, b, sym_pos=False, lower=False, overwrite_a=False, overwrite_b=False, check_finite=True )
- a(array_like): The coefficient matrix A which should be a square matrix i.e., number of rows = number of columns and in most cases non-singular i.e., invertible.
- b(array_like): The right-hand side vector or matrix i.e., b which must have the same number of rows as the matrix A.
- sym_pos(bool, optional): If True then it indicates that the matrix A is symmetric and positive-definite. In this case the function uses more efficient algorithms such as Cholesky decomposition.
- lower(bool, optional): If True then it indicates that the matrix A is lower triangular. This will reduce the computational cost.
- overwrite_a(bool, optional): If True then allows overwriting the input matrix A in memory to save space which perform faster computations but changes the original matrix.
- overwrite_b(bool, optional): If True then allows overwriting the input vector b in memory.
- check_finite(bool, optional): If True then the function checks whether A and b contain only finite numbers. This helps to avoid numerical issues but may incur some overhead.
This function returns the solution vector or matrix i.e., the values of the variables that satisfy the linear system Ax=b.
Example of Solving a Linear System
Following is the example of scipy.linalg.solve() function which is used to solve a system of linear equations with the shape 2 x 2 −
import numpy as np from scipy.linalg import solve # Define matrix A (2x2) A = np.array([[3, 2], [1, 2]]) # Define vector b (2x1) b = np.array([5, 5]) # Solve for x in Ax = b x = solve(A, b) print("Solution x:", x)
Following is the output of the above code −
Solution x: [0. 2.5]
Solving Linear Systems for Non-Square Matrices
In this example we have an over-determined system i.e., more equations than unknowns and we use the least-squares method to solve it −
import numpy as np from scipy.linalg import solve # Define the overdetermined matrix A (3x2 matrix) A = np.array([[1, 2], [2, 3], [3, 4]]) # Define the right-hand side vector b b = np.array([5, 6, 7]) # Solve using least-squares method x = solve(A.T @ A, A.T @ b) print("Least-squares solution x:", x)
Following is the output of the above code −
Least-squares solution x: [-3. 4.]
Solving a Singular System
Following is the example in which we have a singular matrix i.e., the system does not have a unique solution x + y = 1 and x + y = 2 −
import numpy as np from scipy.linalg import solve # Define a singular matrix A (rows are linearly dependent) A = np.array([[1, 1], [1, 1]]) # Define the right-hand side vector b b = np.array([1, 2]) # Attempt to solve the system A * x = b try: x = solve(A, b) print("Solution x:", x) except Exception as e: print(f"Error: {e}")
Following is the output of the above code −
Error: Matrix is singular.