
- 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() Function
The scipy.linalg.solve() function in SciPy accepts two inputs matrices and returns an output array thet represents the solution to the polynomial equation. The two input matrices correspond to the coefficients and constants of the polynomial equations, respectively. Let's take the example to solve the following equations.
Polynomial Equations:
1x+3y+5z=10 2x+5y+z=8 2x+3y+8z=3
The input matrices:
1 3 5 a = 2 5 1 2 3 8 b= [5 8 3]
The output is also a matrix that provides the solution to the equation.
x = [-1.88, 2.36, -0.04]
Syntax
The following is the syntax for the Scipylinalg.solve function.
scipy.linalg.solve(a, b, sym_pos=False, lower=False, overwrite_a=False, overwrite_b=False, debug=None, check_finite=True, assume_a='gen', transposed=False)
Parameters
The parameters for the Scipy linalg() function are listed below −
a: This parameter accepts an array as input, representing a square matrix that contains the coefficients of the polynomial equation.
b: This parameter accepts an array as input, representing the constants of the equation, i.e., the right hand side of the equation.
sym_pos() This parameter accepts a Boolean data type. It assumes a is symmetric and positive definite. This key is deprecated, and the assume_a = pos keyword is recommended instead. The functionality remains the same but will be removed in the future.
lower: The parameter accepts a Boolean data type. If set to True, it only considers the data contained in the lower triangle of the input matrix. By default, it uses the upper triangle.
overwrite_a: This parameter accepts a Boolean data type that discards data in the input matrix a to improve performance. The default value is False.
overwrite_b: This parameter accepts a Boolean data type that discards data in the input matrix b to improve performance. The default value is False.
check_finite(): This parameter accepts a Boolean data type and checks whether the input matrix contains only finite numbers. Disabling the parameter improves performance, but if the input matrix contains infinities or NaNs, it can lead to problems such as crashes or non-termination. The default value is True.
transposed(): This parameter accepts a Boolean data type. If set to true, it solves the equation $\mathrm{a^Tx\:=\:b}$ for real matrices, but raises a NotImplementedError for complex matrices.
Return Value
The linalg.solve function accepts the above parameters and returns the solution to the equations as an array.
Exception
Following are the exceptions of the linalg.solve() function −
ValueError: If the input a is not a squared matrix or if size mismatches are detected.
LinAlgError: If the matrix is singular.
LinAlgWarning: If an ill- conditioned input a is detected.
NotImplementedError: If the transposed parameter is set to True and the input a is a complex matrix.
Example 1
In the example below, we pass two matrices to the linalg.solve() function, which returns the solution as an array. This code solves a systems of linear equations and prints the solution.
import numpy as np from scipy import linalg # Define matrices a and b a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]]) b = np.array([2, 4, -1]) # Solve the system of linear equations x = linalg.solve(a, b) # Print the solution print('The output solution is:', x)
Output
The result is generated as follows −
The output solution is: [ 2. -2. 9.]
Example 2
In the following example, we pass only the input matrices. Initially, we did not set the check_finite parameter in the linalg.solve() function, so it defaults to true. The input matrix contains NaNs, which result in a value error. The check_finite parameter checks for NaNs or infinities and returns a value error when set to True.
import numpy as np from scipy import linalg # Define matrices a and b, replacing np.nan with a valid number a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]]) # Replaced np.nan with 1 b = np.array([2, 4, -1]) # Solve the system of linear equations x = linalg.solve(a, b) # Print the solution print('The output solution is:', x)
Output
The code is generated as follows −
The output solution is: [nan nan nan]
Example 3
In the following example code, we pass both the a and b matrices, where a is a symmetric matrix. We set the assume_a parameter to 'sym' ti indicate this. The code then attempts to solve a system of linear equations using SciPy.
import numpy as np from scipy import linalg # Define the matrices a and b a = np.array([[1, 1, -1], [1, 2, 0], [-1, 0, 5]]) b = np.array([2, 4, -1]) # Solve the system of linear equations x = linalg.solve(a, b) # Print the output solution print('The output solution is:', x)
Output
The output is obtained as follows −
The ouput solution is: [-0.66666667 2.33333333 -0.33333333]