def __div__(self, other): """ Procedure: Scalar division of self. """ #Matrix Division if type(self) == Matrix and type(other) == Matrix: #Enforce the preconditions assert self.n == other.m, "The dimensions of the matrices are invalid." #Create the return matrix quotient_matrix = Matrix(unimath.zeros(self.m, other.n)) #Multiply the two matrices x = 1 y = 1 while y <= other.n: col_b = other.get_col(y) while x <= self.m: #Multiply the all rows by the column and get the product row_a = self.get_row(x) quotient = unimath.div(row_a, col_b) final_sum = unimath.listsum(product) #Set the product to the correct space in the product matrix quotient_matrix.set(x, y, final_sum) x += 1 #Reset the row counter and continue with the next column x = 1 y += 1 return quotient_matrix #Scalar division if type(other) == int or type(other) == float: for x in range(0, self.m): for y in range(0, self.n): self._data[x][y] = self._data[x][y] / other
def __sub__(self, other): """ Returns: The difference of two matrices OR Procedure: Scalar subtraction of self. Precondition: The two matrices must be the same size. """ #Matrix subtraction if type(self) == Matrix and type(other) == Matrix: #Enforce the preconditions assert self.m == other.m, "The two matrices differ in number of rows" assert self.n == other.n, "The two matrices differ in number of cols" #Create the return matrix difference_matrix = Matrix(unimath.zeros(self.m, other.n)) #Subtract the two matrices x = 1 while x <= other.m: row_a = self.get_row(x) row_b = other.get_row(x) difference = unimath.sub(row_a, row_b) difference_matrix.set_row(x,difference) x += 1 return difference_matrix #Scalar subtraction if type(other) == int or type(other) == float: for x in range(0, self.m): for y in range(0, self.n): self._data[x][y] = self._data[x][y] - other
def __add__(self, other): """ Returns: The sum of two matrices OR Procedure: Scalar addition of self. Precondition: The two matrices must be the same size. """ #Matrix Addition if type(self) == Matrix and type(other) == Matrix: #Enforce the preconditions assert self.m == other.m, "The two matrices differ in number of rows" assert self.n == other.n, "The two matrices differ in number of cols" #Create the return matrix sum_matrix = Matrix(unimath.zeros(self.m, other.n)) #Subtract the two matrices x = 1 while x <= other.m: row_a = self.get_row(x) row_b = other.get_row(x) summation = unimath.add(row_a, row_b) sum_matrix.set_row(x,summation) x += 1 return sum_matrix #Add a scalar to every value in the matrix if type(other) == int or type(other) == float: for x in range(0, self.m): for y in range(0, self.n): self._data[x][y] = self._data[x][y] + other
def identity(n): """ Returns: The identity matrix of size n """ identity_matrix = Matrix(unimath.zeros(n,n)) i = 1 while i <= n: identity_matrix.set(i, i, 1) i += 1 return identity_matrix
def trans(matrix): """ Returns: The transpose of a given matrix. """ transpose = unimath.zeros(matrix.n, matrix.m) transpose = Matrix(transpose) col_count = 1 while col_count <= matrix.n: col = matrix.get_col(col_count) ele_count = 1 #Set each value in the column to cooresponding row values for e in col: transpose.set(col_count, ele_count, e) ele_count += 1 #Repeat for the next column col_count += 1 return transpose
def __mul__(self, other): """ Returns: The product of two matrices such that [self] * [other] OR Procedure: Scalar multiplication of self by other. Precondition: self.n == other.m =========== Description =========== Matrix multiplication is more than simple addition where cooresponding values in the first matrix are added to the second matrix. """ #Matrix Multiplication if type(self) == Matrix and type(other) == Matrix: #Enforce the preconditions assert self.n == other.m, "The dimensions of the matrices are invalid." #Create the return matrix product_matrix = Matrix(unimath.zeros(self.m, other.n)) #Multiply the two matrices x = 1 y = 1 while y <= other.n: col_b = other.get_col(y) while x <= self.m: #Multiply the all rows by the column and get the product row_a = self.get_row(x) product = unimath.mult(row_a, col_b) final_sum = unimath.listsum(product) #Set the product to the correct space in the product matrix product_matrix.set(x, y, final_sum) x += 1 #Reset the row counter and continue with the next column x = 1 y += 1 return product_matrix #Scalar multiplication if type(other) == int or type(other) == float: for x in range(0, self.m): for y in range(0, self.n): self._data[x][y] = self._data[x][y] * other