Problem 1.1: Matrix (100pts)
Problem
Implement the
Matrixclass that inherits from theSemiringbase class.The
Matrixclass represents 3x3 matrices and supports matrix operations. We use a nested list (list of lists) to store the elements of the matrix, whereelements[i][j]is the element at rowiand columnj. For example, the list[[1, 2, 3], [4, 5, 6], [7, 8, 9]]represents the matrix\[\begin{bmatrix}1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9\end{bmatrix}\]
实现继承自 Semiring 基类的 Matrix 类。
Matrix 类表示 3x3 矩阵并支持矩阵运算。我们使用嵌套列表(列 表的列表)来存储矩阵的元素,其中 elements[i][j] 是第 i 行第 j 列的元素。
例如,列表 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 表示矩阵
The definition of addition and multiplication for matrices is as follows:
- Addition: The sum of two matrices is obtained by adding their corresponding elements. That is, if \( A \) and \( B \) are two matrices, then their sum \( C = A + B \) is defined by \( C_{i, j} = A_{i, j} + B_{i, j} \).
Accordingly, the zero element (additive identity) is the matrix with all elements equal to 0, and the additive inverse (negation) of a matrix is obtained by negating each of its elements. * Multiplication: The product of two matrices is obtained by taking the dot product of rows and columns. That is, if \( A \) and \( B \) are two matrices, then their product \( C = A \times B \) is defined by $$ C_{i, j} = \sum_{k=0}^{n} A_{i, k} \times B_{k, j} $$ where \( n \) is \( 3 \) for the 3x3 matrices in this problem.
Accordingly, the one element (multiplicative identity) is the identity matrix, which has 1s on the diagonal and 0s elsewhere.
\[\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1\end{bmatrix}\]Here is the skeleton of the
Matrixclass you need to implement:
矩阵的加法和乘法的定义如下:
- 加法:两个矩阵的和是通过将它们对应的元素相加得到的。 也就是说,如果 \( A \) 和 \( B \) 是两个矩阵,那么它们的和 \( C = A + B \) 定义为 \( C_{i, j} = A_{i, j} + B_{i, j} \)。
因此,零元(加法单位元)是所有元素都等于 0 的矩阵,而一个矩阵的加法逆元(负元)是通过将其每个元素取负得到的。 * 乘法:两个矩阵的乘积是通过取行和列的点积得到的。 也就是说,如果 \( A \) 和 \( B \) 是两个矩阵,那么它们的乘积 \( C = A \times B \) 定义为 $$ C_{i, j} = \sum_{k=0}^{n} A_{i, k} \times B_{k, j} $$ 其中在本问题中 \( n \) 对于 3x3 矩阵来说是 \( 3 \)。
因此,幺元(乘法单位元)是单位矩阵,它的对角线上是 1,其他地方是 0。
下面是您需要实现的 Matrix 类的骨架:
class Matrix(Semiring):
"""A class representing 3x3 matrices."""
def __init__(self, elements):
"""Initializes a 3x3 matrix with the given list of lists.
>>> Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
"""
self.elements = elements
def add(self, other):
"""Returns a new Matrix representing the sum of self and other.
>>> m1 = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> m2 = Matrix([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
>>> m1.add(m2)
[[10, 10, 10],
[10, 10, 10],
[10, 10, 10]]
"""
"""YOUR CODE HERE"""
def mult(self, other):
"""Returns a new Matrix representing the product of self and other.
>>> m1 = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> m2 = Matrix([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
>>> m1.mult(m2)
[[30, 24, 18],
[84, 69, 54],
[138, 114, 90]]
"""
"""YOUR CODE HERE"""
def negative(self):
"""Returns a new Matrix representing the additive inverse of self.
>>> m = Matrix([[1, -2, 3], [-4, 5, -6], [7, -8, 9]])
>>> m.negative()
[[-1, 2, -3],
[4, -5, 6],
[-7, 8, -9]]
"""
"""YOUR CODE HERE"""
def zero(self):
"""Returns the zero matrix."""
"""YOUR CODE HERE"""
def one(self):
"""Returns the identity matrix."""
"""YOUR CODE HERE"""
def __repr__(self):
"""Returns the string representation of the matrix.
You are not supposed to understand this code now.
"""
rows = [str(row) for row in self.elements]
return "[{}]".format(",\n ".join(rows))