簡體   English   中英

使用 Python 將矩陣簡化為梯形練習

[英]Matrix reduction to echelon form exercise using 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,0] * A[0]
    if A[1,1] == 0 :
        A[1] = A[1] + A[3]
        A[1] = A[1] - A[1,0] * A[0]
    if 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,0]) * (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() function 時返回此錯誤消息

我可能遲到了回答這個問題。 但是,讓我們為那些可能遇到此練習的人澄清一些事情。

從我的角度來看,你沒有理解手頭的任務:

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

如果您返回 function 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 function 中沒有錯誤,它也是錯誤的,因為它沒有遵循相同的模式。 也許,您的代碼對示例測試很幸運。

在第二行,有兩個子對角元素: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 課程的一部分:機器學習數學:線性代數。 我有一個通過分配的正確代碼,但發布它會適得其反。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM