为什么复杂网格网格数组上的numpy.exp给出错误的实部?

时间:2018-07-13 07:23:24

标签: python arrays numpy complex-numbers

我有一个嵌套的复数数组,

xi:

 [[[ 2.51325641-2.34963293j  2.17949212-1.57079633j  2.51325641-0.79195972j]
  [ 2.15322703+3.14159265j  0.00000000+1.57079633j  2.15322703+0.j        ]
  [ 2.51325641+2.34963293j  2.17949212+1.57079633j  2.51325641+0.79195972j]]

 [[ 2.44651048-2.3486959j   2.11452586-1.57079633j  2.44651048-0.79289676j]
  [ 2.08450333+3.14159265j  0.00000000+1.57079633j  2.08450333+0.j        ]
  [ 2.44651048+2.3486959j   2.11452586+1.57079633j  2.44651048+0.79289676j]]]

使用numpy.exp可以得到以下信息:

np.exp(xi):

 [[[ -8.67181418e+00 -8.78636871e+00j   5.41404995e-16 -8.84181457e+00j
     8.67181418e+00 -8.78636871e+00j]
  [ -8.61260674e+00 +1.05474013e-15j   6.12323400e-17 +1.00000000e+00j
     8.61260674e+00 +0.00000000e+00j]
  [ -8.67181418e+00 +8.78636871e+00j   5.41404995e-16 +8.84181457e+00j
     8.67181418e+00 +8.78636871e+00j]]

 [[ -8.10419460e+00 -8.22665532e+00j   5.07350124e-16 -8.28565631e+00j
     8.10419460e+00 -8.22665532e+00j]
  [ -8.04059693e+00 +9.84689130e-16j   6.12323400e-17 +1.00000000e+00j
     8.04059693e+00 +0.00000000e+00j]
  [ -8.10419460e+00 +8.22665532e+00j   5.07350124e-16 +8.28565631e+00j
     8.10419460e+00 +8.22665532e+00j]]]

但是,当我逐一检查它们时,某些元素的实部是不正确的,例如第一个嵌套数组的第一行的第二列:

In [1]: np.exp(2.17949212-1.57079633j)
Out[1]: (-2.833893031963725e-08-8.8418145374224597j)

但其他方法很好(例如,数组1行1列1)。

In [2]: np.exp(2.51325641-2.34963293j)
Out[2]: (-8.671814171261488-8.7863687332566318j)

这对我来说毫无意义,因为numpy.exp文档似乎暗示e ^(a + ib)的计算方式为e ^ a *(cos(b)+ i sin(b)),所以我不知道没有看到虚部是正确的,而实部却没有。

是否可以使numpy.exp在我的数组中一致地工作?

编辑:

已经指出,如上定义xi确实可以通过np.exp得到正确的结果。当我将其定义为上述值的独立数组时,它也可以在我的python环境中使用。但是,np.exp(xi)似乎仍无法以我生成xi的方式正常工作:

d = np.array([0.91651514, 0.9797959])

spacing = 3
limit = 4
x = np.linspace(-limit, limit,spacing)
y = np.linspace(-limit,limit,spacing)
X, Y = np.meshgrid(x, y)
def z(x,y):
    return x + 1j * y
z = z(X, Y)

xi = []
for i in range(len(d)):
    xxi = np.arccosh(z/d[i])
    xi.append(xxi)
xi = np.asarray(xi)

我创建xi的方式是否与numpy.exp奇怪地起作用?

1 个答案:

答案 0 :(得分:0)

根据我的结果,numpy似乎确实计算出正确的结果:

我的控制台上的跟踪:

import numpy as np

xi = np.array([[
  [2.51325641-2.34963293j,  2.17949212-1.57079633j,  2.51325641-0.79195972j],
  [ 2.15322703+3.14159265j,  0.00000000+1.57079633j,  2.15322703+0.j        ],
  [ 2.51325641+2.34963293j,  2.17949212+1.57079633j , 2.51325641+0.79195972j]],

 [[ 2.44651048-2.3486959j ,  2.11452586-1.57079633j , 2.44651048-0.79289676j],
  [ 2.08450333+3.14159265j , 0.00000000+1.57079633j , 2.08450333+0.j        ],
  [ 2.44651048+2.3486959j ,  2.11452586+1.57079633j , 2.44651048+0.79289676j]]])



xi
Out[7]: 
array([[[2.51325641-2.34963293j, 2.17949212-1.57079633j,
         2.51325641-0.79195972j],
        [2.15322703+3.14159265j, 0.        +1.57079633j,
         2.15322703+0.j        ],
        [2.51325641+2.34963293j, 2.17949212+1.57079633j,
         2.51325641+0.79195972j]],

       [[2.44651048-2.3486959j , 2.11452586-1.57079633j,
         2.44651048-0.79289676j],
        [2.08450333+3.14159265j, 0.        +1.57079633j,
         2.08450333+0.j        ],
        [2.44651048+2.3486959j , 2.11452586+1.57079633j,
         2.44651048+0.79289676j]]])

np.exp(xi)
Out[8]: 
array([[[-8.67181417e+00-8.78636873e+00j,
         -2.83389303e-08-8.84181454e+00j,
          8.67181420e+00-8.78636870e+00j],
        [-8.61260674e+00+3.09174756e-08j,
         -3.20510345e-09+1.00000000e+00j,
          8.61260674e+00+0.00000000e+00j],
        [-8.67181417e+00+8.78636873e+00j,
         -2.83389303e-08+8.84181454e+00j,
          8.67181420e+00+8.78636870e+00j]],

       [[-8.10419466e+00-8.22665531e+00j,
         -2.65563855e-08-8.28565627e+00j,
          8.10419461e+00-8.22665536e+00j],
        [-8.04059697e+00+2.88640789e-08j,
         -3.20510345e-09+1.00000000e+00j,
          8.04059697e+00+0.00000000e+00j],
        [-8.10419466e+00+8.22665531e+00j,
         -2.65563855e-08+8.28565627e+00j,
          8.10419461e+00+8.22665536e+00j]]])

np.exp(2.17949212-1.57079633j)
Out[9]: (-2.833893031963725e-08-8.84181453742246j)

我正在使用Python 3.6.5(Anaconda Python)和NumPy 1.14.3版。您是否尝试过在另一台机器/其他python实例上验证结果?