Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix:
- def __init__(self, body):
- self.body = body
- def nrow(self):
- c=len(self.body)
- return c
- def ncol(self):
- return len(self.body[0])
- def __str__(self):
- s=''
- for i in range(self.nrow()):
- s1=(" ".join(str(self.body[i][x]) for x in range(len(self.body[i]))))
- s+=s1+'\n'
- return s
- def inplace(self, llen,i):
- if i>=0 and llen>i:
- return True
- else:
- return False
- def matrixColSum(self, i, j, coef, inplace):
- if inplace==True:
- for x in range(self.nrow()):
- self.body[x].insert(i,(self.body[x][i]+self.body[x][j]*coef))
- del(self.body[x][i+1])
- return self
- else:
- if j>=self.ncol():
- for x in range(self.nrow()):
- self.body[x].append([]*(j+1-self.ncol()))
- return self
- else:
- for x in range(self.nrow()):
- self.body[x].insert(i,self.body[x][j]*coef)
- return self
- def matrixRowSum(self, i, j, coef, inplace):
- if inplace==True:
- for x in range(self.ncol()):
- self.body[i].insert(x,(self.body[i][x]+self.body[j][x]*coef))
- del(self.body[i][x+1])
- return self
- else:
- if j>=self.nrow():
- for x in range(self.nrow(),j+1):
- self.body.append([])
- return self
- else:
- for x in range(self.nrow(),i+1):
- self.body.append([])
- for p in range(self.ncol()):
- self.body[x].insert(p,self.body[j][p]*coef)
- return self
- def matrixColCoef(self, i, coef,inplace):
- if coef==None:
- return "coef==None"
- if inplace==True:
- for x in range(self.nrow()):
- self.body[x].insert(i,self.body[x][i]*coef)
- del(self.body[x][i+1])
- return self
- else:
- prod=[]
- for i in range(self.nrow()):
- prod.append([])
- for j in range(self.ncol()):
- prod[i].insert(j,self.body[i][j])
- for x in range(self.nrow()):
- prod.insert(i,[])
- return prod
- def matrixRowCoef(self, i, coef, inplace):
- if coef==None:
- return "coef==None"
- if inplace==True:
- l=[]
- for x in range(self.ncol()):
- l.append(self.body[i][x]*coef)
- self.body.insert(i,l)
- del(self.body[i+1])
- return self
- else:
- prod=[]
- for i in range(self.nrow()):
- prod.append([])
- for j in range(self.ncol()):
- prod[i].insert(j,self.body[i][j])
- prod.insert(i,[]*coef)
- m=Matrix(prod)
- return m
- def matrixMult(self, a):
- if a==None:
- return "a==None"
- if type(a) is Matrix:
- a=a.body
- if (self.nrow()!=len(a[0])):
- return None
- else:
- prod=[]
- for i in range(self.nrow()):
- prod.append([])
- for j in range(len(a[0])):
- res=0
- for x in range(self.ncol()):
- res+=a[x][j]*self.body[i][x]
- prod[i].append(res)
- m=Matrix(prod)
- return m
- def matrixSwapRow(self,i,j,inplace):
- if inplace==True:
- a=self.body[i]
- self.body[i]=self.body[j]
- self.body[j]=a
- return self.body
- else:
- a=[]
- for i in range(self.nrow()):
- a.append([])
- for j in range(self.ncol()):
- a[i].insert(j,self.body[i][j])
- for x in range(self.nrow(),j+1):
- a.append([])
- a[j]=a[i]
- a[i]=[]*self.ncol()
- return a
- def matrixSwapCol(self,i,j,inplace):
- if inplace==True:
- for x in range(self.nrow()):
- a=self.body[x][i]
- self.body[x].insert(i,self.body[x][j])
- del(self.body[x][i+1])
- self.body[x].insert(j,a)
- del(self.body[x][j+1])
- return self
- else:
- a=[]
- for i in range(self.nrow()):
- a.append([])
- for j in range(self.ncol()):
- a[i].insert(j,self.body[i][j])
- for p in range(self.nrow()):
- a[p].append([]*(j+1-self.ncol()))
- a[p][j]=a[p][i]
- a[p][i]=[]*self.ncol()
- m=Matrix(a)
- return m
- def matrixTranspose(self):
- l=[]
- for x in range(self.ncol()):
- l.append([])
- for p in range(self.nrow()):
- l[x].append(self.body[p][x])
- m=Matrix(l)
- return m
- def sumOfD(self):
- if self.ncol()==self.nrow():
- sum=0
- for x in range(self.nrow()):
- sum+=self.body[x][self.ncol()-1-x]
- if (self.ncol()-1-x)!=x:
- sum+=self.body[x][x]
- return sum
- else:
- return "self.ncol()!=self.nrow()"
- def solve_linear_system(self,slv):
- a=[]
- for i in range(self.nrow()):
- a.append([])
- for j in range(self.ncol()):
- a[i].insert(j,self.body[i][j])
- for x in range(self.nrow()):
- a[x].append(slv[x])
- x = [a[i][-1] for i in range(len(a))]
- for k in range(1,len(a)-1):
- for j in range(k,len(a)-1):
- m = a[j][k-1]/a[k-1][k-1]
- for i in range(len(a)-1):
- a[j][i] -= m*a[k-1][i]
- x[j] -= m*x[k-1]
- for i in range(len(a)-1,-1,-1):
- for j in range(i+1,len(a)-1):
- x[i] -= a[i][j]*x[j]
- x[i] = x[i]/a[i][i]
- return x
- a=Matrix([[1,2,3],[4,5,6],[7,8,9]])
- i=1
- j=1
- coef=1
- """
- print(a)
- print(a.matrixSwapCol(i,j,a.inplace(a.ncol(),max(i,j))))
- print(a.matrixSwapRow(i,j,a.inplace(a.ncol(),max(i,j))))
- print(a.matrixColCoef(j,coef,a.inplace(a.nrow(),i)))
- print(a.matrixRowCoef(j,coef,a.inplace(a.nrow(),i)))
- print(a.matrixColSum(i,j,coef,a.inplace(a.ncol(),max(i,j))))
- print(a.matrixColSum(i,j,coef,a.inplace(a.ncol(),max(i,j))))
- print(a.matrixTranspose())
- print(a.sumOfD())
- """
- print(a.matrixRowCoef(j,coef,a.inplace(a.nrow(),i)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement