In this article we will discuss the intuition and steps to calculate the upper triangular matrix and lower triangular matrix using Python.
Table of contents
- Introduction
- Upper triangular matrix explained
- Lower triangular matrix explained
- Triangular matrix special forms
- Extract upper triangular matrix in Python
- Extract lower triangular matrix in Python
- Conclusion
Introduction
Triangular matrices aren’t the most popular concepts in linear algebra, however they are very useful and their properties help us understand other special cases of matrices as well as the operations with matrices.
To continue following this tutorial we will need the following Python library: numpy.
If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:
pip install numpy
Upper triangular matrix explained
An upper triangular matrix (or right triangular matrix) is a special case of a square matrix in which all values below the main diagonal are zeros.
For example, consider a 4×4 matrix \(U\):
$$U = \begin{bmatrix} 3 & 2 & 5 & 7 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 9 & 8 \\ 0 & 0 & 0 & 2 \end{bmatrix}$$
This matrix is upper triangular, since all the values below its main diagonal (which is [3 , 1, 9, 2]) are zeros.
This can be further generalized to a square matrix of \(n \times n\) dimension.
$$U = \begin{bmatrix} u_{11} & u_{12} & \dots & u_{1n} \\
& u_{22} & \dots & u_{2n} \\
& & \ddots & \vdots \\
0 & & & u_{nn} \end{bmatrix}$$
Lower triangular matrix explained
An lower triangular matrix (or left triangular matrix) is a special case of a square matrix in which all values above the main diagonal are zeros.
For example, consider a 4×4 matrix \(L\):
$$L = \begin{bmatrix} 2 & 0 & 0 & 0 \\ 7 & 3 & 0 & 0 \\ 9 & 4 & 8 & 0 \\ 6 & 2 & 1 & 5 \end{bmatrix}$$
This matrix is lower triangular, since all the values above its main diagonal (which is [2 , 3, 8, 5]) are zeros.
This can be further generalized to a square matrix of \(n \times n\) dimension.
$$L = \begin{bmatrix} l_{11} & & & 0 \\
l_{21} & l_{22} & & \\
\vdots & \vdots & \ddots & \\
l_{n1} & l_{n2} & \dots & l_{nn} \end{bmatrix}$$
Triangular matrix special forms
There are several special forms of triangular matrices:
Diagonal matrix
A matrix that is both upper triangular and lower triangular is a diagonal matrix, because the only non-zero elements are on the main diagonal.
For example:
$$\begin{bmatrix} 3 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 5 \end{bmatrix}$$
Unitriangular matrix
If a matrix is upper triangular or lower triangular where all main diagonal values are 1, this matrix is called (upper or lower) unitriangular.
Upper unitriangular matrix example:
$$\begin{bmatrix} 1 & 2 & 5 & 7 \\ 0 & 1 & 3 & 4 \\ 0 & 0 & 1 & 8 \\ 0 & 0 & 0 & 1 \end{bmatrix}$$
Lower unitriangular matrix example:
$$\begin{bmatrix} 1 & 0 & 0 & 0 \\ 7 & 1 & 0 & 0 \\ 9 & 4 & 1 & 0 \\ 6 & 2 & 5 & 1 \end{bmatrix}$$
Strictly triangular matrix
If a matrix is upper triangular or lower triangular where all main diagonal values are 1, this matrix is called strictly (upper or lower) triangular.
Strictly upper triangular matrix:
$$\begin{bmatrix} 0 & 2 & 5 & 7 \\ 0 & 0 & 3 & 4 \\ 0 & 0 & 0 & 8 \\ 0 & 0 & 0 & 0 \end{bmatrix}$$
Strictly lower triangular matrix:
$$\begin{bmatrix} 0 & 0 & 0 & 0 \\ 7 & 0 & 0 & 0 \\ 9 & 4 & 0 & 0 \\ 6 & 2 & 5 & 0 \end{bmatrix}$$
Atomic triangular matrix
If a matrix is a unitriangular matrix, where all elements off the main diagonal are zeros, except for a single column, that matrix is called an atomic (upper or lower) triangular matrix.
Atomic upper triangular matrix example:
$$\begin{bmatrix} 1 & 0 & 5 & 0 \\ 0 & 1 & 3 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}$$
Atomic lower triangular matrix example:
$$\begin{bmatrix} 1 & 0 & 0 & 0 \\ 2 & 1 & 0 & 0 \\ 9 & 0 & 1 & 0 \\ 5 & 0 & 0 & 1 \end{bmatrix}$$
Extract upper triangular matrix in Python
In order to create an identity matrix in Python we will use the numpy library. And the first step will be to import it:
import numpy as np
Numpy has a lot of useful functions, and for this operation we will use the triu() function which will extract the upper triangle of a matrix.
Let’s create a sample matrix:
A = np.array([[3, 2, 5, 7],
[2, 1, 3, 4],
[3, 1, 9, 8],
[8, 7, 3, 2]])
print(A)
Sample matrix:
[[3 2 5 7]
[2 1 3 4]
[3 1 9 8]
[8 7 3 2]]
The final step is to extract the upper triangular matrix using Python:
U = np.triu(A)
print(U)
And you should get:
[[3 2 5 7]
[0 1 3 4]
[0 0 9 8]
[0 0 0 2]]
which looks identical to the sample upper triangular matrix used in the explanation section.
Extract lower triangular matrix in Python
In order to create an identity matrix in Python we will use the numpy library. And the first step will be to import it:
import numpy as np
Numpy has a lot of useful functions, and for this operation we will use the tril() function which will extract the lower triangle of a matrix.
Let’s create a sample matrix:
A = np.array([[2, 6, 5, 7],
[7, 3, 9, 4],
[9, 4, 8, 2],
[6, 2, 1, 5]])
print(A)
Sample matrix:
[[2 6 5 7]
[7 3 9 4]
[9 4 8 2]
[6 2 1 5]]
The final step is to extract the lower triangular matrix using Python:
L = np.tril(A)
print(L)
And you should get:
[[2 0 0 0]
[7 3 0 0]
[9 4 8 0]
[6 2 1 5]]
which looks identical to the sample lower triangular matrix used in the explanation section.
Conclusion
In this article we discussed the steps and intuition to calculate upper triangular matrix and lower triangular matrix using Python.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.