Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. class matrix:
  2. #you should not change this value on its own, but I'm going to let you
  3. array = []
  4. def __init__(self, arr):
  5. self.array = self.validate(arr)
  6. if(self.array == False):
  7. self.array = []
  8. def isSquare(self):
  9. return len(self.array) == len(self.array[0])
  10. def infnorm(self, v = array):
  11. max = 0
  12. for x in v:
  13. if abs(x) > max:
  14. max = x
  15. return max
  16. #row b + c times row a
  17. def rowAdd(self, a, b, c = 1):
  18. for i in range(len(self.array[b])):
  19. self.array[b][i] += c*self.array[a][i]
  20. #row a = c*row a
  21. def rowMul(self, a, c):
  22. for i in range(len(self.array[a])):
  23. self.array[a][i] *= c
  24.  
  25. def validate(self, arr):
  26. array = []
  27. for i in arr:
  28. if (type(i) in [list]):
  29. for j in i:
  30. if type(j) not in [int, float]:
  31. return False
  32. if(len(i) == len(arr[0])):
  33. array.append(i)
  34. else:
  35. return False
  36. elif type(i) in [int, float]:
  37. array.append([i])
  38. else:
  39. print("input array not valid")
  40. return False
  41. return array
  42.  
  43. def __str__(self):
  44. string = ""
  45. for i in self.array:
  46. string += str(i)
  47. string += "\n"
  48. return string
  49.  
  50. def __add__(self,other):
  51. array = self.array
  52. try:
  53. for i in range(len(other.array)):
  54. for j in range(len(other.array[0])):
  55. array[i][j] += other.array[i][j]
  56. except:
  57. print("arrays are probably not the same size")
  58. return self
  59. return matrix(array)
  60.  
  61. def __sub__(self,other):
  62. array = self.array
  63. try:
  64. for i in range(len(other.array)):
  65. for j in range(len(other.array[0])):
  66. array[i][j] -= other.array[i][j]
  67. except:
  68. print("arrays are probably not the same size")
  69. return self
  70. return matrix(array)
  71.  
  72.  
  73. def __mul__(self, other):
  74. otherA = []
  75. array = []
  76. if type(other) in {int, float}:
  77. for i in range(len(array)):
  78. otherA.append([])
  79. for j in range(len(array[0])):
  80. otherA.append(other*array[i][j])
  81. return otherA
  82.  
  83.  
  84. for i in range(len(self.array)):
  85. array.append([])
  86. for j in range(len(other.array[0])):
  87. array[i].append(0)
  88. try:
  89. for i in range(len(self.array)):
  90. for j in range(len(other.array[0])):
  91. for k in range(len(other.array)):
  92. array[i][j] += self.array[i][k] * other.array[k][j]
  93.  
  94. except Exception as e:
  95. print(e, ": arrays are probably the wrong size")
  96. return self
  97. return matrix(array)
  98.  
  99. def __len__(self):
  100. return len(self.array)
  101.  
  102. def __getitem__(self, index):
  103. return self.array[index]
  104. def __setitem__(self, key, value):
  105. self.array[key] = value
  106.  
  107. A = [[15,7,-7],[-1,1,1],[13,7,-5]]
  108.  
  109. def power(A, x = [[1],[0],[0]], y =[[0],[0],[0]], it = 0):
  110. p = 0
  111. z = 0
  112. x = matrix(x)
  113. A = matrix(A)
  114. y = A*x
  115. for i in range(len(y)):
  116. if abs(y[i][0]) > abs(z):
  117. z = y[i][0]
  118. p = i
  119. break
  120. for i in range(len(y)):
  121. x[i] = y[i][0]/z
  122. lbd = y[p]
  123. if(it < 5):
  124. return power(A, x, y, it+1)
  125. else:
  126. print(lbd)
  127. power(A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement