Untitled
unknown
python
2 years ago
5.4 kB
5
Indexable
import sys class Matrix: def __init__(self, data): self.data = data def transpose(self): rows = len(self.data) cols = len(self.data[0]) transposed = [[self.data[j][i] for j in range(rows)] for i in range(cols)] return Matrix(transposed) def add(self, other): rows = len(self.data) cols = len(self.data[0]) result = [[self.data[i][j] + other.data[i][j] for j in range(cols)] for i in range(rows)] return Matrix(result) def determinant(self): if len(self.data) != len(self.data[0]): raise ValueError("Matrix is not square") n = len(self.data) if n == 1: return self.data[0][0] det = 0 for j in range(n): det += ((-1) ** j) * self.data[0][j] * self.submatrix(0, j).determinant() return det def submatrix(self, i, j): sub = [row[:j] + row[j + 1:] for row in (self.data[:i] + self.data[i + 1:])] return Matrix(sub) class MatrixOps: def execute(self, matrix): raise NotImplementedError() class MatrixMultiply(MatrixOps): def execute(self, matrix): print("Matrix multiplication") class MatrixAdd(MatrixOps): def execute(self, matrix): print("Matrix addition") class MatrixTranspose(MatrixOps): def execute(self, matrix): print("Matrix transposition") class MatrixDeterminant(MatrixOps): def execute(self, matrix): print("Matrix determinant") class Command: def execute(self): raise NotImplementedError() class ReadCommand(Command): def __init__(self, filename): self.filename = filename def execute(self): # Чтение данных из файла и создание объекта Matrix # ... matrix = Matrix(data) return matrix class WriteCommand(Command): def __init__(self, filename, matrix): self.filename = filename self.matrix = matrix def execute(self): # Запись матрицы в файл # ... pass class MultiplyCommand(Command): def __init__(self, matrix1, matrix2): self.matrix1 = matrix1 self.matrix2 = matrix2 def execute(self): result = self.matrix1.multiply(self.matrix2) return result class AddCommand(Command): def __init__(self, matrix1, matrix2): self.matrix1 = matrix1 self.matrix2 = matrix2 def execute(self): result = self.matrix1.add(self.matrix2) return result class TransposeCommand(Command): def __init__(self, matrix): self.matrix = matrix def execute(self): result = self.matrix.transpose() return result class DeterminantCommand(Command): def __init__(self, matrix): self.matrix = matrix def execute(self): result = self.matrix.determinant() return result class RunCommand(Command): def __init__(self, operation, *args): self.operation = operation self.args = args def execute(self): result = None if self.operation == '*': command = MultiplyCommand(*self.args) elif self.operation == '+': command = AddCommand(*self.args) elif self.operation == 't': command = TransposeCommand(*self.args) elif self.operation == 'd': command = DeterminantCommand(*self.args) else: raise ValueError("Invalid operation") result = command.execute() return result class Logger: __instance = None @staticmethod def get_instance(): if Logger.__instance is None: Logger() return Logger.__instance def __init__(self): if Logger.__instance is not None: raise Exception("This class is a singleton!") else: Logger.__instance = self def log(self, message): print(f"[LOG] {message}") class Invoker: def __init__(self): self.commands = [] self.logger = Logger.get_instance() def add_command(self, command): self.commands.append(command) def execute_commands(self): for command in self.commands: result = command.execute() if result: self.logger.log(f"Command executed. Result: {result}") def main(): # Чтение аргументов командной строки if len(sys.argv) < 4: print("Usage: python matrix_operations.py <operation> <input1> [<input2>] <output>") return operation = sys.argv[1] input1 = sys.argv[2] input2 = None output = sys.argv[-1] if len(sys.argv) == 5: input2 = sys.argv[3] # Создание объекта Invoker invoker = Invoker() # Создание команд чтения из файла invoker.add_command(ReadCommand(input1)) if input2: invoker.add_command(ReadCommand(input2)) # Создание команды операции invoker.add_command(RunCommand(operation, invoker.commands[0].execute(), invoker.commands[1].execute())) # Создание команды записи в файл invoker.add_command(WriteCommand(output, invoker.commands[-1].execute())) # Выполнение команд invoker.execute_commands() if __name__ == "__main__": main()
Editor is loading...