0

我必须实现一些将矩阵设置为单位矩阵的方法。听起来很简单,但我不允许使用 NumPy。

class Matrix4():
    def __init__(self, row1=None, row2=None, row3=None, row4=None):
        """Constructor for Matrix4
        DO NOT MODIFY THIS METHOD"""
        if row1 is None: row1 = Vec4()
        if row2 is None: row2 = Vec4()
        if row3 is None: row3 = Vec4()
        if row4 is None: row4 = Vec4()
        self.m_values = [row1,row2,row3,row4]

    def __str__(self):
        """Returns a string representation of the matrix
        DO NOT MODIFY THIS METHOD"""
        toReturn = ''
        if self is None: return '0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00'
        for r in range(0,4):
            for c in range(0,4):
                toReturn += "%.2f" % self.m_values[r].values[c]
                if c != 3:
                    toReturn += ' '
            toReturn += '\n'
        return toReturn


    def setIdentity(self):
        """Sets the current Matrix to an identity matrix
        self is an identity matrix after calling this method"""
        row1 = Vec4(1,0,0,0)
        row2 = Vec4(0,1,0,0)
        row3 = Vec4(0,0,1,0)
        row4 = Vec4(0,0,0,1)
        setIdentity.Matrix4()
        return Matrix4(row1, row2, row3, row4)

如您所见,我们有一个 Matrix4() 类,到目前为止我已经实现了该方法。如果我尝试打印出单位矩阵,它会失败。命令

print(Matrix4())

打印出零矩阵。执行以下命令

print(setIdentity.Matrix4())

告诉我 setIdentity 没有实现。我的代码有什么问题?

我愿意接受你的建议。

谢谢!

4

2 回答 2

1

你真的应该部分地这样做,因为你似乎遗漏了一些概念。

m = Matrix4()

现在你有一个全为零的矩阵。接下来你想让它成为一个单位矩阵。

m.setIdentity()

您当前的实现在很多方面都被破坏了。

def setIdentity(self):
    """Sets the current Matrix to an identity matrix
    self is an identity matrix after calling this method"""
    row1 = Vec4(1,0,0,0)
    row2 = Vec4(0,1,0,0)
    row3 = Vec4(0,0,1,0)
    row4 = Vec4(0,0,0,1)
    #setIdentity.Matrix4()~
    #return Matrix4(row1, row2, row3, row4)
    self.m_values = [row1, row2, row3, row4]

这解决了setIdentity未定义的两个问题,而不是返回一个新的矩阵,它修改了现有的矩阵。

我将在下面修复您的答案代码。

class Matrix4():
    def __init__(self, row1=None, row2=None, row3=None, row4=None):
        """Constructor for Matrix4
        DO NOT MODIFY THIS METHOD"""
        if row1 is None: row1 = Vec4()
        if row2 is None: row2 = Vec4()
        if row3 is None: row3 = Vec4()
        if row4 is None: row4 = Vec4()
        self.m_values = [row1,row2,row3,row4]

    def __str__(self):
        """Returns a string representation of the matrix
        DO NOT MODIFY THIS METHOD"""
        toReturn = ''
        if self is None: return '0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00\n0.00 0.00 0.00 0.00'
        for r in range(0,4):
            for c in range(0,4):
                toReturn += "%.2f" % self.m_values[r].values[c]
                if c != 3:
                    toReturn += ' '
            toReturn += '\n'
        return toReturn


    def setIdentity(self):
        """Sets the current Matrix to an identity matrix
        self is an identity matrix after calling this method"""
        #Dont do this either, it is unescessary!
        #m = Matrix4()
        row1 = Vec4(1,0,0,0)
        row2 = Vec4(0,1,0,0)
        row3 = Vec4(0,0,1,0)
        row4 = Vec4(0,0,0,1)
        self.m_values = [row1, row2, row3, row4]
        #No, do do this! this is causing the recursion!
        #m.setIdentity()
        #Stop returning a new matrix!
        #return Matrix4(row1, row2, row3, row4)

m = Matrix4()
m.setIdentity()
print(m)

创建矩阵并将其设置为标识的代码应该在您的班级之外。那时您正在使用该课程。我在删除的行上方添加了评论。我只更改了方法 setIdentity。

于 2019-05-12T16:40:16.070 回答
0

如果要从Matrix4类执行setIdentity函数,则必须按以下方式编写:

(class instance).function()

因此,在您的情况下:

print(Matrix4().setIdentity())

至于你的代码:

打印(矩阵4())

它不起作用,因为它调用构造函数 ( init ) 来创建 Matrix4 的默认实例。如果您想要一个不同的矩阵作为默认值,则必须修改 init 函数。

于 2019-05-12T16:28:29.790 回答