身份矩阵应在没有NumPy的情况下创建

时间:2019-05-12 16:22:50

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

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

我愿意接受您的建议。

谢谢!

3 个答案:

答案 0 :(得分: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方法。

答案 1 :(得分:0)

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

(class instance).function()

因此,在您的情况下:

print(Matrix4().setIdentity())

关于您的代码:

  

print(Matrix4())

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

答案 2 :(得分:0)

replace("Mississippi", "i", "I")

如果使用此代码,则在解释器中将出现以下错误: enter image description here

@matt:这就是我的意思。