Linear Algebra Module

Linear Systems, Elimination & Determinants

Systems of linear equations, Gaussian elimination with partial pivoting, Row Echelon Form (RREF), determinant properties, and signed volume scaling.

1. The Core Problem of Scientific Computing

At the heart of graphics physics engines, finite element analysis, circuit simulations, and neural network solvers lies a single fundamental mathematical problem: solving a system of $m$ linear equations with $n$ unknowns:
$$\begin{cases} a_{11} x_1 + a_{12} x_2 + \dots + a_{1n} x_n = b_1 \ a_{21} x_1 + a_{22} x_2 + \dots + a_{2n} x_n = b_2 \ \vdots \ a_{m1} x_1 + a_{m2} x_2 + \dots + a_{mn} x_n = b_m \end{cases} \iff \mathbf{A}\mathbf{x} = \mathbf{b}$$
where $\mathbf{A} \in \mathbb{R}^{m \times n}$ is the coefficient matrix, $\mathbf{x} \in \mathbb{R}^n$ is the unknown vector, and $\mathbf{b} \in \mathbb{R}^m$ is the target observation.
text
+-----------------------------------------------------------------------------------------+
|                                THE THREE EXISTENCE REGIMES                              |
+-----------------------------------------------------------------------------------------+
|                                                                                         |
|  1. UNIQUE SOLUTION:            rank(A) == rank([A | b]) == n                           |
|                                 Square invertible matrix, lines intersect at 1 point.   |
|                                                                                         |
|  2. INFINITELY MANY SOLUTIONS:  rank(A) == rank([A | b]) < n                            |
|                                 Underdetermined system, lines intersect along a line/plane
|                                                                                         |
|  3. NO SOLUTION (Inconsistent): rank(A) < rank([A | b])                                 |
|                                 Parallel planes; contradiction like 0 == 5.             |
+-----------------------------------------------------------------------------------------+

2. Gaussian Elimination & Row Echelon Form

To solve $\mathbf{A}\mathbf{x} = \mathbf{b}$, we construct the Augmented Matrix $[\mathbf{A} \mid \mathbf{b}]$ and apply elementary row operations that preserve the solution set:
  1. Row Swap: $R_i \leftrightarrow R_j$
  2. Row Scaling: $R_i \leftarrow c R_i$ ($c \neq 0$)
  3. Row Addition: $R_i \leftarrow R_i + c R_j$

2.1 Row Echelon Form (REF) & Reduced REF (RREF)

Through forward elimination, the augmented matrix is converted into upper-triangular Row Echelon Form (REF):
$$\begin{bmatrix} \mathbf{p_1} & * & * & * & \mid & * \ 0 & \mathbf{p_2} & * & * & \mid & * \ 0 & 0 & \mathbf{p_3} & * & \mid & * \ 0 & 0 & 0 & 0 & \mid & 0 \end{bmatrix}$$
where the leading non-zero entry of each row is a pivot. In Reduced Row Echelon Form (RREF):
  1. Every pivot is scaled to strictly $1$.
  2. Every entry above and below a pivot is eliminated to strictly $0$.
Once in RREF, solutions can be read off immediately via Back-Substitution in $O(n^2)$ arithmetic operations.

3. Numerical Stability: Partial Pivoting

In textbook manual arithmetic, any non-zero pivot can be chosen. In real-world software engineering with 64-bit IEEE-754 floating-point numbers, naive Gaussian elimination fails catastrophically if a tiny pivot is chosen!

Catastrophic Floating-Point Cancellation Example:

Consider solving: $$\begin{bmatrix} 0.0001 & 1.0 \ 1.0 & 1.0 \end{bmatrix} \begin{bmatrix} x_1 \ x_2 \end{bmatrix} = \begin{bmatrix} 1.0 \ 2.0 \end{bmatrix}$$ The exact mathematical solution is $x_1 \approx 1.0001, x_2 \approx 0.9999$.
If we eliminate using $0.0001$ as the pivot: $$R_2 \leftarrow R_2 - \left(\frac{1.0}{0.0001}\right) R_1 = R_2 - 10000 R_1$$ $$(1.0 - 10000 \times 1.0) x_2 = 2.0 - 10000 \times 1.0 \implies -9999 x_2 = -9998 \implies x_2 \approx 1.0$$ Substituting back into $R_1$: $$0.0001 x_1 + 1.0(1.0) = 1.0 \implies 0.0001 x_1 = 0 \implies x_1 = 0.0 \quad (\text{100% relative error!})$$

The Partial Pivoting Strategy:

At step $k$, scan column $k$ from row $k$ down to row $n$. Find the row with the maximum absolute value:
$$\text{pivot row } p = \arg\max_{i \ge k} |A_{ik}|$$
Swap row $k$ with row $p$ before eliminating. Partial pivoting ensures multipliers $|L_{ik}| = |A_{ik} / A_{kk}| \le 1.0$, preventing exponential growth of roundoff error.

4. Determinants: Mathematical & Geometric Meaning

For an $n \times n$ square matrix $\mathbf{A}$, the Determinant $\det(\mathbf{A}) \in \mathbb{R}$ characterizes invertibility and volume scaling.

4.1 Axiomatic Definition

The determinant is the unique multilinear alternating function of the matrix columns such that: $$\det(\mathbf{I}) = 1$$

4.2 Key Algebraic Properties:

  1. $\det(\mathbf{A}\mathbf{B}) = \det(\mathbf{A}) \cdot \det(\mathbf{B})$.
  2. $\det(\mathbf{A}^{-1}) = \frac{1}{\det(\mathbf{A})}$ (Matrix is invertible if and only if $\det(\mathbf{A}) \neq 0$).
  3. $\det(\mathbf{A}^T) = \det(\mathbf{A})$.
  4. Swapping two rows/columns multiplies the determinant by $-1$.
  5. Scaling a row by scalar $c$ scales the determinant by $c$ ($\det(c \mathbf{A}) = c^n \det(\mathbf{A})$).
  6. For an upper or lower triangular matrix, the determinant is the product of its diagonal entries: $$\det(\mathbf{U}) = \prod_{i=1}^n U_{ii}$$

4.3 Geometric Interpretation: Signed Volume Scaling Factor

The determinant $\det(\mathbf{A})$ represents the factor by which the linear transformation scales $n$-dimensional hyper-volumes:
text
Geometric Transformation of Unit Square:
     ^                                         ^
   1 +---+ (Area = 1.0)                        |       /------/ (Area = |det(A)|)
     |   |                  ==== A ====>       |     /      /
   0 +---+-------->                            |   /------/
     0   1                                     +------------------>
  • In 2D: $\det(\mathbf{A})$ is the area of the parallelogram formed by the transformed basis vectors.
  • In 3D: $\det(\mathbf{A})$ is the volume of the transformed parallelepiped.
  • The sign of $\det(\mathbf{A})$ indicates orientation:
    • $\det(\mathbf{A}) > 0$: Preserves orientation (right-handed coordinate system stays right-handed).
    • $\det(\mathbf{A}) < 0$: Inverts orientation (reflection, like looking in a mirror).

5. Algorithmic Complexity: Why Never Use Cramer's Rule

Cramer's Rule expresses solutions analytically via determinant ratios: $$x_i = \frac{\det(\mathbf{A}_i)}{\det(\mathbf{A})}$$ Computing an $n \times n$ determinant via Laplace cofactor expansion requires $O(n!)$ operations! For $n = 30$, $30! \approx 2.65 \times 10^{32}$ operations—taking longer than the age of the universe!
In contrast:
  • Gaussian Elimination with Partial Pivoting: $$\text{Arithmetic Flops} = \sum_{k=1}^n 2k^2 \approx \frac{2}{3} n^3 \text{ operations}$$ For $n = 1000$, $\frac{2}{3}(1000)^3 \approx 6.7 \times 10^8$ operations (executed in under $0.05$ seconds on modern CPUs).

6. Python Implementation: Gaussian Elimination with Partial Pivoting

python
import numpy as np

def gaussian_elimination_solve(A: np.ndarray, b: np.ndarray) -> np.ndarray:
    """
    Solve Ax = b using Gaussian Elimination with Partial Pivoting.
    Time Complexity: O(n^3).
    """
    n = len(b)
    # Construct augmented matrix [A | b] as float64
    M = np.hstack([A.astype(float), b.astype(float).reshape(-1, 1)])

    # Forward Elimination with Partial Pivoting
    for k in range(n):
        # 1. Pivot selection: find largest absolute value in column k from row k to n-1
        pivot_row = k + np.argmax(np.abs(M[k:, k]))
        if np.abs(M[pivot_row, k]) < 1e-12:
            raise ValueError("Matrix is singular or near-singular!")

        # 2. Swap current row with pivot row
        if pivot_row != k:
            M[[k, pivot_row]] = M[[pivot_row, k]]

        # 3. Eliminate rows below pivot
        for i in range(k + 1, n):
            factor = M[i, k] / M[k, k]
            M[i, k:] -= factor * M[k, k:]

    # Back-Substitution
    x = np.zeros(n)
    for i in range(n - 1, -1, -1):
        x[i] = (M[i, -1] - np.dot(M[i, i + 1:n], x[i + 1:n])) / M[i, i]

    return x

if __name__ == "__main__":
    # Test on a 3x3 non-trivial linear system
    A_mat = np.array([
        [2.0, 1.0, -1.0],
        [-3.0, -1.0, 2.0],
        [-2.0, 1.0, 2.0]
    ])
    b_vec = np.array([8.0, -11.0, -3.0])

    sol = gaussian_elimination_solve(A_mat, b_vec)
    print("=== Gaussian Elimination Solution ===")
    print(f"Computed x: {sol}")

    # Verify residual: ||A*x - b|| == 0
    residual = np.linalg.norm(A_mat @ sol - b_vec)
    print(f"Residual Norm: {residual:.2e}")
    assert np.isclose(residual, 0.0, atol=1e-10)

7. Key Takeaways

  1. Partial Pivoting is Essential: Swapping rows to pick the maximum absolute pivot prevents catastrophic floating-point cancellation.
  2. Determinant is Volume Scaling: $\det(\mathbf{A})$ measures how hyper-volume expands or shrinks under transformation.
  3. Complexity $O(n^3)$: Never invert matrices directly or use Cramer's rule; solve linear systems via triangular factorization.