Advertisement
Guest User

Matrices @ work 1

a guest
Aug 28th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. from random import randint
  2.  
  3. class Matrix:
  4.  
  5.  
  6.     def __init__(self, width, height):
  7.        
  8.         #initializes a matrix full of zeros
  9.        
  10.         if height <= 0 or width <= 0:
  11.             raise ValueError('Height and Width must be over 0')
  12.    
  13.         if type(height) != int or type(width) != int:
  14.             raise TypeError("height and width must be integers")
  15.  
  16.         self.width = width
  17.         self.height = height
  18.         self.matrix = [[0 for x in range(width)] for x in range(height)]
  19.  
  20.  
  21.     def row(self,i):
  22.        
  23.         return self.matrix[i]
  24.  
  25.  
  26.     def column(self,i):
  27.        
  28.         return [x[i] for x in self.matrix]
  29.  
  30.  
  31.     def Transpose(self):
  32.  
  33.         final = Matrix(self.width, self.height)
  34.  
  35.         final.matrix = [[x[y] for x in self.matrix] for y in range(self.width)]
  36.  
  37.         return final
  38.  
  39.  
  40.     def printMat(self):
  41.  
  42.         for x,y in enumerate(self.matrix):
  43.             print('row {0}: '.format(x),y)
  44.  
  45.  
  46.     def randomize(self):
  47.  
  48.         for a,b in enumerate(self.matrix):
  49.             for c,d in enumerate(b):
  50.                 self.matrix[a][c] = randint(1,9)
  51.  
  52.  
  53.     def __add__(self,o):
  54.  
  55.         if type(o) != Matrix:
  56.             raise TypeError("You can only add matrices to other matrices")
  57.        
  58.         if self.height != o.height or self.width != o.width:
  59.             raise ValueError("Matrices must be of equal dimensions. Matrix 1: {0}x{1}  Matrix 2: {2}x{3}".format(self.height, self.width, o.height, o.width))
  60.        
  61.         final = Matrix(self.height, self.width)
  62.        
  63.         for a,b in enumerate(final.matrix):
  64.             for c,d in enumerate(b):
  65.                 final.matrix[a][c] = self.matrix[a][c] + o.matrix[a][c]
  66.                
  67.         return final
  68.  
  69.  
  70.     def __sub__(self,o):
  71.  
  72.         if type(o) != Matrix:
  73.             raise TypeError("You can only subtract matrices from other matrices.")
  74.        
  75.         if self.height != o.height or self.width != o.width:
  76.             raise ValueError("Matrices must be of equal dimensions. Matrix 1: {0}x{1}  Matrix 2: {2}x{3}".format(self.height, self.width, o.height, o.width))
  77.        
  78.         final = Matrix(self.height, self.width)
  79.        
  80.         for a,b in enumerate(final.matrix):
  81.             for c,d in enumerate(b):
  82.                 final.matrix[a][c] = self.matrix[a][c] - o.matrix[a][c]
  83.                
  84.         return final
  85.  
  86.  
  87.     def __mul__(self,o):
  88.  
  89.         if type(o) != int and type(o) != float:
  90.             raise TypeError("You can only perform scalar multiplication on matrices with int or float: {0} '{1}'".format(o,type(o)))
  91.        
  92.         final = Matrix(self.height, self.width)
  93.  
  94.         for a,b in enumerate(final.matrix):
  95.             for c,d in enumerate(b):
  96.                 final.matrix[a][c] = self.matrix[a][c] * o
  97.                
  98.         return final
  99.  
  100.  
  101.     def __rmul__(self,o):
  102.  
  103.         if type(o) != int and type(o) != float:
  104.             raise TypeError("You can only perform scalar multiplication on matrices with int or float: {0} '{1}'".format(o,type(o)))
  105.        
  106.         final = Matrix(self.height, self.width)
  107.  
  108.         for a,b in enumerate(final.matrix):
  109.             for c,d in enumerate(b):
  110.                 final.matrix[a][c] = self.matrix[a][c] * o
  111.                
  112.         return final
  113.  
  114.     def __matmul__(self,o):
  115.  
  116.         if type(o) != Matrix:
  117.             raise TypeError("You cannot perform matrix multiplication on a non-matrix")
  118.  
  119.         if self.width != o.height:
  120.             raise ValueError("Matrix 2's height ({0}) must be equal to Matrix 1's width ({1})".format(o.height,self.width))
  121.        
  122.         final = Matrix(self.height, o.width)
  123.        
  124.         for x,y in enumerate(self.matrix):
  125.             for w,z in enumerate(o.Transpose().matrix):
  126.                 final.matrix[x][w] = dot(y,z)
  127.  
  128.         return final
  129.  
  130. def dot(a,b):
  131.  
  132.     numsum = 0
  133.  
  134.     for x,y in enumerate(a):
  135.         numsum+=a[x]*b[x]
  136.    
  137.     return numsum
  138.  
  139. a = Matrix(2,2)
  140. a.randomize()
  141. b = Matrix(2,3)
  142. b.randomize()
  143. c = Matrix(randint(1,9),randint(1,9))
  144. c.randomize()
  145. d = c.Transpose()
  146. d.randomize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement