Beyond the fundamental types like square, identity, and zero matrices, certain structural patterns within square matrices are particularly useful in computations. Let's look at diagonal and triangular matrices.
A diagonal matrix is a square matrix where all the elements off the main diagonal are zero. The elements on the main diagonal can be any value, including zero.
Think of it like this: if you have a square matrix A, it's diagonal if aij=0 whenever i=j.
Here's a 3×3 example:
D=7000−20003Notice that only the elements a11, a22, and a33 (the main diagonal) are potentially non-zero. The identity matrix I we saw earlier is a special case of a diagonal matrix where all diagonal elements are exactly 1.
Diagonal matrices are computationally convenient. Many operations simplify significantly when dealing with them.
Creating Diagonal Matrices with NumPy
NumPy provides the handy np.diag()
function. This function has dual behavior:
np.diag()
, it creates a square matrix with those elements along the main diagonal and zeros elsewhere.np.diag()
, it extracts the main diagonal elements and returns them as a 1D array.Let's see how to create a diagonal matrix:
import numpy as np
# Create a diagonal matrix from a list of diagonal elements
diag_elements = [4, -1, 6]
D = np.diag(diag_elements)
print("Diagonal elements:", diag_elements)
print("Resulting diagonal matrix D:\n", D)
Output:
Diagonal elements: [4, -1, 6]
Resulting diagonal matrix D:
[[ 4 0 0]
[ 0 -1 0]
[ 0 0 6]]
Now, let's extract the diagonal from an existing matrix:
# A sample matrix
A = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# Extract the main diagonal
diagonal_of_A = np.diag(A)
print("Original matrix A:\n", A)
print("Extracted diagonal:", diagonal_of_A)
Output:
Original matrix A:
[[1 2 3]
[4 5 6]
[7 8 9]]
Extracted diagonal: [1 5 9]
Triangular matrices are square matrices that have all zeros either above or below the main diagonal. There are two types:
An upper triangular matrix has all its non-zero elements on or above the main diagonal. All elements below the main diagonal are zero. Formally, for a matrix U, we have uij=0 if i>j.
Example:
U=100250369Conversely, a lower triangular matrix has all its non-zero elements on or below the main diagonal. All elements above the main diagonal are zero. Formally, for a matrix L, we have lij=0 if i<j.
Example:
L=147058009Visualizing the Structure
The names "upper" and "lower" come from where the non-zero elements form a triangle shape.
Structure of Diagonal, Upper Triangular, and Lower Triangular 3x3 matrices. Colored cells represent potentially non-zero elements; '0' represents required zero elements.
Triangular Matrices with NumPy
NumPy provides functions to extract these triangular parts from an existing matrix:
np.triu()
(Triangle Upper): Returns a copy of the matrix with elements below the main diagonal zeroed out.np.tril()
(Triangle Lower): Returns a copy of the matrix with elements above the main diagonal zeroed out.# Using the same matrix A from before
A = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# Get the upper triangle (including diagonal)
upper_A = np.triu(A)
# Get the lower triangle (including diagonal)
lower_A = np.tril(A)
print("Original matrix A:\n", A)
print("\nUpper triangular part of A:\n", upper_A)
print("\nLower triangular part of A:\n", lower_A)
Output:
Original matrix A:
[[1 2 3]
[4 5 6]
[7 8 9]]
Upper triangular part of A:
[[1 2 3]
[0 5 6]
[0 0 9]]
Lower triangular part of A:
[[1 0 0]
[4 5 0]
[7 8 9]]
Both np.triu()
and np.tril()
can take an optional second argument k
. If k=0
(the default), it uses the main diagonal. If k=1
, it refers to the diagonal above the main one; if k=-1
, it refers to the diagonal below the main one, and so on. This allows you to zero out elements relative to different diagonals.
Diagonal and triangular matrices might seem like specific cases, but they appear frequently in linear algebra algorithms, particularly when solving systems of linear equations and in techniques for breaking down matrices into simpler components (matrix factorizations). Recognizing these structures is a helpful step in understanding more advanced matrix operations.
© 2025 ApX Machine Learning