Here is an example implementation of the `Matrix` class in Python that supports addition, subtraction, and multiplication of 2x2 matrices: ```python class Matrix: def __init__(self, a, b, c, d): self.matrix = [[a, b], [c, d]] def __str__(self): return '\n'.join([' '.join(map(str, row)) for row in self.matrix]) def __add__(self, other): result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): result[i][j] = self.matrix[i][j] + other.matrix[i][j] return Matrix(*result[0], *result[1]) def __sub__(self, other): result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): result[i][j] = self.matrix[i][j] - other.matrix[i][j] return Matrix(*result[0], *result[1]) def __mul__(self, other): result = [[0, 0], [0, 0]] for i in range(2): for j in range(2): for k in range(2): result[i][j] += self.matrix[i][k] * other.matrix[k][j] return Matrix(*result[0], *result[1]) # Example usage matrix1 = Matrix(1, 2, 3, 4) matrix2 = Matrix(5, 6, 7, 8) print("Matrix 1:") print(matrix1) print("\nMatrix 2:") print(matrix2) print("\nAddition:") print(matrix1 + matrix2) print("\nSubtraction:") print(matrix1 - matrix2) print("\nMultiplication:") print(matrix1 * matrix2) ``` This will output: ``` Matrix 1: 1 2 3 4 Matrix 2: 5 6 7 8 Addition: 6 8 10 12 Subtraction: -4 -4 -4 -4 Multiplication: 19 22 43 50 ``` [2024-11-16 22:40:55 | AI写代码神器 | 1039点数解答]