Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class matrix:
- #you should not change this value on its own, but I'm going to let you
- array = []
- def __init__(self, arr):
- self.array = self.validate(arr)
- if(self.array == False):
- self.array = []
- def isSquare(self):
- return len(self.array) == len(self.array[0])
- def infnorm(self, v = array):
- max = 0
- for x in v:
- if abs(x) > max:
- max = x
- return max
- #row b + c times row a
- def rowAdd(self, a, b, c = 1):
- for i in range(len(self.array[b])):
- self.array[b][i] += c*self.array[a][i]
- #row a = c*row a
- def rowMul(self, a, c):
- for i in range(len(self.array[a])):
- self.array[a][i] *= c
- def validate(self, arr):
- array = []
- for i in arr:
- if (type(i) in [list]):
- for j in i:
- if type(j) not in [int, float]:
- return False
- if(len(i) == len(arr[0])):
- array.append(i)
- else:
- return False
- elif type(i) in [int, float]:
- array.append([i])
- else:
- print("input array not valid")
- return False
- return array
- def __str__(self):
- string = ""
- for i in self.array:
- string += str(i)
- string += "\n"
- return string
- def __add__(self,other):
- array = self.array
- try:
- for i in range(len(other.array)):
- for j in range(len(other.array[0])):
- array[i][j] += other.array[i][j]
- except:
- print("arrays are probably not the same size")
- return self
- return matrix(array)
- def __sub__(self,other):
- array = self.array
- try:
- for i in range(len(other.array)):
- for j in range(len(other.array[0])):
- array[i][j] -= other.array[i][j]
- except:
- print("arrays are probably not the same size")
- return self
- return matrix(array)
- def __mul__(self, other):
- otherA = []
- array = []
- if type(other) in {int, float}:
- for i in range(len(array)):
- otherA.append([])
- for j in range(len(array[0])):
- otherA.append(other*array[i][j])
- return otherA
- for i in range(len(self.array)):
- array.append([])
- for j in range(len(other.array[0])):
- array[i].append(0)
- try:
- for i in range(len(self.array)):
- for j in range(len(other.array[0])):
- for k in range(len(other.array)):
- array[i][j] += self.array[i][k] * other.array[k][j]
- except Exception as e:
- print(e, ": arrays are probably the wrong size")
- return self
- return matrix(array)
- def __len__(self):
- return len(self.array)
- def __getitem__(self, index):
- return self.array[index]
- def __setitem__(self, key, value):
- self.array[key] = value
- A = [[15,7,-7],[-1,1,1],[13,7,-5]]
- def power(A, x = [[1],[0],[0]], y =[[0],[0],[0]], it = 0):
- p = 0
- z = 0
- x = matrix(x)
- A = matrix(A)
- y = A*x
- for i in range(len(y)):
- if abs(y[i][0]) > abs(z):
- z = y[i][0]
- p = i
- break
- for i in range(len(y)):
- x[i] = y[i][0]/z
- lbd = y[p]
- if(it < 5):
- return power(A, x, y, it+1)
- else:
- print(lbd)
- power(A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement