- 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 - solve_triangular() Function
Scipy.linalg.solve_triangular() simplifies the solution of linear systems Ax=b when A is upper or lower triangular. The function uses forward or backward substitution to take advantage of triangular structure and thereby improve computational speed.
Experts combine this technique with matrix multiplications or LU and QR decompositions in handling complex systems.
Scientists use it very often in order to solve triangular problems that have an important role in many numerical techniques, including Cholesky and LU decomposition. This technique also applies in data fitting, models for engineering, and simulations for physics.
This is useful when we are working with sparse or structured systems where triangular matrices naturally arise.
Syntax
Following is the syntax of the SciPy solve_triangular() method −
scipy.linalg.solve_triangular(a, b, trans=0, lower=False, unit_diagonal=False, overwrite_b=False, check_finite=True)
Parameters
Following is the parameters of solve_triangular() method
a (array_like) − Coefficient matrix , which must be triangular.
b array_like, shape (n,) or (n, k) − Input equation of Right-hand side.
trans (int, optional) − Specifies the equation form Ax = b (default), A^T.x=b (Transpose), A^H.x=b (Hermitian transpose).
lower (bool, optional)− If True, A is assumed to be lower triangular. If False, A is upper triangular.
unit_diagonal (bool, optional) − If True, diagonal elements of A are assumed to be 1 and are not referenced. Default is False.
overwrite_b (bool, optional) − If True, the data in b may be overwritten to save memory. Default is False.
check_finite (bool, optional) − If True, checks whether the input contains only finite numbers. Disabling this can improve performance.
Return Value
x (ndarray) − The solution to the triangular system.
Example 1: Upper Triangular System
The solve_triangular() function uses forward or backward substitution to solve linear systems with triangular matrices.
In the below code we have created a upper triangular matrix A and a vector b. The function solves the system Ax = b.
import numpy as np
import scipy.linalg
from scipy.linalg import solve_triangular
# Upper triangular matrix
A = np.array([[2, 1],
[0, 3]])
# Right-hand side vector
b = np.array([3, 9])
# Solve the system
x = scipy.linalg.solve_triangular(A, b)
print(x)
When we run above program, it produces following result
[0. 3.]
Example 2: Lower Triangular System
The lower triangular systems can be solved using the lower=True parameter.
In this code, we defined the lower triangular matrix and used the lower=True parameter to indicate its structure.
import numpy as np
import scipy.linalg
from scipy.linalg import solve_triangular
# Lower triangular matrix
a = np.array([[4, 0, 0],
[1, 5, 0],
[2, 3, 6]])
b = np.array([8, 18, 34])
x = solve_triangular(a, b, lower=True)
print(x)
Following is an output of the above code
[2. 3.2 3.4]
Example 3: Transposed Triangular System
Systems that involve the transpose of the triangular matrix can be solved using the trans=1 parameter.
In this example, A^T.x=b, where A^T is the transpose of the triangular matrix.
import numpy as np
from scipy.linalg import solve_triangular
A = np.array([[2, 1, 1],
[0, 3, 2],
[0, 0, 4]])
b = np.array([5, 13, 16])
# Solve the system for A^T
x = solve_triangular(A, b, trans=1)
print("Solution x for A^T:", x)
Following is an output of the above code
Solution x for A^T: [2.5 3.5 1.625]
Example 4: Combining with LU Decomposition
The use of LU decomposition with the solve_triangular() gives a very robust way of solving linear systems. It breaks down the problem into triangular subproblems making the most of forward and backward substitution's effectiveness.
This code determines a solution to a linear system Ax=b. This first decomposes A into three matrices: permutation (P) lower triangular (L), and upper triangular (U). This is referred to as LU decomposition.
The code then solves the triangular systems one by one using solve_triangular(). This approach is fast and reliable when solving big or intricate systems in scientific number crunching.
import numpy as np
from scipy.linalg import lu, solve_triangular
A = np.array([[4, 3], [6, 3]])
b = np.array([10, 12])
# Perform LU decomposition
P, L, U = lu(A)
# Solve Ly = Pb (forward substitution for lower triangular L)
y = solve_triangular(L, np.dot(P, b), lower=True)
# Solve Ux = y (backward substitution for upper triangular U)
x = solve_triangular(U, y)
print("Solution x:", x)
Following is an output of the above code
Solution x: [1. 2.]