简体   繁体   English

Python 3.8 numpy 数组减法

[英]Python 3.8 numpy array subtraction

Update: u_n = u[n,:].copy() fixed the issue.更新: u_n = u[n,:].copy()解决了这个问题。 Thanks, everyone for their valuable suggestions.谢谢大家提出宝贵的建议。 The answer suggesting the fix is marked.建议修复的答案已标记。


I have a code that generates two arrays:我有一个生成两个 arrays 的代码:

u_n = [0.00000000e+00 -3.55754723e-04 -5.83161988e-04 -7.28203241e-04
 -8.20386731e-04 -8.78649151e-04 -9.15142981e-04 -9.37666984e-04
 -9.51225955e-04 -9.59031686e-04 -9.63145318e-04 -9.64889573e-04
 -9.65113299e-04 -9.64361236e-04 -9.62982969e-04 -9.61202840e-04...]

u = [ 0.00000000e+00 -5.71470888e-04 -9.86586605e-04 -1.28338884e-03
 -1.49272978e-03 -1.63854091e-03 -1.73883197e-03 -1.80686241e-03
 -1.85223351e-03 -1.88180768e-03 -1.90043862e-03 -1.91152978e-03
 -1.91745053e-03 -1.91984028e-03 -1.91982739e-03 -1.91818493e-03
 -1.91544043e-03 -1.91195258e-03 -1.90796453e-03 -1.90364059e-03...]

I am subtracting these two arrays using np.subtract (also tried subtracting individual elements like u-u_n ).我正在使用np.subtract减去这两个 arrays (也尝试减去单个元素,如u-u_n )。 Python is computing (not just printing) the result as 0 for each element. Python 正在计算(不仅仅是打印)每个元素的结果为 0。 This is affecting the convergence of my code.这影响了我的代码的收敛性。

How do I use the arithmetics properly?如何正确使用算术? Thanks in advance.提前致谢。

Edit: Non-zero results are expected as there is some difference between elements of the two arrays.编辑:预计非零结果,因为两个 arrays 的元素之间存在一些差异。 Python, however, returns [0,0,0,0,......,0] for np.subtract(u-u_n) .然而,Python 返回[0,0,0,0,......,0]np.subtract(u-u_n) My code is below.我的代码如下。

# Compute b and solve linear system
print(u_n)
for i in range(1, Nx, 1):
    b[i] = u_n[i]-(k*np.cos(t[n])*(L-x[i]))/L

    b[0] = 0; b[Nx] = 0
    u[n,:] = scipy.sparse.linalg.spsolve(A, b)
    u[n,0] = 0; u[n,Nx] = 0
    print(u[n,:])

    R = np.subtract(u[n,:],u_n)/k
    print(R)
    R = R**2
    R2 = np.sum(R)
    R2 = np.sqrt(R2)
    print ('R2 = %.9f' %R2)

    #Update u_n before next step

    u_n = u[n,:]

Output: Output:

u_n = [ 0.00000000e+00 -3.55754723e-04 -5.83161988e-04 -7.28203241e-04
 -8.20386731e-04 -8.78649151e-04 -9.15142981e-04 -9.37666984e-04
 -9.51225955e-04 -9.59031686e-04 -9.63145318e-04 -9.64889573e-04
 -9.65113299e-04 -9.64361236e-04 -9.62982969e-04 -9.61202840e-04
 -9.59164819e-04 -9.56961297e-04 -9.54651567e-04 -9.52273679e-04....]

u = [ 0.00000000e+00 -5.71470888e-04 -9.86586605e-04 -1.28338884e-03
 -1.49272978e-03 -1.63854091e-03 -1.73883197e-03 -1.80686241e-03
 -1.85223351e-03 -1.88180768e-03 -1.90043862e-03 -1.91152978e-03
 -1.91745053e-03 -1.91984028e-03 -1.91982739e-03 -1.91818493e-03
 -1.91544043e-03 -1.91195258e-03 -1.90796453e-03 -1.90364059e-03...]

# R = u - u_n

R = [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ....]

Try to change line尝试换行

u_n = u[n,:]

to

u_n = u[n,:].copy()

Slicing creates a view, so modifying the view modifies the original array as well and vice versa.切片会创建一个视图,因此修改视图也会修改原始数组,反之亦然。 As both arrays points to the same data the difference is a bunch of zeros.由于 arrays 都指向相同的数据,因此差异是一堆零。 The problem can be solve by coping the data while extracting u_n .该问题可以通过在提取u_n时处理数据来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM