计算两个解之差的无穷范数

时间:2018-10-19 19:31:53

标签: python numpy

在以下代码中,我能够:

  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

1 个答案:

答案 0 :(得分:0)

如果您想要的只是矩阵的无穷范数, 通常应该看起来像这样:

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

但是由于您的my_solutionnumpy_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)