Informatika2-2014/FeladatMatrix
A MathWikiből
A lap korábbi változatát látod, amilyen Rpalovics (vitalap | szerkesztései) 2014. április 4., 08:33-kor történt szerkesztése után volt.
class Matrix: def const(self,n,x): self.A = [] for i in range(n): self.A.append([]) for j in range(n): self.A[i].append(x) def zeros(self,n): self.const(n,0) def printer(self): for row in self.A: print row def set(self,i,j,x): if len(self.A)>i: if len(self.A[i])>j: self.A[i][j] = x def get(self,i,j): if len(self.A)>i: if len(self.A[i])>j: return self.A[i][j] else: return 0 else: return 0 def -__init__(self,n,x): self.const(n,x) def __add__(self,other): new = Matrix(len(self.A),0) if (len(self.A) == len(other.A)): for i in range(len(self.A)): for j in range(len(self.A[i])): new.A[i][j] = self.A[i][j] + other.A[i][j] return new def __mul__(self,other): n = len(self.A) new = Matrix(n,0) for i in range(n): for j in range(n): for k in range(n): new.A[i][j] += self.A[i][k] * other.A[k][j] return new matrix = Matrix(6,1) matrix.printer() matrix.set(0,0,8) print "\n" matrix.printer() print matrix.get(0,0) print matrix.get(1,1) matrix2 = Matrix(6,1) matrix3 = Matrix(6,2) matrix3 = matrix2 * matrix3 matrix3.printer()