Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- class Matrix:
- def __init__(self, width, height):
- #initializes a matrix full of zeros
- if height <= 0 or width <= 0:
- raise ValueError('Height and Width must be over 0')
- if type(height) != int or type(width) != int:
- raise TypeError("height and width must be integers")
- self.width = width
- self.height = height
- self.matrix = [[0 for x in range(width)] for x in range(height)]
- def row(self,i):
- return self.matrix[i]
- def column(self,i):
- return [x[i] for x in self.matrix]
- def Transpose(self):
- final = Matrix(self.width, self.height)
- final.matrix = [[x[y] for x in self.matrix] for y in range(self.width)]
- return final
- def printMat(self):
- for x,y in enumerate(self.matrix):
- print('row {0}: '.format(x),y)
- def randomize(self):
- for a,b in enumerate(self.matrix):
- for c,d in enumerate(b):
- self.matrix[a][c] = randint(1,9)
- def __add__(self,o):
- if type(o) != Matrix:
- raise TypeError("You can only add matrices to other matrices")
- if self.height != o.height or self.width != o.width:
- 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))
- final = Matrix(self.height, self.width)
- for a,b in enumerate(final.matrix):
- for c,d in enumerate(b):
- final.matrix[a][c] = self.matrix[a][c] + o.matrix[a][c]
- return final
- def __sub__(self,o):
- if type(o) != Matrix:
- raise TypeError("You can only subtract matrices from other matrices.")
- if self.height != o.height or self.width != o.width:
- 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))
- final = Matrix(self.height, self.width)
- for a,b in enumerate(final.matrix):
- for c,d in enumerate(b):
- final.matrix[a][c] = self.matrix[a][c] - o.matrix[a][c]
- return final
- def __mul__(self,o):
- if type(o) != int and type(o) != float:
- raise TypeError("You can only perform scalar multiplication on matrices with int or float: {0} '{1}'".format(o,type(o)))
- final = Matrix(self.height, self.width)
- for a,b in enumerate(final.matrix):
- for c,d in enumerate(b):
- final.matrix[a][c] = self.matrix[a][c] * o
- return final
- def __rmul__(self,o):
- if type(o) != int and type(o) != float:
- raise TypeError("You can only perform scalar multiplication on matrices with int or float: {0} '{1}'".format(o,type(o)))
- final = Matrix(self.height, self.width)
- for a,b in enumerate(final.matrix):
- for c,d in enumerate(b):
- final.matrix[a][c] = self.matrix[a][c] * o
- return final
- def __matmul__(self,o):
- if type(o) != Matrix:
- raise TypeError("You cannot perform matrix multiplication on a non-matrix")
- if self.width != o.height:
- raise ValueError("Matrix 2's height ({0}) must be equal to Matrix 1's width ({1})".format(o.height,self.width))
- final = Matrix(self.height, o.width)
- for x,y in enumerate(self.matrix):
- for w,z in enumerate(o.Transpose().matrix):
- final.matrix[x][w] = dot(y,z)
- return final
- def dot(a,b):
- numsum = 0
- for x,y in enumerate(a):
- numsum+=a[x]*b[x]
- return numsum
- a = Matrix(2,2)
- a.randomize()
- b = Matrix(2,3)
- b.randomize()
- c = Matrix(randint(1,9),randint(1,9))
- c.randomize()
- d = c.Transpose()
- d.randomize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement