Special Matrices - Identity, Diagonal, Orthogonal
Special Matrices: Identity, Diagonal, Orthogonal
Section titled “Special Matrices: Identity, Diagonal, Orthogonal”Some matrices have special properties that make them particularly important in machine learning and linear algebra. In this lesson, we’ll cover identity matrices, diagonal matrices, and orthogonal matrices.
Identity Matrix
Section titled “Identity Matrix”The identity matrix is a square matrix with ones on the diagonal and zeros elsewhere:
Property:
for any matrix of compatible dimensions.
::: info Explanation of Identity The identity matrix is like the number 1 for matrices. Multiplying by leaves the matrix unchanged.
- In ML: shows up in regularization (e.g., in Ridge regression).
:::
Diagonal Matrix
Section titled “Diagonal Matrix”A diagonal matrix has nonzero entries only on its main diagonal:
Property: multiplying a vector scales each coordinate by the corresponding diagonal element.
Mini Example:
::: info Explanation of Diagonal Diagonal matrices represent scaling transformations.
- In ML: eigenvalues often appear in diagonal matrices after decomposition (PCA, SVD).
:::
Orthogonal Matrix
Section titled “Orthogonal Matrix”A square matrix is orthogonal if:
This means:
- Rows (and columns) of are orthonormal vectors (unit length and mutually perpendicular).
- .
Mini Example:
Here, .
::: info Explanation of Orthogonal Orthogonal matrices preserve lengths and angles. They represent rotations and reflections.
- In ML: orthogonal transformations are used in PCA, whitening, and optimization stability.
:::
Hands-on with Python and Rust
Section titled “Hands-on with Python and Rust”::: code-group
import numpy as np
# Identity matrixI = np.eye(3)
# Diagonal matrixD = np.diag([2, 3])
# Orthogonal matrix (rotation by 90 degrees)Q = np.array([[0, 1], [-1, 0]])
print("Identity matrix:\n", I)print("Diagonal matrix:\n", D)print("Orthogonal matrix:\n", Q)print("Check Q^T Q = I:\n", Q.T.dot(Q))use ndarray::{array, Array2};use ndarray_linalg::Norm;
fn main() { // Identity matrix let i: Array2<f64> = Array2::eye(3);
// Diagonal matrix let d: Array2<f64> = array![[2.0, 0.0], [0.0, 3.0]];
// Orthogonal matrix (rotation by 90 degrees) let q: Array2<f64> = array![[0.0, 1.0], [-1.0, 0.0]]; let qtq = q.t().dot(&q);
println!("Identity matrix:\n{:?}", i); println!("Diagonal matrix:\n{:?}", d); println!("Orthogonal matrix:\n{:?}", q); println!("Q^T Q =\n{:?}", qtq);}:::
Connection to ML
Section titled “Connection to ML”- Identity → regularization, basis for “do nothing” transformations.
- Diagonal → scaling features, eigenvalues, covariance structure.
- Orthogonal → rotations, PCA, numerical stability in optimization.
Next Steps
Section titled “Next Steps”Continue to Rank, Determinant, and Inverses.