NumPy模块中提供了线性代数函数库linalg,该库包含了线性代数所需的所有功能
numpy.linalg.det()可以计算方阵的行列式
import numpy as np A = np.array([[1,2], [1,1]]) try: A_det = np.linalg.det(A) except Exception as e: print(e) else: print("det(A) = ", A_det)numpy.linalg.inv()可以计算方阵的逆矩阵
import numpy as np A = np.array([[1,2], [1,1]]) try: A_inv = np.linalg.inv(A) except Exception as e: print(e) else: print("The inverse matrix of A is") print(A_inv)numpy.linalg.solve()可以求解有唯一解的线性方程组Ax=b
import numpy as np A = np.array([[1,2], [1,1]]) b = np.array([[1,0], [0,1]]) try: X = np.linalg.solve(A, b) except Exception as e: print(e) else: print("The solution of AX=b is") print(X)numpy.linalg.eig()可以计算方阵的特征值λ和特征向量X0,即(A-λE)X0=0,其中E为单位矩阵
import numpy as np A = np.array([[1, 2], [1, 1]]) try: A_lambda, X0 = np.linalg.eig(A) except Exception as e: print(e) else: print("The eigenvalues of A has", A_lambda) print("The eigenvector of A has") print(X0)