Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def custom_input(self, equation):
- equation = equation.replace(' ', '') # remove whitespace
- equation_list = []
- temp_str = ""
- for char in equation:
- if char == "(":
- equation_list.append(temp_str)
- temp_str = ""
- temp_str += char
- elif char == ")":
- temp_str += char
- equation_list.append(temp_str)
- temp_str = ""
- else:
- temp_str += char
- if temp_str:
- equation_list.append(temp_str)
- result_matrix = None
- for equation in equation_list:
- if '*' in equation:
- matrices = equation.split('*')
- matrix1 = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
- matrix2 = next(matrix for matrix in self.matrices if matrix.name == matrices[1])
- result_matrix = self.multiply_matrices(matrix1.name, matrix2.name)
- elif '-' in equation:
- matrices = equation.split('-')
- matrix1 = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
- matrix2 = next(matrix for matrix in self.matrices if matrix.name == matrices[1])
- result_matrix = self.subtract_matrices(matrix1.name, matrix2.name)
- elif 's' in equation:
- matrices = equation.split('s')
- matrix = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
- scalar = float(matrices[1])
- result_matrix = self.scalar_multiply(matrix.name, scalar)
- elif 't' in equation:
- matrix_name = equation.split('t')[0]
- matrix = next(matrix for matrix in self.matrices if matrix.name == matrix_name)
- result_matrix = self.flip_matrix(matrix.name)
- else:
- raise ValueError(f"Invalid equation '{equation}'")
- if result_matrix is not None:
- self.matrices[self.matrices.index(next(matrix for matrix in self.matrices if matrix.name == matrix_name))] = result_matrix
- return result_matrix
Advertisement
Add Comment
Please, Sign In to add comment