Matrix Calculations solver in python
This program is capable of solving matrix calculations such as addition of matrices, Subtraction of matrices etcCoder
python
3 years ago
4.8 kB
1
Indexable
Never
import math,random class Matrix(): """this module is used to work with matrices""" def __init__(self,matrix): self.matrix =[[]] self.rows = 0 self.colums = 0 self.size = 0 is_matrix = False is_same_dimention = False for row in range(len(matrix)): if type(matrix[row]) == type([]): is_matrix = True continue else: is_matrix = False break if is_matrix == True: self.matrix = matrix self.rows = len(self.matrix) self.colums = len(self.matrix[0]) self.size = self.rows * self.colums def get_rows(self): """returns the number of rows in a matrix""" return self.rows def get_colums(self): """returns number of cols a matrix has""" return self.colums def get_order(self): """returns the dimensions of the matrix""" return str(self.rows) +"x"+str(self.colums) def add_matrix(self,matrix): """adds two matrices together""" matrix2 =[[0] *self.colums for i in range(self.rows)] for row in range(len(self.matrix)): for colum in range(len(self.matrix[0])): matrix2[row][colum]=self.matrix[row][colum] + matrix[row][colum] return matrix2 def subtract_matrices(self,matrix): """subtracts two matrices and returns the result""" matrix2 =[[0] *self.colums for i in range(self.rows)] for row in range(len(self.matrix)): for colum in range(len(self.matrix[0])): matrix2[row][colum]=self.matrix[row][colum] - matrix[row][colum] return matrix2 def multiply_by(self,num): """multiplies the matrix by the specified num""" matrix2 =[[0] *self.colums for i in range(self.rows)] for row in range(len(self.matrix)): for colum in range(len(self.matrix[0])): matrix2[row][colum]=self.matrix[row][colum] * num return matrix2 def get_Matrix(self): """returns the matrix""" return self.matrix def get_row_at(self,index): return self.matrix[index] def get_matrix(self): """this method returns the matrix""" return self.matrix def sort_matrix(self): """this metjod sorts the matrix in acending order""" for i in range(len(self.matrix)): self.matrix[i]=sorted(self.matrix[i]) def get_element(self,row,colum): """this method returns the element at a specific index""" return self.matrix[row-1][colum-1] def get_square_elements(self): """this method returns all elements in the matrix that are perfect square numbers""" squares =[] for row in range(len(self.matrix)): for colum in range(len(self.matrix[0])): s = math.sqrt(self.matrix[row][colum]) if s * s == self.matrix[row][colum]: squares.append(self.matrix[row][colum]) return squares def multiply(self,matrix): result =[[0]* self.colums for i in range(self.rows)] #loop through the rows of self.matrix for row1 in range(len(self.matrix)): #iterate through the colums of matrix for colum1 in range(len(self.matrix[0])): #iterate through the rows of matrix for row2 in range(len(matrix)): result[row1][colum1] += self.matrix[row1][row2] * matrix[row2][colum1] return result def generate_matrix(self,row,colums,ranges = range(0,50)): """this method returns a generated matrix""" try: matrix =[[0]* colums for i in range(row)] r = list(ranges) for row in range(len(matrix)): for colum in range(len(matrix[0])): matrix[row][colum] = random.randrange(len(r)) except TypeError: print("invalid argument ",ranges) else: return matrix def find_determinant(self): """returns the determinant of 2x2 matrix""" if self.rows == 2 and self.colums == 2: return (self.matrix[0][0] * self.matrix[1][-1]) - (self.matrix[0][1]* self.matrix[1][0]) def decode_matrix(self): """this method decodes a hidden .essage in a matrix""" decoded = [] code_alphabets = dict() alphabets ="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(" ") i = 1 #adds letters and associate code numbers to the code_alphabets dictionary""" for letter in alphabets: if letter not in code_alphabets.keys(): code_alphabets.setdefault(i,letter) i+=1 #loops through matrix and searches for a match in dictionary""" for row in range(len(self.matrix)): for colum in range(len(self.matrix[row])): """checks wether the number is in the dictionary""" if self.matrix[row][colum] in code_alphabets.keys(): value = self.matrix[row][colum] decoded.append(code_alphabets[value]) else: return None return str(decoded) def encode_matrix(self): """encodes the matrix"""