import numpy as np from numpy.linalg import inv, qr, eig, det, pinv, svd, lstsq, solve from numpy.matlib import randn, trace x = np.array([[1,2,3],[4,5,6]]) y = np.array([[6,23],[-1,7],[8,9]]) print(x.dot(y)) # np.dot(x,y) ''' 1*6+2*(-1)+3*8=28 [[ 28 64] [ 67 181]] ''' x = np.array([[1,2,3],[4,5,6],[7,8,9]]) # 以一维数组的形式返回方阵对角线元素 print(np.diag(x)) # [1 5 9]
# 矩阵乘法 print(np.dot(x,y)) ''' [[ 30 36 42] [ 66 81 96] [102 126 150]] '''
# 计算对角线元素的和 print(trace(x)) # 15
# 计算矩阵行列式 det(x)
# 计算方阵的本征值和本征向量 eig(x)
# 计算方阵的逆 inv(x)
# 就算矩阵的Moose-Penrose的逆 pinv(x)
# 计算QR分解 qr(x)
# 计算奇异值分解(SVD) svd(x)
# 解线性方程组Ax = b ,其中A为一个方阵 A = np.array([[1,1,1],[0,2,5],[2,5,1]]) x = np.array([4.29411765,4.17647059,-2.47058824]) b = np.array([6,-4,27]) print(solve(A,b)) # [4.29411765,4.17647059,-2.47058824]
# 计算Ax = b的最小乘解 print(lstsq(A,b))