Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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:
NumPy's linear algebra module (numpy.linalg) provides tools to solve these systems with numerical stability.
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:
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.np.linalg.inv()When theoretical requirements strictly dictate computing the explicit inverse matrix $A^{-1}$ (such as calculating covariance parameter matrices in statistics):
Output:
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):
np.linalg.solve(A, b) preferred over np.linalg.inv(A) @ b?np.linalg.solve() is given a singular matrix?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.