Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 应在不使用NumPy的情况下创建标识矩阵_Python 3.x_Matrix Multiplication_Vector Multiplication - Fatal编程技术网

Python 3.x 应在不使用NumPy的情况下创建标识矩阵

Python 3.x 应在不使用NumPy的情况下创建标识矩阵,python-3.x,matrix-multiplication,vector-multiplication,Python 3.x,Matrix Multiplication,Vector Multiplication,我必须实现一些将矩阵设置为单位矩阵的方法。听起来很简单,但我不允许使用NumPy 类Matrix4(): def u uu init_uuuu(self,行1=None,行2=None,行3=None,行4=None): “”Matrix4的构造函数 请勿修改此方法“” 如果行1为无:行1=Vec4() 如果第2行为None:row2=Vec4() 如果第3行为None:row3=Vec4() 如果第4行为None:row4=Vec4() self.m_值=[row1,row2,row3,row

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

类Matrix4():
def u uu init_uuuu(self,行1=None,行2=None,行3=None,行4=None):
“”Matrix4的构造函数
请勿修改此方法“”
如果行1为无:行1=Vec4()
如果第2行为None:row2=Vec4()
如果第3行为None:row3=Vec4()
如果第4行为None:row4=Vec4()
self.m_值=[row1,row2,row3,row4]
定义(自我):
“”“返回矩阵的字符串表示形式
请勿修改此方法“”
toReturn=''
如果self为None:返回“0.00 0.00 0.00 0.00\n0.00 0.00 0.00\n0.00 0.00 0.00\n0.00 0.00 0.00”
对于范围(0,4)内的r:
对于范围(0,4)内的c:
toReturn+=“%.2f”%self.m_值[r]。值[c]
如果c!=三:
toReturn+=“”
toReturn+='\n'
返回返回
def setIdentity(自我):
“”“将当前矩阵设置为标识矩阵
self是调用此方法“”后的标识矩阵
行1=Vec4(1,0,0,0)
行2=Vec4(0,1,0,0)
行3=Vec4(0,0,1,0)
行4=Vec4(0,0,0,1)
setIdentity.Matrix4()
返回矩阵X4(第1行、第2行、第3行、第4行)
如您所见,我们有一个Matrix4()类,到目前为止,我已经实现了该方法。如果我试图打印出身份矩阵,它就会失败。 命令

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

print(setIdentity.Matrix4())
告诉我setIdentity未实现。我的代码有什么问题

我愿意听取你的建议


谢谢

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

(class instance).function()
因此,在你的情况下:

print(Matrix4().setIdentity())
至于你的代码:

打印(Matrix4())


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

您确实应该分部分执行此操作,因为您似乎缺少一些概念

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)

创建矩阵并将其设置为identity的代码应该在您的类之外。此时您正在使用该类。我在删除的行上方添加了注释。我只更改了setIdentity方法

Vec4()做什么?你需要做,
Matrix4().setIdentity()
这对我不管用,马特。它说没有定义setIdentity。您可以更改什么方法?您必须从方法
setIdentity.Matrix4()
中删除该行,该行乱七八糟且已损坏。谢谢,这就是问题所在。现在它可以正常工作了。这并不正确,因为setIdentity是一个实例方法。例如,您需要Matrix4的一个实例来调用set Identity with.Traceback(最近一次调用last):文件“”,第1行,打印(Matrix4.setIdentity())类型错误:setIdentity()缺少1个必需的位置参数:“self”您的解决方案对我无效。它告诉我一些关于递归错误的事情:超过了最大递归深度。现在它似乎工作正常。我必须在Python解释器中使用命令m=Matrix4()和m.setIdentity(),对吗?可能吧?您可以在脚本中使用它,也可以在解释器中使用它。Matrix4()创建一个矩阵,但不将其分配给任何对象
m=Matrix4()
创建一个矩阵,然后您就可以使用它了。例如,如果您想使其成为一个身份矩阵,您可以使用
m.setIdentity()
那么m是一个单位矩阵。如果我在解释器中使用它们,一切正常。如果在脚本中使用它们,我会得到一个错误,它告诉我递归长度已经超过。我不知道你在脚本中做什么,这应该不会有什么区别。解释器就像在编写脚本一样。