使用Python将矩阵简化为梯形表格练习

问题描述

我正在定义变量,以将给定的矩阵转换为梯形形式,练习要求我定义不同的变量以检查矩阵是否为奇数,然后将其转换为梯形形式,这是练习和到目前为止我所做的:

import numpy as np

# Our function will go through the matrix replacing each row in order turning it into echelon form.
# If at any point it fails because it can't put a 1 in the leading diagonal,# we will return the value True,otherwise,we will return False.
# There is no need to edit this function.
def isSingular(A) :
    B = np.array(A,dtype=np.float_) # Make B as a copy of A,since we're going to alter it's values.
    try:
        fixRowZero(B)
        fixRowOne(B)
        fixRowTwo(B)
        fixRowThree(B)
    except MatrixIsSingular:
        return True
    return False

# This next line defines our error flag. For when things go wrong if the matrix is singular.
# There is no need to edit this line.
class MatrixIsSingular(Exception): pass

# For Row Zero,all we require is the first element is equal to 1.
# We'll divide the row by the value of A[0,0].
# This will get us in trouble though if A[0,0] equals 0,so first we'll test for that,# and if this is true,we'll add one of the lower rows to the first one before the division.
# We'll repeat the test going down each lower row until we can do the division.
# There is no need to edit this function.
def fixRowZero(A) :
    if A[0,0] == 0 :
        A[0] = A[0] + A[1]
    if A[0,0] == 0 :
        A[0] = A[0] + A[2]
    if A[0,0] == 0 :
        A[0] = A[0] + A[3]
    if A[0,0] == 0 :
        raise MatrixIsSingular()
    A[0] = A[0] / A[0,0]
    return A

# First we'll set the sub-diagonal elements to zero,i.e. A[1,0].
# Next we want the diagonal element to be equal to one.
# We'll divide the row by the value of A[1,1].
# Again,we need to test if this is zero.
# If so,we'll add a lower row and repeat setting the sub-diagonal elements to zero.
# There is no need to edit this function.
def fixRowOne(A) :
    A[1] = A[1] - A[1,0] * A[0]
    if A[1,1] == 0 :
        A[1] = A[1] + A[2]
        A[1] = A[1] - A[1,1] == 0 :
        A[1] = A[1] + A[3]
        A[1] = A[1] - A[1,1] == 0 :
        raise MatrixIsSingular()
    A[1] = A[1] / A[1,1]
    return A

# This is the first function that you should complete.
# Follow the instructions inside the function at each comment.
def fixRowTwo(A) :
    # Insert code below to set the sub-diagonal elements of row two to zero (there are two of them).
    A[2] = (A[2] - A[2,0]) * (A[2] - A[2,1])
    # Next we'll test that the diagonal element is not zero.
    if A[2,2] == 0 :
        # Insert code below that adds a lower row to row 2.
        A[2] = A[2] + A[3]
        # Now repeat your code which sets the sub-diagonal elements to zero.
        A[2] = (A[2] - A[2,1])
    if A[2,2] == 0 :
        raise MatrixIsSingular()
    # Finally set the diagonal element to one by dividing the whole row by that element.
    A[2] = A[2]/A[2,2]
    return A

# You should also complete this function
# Follow the instructions inside the function at each comment.
def fixRowThree(A) :
    # Insert code below to set the sub-diagonal elements of row three to zero.
    A[3] = (A[3] - A[3,0]) * (A[3] - A[3,1]) * (A[3] - A[3,2])
    # Complete the if statement to test if the diagonal element is zero.
    if A[3,3] == 0:
        raise MatrixIsSingular()
    # Transform the row to set the diagonal element to one.
    A[3] = A[3]/A[3,3]
    return A

我可以使用fixRowZero(A),fixRowOne(A)和fixRowTwo(A),但是当我使用fixRowThree(A)时,会出现以下错误消息:

---------------------------------------------------------------------------
MatrixIsSingular                          Traceback (most recent call last)
<ipython-input-58-d25352be9b2c> in <module>
----> 1 fixRowThree(A)

<ipython-input-57-4faff915d9e0> in fixRowThree(A)
     82     # Complete the if statement to test if the diagonal element is zero.
     83     if A[3,3] == 0:
---> 84         raise MatrixIsSingular()
     85     # Transform the row to set the diagonal element to one.
     86     A[3] = A[3]/A[3,3]

MatrixIsSingular: 

我在做什么错?当我通过一个矩阵来检查它是否为奇数时,它会相应地返回True或False,因此它似乎可以正常工作,但是当我使用fixRowThree()函数时,会返回此错误消息

解决方法

我可能回答这个问题太晚了。但是,让我们为可能会遇到此练习的人澄清一些事情。

从我的角度来看,您不了解手头的任务:

#  set the sub-diagonal elements of row three to zero. 

如果返回功能fixRowOne,您将看到以下表达式:

 A[1] = A[1] - A[1,0] * A[0] 

这个表达式有什么作用?

 # First we'll set the sub-diagonal elements to zero,i.e. A[1,0]. 

您要做的就是遵循这个表达方式。即使您的FixSecondRow函数没有错误,这也是错误的,因为它没有遵循相同的模式。也许,您的代码对示例测试很幸运。

在第二行中,有两个子对角线元素:A [2,0]和A [2,1]。子对角线元素是在所述行的对角线元素之前的元素。您必须执行使每个无效的相同过程。因为在其他前几行中,对角线元素已经为1,所以我们可以使用它们通过矩阵变换轻松消除子对角线:

A[2] = A[2] - A[2,0]* A[0]

A[2] = A[2] - A[2,1]* A[1]

对于第三行,您必须遵循相同的模式。

我希望这种解释能消除任何试图理解梯形和矩阵变换的困惑。

之所以没有提供完整的解决方案,是因为该练习是Coursera课程的一部分:机器学习数学:线性代数。我有一个正确的代码通过了作业,但是将其发布会适得其反。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...