Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 存储由两个for循环计算出的矩阵(添加列表不起作用)_Python_Python 3.x_Numpy_Matrix - Fatal编程技术网

Python 存储由两个for循环计算出的矩阵(添加列表不起作用)

Python 存储由两个for循环计算出的矩阵(添加列表不起作用),python,python-3.x,numpy,matrix,Python,Python 3.x,Numpy,Matrix,我正在实现Jacobi迭代法 问题是我无法在每次迭代后存储计算出的矩阵,我尝试将其附加到一个空列表中,但它会不断覆盖该列表中以前的元素,结果是一个矩阵重复K次 我需要对这些矩阵进行减法运算,以获得收敛标准 # Iterate Jacobi until convergence U = np.array([[8.9,8.9,8.9,8.9,8.9],[8.4,0,0,0,9.2],[7.2,0,0,0,9.4],[6.1,6.8,7.7,8.7,6.1]]) UI=U UF=U UFK=[] k

我正在实现Jacobi迭代法

问题是我无法在每次迭代后存储计算出的矩阵,我尝试将其附加到一个空列表中,但它会不断覆盖该列表中以前的元素,结果是一个矩阵重复K次

我需要对这些矩阵进行减法运算,以获得收敛标准

# Iterate Jacobi until convergence  
U = np.array([[8.9,8.9,8.9,8.9,8.9],[8.4,0,0,0,9.2],[7.2,0,0,0,9.4],[6.1,6.8,7.7,8.7,6.1]])
UI=U
UF=U
UFK=[]
k=0
while k<3:
    k=k+1 # update the iteration counter
    for i in range (1,Nx-1):
        for j in range (1,Ny-1):
            UF[j,i] = (UI[j+1,i]+UI[j,i+1]+UI[j-1,i]+UI[j,i-1])*0.25 #the matrix i want to store after each iteration 
    UFK.append(UF) # 
    print (UF) # when i print UF i get the correct matrix at each iteration displayed 

[[ 8.9         8.9         8.9         8.9         8.9       ]
 [ 8.4         4.325       3.30625     5.3515625   9.2       ]
 [ 7.2         4.58125     3.896875    6.83710938  9.4       ]
 [ 6.1         6.8         7.7         8.7         6.1       ]]
[[ 8.9         8.9         8.9         8.9         8.9       ]
 [ 8.4         6.296875    6.11132812  7.76210937  9.2       ]
 [ 7.2         6.0484375   6.67421875  8.13408203  9.4       ]
 [ 6.1         6.8         7.7         8.7         6.1       ]]
[[ 8.9         8.9         8.9         8.9         8.9       ]
 [ 8.4         7.36494141  7.67531738  8.47734985  9.2       ]
 [ 7.2         7.00979004  7.62979736  8.5517868   9.4       ]
 [ 6.1         6.8         7.7         8.7         6.1       ]]

print(UFK) # when i display the appended UFK it is just repeating a single matrix 3 times

[array([[ 8.9       ,  8.9       ,  8.9       ,  8.9       ,  8.9       ],
        [ 8.4       ,  7.36494141,  7.67531738,  8.47734985,  9.2       ],
        [ 7.2       ,  7.00979004,  7.62979736,  8.5517868 ,  9.4       ],
        [ 6.1       ,  6.8       ,  7.7       ,  8.7       ,  6.1       ]]),
 array([[ 8.9       ,  8.9       ,  8.9       ,  8.9       ,  8.9       ],
        [ 8.4       ,  7.36494141,  7.67531738,  8.47734985,  9.2       ],
        [ 7.2       ,  7.00979004,  7.62979736,  8.5517868 ,  9.4       ],
        [ 6.1       ,  6.8       ,  7.7       ,  8.7       ,  6.1       ]]),
 array([[ 8.9       ,  8.9       ,  8.9       ,  8.9       ,  8.9       ],
        [ 8.4       ,  7.36494141,  7.67531738,  8.47734985,  9.2       ],
        [ 7.2       ,  7.00979004,  7.62979736,  8.5517868 ,  9.4       ],
        [ 6.1       ,  6.8       ,  7.7       ,  8.7       ,  6.1       ]])]
#迭代Jacobi直到收敛
U=np.数组([[8.9,8.9,8.9,8.9,8.9],[8.4,0,0,0,9.2],[7.2,0,0,0,9.4],[6.1,6.8,7,8.7,6.1])
UI=U
UF=U
UFK=[]
k=0

当k时,您必须在附加UFK之前设置其维度,或者总是多次复制同一矩阵。以下代码可以正确生成输出:

UFK = np.array([]).reshape(0,5)
k = 0
while k < 3:
    k += 1
    for i in range(1, Nx-1):
        for j in range(1, Ny-1):
            UF[j, i] = (UI[j+1, i] + UI[j, i+1] + UI[j-1, i] + UI[j, i-1]) * 0.25
    UFK = np.append(UFK, UF, axis=0)
UFK=np.array([])。重塑(0,5)
k=0
当k<3时:
k+=1
对于范围内的i(1,Nx-1):
对于范围(1,Ny-1)内的j:
UF[j,i]=(UI[j+1,i]+UI[j,i+1]+UI[j-1,i]+UI[j,i-1])*0.25
UFK=np.append(UFK,UF,axis=0)
另一种附加数组的方法是
UFK=np.vstack((UFK,UF))
,它将给出相同的结果

UI=U  # why?  UI is not a copy of U, it IS U
# UF=U  # another why?  Changes of UF will change UI and U as well
UFK=[]    # appending to a list is great
k=0
while k<3:
    k=k+1 # update the iteration counter
    UF = np.zeros_like(U)  # a fresh copy for iteration
    for i in range (1,Nx-1):
        for j in range (1,Ny-1):
            UF[j,i] = (UI[j+1,i]+UI[j,i+1]+UI[j-1,i]+UI[j,i-1])*0.25 
    UFK.append(UF) # 
    print (UF) 
print(UFK)
列表包含指向对象的指针。如果我写

alist = [U, U, U]
U[0,0] = 10000
10000
将出现在列表的所有3个元素中,因为它们是相同的东西


在代码中,将case
UF
添加到列表中,然后在每次迭代中修改它。结果是,您的列表只包含指向同一数组的
k
指针。

请直接添加代码、输入和输出-无屏幕截图。您是对的,我编辑了我的请求。这是我第一次问关于stackoverflow的问题,我不知道如何添加代码,现在我知道了:)谢谢!这里还有一些提示:非常感谢,这并不是我想要的,但它是80%,我有一个充满矩阵行的列表,但它们是分开的,我添加了一个拆分,它可以工作知道:
而kappinding to a list
UFK
是非常好的;事实上,我们中的一些人认为它比将(
np.append
)连接到数组要好。谢谢你,它可以与
UFK.append(U.copy())
一起工作!!你完全正确,我正在用python编程的第一步,我不知道
UI=U
UF=U
会覆盖原来的,我希望
U
保持不变。如何将其保存在变量中不受影响?U是计划的一部分,因为我只修改了矩阵的内部第一行,列将保持不变,因此
UF=np。像(U)一样的零将不起作用。再次感谢你们,我有太多的东西要学。我遵循了同样的技巧
UI=U.copy()
,现在
U
没有改变。酷!!
alist = [U, U, U]
U[0,0] = 10000