Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix:
- def __init__(self, name, m, n, values):
- self.name = name
- self.m = m
- self.n = n
- self.values = values
- def __str__(self):
- rows = [self.values[i:i+self.n] for i in range(0, len(self.values), self.n)]
- return f'{self.name} = \n' + '\n'.join([' '.join(map(str, row)) for row in rows])
- class MatrixMultiplier:
- def __init__(self):
- self.matrices = []
- #DEBUG#
- #self.matrices.append(Matrix('A', 3, 3, [1, -1, 0, 0, -2, 1, -2, 3, 1]))
- #self.matrices.append(Matrix('B', 3, 3, [1, 1, -1, 0, 2, 3, 1, 4, 1]))
- #######
- def create_paste(self, data):
- self.matrices.append(data)
- def create_matrix(self, name):
- m = int(input(f'Introduceți numărul de rânduri pentru matricea {name}: '))
- n = int(input(f'Introduceți numărul de coloane pentru matricea {name}: '))
- values = []
- for i in range(m):
- for j in range(n):
- value = int(input(f'Introduceți valoarea pentru poziție ({i}, {j}) în matricea {name}: '))
- values.append(value)
- matrix = Matrix(name, m, n, values)
- self.matrices.append(matrix)
- def add_matrices(self, name1, name2):
- matrix1 = next(matrix for matrix in self.matrices if matrix.name == name1)
- matrix2 = next(matrix for matrix in self.matrices if matrix.name == name2)
- if matrix1.m != matrix2.m or matrix1.n != matrix2.n:
- raise ValueError('Cele două matrice trebuie să aibă aceleași dimensiuni')
- result = [matrix1.values[i] + matrix2.values[i] for i in range(len(matrix1.values))]
- return Matrix(f'{name1}+{name2}', matrix1.m, matrix1.n, result)
- def multiply_matrices(self, name1, name2):
- matrix1 = next(matrix for matrix in self.matrices if matrix.name == name1)
- matrix2 = next(matrix for matrix in self.matrices if matrix.name == name2)
- if matrix1.n != matrix2.m:
- raise ValueError('Numărul de coloane din prima matrice trebuie să se potrivească cu numărul de rânduri din a doua matrice')
- result = []
- for i in range(matrix1.m):
- for j in range(matrix2.n):
- val = 0
- for k in range(matrix1.n):
- val += matrix1.values[i * matrix1.n + k] * matrix2.values[k * matrix2.n + j]
- result.append(val)
- return Matrix(f'{name1}', matrix1.m, matrix2.n, result)
- def scalar_multiply(self, name, scalar):
- matrix = next(matrix for matrix in self.matrices if matrix.name == name)
- result = [scalar * val for val in matrix.values]
- matrix.values = result
- matrix.name = f'{name}'
- return Matrix(matrix.name, matrix.m, matrix.n, result)
- def flip_matrix(self, name):
- matrix = next(matrix for matrix in self.matrices if matrix.name == name)
- result = []
- for j in range(matrix.n):
- for i in range(matrix.m):
- result.append(matrix.values[i * matrix.n + j])
- matrix.values = result
- matrix.m, matrix.n = matrix.n, matrix.m
- matrix.name = f'{name}'
- return matrix
- def subtract_matrices(self, name1, name2):
- matrix1 = next(matrix for matrix in self.matrices if matrix.name == name1)
- matrix2 = next(matrix for matrix in self.matrices if matrix.name == name2)
- if matrix1.m != matrix2.m or matrix1.n != matrix2.n:
- raise ValueError('Cele două matrice trebuie să aibă aceleași dimensiuni')
- result = [matrix1.values[i] - matrix2.values[i] for i in range(len(matrix1.values))]
- return Matrix(f'{name1}', matrix1.m, matrix1.n, result)
- def proprocess_transpose(self, ops):
- ops = ops.split()
- for i in range(len(ops)):
- if ops[i] == 't':
- name = ops[i - 1]
- matrix = self.flip_matrix(name)
- self.matrices[self.matrices.index(next(matrix for matrix in self.matrices if matrix.name == matrix.name))] = matrix
- ops.remove(ops[i])
- ops[i-1] = f'{name}'
- return ' '.join(ops)
- def parse_parentheses(self,input_str):
- stack = []
- term = ""
- if "(" in input_str:
- for char in input_str:
- if char in ['+', '-', '*']:
- if term:
- stack.append(term)
- term = ""
- stack.append(char)
- elif char == "(":
- if term:
- stack.append(term)
- term = ""
- stack.append(char)
- elif char == ")":
- if term:
- stack.append(term)
- term = ""
- temp = []
- while stack and stack[-1] != "(":
- temp.append(stack.pop())
- if stack[-1] == "(":
- stack.pop()
- if temp:
- stack += temp[::-1]
- else:
- term += char
- if term:
- stack.append(term)
- for op in ['*', '-', '+']:
- while op in stack:
- op_index = stack.index(op)
- stack.pop(op_index)
- stack.append(op)
- if op not in stack[op_index + 1:]:
- break
- return stack
- else:
- return [input_str]
- def custom_input(self, equation):
- equation = self.parse_parentheses(equation)
- equation_list = []
- temp_str = ""
- if len(equation)>1:
- #IT IS FIXED SO WORKS FOR 2 () sets
- _n1 = equation[0][0]
- _n2 = equation[1][0]
- ###################################
- for ec in equation:
- if ec not in ['+', '-', '*'] and len(ec) > 1:
- equation_list.append(ec)
- elif ec in ['+', '-', '*']:
- equation_list.append(f'{_n1}{ec}{_n2}')
- else:
- equation_list.append(equation[0])
- 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)
- matrix_name = matrix1.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)
- matrix_name = matrix1.name
- elif 'o' in equation: # SELF MULTIPLY
- matrices = equation.split('o')
- matrix = next(matrix for matrix in self.matrices if matrix.name == matrices[0])
- result_matrix = self.multiply_matrices(matrix.name, matrix.name)
- matrix_name = matrix.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)
- matrix_name = matrix.name
- 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)
- matrix_name = 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
- def print_menu():
- print('1. Creați matrice')
- print('2. Înmulțiți matrice')
- print('3. Înmulțire scalară')
- print('4. Adăugați matrice')
- print('5. Scăderea matricilor')
- print('6. Flip Matrix')
- print('7. Intrare personalizată')
- #print('8. Creați matrice din input paste')
- print('8. Ieșire')
- def main():
- matrix_multiplier = MatrixMultiplier()
- while True:
- print_menu()
- choice = input('Introdu alegerea ta: ')
- if choice == '1':
- name = input('Introduceți numele matricei: ')
- matrix_multiplier.create_matrix(name)
- elif choice == '2':
- name1 = input('Introduceți numele primei matrice: ')
- name2 = input('Introduceți numele celei de-a doua matrice: ')
- try:
- result = matrix_multiplier.multiply_matrices(name1, name2)
- print(result)
- except ValueError as e:
- print(e)
- elif choice == '3':
- name = input('Introduceți numele primei matrice: ')
- scalar = int(input('Introduceți scalarul: '))
- result = matrix_multiplier.scalar_multiply(name, scalar)
- print(result)
- elif choice == '4':
- name1 = input('Introduceți numele primei matrice: ')
- name2 = input('Introduceți numele celei de-a doua matrice: ')
- try:
- result = matrix_multiplier.add_matrices(name1, name2)
- print(result)
- except ValueError as e:
- print(e)
- elif choice == '5':
- name1 = input('Introduceți numele primei matrice: ')
- name2 = input('Introduceți numele celei de-a doua matrice: ')
- try:
- result = matrix_multiplier.subtract_matrices(name1, name2)
- print(result)
- except ValueError as e:
- print(e)
- elif choice == '6':
- name = input('Introduceți numele matrice: ')
- result = matrix_multiplier.flip_matrix(name)
- print(result)
- elif choice == '7':
- update_con = input('Vrei sa salvezi noile valori?: ')
- if update_con in ['yes', 'da', 'y']:
- update_con = 1
- operations = input('Introduceți succesiunea operațiilor cu matrice: ')
- try:
- result = matrix_multiplier.custom_input(operations)
- print(result)
- except (ValueError, IndexError) as e:
- print(e)
- else:
- print('Alegere nevalidă')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement