Selected Reading

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.]
scipy_linalg.htm
Advertisements