Solving Linear Equations
One of the most common applications of linear algebra is solving systems of linear equations. Consider the following system:
2x + 3y = 8
x - y = -1
We can represent this system in matrix form as Ax = b
, where:
A = [[2, 3], [1, -1]]
x = [[x], [y]]
b = [[8], [-1]]
NumPy’s linalg.solve()
function elegantly handles this:
import numpy as np
= np.array([[2, 3], [1, -1]])
A = np.array([8, -1])
b = np.linalg.solve(A, b)
x print(x) # Output: [2. 1.40000000e-16] (approximately [2, 0])
The output shows the solution x ≈ 2
and y ≈ 0
. Note that small numerical inaccuracies can sometimes lead to values extremely close to zero, rather than exactly zero.
Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors are fundamental concepts in linear algebra, crucial for understanding the behavior of linear transformations. NumPy’s linalg.eig()
function computes these:
= np.array([[1, 2], [2, 1]])
A = np.linalg.eig(A)
eigenvalues, eigenvectors print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
This code calculates the eigenvalues and corresponding eigenvectors of matrix A
. The eigenvectors are arranged column-wise in the eigenvectors
array.
Matrix Decompositions
NumPy supports various matrix decompositions, including LU, QR, and SVD decompositions. These decompositions are vital for solving diverse problems like least squares fitting and solving eigenvalue problems efficiently.
Singular Value Decomposition (SVD):
SVD decomposes a matrix into three matrices: U
, S
, and V
. This decomposition is especially useful for dimensionality reduction and finding the rank of a matrix.
= np.array([[1, 2], [3, 4], [5,6]])
A = np.linalg.svd(A)
U, S, V print("U:\n", U)
print("S:\n", S)
print("V:\n", V)
The S
array contains the singular values, which are essentially the square roots of the eigenvalues of A*A.T
(or A.T*A
).
Matrix Inverse and Determinant
Calculating the inverse of a matrix and its determinant are also easily accomplished using NumPy.
= np.array([[2, 1], [1, -1]])
A = np.linalg.inv(A)
inverse_A = np.linalg.det(A)
determinant_A
print("Inverse of A:\n", inverse_A)
print("Determinant of A:", determinant_A)
This demonstrates how straightforwardly NumPy handles these core linear algebra operations. These functions offer significant performance advantages over manual calculation, particularly for large matrices.