def RandInt(i,j):
if i==0:
return random.randint(0,10000)%(j+1)
else:
return random.randint(0,10000)%(j-i+1)+i
def zh(a,n):
for i in range(0,n):
a[i] = i+1
for i in range(1,n):
a1 = i
a2 = RandInt(0,i)
tmp = a[a1]
a[a1] = a[a2]
a[a2] = tmp
#print(a)
return a
def Matrix_Trans(X): #矩阵转置
XT = np.transpose(X)
#print(XT)
return XT
def zhihuan_R(x,m,n): #矩阵行置换
# print(x)
# print(m,n)
x1 = [ [0] * n for i in range(m) ]
zh1 = [0]*(n+1)
zh1 = zh(zh1,n)
zh1[n] = zh1[0]
for i in range(0,n):
t = zh1[i]
t1 = zh1[i+1]
for j in range(0,m):
x1[j][t-1] = x[j][t1-1]
return x1
def zhihuan_L(x,m,n): #矩阵列置换
x1 = [ [0] * n for i in range(m) ]
zh1 = [0]*(m+1)
zh1 = zh(zh1,m)
zh1[m] = zh1[0]
for i in range(0,m):
t = zh1[i]
t1 = zh1[i+1]
for j in range(0,n):
x1[t-1][j] = x[t1-1][j]
return x1
def Matrix_Multiply(a,b,m,n,s): #两个矩阵相乘
c=[[0 for i in range(s)] for i in range(m)]
for i in range(0,m):
for j in range(0,s):
c[i][j]=0
for z in range(0,n):
c[i][j]+=a[i][z]*b[z][j]
#print(c)
return c
def Matrix_Multiplication_R(a,m,n): #矩阵的每一行分别乘不同的k
for i in range(0,n):
k=random.randint(2,10)
#print(k)
for j in range(0,m):
a[j][i]=a[j][i]*k
#print(a)
return a
def Matrix_Multiplication_L(a,m,n): #矩阵的每一列分别乘不同的k
for i in range(0,m):
k=random.randint(2,10)
for j in range(0,n):
a[i][j]=a[i][j]*k
#print(a)
return a
def Matrix_Addition_R(a,m,n): #矩阵的某行*k加到零一行
i=random.randint(0,n-1)
j=random.randint(0,n-1)
k = random.randint(2, 10)
#print(i,j,k)
for c in range(0,m):
a[c][j]=a[c][j]+a[c][i]*k
#print(a)
return a
def Matrix_Addition_L(a,m,n): #矩阵的某列*k加到另一列
i=random.randint(0,m-1)
j=random.randint(0,m-1)
k = random.randint(2, 10)
for c in range(0,n):
a[j][c]=a[j][c]+a[i][c]*k
#print(a)
return a
def Vector_mul_Matrix(r,a,m,n): #向量乘矩阵
c = [[0 for i in range(n)]]
for i in range(0, n):
c[0][i] = 0
for z in range(0, m):
c[0][i] += r[0][z]*a[z][i]
#print(c)
return c
def Matrix_mul_Vector(R,y,m,n): #矩阵乘向量
c = [[0 for i in range(1)] for i in range(m)]
for i in range(0, m):
c[i][0] = 0
for z in range(0, n):
c[i][0] += R[i][z] * y[z][0]
#print(c)
return c
def Matrix_Inverse(a): #矩阵求逆
b = np.linalg.inv(a)
#print(b)
return b
需要导入的库有
import numpy
from numpy import dot
import numpy as np
import datetime
import random
import time
矩阵变换是线性代数中矩阵的一种运算形式。
(1) 交换矩阵的两行(对调i,j,两行记为ri,rj);
(2) 以一个非零数k乘矩阵的某一行所有元素(第i行乘以k记为ri×k);
(3) 把矩阵的某一行所有元素乘以一个数k后加到另一行对应的元素(第j行乘以k加到第i行记为ri+krj)。
类似地,把以上的“行”改为“列”便得到矩阵初等变换的定义,把对应的记号“r”换为“c”。
矩阵的初等行变换与初等列变换合称为矩阵的初等变换。
最近没时间写详细的,有需要详细解释的可私聊我