Guest User

Untitled

a guest
Feb 15th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. def custom_input(self, equation):
  2. equation = equation.replace(' ', '') # remove whitespace
  3. equation_list = []
  4. temp_str = ""
  5. for char in equation:
  6. if char == "(":
  7. equation_list.append(temp_str)
  8. temp_str = ""
  9. temp_str += char
  10. elif char == ")":
  11. temp_str += char
  12. equation_list.append(temp_str)
  13. temp_str = ""
  14. else:
  15. temp_str += char
  16. if temp_str:
  17. equation_list.append(temp_str)
  18.  
  19. result_matrix = None
  20. for equation in equation_list:
  21. if '*' in equation:
  22. matrices = equation.split('*')
  23. matrix1 = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
  24. matrix2 = next(matrix for matrix in self.matrices if matrix.name == matrices[1])
  25. result_matrix = self.multiply_matrices(matrix1.name, matrix2.name)
  26. elif '-' in equation:
  27. matrices = equation.split('-')
  28. matrix1 = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
  29. matrix2 = next(matrix for matrix in self.matrices if matrix.name == matrices[1])
  30. result_matrix = self.subtract_matrices(matrix1.name, matrix2.name)
  31. elif 's' in equation:
  32. matrices = equation.split('s')
  33. matrix = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
  34. scalar = float(matrices[1])
  35. result_matrix = self.scalar_multiply(matrix.name, scalar)
  36. elif 't' in equation:
  37. matrix_name = equation.split('t')[0]
  38. matrix = next(matrix for matrix in self.matrices if matrix.name == matrix_name)
  39. result_matrix = self.flip_matrix(matrix.name)
  40. else:
  41. raise ValueError(f"Invalid equation '{equation}'")
  42.  
  43. if result_matrix is not None:
  44. self.matrices[self.matrices.index(next(matrix for matrix in self.matrices if matrix.name == matrix_name))] = result_matrix
  45.  
  46. return result_matrix
Advertisement
Add Comment
Please, Sign In to add comment