Determinant & Solving Equations
Determinants tell you if a matrix has an inverse, and solving linear equations is like solving puzzles. Think of it like figuring out how many solutions a system of equations has.
Determinant
np.linalg.det() gives the determinant. If it's 0, no inverse exists.
import numpy as np
A = np.array([[1, 2], [3, 4]])
det = np.linalg.det(A)
print(f"Matrix:\n{A}")
print(f"Determinant: {det:.2f}")
For a 2x2 matrix [[a,b],[c,d]], determinant is ad - bc. Here: 1*4 - 2*3 = -2.
Solving Linear Equations
np.linalg.solve() finds x in Ax = b.
import numpy as np
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(f"Coefficient matrix:\n{A}")
print(f"Constants: {b}")
print(f"Solution: {x}")
Here is the thing - this solves 3x + y = 9 and x + 2y = 8. The solution is x=2, y=3.
Checking Your Solution
Always verify by plugging back in.
import numpy as np
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
check = A @ x
print(f"Solution: {x}")
print(f"A @ x: {check}")
print(f"Original b: {b}")
print(f"Close enough: {np.allclose(check, b)}")
One thing that confused me at first was when solve fails. It fails when the determinant is zero (singular matrix).
Try it Yourself →Key Takeaways
- np.linalg.det() computes the determinant
- Determinant 0 means no inverse exists
- np.linalg.solve() solves Ax = b systems
- Always verify solutions with A @ x