我得到一个 NameError 'matrix_add' is not define: 当试图重载 __add__ 函数时
I get a NameError 'matrix_add' is not define: when trying to overload the __add__ function
我正在为编程 class 做作业,我需要重载标准运算符(+、*、-)并让它们与 Matrix class 对象一起使用。我认为我做得很好,但 Python 一直吐出一个名称错误,我不知道为什么,因为函数已定义。
我已经尝试了很多东西,但我仍然不断回到我原来的代码(如下)。请帮助
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return matrix_add(self.a, b)
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
我使用以下方式调用它:
A = Matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
B = Matrix([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
我收到此错误消息:
----------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-113-74230a6e5fb2> in <module>()
----> 1 C = A + B
<ipython-input-110-e27f8d893b4d> in __add__(self, b)
22 def __add__(self, b):
23
---> 24 return matrix_add(self.a, b)
25
NameError: name 'matrix_add' is not defined
您必须缩进除第一行以外的所有行,在 matrix_add
函数调用之前加上 self
(如@Arman 所说)和 implement a __len__(self)
function:
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return self.matrix_add(self.a, b)
def __len__(self):
# TODO: FILL THIS IN
pass
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
在此之后,您还会遇到另一个错误,要求您必须实现另一个功能。 Google吧,祝你好运;-)
只需将列表直接发送到矩阵
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return self.matrix_add(self.a, b.a)
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
A = Matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
B = Matrix([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
print(A+B)
我正在为编程 class 做作业,我需要重载标准运算符(+、*、-)并让它们与 Matrix class 对象一起使用。我认为我做得很好,但 Python 一直吐出一个名称错误,我不知道为什么,因为函数已定义。
我已经尝试了很多东西,但我仍然不断回到我原来的代码(如下)。请帮助
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return matrix_add(self.a, b)
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
我使用以下方式调用它:
A = Matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
B = Matrix([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
我收到此错误消息:
----------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-113-74230a6e5fb2> in <module>()
----> 1 C = A + B
<ipython-input-110-e27f8d893b4d> in __add__(self, b)
22 def __add__(self, b):
23
---> 24 return matrix_add(self.a, b)
25
NameError: name 'matrix_add' is not defined
您必须缩进除第一行以外的所有行,在 matrix_add
函数调用之前加上 self
(如@Arman 所说)和 implement a __len__(self)
function:
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return self.matrix_add(self.a, b)
def __len__(self):
# TODO: FILL THIS IN
pass
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
在此之后,您还会遇到另一个错误,要求您必须实现另一个功能。 Google吧,祝你好运;-)
只需将列表直接发送到矩阵
class Matrix:
"""A Class Matrix which can implement addition, subtraction and multiplication
of two matrices; scalar multiplication; and inversion, transposition and
determinant of the matrix itself"""
def __init__(self, a):
"""Constructor for the Class Matrix"""
#what if you only want to work with one matrix
self.a = a
def __add__(self, b):
return self.matrix_add(self.a, b.a)
def matrix_add(self, a, b):
"""
Add two matrices.
Matrices are represented as nested lists, saved row-major.
>>> matrix_add([[1,1],[2,2]], [[0,-2],[3,9]])
[[1, -1], [5, 11]]
>>> matrix_add([[2,3],[5,7]], [[11,13],[17,19]])
[[13, 16], [22, 26]]
>>> matrix_add([[2,3,4],[5,7,4]], [[11,13,1],[17,19,1]])
[[13, 16, 5], [22, 26, 5]]
>>> matrix_add([[1,2],[3,4]],[[1,2]])
Traceback (most recent call last):
...
MatrixException: matrices must have equal dimensions
"""
rows = len(a) # number of rows
cols = len(a[0]) # number of cols
if rows != len(b) or cols != len(b[0]):
raise MatrixException("matrices must have equal dimensions")
return [[a[i][j] + b[i][j] for j in range(cols)] for i in range(rows)]
A = Matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
B = Matrix([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
print(A+B)