Numpy - Linear Algebra
NumPy provides many functions for performing linear algebra operations, such as solving systems of linear equations, computing eigenvalues and eigenvectors, and singular value decomposition.
To solve a system of linear equations, you can use the linalg.solve
function from NumPy's linalg
module, which takes a coefficient matrix and an independent term and returns the solution to the system. For example:
import numpy.linalg as la
# Solve the system of equations 3x + y = 9 and x + 2y = 8
coeffs = np.array([[3, 1], [1, 2]])
depvars = np.array([9, 8])
solution = la.solve(coeffs, depvars)
print(solution) # [2. 3.]
To compute the eigenvalues and eigenvectors of a matrix, you can use the linalg.eig
function from NumPy's linalg
module, which takes an array and returns a tuple of the eigenvalues and eigenvectors. For example:
# Compute the eigenvalues and eigenvectors of a 2x2 matrix
x = np.array([[1, 2], [3, 4]])
eigenvals, eigenvecs = la.eig(x)
print(eigenvals) # [-0.37228132 5.37228132]
print(eigenvecs) # [[-0.82456484 -0.41597356]
# [ 0.56576746 -0.90937671]]
To compute the singular value decomposition of a matrix, you can use the linalg.svd
function from NumPy's linalg
module, which takes an array and returns a tuple of the singular values and singular vectors. For example:
# Compute the singular value decomposition of a 2x2 matrix
x = np.array([[1, 2], [3, 4]])
singvals, singvecs = la.svd(x)
print(singvals) # [5.4649857 0.36596619]
print(singvecs) # [[-0.68094473 -0.73135371]
# [-0.73135371 0.68094473]]
The singular value decomposition of a matrix is a factorization of the matrix into three matrices: a unitary matrix, a diagonal matrix, and the conjugate transpose of the unitary matrix. The diagonal elements of the diagonal matrix are the singular values of the matrix, and the columns of the unitary matrix are the corresponding left singular vectors, while the rows of the conjugate transpose of the unitary matrix are the right singular vectors.
The singular value decomposition is a useful tool for many applications, such as data compression, image processing, and machine learning. It can be used to reduce the rank of a matrix, or to approximate a matrix with a lower-rank matrix.