Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. class Matrix:
  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 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 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 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 get_data(self):
  75.         return self.data
  76.  
  77.     def transpose(self):
  78.         trans= [list(i) for i in zip(*self.data)]
  79.         return trans
  80.  
  81.     # def mean(self):
  82.     #     lst = self.data
  83.     #     result = [sum(x) for x in zip(*lst)]
  84.     #     for i, v in enumerate(result):
  85.     #         result[i] = v /len(lst)
  86.     #     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement