Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. class Matrix2:
  2.  
  3.     def __init__(self, data): #takes in a double array no matter the size of it.
  4.         self.data = data
  5.         self.rows = len(data) #determines row by number of items in list
  6.         self.cols = len(data[0]) #determines columns by number of items withing list of a list
  7.         self.size = [self.rows, self.cols] #determines how big the matrix is by number of rows and columns
  8.  
  9.  
  10.     def multiply(self, other):
  11.  
  12.         if self.cols == other.rows: #number of columns in first matrix should be same as number of rows on 2nd matric
  13.  
  14.             result = [[0 for i in range(other.cols)] for i in range(self.rows)] #number of rows as 1st matrix + number of columns of 2nd matrix
  15.             #print(result)
  16.  
  17.             #iterate by rows of first matrix
  18.             for i in range(self.rows):
  19.  
  20.                 # iterating by columns of second matrix
  21.                 for j in range(other.cols):
  22.  
  23.                     # iterating by rows of second matrix
  24.                     for k in range(other.rows):
  25.  
  26.                         result[i][j] += self.data[i][k] * other.data[k][j]
  27.  
  28.  
  29.             return Matrix2(result)
  30.  
  31.  
  32.         else:
  33.             return "cant be multiplied"
  34.  
  35.  
  36.     def add(self, other): #works good
  37.         if self.size == other.size:
  38.             result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
  39.             for i in range(self.rows):
  40.                 # iterate through columns
  41.                 for j in range(other.cols):
  42.                     result[i][j] = self.data[i][j] + other.data[i][j]
  43.  
  44.             return Matrix2(result)
  45.  
  46.         else:
  47.             return " can't be added, dimensions are not equal"
  48.  
  49.     def subtract(self, other):
  50.         if self.size == other.size:
  51.             result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
  52.             for i in range(self.rows):
  53.                 # iterate through columns
  54.                 for j in range(other.cols):
  55.                     result[i][j] = self.data[i][j] - other.data[i][j]
  56.  
  57.             return Matrix2(result)
  58.  
  59.         else:
  60.             return " can't be subtracted, dimensions are not equal"
  61.  
  62.     def scalar_multi(self, number):
  63.  
  64.         self.data = [[i * number for i  in sublist] for sublist in self.data]  #returns scalar multiplication
  65.         return self.data
  66.         #     for x in range(self.cols):
  67.         #         i[x] = i[x]*number
  68.         # return self.data
  69.  
  70.     def print_matrix(self):
  71.         for i in self.data:
  72.             print(*i)
  73.  
  74.     def inverse(self):
  75.         arr = self.data  # arr is a list of a list
  76.         # Add identity matrix on the right
  77.         for i in range(self.rows):
  78.             for j in range(self.cols):
  79.                 if j == i:
  80.                     arr[i].append(1)
  81.                 else:
  82.                     arr[i].append(0)
  83.         # Reset all that is below the main diagonal
  84.         for i in range(self.rows):
  85.             arr[i] = [round(x * (1 / arr[i][i]), 4) for x in arr[i]]
  86.             for j in range(self.cols):
  87.                 try:
  88.                     j += i
  89.                     arr1 = [round(x * (-arr[j + 1][i]), 4) for x in arr[i]]
  90.                     arr[j + 1] = map(lambda x, y: round(x + y, 4), arr[j + 1], arr1)
  91.                 except IndexError:
  92.                     break
  93.         # Reset all that is above the main diagonal
  94.         for i in range(self.rows - 1, -1, -1):
  95.             arr[i] = [round(x * (1 / arr[i][i]), 4) for x in arr[i]]
  96.             for j in range(self.cols - 1, 0, -1):
  97.                 if j <= i:
  98.                     arr1 = [round(x * (-arr[j - 1][i]), 4) for x in arr[i]]
  99.                     arr[j - 1] = map(lambda x, y: round(x + y, 4), arr[j - 1], arr1)
  100.         arr2 = [arr[l][self.rows:] for l in range(self.rows)]
  101.         return Matrix2(arr2)
  102.  
  103.  
  104.     def get_data(self):
  105.         return self.data
  106.     def get_rows(self):
  107.         return self.rows
  108.     def get_colms(self):
  109.         return self.cols
  110.     def transpose(self):
  111.         trans= [list(i) for i in zip(*self.data)]
  112.         return Matrix2(trans)
  113.  
  114.     # def mean(self):
  115.     #     lst = self.data
  116.     #     result = [sum(x) for x in zip(*lst)]
  117.     #     for i, v in enumerate(result):
  118.     #         result[i] = v /len(lst)
  119.     #     return result
  120.  
  121.  
  122. one = Matrix2([[1,2],[7,9]])
  123. inverse = one.inverse()
  124. print(inverse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement