Advertisement
room404

clMatrix

Feb 18th, 2020
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. class Matrix:
  2. def __init__(self, body):
  3. self.body = body
  4. def nrow(self):
  5. c=len(self.body)
  6. return c
  7. def ncol(self):
  8. return len(self.body[0])
  9. def __str__(self):
  10. s=''
  11. for i in range(self.nrow()):
  12. s1=(" ".join(str(self.body[i][x]) for x in range(len(self.body[i]))))
  13. s+=s1+'\n'
  14. return s
  15. def inplace(self, llen,i):
  16. if i>=0 and llen>i:
  17. return True
  18. else:
  19. return False
  20. def matrixColSum(self, i, j, coef, inplace):
  21. if inplace==True:
  22. for x in range(self.nrow()):
  23. self.body[x].insert(i,(self.body[x][i]+self.body[x][j]*coef))
  24. del(self.body[x][i+1])
  25. return self
  26. else:
  27. if j>=self.ncol():
  28. for x in range(self.nrow()):
  29. self.body[x].append([]*(j+1-self.ncol()))
  30. return self
  31. else:
  32. for x in range(self.nrow()):
  33. self.body[x].insert(i,self.body[x][j]*coef)
  34. return self
  35. def matrixRowSum(self, i, j, coef, inplace):
  36. if inplace==True:
  37. for x in range(self.ncol()):
  38. self.body[i].insert(x,(self.body[i][x]+self.body[j][x]*coef))
  39. del(self.body[i][x+1])
  40. return self
  41. else:
  42. if j>=self.nrow():
  43. for x in range(self.nrow(),j+1):
  44. self.body.append([])
  45. return self
  46. else:
  47. for x in range(self.nrow(),i+1):
  48. self.body.append([])
  49. for p in range(self.ncol()):
  50. self.body[x].insert(p,self.body[j][p]*coef)
  51. return self
  52. def matrixColCoef(self, i, coef,inplace):
  53. if coef==None:
  54. return "coef==None"
  55. if inplace==True:
  56. for x in range(self.nrow()):
  57. self.body[x].insert(i,self.body[x][i]*coef)
  58. del(self.body[x][i+1])
  59. return self
  60. else:
  61. prod=[]
  62. for i in range(self.nrow()):
  63. prod.append([])
  64. for j in range(self.ncol()):
  65. prod[i].insert(j,self.body[i][j])
  66. for x in range(self.nrow()):
  67. prod.insert(i,[])
  68. return prod
  69. def matrixRowCoef(self, i, coef, inplace):
  70. if coef==None:
  71. return "coef==None"
  72. if inplace==True:
  73. l=[]
  74. for x in range(self.ncol()):
  75. l.append(self.body[i][x]*coef)
  76. self.body.insert(i,l)
  77. del(self.body[i+1])
  78. return self
  79. else:
  80. prod=[]
  81. for i in range(self.nrow()):
  82. prod.append([])
  83. for j in range(self.ncol()):
  84. prod[i].insert(j,self.body[i][j])
  85. prod.insert(i,[]*coef)
  86. m=Matrix(prod)
  87. return m
  88. def matrixMult(self, a):
  89. if a==None:
  90. return "a==None"
  91. if type(a) is Matrix:
  92. a=a.body
  93. if (self.nrow()!=len(a[0])):
  94. return None
  95. else:
  96. prod=[]
  97. for i in range(self.nrow()):
  98. prod.append([])
  99. for j in range(len(a[0])):
  100. res=0
  101. for x in range(self.ncol()):
  102. res+=a[x][j]*self.body[i][x]
  103. prod[i].append(res)
  104. m=Matrix(prod)
  105. return m
  106. def matrixSwapRow(self,i,j,inplace):
  107. if inplace==True:
  108. a=self.body[i]
  109. self.body[i]=self.body[j]
  110. self.body[j]=a
  111. return self.body
  112. else:
  113. a=[]
  114. for i in range(self.nrow()):
  115. a.append([])
  116. for j in range(self.ncol()):
  117. a[i].insert(j,self.body[i][j])
  118. for x in range(self.nrow(),j+1):
  119. a.append([])
  120. a[j]=a[i]
  121. a[i]=[]*self.ncol()
  122. return a
  123. def matrixSwapCol(self,i,j,inplace):
  124. if inplace==True:
  125. for x in range(self.nrow()):
  126. a=self.body[x][i]
  127. self.body[x].insert(i,self.body[x][j])
  128. del(self.body[x][i+1])
  129. self.body[x].insert(j,a)
  130. del(self.body[x][j+1])
  131. return self
  132. else:
  133. a=[]
  134. for i in range(self.nrow()):
  135. a.append([])
  136. for j in range(self.ncol()):
  137. a[i].insert(j,self.body[i][j])
  138. for p in range(self.nrow()):
  139. a[p].append([]*(j+1-self.ncol()))
  140. a[p][j]=a[p][i]
  141. a[p][i]=[]*self.ncol()
  142. m=Matrix(a)
  143. return m
  144. def matrixTranspose(self):
  145. l=[]
  146. for x in range(self.ncol()):
  147. l.append([])
  148. for p in range(self.nrow()):
  149. l[x].append(self.body[p][x])
  150. m=Matrix(l)
  151. return m
  152. def sumOfD(self):
  153. if self.ncol()==self.nrow():
  154. sum=0
  155. for x in range(self.nrow()):
  156. sum+=self.body[x][self.ncol()-1-x]
  157. if (self.ncol()-1-x)!=x:
  158. sum+=self.body[x][x]
  159. return sum
  160. else:
  161. return "self.ncol()!=self.nrow()"
  162. def solve_linear_system(self,slv):
  163. a=[]
  164. for i in range(self.nrow()):
  165. a.append([])
  166. for j in range(self.ncol()):
  167. a[i].insert(j,self.body[i][j])
  168. for x in range(self.nrow()):
  169. a[x].append(slv[x])
  170. x = [a[i][-1] for i in range(len(a))]
  171. for k in range(1,len(a)-1):
  172. for j in range(k,len(a)-1):
  173. m = a[j][k-1]/a[k-1][k-1]
  174. for i in range(len(a)-1):
  175. a[j][i] -= m*a[k-1][i]
  176. x[j] -= m*x[k-1]
  177. for i in range(len(a)-1,-1,-1):
  178. for j in range(i+1,len(a)-1):
  179. x[i] -= a[i][j]*x[j]
  180. x[i] = x[i]/a[i][i]
  181. return x
  182. a=Matrix([[1,2,3],[4,5,6],[7,8,9]])
  183. i=1
  184. j=1
  185. coef=1
  186. """
  187. print(a)
  188. print(a.matrixSwapCol(i,j,a.inplace(a.ncol(),max(i,j))))
  189. print(a.matrixSwapRow(i,j,a.inplace(a.ncol(),max(i,j))))
  190. print(a.matrixColCoef(j,coef,a.inplace(a.nrow(),i)))
  191. print(a.matrixRowCoef(j,coef,a.inplace(a.nrow(),i)))
  192. print(a.matrixColSum(i,j,coef,a.inplace(a.ncol(),max(i,j))))
  193. print(a.matrixColSum(i,j,coef,a.inplace(a.ncol(),max(i,j))))
  194. print(a.matrixTranspose())
  195. print(a.sumOfD())
  196. """
  197.  
  198. print(a.matrixRowCoef(j,coef,a.inplace(a.nrow(),i)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement