【问题标题】:Identity Matrix should be created without NumPy应在没有 NumPy 的情况下创建身份矩阵
【发布时间】:2019-05-12 16:22:50
【问题描述】:

我必须实现一些将矩阵设置为单位矩阵的方法。听起来很简单,但我不能使用 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 没有实现。我的代码有什么问题?

我愿意接受您的建议。

谢谢!

【问题讨论】:

  • Vec4() 是做什么的?
  • 你需要做的,Matrix4().setIdentity()
  • 这对我不起作用,马特。它说 setIdentity 没有定义。
  • 你可以改变什么方法?您必须从您的方法 setIdentity.Matrix4() 中删除该行是乱码和损坏的。
  • 谢谢,这就是问题所在。现在它工作正常。

标签: python-3.x matrix-multiplication vector-multiplication


【解决方案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)

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

【讨论】:

  • 您的解决方案对我不起作用。它告诉我一些关于 RecursionError: maximum recursion depth exceeded.
  • 现在它似乎工作正常。我必须在 Python 解释器中使用命令 m=Matrix4() 和 m.setIdentity(),对吧?
  • 可能吗?您可以在脚本中使用它,也可以在解释器中使用它。 Matrix4() 创建一个矩阵,但不将其分配给任何东西。 m = Matrix4() 创建一个矩阵,然后您可以使用它。例如,如果你想让它成为你可以使用的单位矩阵。 m.setIdentity() 那么 m 是一个单位矩阵。
  • 如果我在解释器中使用它们,一切正常。如果在脚本中使用它们,我会收到一个错误,告诉我已经超出递归长度。
  • 我不知道你在你的脚本中做了什么,解释器应该不会有什么不同就像你边写脚本一样。
【解决方案2】:

如果你想从 Matrix4 类中执行 setIdentity 函数,你必须这样写:

(class instance).function()

因此,在你的情况下:

print(Matrix4().setIdentity())

至于你的代码:

打印(Matrix4())

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

【讨论】:

  • 这并不正确,因为 setIdentity 是一个实例方法。例如,您需要一个 Matrix4 的实例来调用设置身份。
  • 回溯(最近一次调用最后一次):文件“”,第 1 行,在 print(Matrix4.setIdentity()) 类型错误:setIdentity() 缺少 1 个必需的位置参数:“自我”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 2014-08-09
  • 2019-05-22
  • 1970-01-01
相关资源
最近更新 更多