首页 > 解决方案 > 计算两个解之间差异的无穷范数

问题描述

在下面的代码中,我已经能够:

  1. 为一般方形线性系统实现高斯消元,无需旋转。
  2. 我通过求解 Ax=b 对其进行了测试,其中 A 是一个随机 100x100 矩阵,b 是一个随机 100x1 向量。
  3. 我将我的解决方案与使用 numpy.linalg.solve 获得的解决方案进行了比较

然而,在最后的任务中,我需要计算两个解决方案之间差异的无穷范数。我知道无穷范数是矩阵的最大绝对行和。但是我怎么能这样做来计算两个解决方案之间差异的无穷范数,我的解决方案和 numpy.linalg.solve。寻求一些帮助!

import numpy as np
def GENP(A, b):
    '''
    Gaussian elimination with no pivoting.
    % input: A is an n x n nonsingular matrix
    %        b is an n x 1 vector
    % output: x is the solution of Ax=b.
    % post-condition: A and b have been modified. 
    '''
    n =  len(A)
    if b.size != n:
        raise ValueError("Invalid argument: incompatible sizes between A & b.", b.size, n)
    for pivot_row in range(n-1):
        for row in range(pivot_row+1, n):
            multiplier = A[row][pivot_row]/A[pivot_row][pivot_row]
            #the only one in this column since the rest are zero
            A[row][pivot_row] = multiplier
            for col in range(pivot_row + 1, n):
                A[row][col] = A[row][col] - multiplier*A[pivot_row][col]
            #Equation solution column
            b[row] = b[row] - multiplier*b[pivot_row]
    x = np.zeros(n)
    k = n-1
    x[k] = b[k]/A[k,k]
    while k >= 0:
        x[k] = (b[k] - np.dot(A[k,k+1:],x[k+1:]))/A[k,k]
        k = k-1
    return x
if __name__ == "__main__":
    A = np.round(np.random.rand(100, 100)*10)
    b =  np.round(np.random.rand(100)*10)
    print (GENP(np.copy(A), np.copy(b)))

例如,此代码为上面列出的任务 1 提供以下输出:

[-6.61537666  0.95704368  1.30101768 -3.69577873 -2.51427519 -4.56927017
 -1.61201589  2.88242622  1.67836096  2.18145556  2.60831672  0.08055869
 -2.39347903  2.19672137 -0.91609732 -1.17994959 -3.87309152 -2.53330865
  5.97476318  3.74687301  5.38585146 -2.71597978  2.0034079  -0.35045844
  0.43988439 -2.2623829  -1.82137544  3.20545721 -4.98871738 -6.94378666
 -6.5076601   3.28448129  3.42318453 -1.63900434  4.70352047 -4.12289961
 -0.79514656  3.09744616  2.96397264  2.60408589  2.38707091  8.72909353
 -1.33584905  1.30879264 -0.28008339  0.93560728 -1.40591226  1.31004142
 -1.43422946  0.41875924  3.28412668  3.82169545  1.96675247  2.76094378
 -0.90069455  1.3641636  -0.60520103  3.4814196  -1.43076816  5.01222382
  0.19160657  2.23163261  2.42183726 -0.52941262 -7.35597457 -3.41685057
 -0.24359225 -5.33856181 -1.41741354 -0.35654736 -1.71158503 -2.24469314
 -3.26453092  1.0932765   1.58333208  0.15567584  0.02793548  1.59561909
  0.31732915 -1.00695954  3.41663177 -4.06869021  3.74388762 -0.82868155
  1.49789582 -1.63559124  0.2741194  -1.11709237  1.97177449  0.66410154
  0.48397714 -1.96241854  0.34975886  1.3317751   2.25763568 -6.80055066
 -0.65903682 -1.07105965 -0.40211347 -0.30507635]

然后对于任务二,我的代码给出以下内容:

my_solution = GENP(np.copy(A), np.copy(b))
numpy_solution = np.linalg.solve(A, b)
print(numpy_solution)

导致:

[-6.61537666  0.95704368  1.30101768 -3.69577873 -2.51427519 -4.56927017
-1.61201589  2.88242622  1.67836096  2.18145556  2.60831672  0.08055869
-2.39347903  2.19672137 -0.91609732 -1.17994959 -3.87309152 -2.53330865
 5.97476318  3.74687301  5.38585146 -2.71597978  2.0034079  -0.35045844
 0.43988439 -2.2623829  -1.82137544  3.20545721 -4.98871738 -6.94378666
-6.5076601   3.28448129  3.42318453 -1.63900434  4.70352047 -4.12289961
-0.79514656  3.09744616  2.96397264  2.60408589  2.38707091  8.72909353
-1.33584905  1.30879264 -0.28008339  0.93560728 -1.40591226  1.31004142
-1.43422946  0.41875924  3.28412668  3.82169545  1.96675247  2.76094378
 -0.90069455  1.3641636  -0.60520103  3.4814196  -1.43076816  5.01222382
 0.19160657  2.23163261  2.42183726 -0.52941262 -7.35597457 -3.41685057
-0.24359225 -5.33856181 -1.41741354 -0.35654736 -1.71158503 -2.24469314
-3.26453092  1.0932765   1.58333208  0.15567584  0.02793548  1.59561909
 0.31732915 -1.00695954  3.41663177 -4.06869021  3.74388762 -0.82868155
 1.49789582 -1.63559124  0.2741194  -1.11709237  1.97177449  0.66410154
 0.48397714 -1.96241854  0.34975886  1.3317751   2.25763568 -6.80055066
-0.65903682 -1.07105965 -0.40211347 -0.30507635]

最后是任务3:

if np.allclose(my_solution, numpy_solution):
    print("These solutions agree")
else:
   print("These solutions do not agree")

导致:

These solutions agree

标签: pythonnumpy

解决方案


如果你想要的只是矩阵的无穷范数,它通常应该是这样的:

def inf_norm(matrix):
    return max(abs(row.sum()) for row in matrix)

但是由于您的my_solutionandnumpy_solution只是一维向量,您可以重塑它们(我假设 100x1 这是您在示例中所拥有的)以用于上述函数:

备选方案 1:

def inf_norm(matrix):
    return max(abs(row.sum()) for row in matrix)

diff = my_solution - numpy_solution
inf_norm_result = inf_norm(diff.reshape((100, 1))

备选方案 2:

或者如果你知道它们总是一维向量,你可以省略总和(因为行的长度都是 1)并直接计算它:

abs(my_solution - numpy_solution).max()

备选方案 3:

numpy.linalg.norm或如(见下文)文档中所写:

max(sum(abs(my_solution - numpy_solution), axis=1))

备选方案 4:

或使用numpy.linalg.norm()(参见:https ://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.linalg.norm.html ):

np.linalg.norm(my_solution - numpy_solution, np.inf)

推荐阅读