Concept 04: Determinants, Inverses & Singularity

▶ Interactive Demo: Determinant & Singularity Sandbox

Open the interactive demo below to squash the 2D plane down to a 1D line and observe when a matrix becomes singular (det = 0) and loses its inverse.


1. The Real-World Problem: Running the Movie Backward

Suppose your robot’s swerve drive kinematics software uses a matrix A to convert robot velocities [vx, vy] into wheel motor speeds:

   wheel_speeds = A · robot_velocity

During autonomous navigation, the problem is reversed:

“The wheel encoders tell us the 4 wheel speeds. How fast is the robot moving across the field?”

To solve for robot_velocity, the software must compute the Matrix Inverse (A⁻¹):

   robot_velocity = A⁻¹ · wheel_speeds
det > 0 (Invertible 2D) det = 0 (Singular 1D Line)

What happens if the matrix squashes 2D space down into a single 1D line? You lose information—it is impossible to reconstruct the original 2D speeds, and the software crashes with Division by Zero!


2. Solving It in Code (Java & WPILib)

First-Principles Java: 2x2 Matrix Inversion

// Matrix M = [[a, b], [c, d]]
double a = 2.0, b = 1.0;
double c = 1.0, d = 3.0;

// 1. Calculate Determinant: det(M) = a*d - b*c
double det = a * d - b * c; // 2*3 - 1*1 = 5.0

if (Math.abs(det) < 1e-9) {
    throw new IllegalArgumentException("Matrix is singular (cannot be inverted)!");
}

// 2. Invert Matrix: M^(-1) = (1/det) * [[d, -b], [-c, a]]
double invA =  d / det;
double invB = -b / det;
double invC = -c / det;
double invD =  a / det;

System.out.printf("Inverse Matrix: [[%.2f, %.2f], [%.2f, %.2f]]%n", invA, invB, invC, invD);

Production WPILib Matrix

import edu.wpi.first.math.Matrix;
import edu.wpi.first.math.Nat;
import edu.wpi.first.math.numbers.*;

// WPILib Matrix types: Matrix<Rows, Cols>
Matrix<N2, N2> mat = new Matrix<>(Nat.N2(), Nat.N2());
mat.set(0, 0, 2.0); mat.set(0, 1, 1.0);
mat.set(1, 0, 1.0); mat.set(1, 1, 3.0);

Matrix<N2, N2> inv = mat.inv(); // Invert matrix
double det = mat.det();          // Compute determinant

3. Bridge to Machine Learning: Linear Regression

In machine learning:


4. Review Checkpoints

Checkpoint 1

Matrix A = [[3, 1], [6, 2]].

  1. Compute det(A).
  2. Can this matrix be inverted?

Solution:

  1. det(A) = (3)(2) - (1)(6) = 6 - 6 = 0.0.
  2. No. The determinant is zero (singular), so its inverse does not exist.

Checkpoint 2

If a transformation matrix has det(A) = -1.0, what physical effect did it have on the coordinate grid?

Solution: A negative determinant means the grid was reflected (flipped inside out, like looking into a mirror).


← Concept 10: Matrix Transformations
Module 3 Overview
Module 4: Calculus →