Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 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.             print("Can be multiplied")
  14.  
  15.             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
  16.             #print(result)
  17.  
  18.             #iterate by rows of first matrix
  19.             for i in range(self.rows):
  20.  
  21.                 # iterating by columns of second matrix
  22.                 for j in range(other.cols):
  23.  
  24.                     # iterating by rows of second matrix
  25.                     for k in range(other.rows):
  26.  
  27.                         result[i][j] += self.data[i][k] * other.data[k][j]
  28.  
  29.  
  30.             return(result)
  31.  
  32.  
  33.         else:
  34.             return "cant be multiplied"
  35.  
  36.  
  37.     def add(self, other): #works good
  38.         if self.size == other.size:
  39.             result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
  40.             for i in range(self.rows):
  41.                 # iterate through columns
  42.                 for j in range(other.cols):
  43.                     result[i][j] = self.data[i][j] + other.data[i][j]
  44.  
  45.             return result
  46.  
  47.         else:
  48.             return " can't be added, dimensions are not equal"
  49.  
  50.     def subtract(self, other):
  51.         if self.size == other.size:
  52.             result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
  53.             for i in range(self.rows):
  54.                 # iterate through columns
  55.                 for j in range(other.cols):
  56.                     result[i][j] = self.data[i][j] - other.data[i][j]
  57.  
  58.             return result
  59.  
  60.         else:
  61.             return " can't be subtracted, dimensions are not equal"
  62.  
  63.     def scalar_multi(self, number):
  64.  
  65.         self.data = [[number * 2 for number in sublist] for sublist in self.data]  #returns scalar multiplication
  66.         return self.data
  67.         #     for x in range(self.cols):
  68.         #         i[x] = i[x]*number
  69.         # return self.data
  70.  
  71.     def print_matrix(self):
  72.         for i in self.data:
  73.             print(*i)
  74.  
  75.     def get_data(self):
  76.         return self.data
  77.  
  78.     def transpose(self):
  79.         trans= [list(i) for i in zip(*self.data)]
  80.         return trans
  81.  
  82.     # def mean(self):
  83.     #     lst = self.data
  84.     #     result = [sum(x) for x in zip(*lst)]
  85.     #     for i, v in enumerate(result):
  86.     #         result[i] = v /len(lst)
  87.     #     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement