簡體   English   中英

壓縮稀疏行的 Jacobi 方法

[英]Jacobi method for compressed sparse row

我正在嘗試研究論文中的算法,該論文用於壓縮稀疏行格式以執行雅可比迭代。 我正在嘗試研究論文中給出的例子。 我正在使用 Python,但遇到了錯誤。 請幫忙。 感謝您的時間和考慮。

import numpy as np
from scipy import sparse
from scipy.sparse import diags
from numpy import linalg as LA
np_1=np.array([[3,0,0,1],[1,4,0,2]])
np_2=np.array([[0,0,2,0],[2,0,3,6]])
np.append(np_1, np_2, axis=0)
A=np.append(np_1, np_2, axis=0)
ACSR=sparse.csr_matrix(A)
ACSR.indptr
IA=ACSR.indptr
jA=ACSR.indices
x=np.array([0,0,0,0])
N=20
n=len(A)
b=np.array([6,-2,6,13])


for p in range(20):
    for i in range(n):
        t=b[i]
        for j in range(IA[i],IA[i+1]-1):
            if j==i:
                D=A[j]
                print(D)
            else:
                t=t-A[j]*x[jA[j]]
            print(t)
        
        x[i]=t/D
    print(x)
endfor 


以上是我在 Python 中嘗試的編碼。[下面給出的圖像是研究論文中給出的算法] ( https://i.stack.imgur.com/pyunM.png )

[下面是research paper給出的例子,如何獲取research paper給出的output?] ( https://i.stack.imgur.com/VatAP.png )

我運行了你的代碼和 go 這個 output:

[3 0 0 1]
6
C:\Users\paul\AppData\Local\Temp\ipykernel_6836\3967110093.py:29: RuntimeWarning: divide by zero encountered in true_divide
  x[i]=t/D
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[103], line 29
     26                 t=t-A[j]*x[jA[j]]
     27             print(t)
---> 29         x[i]=t/D
     30     print(x)
     31 endfor

ValueError: setting an array element with a sequence.

因為我在交互式 ipython session 中運行了它,所以我可以檢查值,因為它們是由於錯誤而留下的:

In [104]: x
Out[104]: array([0, 0, 0, 0])    # 1d array as expect by the creation line

In [105]: i           # x[i] is a slot for ONE value
Out[105]: 0
In [106]: x[i]
Out[106]: 0

你想分配給那個插槽的是什么:

In [107]: t
Out[107]: 6    
In [109]: D
Out[109]: array([3, 0, 0, 1])    # 4 element array

In [110]: t/D
C:\Users\paul\AppData\Local\Temp\ipykernel_6836\1390120621.py:1: RuntimeWarning: divide by zero encountered in true_divide
  t/D
Out[110]: array([ 2., inf, inf,  6.])

這解釋了警告。 錯誤 - 4 元素數組/序列不能放入 1 元素槽中。

哎呀,我看到你已經打印了tD值。 那里你應該已經知道這個問題的根源了。

DA[j] ,從 2d A中提取的 1d 行。

我不知道你打算在這里發生什么,但我看到的是明顯缺乏調試技巧。 當您遇到錯誤時 - 展示它們。 並絕望地舉起雙手,跑到互聯網上尋求幫助。 :) 確定問題,然后您可以決定進行更正。

我仍然無法讀取圖像,但我懷疑部分問題是您選擇的是A數組的整行,而不是A (或ACSR )的連續非零元素。 因此,讓我們獲取這些元素:

In [160]: dA =ACSR.data
In [161]: dA
Out[161]: array([3, 1, 1, 4, 2, 2, 2, 3, 6], dtype=int32)

並更改循環以逐步執行這些操作(我刪除了 -1)。 現在x[i]的分配工作正常(它是標量):

In [162]: x = np.zeros(4, float)
     ...: for i in range(4):
     ...:     t=b[i]
     ...:     for j in range(IA[i],IA[i+1]):
     ...:         print('j',j, dA[j])
     ...:         if j==i:
     ...:             D=dA[j]
     ...:         else:
     ...:             t=t-dA[j]*x[jA[j]]
     ...:     x[i] = t/D
     ...:     
j 0 3     # i=0
j 1 1
j 2 1     # i=1
j 3 4
j 4 2
j 5 2     # i=2
j 6 2     # i=3
j 7 3
j 8 6

In [163]: x
Out[163]: array([ 2.        , -1.33333333,  2.        ,  1.        ])

你的首字母x[0,0,0,0] , int dtype; 我讓它是浮動的,因為t/D可能是小數。

這可能不對,但至少它運行時沒有出現明顯的錯誤。

暫無
暫無

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

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