Matrix Determinant, Inversion & Solving Linear Systems
Solving Linear Systems: inv(), det(), & solve()
Systems of simultaneous linear equations arise everywhere: in engineering simulations, structural load calculations, econometric models, and machine learning normal equations.
A system of linear equations with $n$ variables is represented in matrix notation as:
$$A \mathbf{x} = \mathbf{b}$$
Where:
- $A$ is the $n \times n$ coefficient matrix.
- $\mathbf{x}$ is the vector of unknown variables.
- $\mathbf{b}$ is the vector of constants.
NumPy's linear algebra module (numpy.linalg) provides tools to solve these systems with numerical stability.
1. Matrix Determinants: np.linalg.det()
A square matrix $A$ has a unique inverse if and only if its determinant is non-zero ($\det(A) \neq 0$). A matrix with $\det(A) = 0$ is called singular or non-invertible:
2. Solving Systems: Why You Should NEVER Use inv()!
Mathematically, the analytical solution to $A \mathbf{x} = \mathbf{b}$ is:
$$\mathbf{x} = A^{-1} \mathbf{b}$$
Many beginners compute this directly using np.linalg.inv(A) @ b.
np.linalg.solve(A, b)! solve() uses LAPACK's optimized LU decomposition with partial pivoting, which is significantly faster and far more numerically precise.3. Matrix Inversion: np.linalg.inv()
When theoretical requirements strictly dictate computing the explicit inverse matrix $A^{-1}$ (such as calculating covariance parameter matrices in statistics):
Output:
4. Singular Matrices and Least-Squares: np.linalg.lstsq()
If a matrix is non-square (more equations than unknowns) or singular ($det(A) = 0$), np.linalg.solve() raises LinAlgError: Singular matrix.
To find the best-fit approximate solution that minimizes the sum of squared residuals $\|A\mathbf{x} - \mathbf{b}\|^2_2$, use Linear Least Squares (np.linalg.lstsq):
Multiple Choice Questions
1. What does a matrix determinant equal to zero ($det(A) = 0$) indicate?
A. The matrix is symmetric B. The matrix is singular and cannot be inverted C. All matrix elements are zero D. The matrix is an identity matrix Answer: B Explanation: A matrix with a determinant of zero has linearly dependent rows or columns, making it singular (non-invertible).
2. Why is np.linalg.solve(A, b) preferred over np.linalg.inv(A) @ b?
A. solve() is written in pure Python B. solve() uses LAPACK LU decomposition, which is faster and substantially more numerically stable than explicit matrix inversion C. np.linalg.inv only works on integers D. solve() returns integers only Answer: B Explanation: Computing an explicit matrix inverse introduces severe floating-point roundoff errors and requires extra computation. np.linalg.solve() solves the system directly via LU decomposition.
3. Which function solves overdetermined or singular linear systems by minimizing squared errors?
A. np.linalg.det B. np.linalg.lstsq C. np.linalg.inv D. np.linalg.norm Answer: B Explanation: np.linalg.lstsq() computes the least-squares solution to linear matrix equations, accommodating non-square and singular matrices.
4. What is the expected result of multiplying an invertible matrix $A$ with its inverse $A^{-1}$ ($A @ A^{-1}$)?
A. The zero matrix B. The identity matrix $I$ C. The transpose of $A$ D. A scalar determinant Answer: B Explanation: By definition of a matrix inverse, multiplying a matrix by its inverse yields the identity matrix ($A A^{-1} = I$).
5. What exception is raised if np.linalg.solve() is given a singular matrix?
A. ZeroDivisionError B. LinAlgError C. ValueError D. FloatingPointError Answer: B Explanation: When encountering a non-invertible matrix, NumPy's linear algebra routines raise a numpy.linalg.LinAlgError.
Eigenvalues, Eigenvectors & Singular Value Decomposition (SVD)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.