python中的矩阵运算

    科技2022-07-11  129

    python中的矩阵运算

    1-1 python矩阵运算所需模块1-2定义矩阵和进行相关的矩阵运算RREF numpy/scipy/sympy: 搬运: python矩阵的运算大全:

    python矩阵运算可以用numpy模块,也可以用scipy模块,主要运算包括以下几种:

    1-1 python矩阵运算所需模块

    import numpy as np import matplotlib.pyplot as plt import scipy.linalg as lg #scipy矩阵运算模块

    1-2定义矩阵和进行相关的矩阵运算

    a=np.array([[1,2],[3,4]]) #定义原始矩阵 print(a) print(lg.inv(a)) #求取矩阵的逆矩阵 print(lg.det(a)) #求取矩阵的行列式 b=np.array([6,14]) #定义线性方程组的结果向量 print(lg.solve(a,b)) #求解线性方程组的解 print(lg.eig(a)) #求取矩阵的特征值与特征向量 print("LU:",lg.lu(a)) #矩阵的LU分解 print("QR:",lg.qr(a)) #矩阵的QR分解 print("SVD:",lg.svd(a)) #矩阵的奇异值分解(SVD分解) print("Schur:",lg.schur(a)) #矩阵的Schur分解

    RREF

    搬运:Python3 矩阵求最简行阶梯矩阵

    import numpy as np from sympy import Matrix from sympy.matrices import dense # Matrix convert to array A_mat = Matrix([[1, 2, 1, 1], [2, 1, -2, -2], [1, -1, -4, 3]]) A_arr1 = np.array(A_mat.tolist()).astype(np.int32) A_arr2 = dense.matrix2numpy(A_mat, dtype=np.int32) # array convert to Matrix B_arr = np.array([[1, 2, 1, 1], [2, 1, -2, -2], [1, -1, -4, 3]]) B_mat = Matrix(B_arr) # RREF A_rref = np.array(A.rref()[0].tolist()).astype(np.int32) B_rref = (Matrix(B_arr).rref()[0].tolist()).astype(np.int32)
    Processed: 0.076, SQL: 8