很难找出行操作出了问题的地方。按照给出的操作

问题描述 投票:0回答:1

我正在通过 DeepLearning.AI 学习数据科学线性代数课程,其中一个练习涉及线性方程组问题,其中定义 3 个函数(MultiplyRow、AddRows、SwapRows)以将基本运算应用于定义的矩阵 A ,根据给定的说明执行行缩减。

A = np.array([     
            [2, -1, 1, 1],
            [1, 2, -1, -1],
            [-1, 2, 2, 2],
            [1, -1, 2, 1]    
        ], dtype=np.dtype(int)) 
    b = np.array([6,3,14,8], dtype=np.dtype(int))

def MultiplyRow(M, row_num, row_num_multiple):
        M_new = M.copy()
        M_new[row_num] = M_new[row_num] * row_num_multiple
        return M_new
    
    def AddRows(M, row_num_1, row_num_2, row_num_1_multiple):
        M_new = M.copy()   
        M_new[row_num_2] = row_num_1_multiple * M_new[row_num_1] + M_new[row_num_2]
        return M_new

    def SwapRows(M, row_num_1, row_num_2):
        M_new = M.copy()
        M_new[[row_num_1, row_num_2]] = M_new[[row_num_2, row_num_1]]
        return M_new
    def augmented_to_ref(A, b):    
        A_system = np.hstack((A, b.reshape((4, 1))))
        print(A_system)

# swap row 0 and row 1 of matrix A_system (remember that indexing in NumPy array startsfrom0)
        A_ref = SwapRows(A_system, 0, 1)
    
        # multiply row 0 of the new matrix A_ref by -2 and add it to the row 1
        A_ref = AddRows(A_ref, 0, 1, -2)
    
        # multiply row 0 of the new matrix A_ref by -1 and add it to the row 3
        A_ref = AddRows(A_ref, 0, 3, -1)
    
        # add row 2 of the new matrix A_ref to the row 3, replacing row 3
        A_ref = AddRows(A_ref, 2, 3, 1)
    
        # swap row 1 and 3 of the new matrix A_ref
        A_ref = SwapRows(A_ref, 1, 3)
    
        # add row 2 of the new matrix A_ref to the row 3, replacing row 3
        A_ref = AddRows(A_ref, 2, 3, 1)
    
        # multiply row 1 of the new matrix A_ref by -4 and add it to the row 2
        A_ref = AddRows(A_ref, 1, 2, -4)
    
        # add row 1 of the new matrix A_ref to the row 3, replacing row 3
        A_ref = AddRows(A_ref, 1, 3, 1)
        
        # multiply row 3 of the new matrix A_ref by 2 and add it to the row 2
        A_ref = AddRows(A_ref, 3, 2, 2)
    
        # multiply row 2 of the new matrix A_ref by -8 and add it to the row 3
        A_ref = AddRows(A_ref, 2, 3, -8)
    
        # multiply row 3 of the new matrix A_ref by -1/17
        A_ref = MultiplyRow(A_ref, 3, (-1/17))
    
        return A_ref

    A_ref = augmented_to_ref(A, b)

    print(A_ref)

预期输出:

[[ 1  2 -1 -1  3]
 [ 0  1  4  3 22]
 [ 0  0  1  3  7]
 [ 0  0  0  1  1]]

我在这里得到的输出是:

[[ 1  2 -1 -1  3]
 [-1 -1  5  4 19]
 [-1 -2  2  4  4]
 [ 0  0  0  1  0]]
python linear-algebra reduction
1个回答
0
投票
def augmented_to_ref(A, b):    
    ### START CODE HERE ###
    # stack horizontally matrix A and vector b, which needs to be reshaped as a vector (4, 1)
    A_system = np.hstack((A,b.reshape((4,1))))
    
    # swap row 0 and row 1 of matrix A_system (remember that indexing in NumPy array starts from 0)
    A_ref = SwapRows(A_system,0,1)
    
    # multiply row 0 of the new matrix A_ref by -2 and add it to the row 1
    A_ref = AddRows(A_ref,0,1,-2)
    
    # add row 0 of the new matrix A_ref to the row 2, replacing row 2
    A_ref = AddRows(A_ref, 0, 2, 1)
    
    # multiply row 0 of the new matrix A_ref by -1 and add it to the row 3
    A_ref =  AddRows(A_ref,0,3,-1)
    
    # add row 2 of the new matrix A_ref to the row 3, replacing row 3
    A_ref = AddRows(A_ref,2,3,1)
    
    # swap row 1 and 3 of the new matrix A_ref
    A_ref = SwapRows(A_ref,1,3)
    
    # add row 2 of the new matrix A_ref to the row 3, replacing row 3
    A_ref = AddRows(A_ref,2,3,1)
    
    # multiply row 1 of the new matrix A_ref by -4 and add it to the row 2
    A_ref = AddRows(A_ref,1,2,-4)
    
    # add row 1 of the new matrix A_ref to the row 3, replacing row 3
    A_ref = AddRows(A_ref,1,3,1)
    
    # multiply row 3 of the new matrix A_ref by 2 and add it to the row 2
    A_ref = AddRows(A_ref,3,2,2)
    
    # multiply row 2 of the new matrix A_ref by -8 and add it to the row 3
    A_ref = AddRows(A_ref,2,3,-8)
    
    # multiply row 3 of the new matrix A_ref by -1/17
    A_ref = MultiplyRow(A_ref,3,(-1/17))
    ### END CODE HERE ###
    
    return A_ref

A_ref = augmented_to_ref(A, b)

print(A_ref)
© www.soinside.com 2019 - 2024. All rights reserved.