Octave与Python中复数算术运算结果不一致问题求助
Octave与Python复数运算结果不一致问题
现象说明
单独计算v和w时,Octave与Python的输出结果非常接近:
Octave中v的输出
v octave values = Columns 1 through 3: 0 - 223.4950i 0 - 194.4942i 0 - 167.4933i ... Columns 16 through 18: 0 + 0i 1.4142 + 0i 0 + 2.0000i ... Columns 28 through 30: 0 + 142.4921i 0 + 167.4933i 0 + 194.4942i Column 31: 0 + 223.4950i
Python中v的输出
v python values [-0. -223.49496639j -0. -194.49421585j -0. -167.49328345j -0. -142.49210504j ... -1.41421356 +0.j 0. +0.j ... 0. +167.49328345j 0. +194.49421585j 0. +223.49496639j]
Octave中w的输出
w octave values = Columns 1 through 4: %before 0 -4.7124 + 8.5456i -4.7124 + 8.3371i -4.7124 + 8.1130i -4.7124 + 7.8705i ... Columns 13 through 16: -4.7124 + 3.4386i -4.7124 + 1.6479i -1.8464 + 0i 0 + 0i ... Columns 25 through 28: %after 0 4.7124 - 6.9950i 4.7124 - 7.3165i 4.7124 - 7.6064i 4.7124 - 7.8705i Columns 29 through 31: 4.7124 - 8.1130i 4.7124 - 8.3371i 4.7124 - 8.5456i
Python中w的输出
w python values [-4.71238898+8.54562334j -4.71238898+8.33714905j -4.71238898+8.11296806j ... 0. +0.j 1.84643913+0.j ... 4.71238898+7.87049456j 4.71238898+8.11296806j 4.71238898+8.33714905j 4.71238898+8.54562334j]
但计算z = v + w时,两者结果差异明显:
Octave中z的输出
z = v+w octave values = Columns 1 through 3: -4.7124 - 214.9493i -4.7124 - 186.1571i -4.7124 - 159.3803i ... Columns 16 through 18: %after 0 0 + 0i 3.2607 + 0i 4.7124 + 0.3521i Columns 19 through 21: 4.7124 + 3.9098i 4.7124 + 9.9835i 4.7124 + 18.2866i ... Columns 28 through 30: 4.7124 + 134.6216i 4.7124 + 159.3803i 4.7124 + 186.1571i Column 31: 4.7124 + 214.9493i
Python中z的输出
z= v+w python values [-4.71238898-214.94934305j -4.71238898-186.1570668j -4.71238898-159.38031539j -4.71238898-134.62161049j ... -3.26065269 +0.j 0. +0.j 3.26065269 +0.j 4.71238898 +3.64791843j ... 4.71238898+127.09702398j 4.71238898+150.3625996j 4.71238898+175.60625151j 4.71238898+202.8313649j 4.71238898+232.04058972j]
代码实现
Octave代码
%octave code x = -15:1:15; E = 3; p = sqrt(E - x.^2); v = x.*p; w = E.*asin(x./sqrt(E)); z = w + v;
Python代码
#python Code import numpy as np x = np.arange(-15, 16, 1) E = 3 p = np.sqrt(E - x.astype(complex)**2) v = x.astype(complex)*p w = E*np.arcsin(x.astype(complex)/np.sqrt(E)) z = w + v
问题
请问该差异产生的原因是什么?如何解决这个问题?
内容的提问来源于stack exchange,提问作者user20186251
相关产品推荐
相关产品推荐

