How to solve triangular matrix equations using Python SciPy?


The linear function named scipy.linalg.solveh_triangular is used to solve the banded matrix equation. In the below given example we will be solving the triangular system ax = b where −

$$\mathrm{a} = \begin{bmatrix} 3 & 0 & 0 & 0\ 2 & 1 & 0 & 0\ 1 &0 &1 &0 \ 1& 1& 1& 1 \end{bmatrix};\; \mathrm{b} =\begin{bmatrix} 1\ 2\ 1\ 2 \end{bmatrix}$$

Example

from scipy.linalg import solve_triangular
import numpy as np
a = np.array([[3, 0, 0, 0], [2, 1, 0, 0], [1, 0, 1, 0], [1, 1, 1, 1]])
b = np.array([1, 2, 1, 2])
x = solve_triangular(a, b, lower=True)
print (x)

Output

array([ 0.33333333, 1.33333333, 0.66666667, -0.33333333])

Updated on: 24-Nov-2021

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements