Numpy在meshgrid中迭代计算 [英] Numpy Iterating calculations in a meshgrid
问题描述
Emag是我创建的一个长函数。
import numpy as np
X,Y = np.mgrid [0.1:0.15:3j,0.1:0.15:3j]
Z = np.zeros_like )
po = np.sqrt(X ** 2 + Y ** 2)
phio = np.arctan2(Y,X)
print po
print phio
为范围内(po.shape [0]):
为范围内的j(po.shape [1]):
print po [i,j]
打印phio [i,j]
Z [i,j] = Emag(po [i,j],phio [i,j],0)
print Z [i,j]
下面是回应
%run C:/Users/Nigel/Desktop/Nigel07112014.py
[[0.14142136 0.16007811 0.18027756] <--- po
[0.16007811 0.1767767 0.19525624]
[0.18027756 0.19525624 0.21213203]]
[[0.78539816 0.89605538 0.98279372]< --- phio
[0.67474094 0.78539816 0.87605805]
[0.5880026 0.69473828 0.78539816]]
0.141421356237 <--- po [0,0]
0.785398163397 < - phio [0,0]
732.46186213 <--- Z [ 0,0]
0.160078105936 <--- po [0,1]
0.896055384571 < - phio [0,1]
---------- -------------------------------------------------- ---------------
OverflowError Traceback(最近一次调用最后一次)
对于meshgrid的第二个元素来说,计算机似乎无法为Z生成输出。但是,当我分别对po和phio的第二个元素执行Emag时,它工作得很好(见下面):
Emag(0.160078105936,0.896055384571,0)
> 589.541876301
这应该是Z meshgrid的第二个元素。
那么哪里出错?
完整的错误消息如下:
-------------------------------------- -------------------------------------
OverflowError Traceback(最近一次调用的最后一个)
C:\ Users\Nigel\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\\lib\site-packages\在execfile(fname,glob,loc)中的IPython \utils \py3compat.pyc
195 else:
196 filename = fname
- > 197 exec execfilefile(fname,* where):
$ b C:\ Users \ Nigel \Desktop\Nigel07112014.py in< module>()
220 print po [i,j]
221 print phio [i,j]
- > 222 z [i,j] = Emag(po [i,j],phio [i,j],0)
223 print Z [i,j]
224
C:\ Users \ Nigel \Desktop\Nigel07112014.py in Emag(p,phi,z)
149 def Emag(p,phi,z):
150如果z> = 0:
- > VE1p =(p *(z-z0)*((MuZ)/(4 * pi * Eps0 * Eps1))* exp(j * k1 * R0 /(R0 ** 3))*((3 / **(1)* - (3 * j * k1 / R0) - (k1 ** 2))) - (j *((MuZ)/(4 * pi * Eps0 * Eps1))* INTEGRATEZI(lambda kp: (kp * p)* A1(kp)* kp * k1z(kp)*(exp(1j * k1z(kp)*(z + z0)))))
152
153 VE1phi = 0
OverflowError:数学范围错误
<在您的 Emag
中将 exp(j ...
更改为 exp(1j .. 。
如果你正在处理虚构参数的指数,当它在循环内执行时, j
可以收到 bad 值从 j
循环的索引。
如果我对虚构的论点是正确的,检查你的 Emag
函数的结果。
Recently, by the kind response of a stackoverflow member, I learnt how to perform operations on meshgrids with functions that weren't contiguous with with numpy using a for-loop trick to iterate over each of the elements in the grid. I found that it worked on most occasions however, below, for some reason it seems to be failing:
Emag is a long function that I created.
import numpy as np
X, Y = np.mgrid[0.1:0.15:3j, 0.1:0.15:3j]
Z = np.zeros_like(Y)
po = np.sqrt(X**2 + Y**2)
phio = np.arctan2(Y,X)
print po
print phio
for i in range(po.shape[0]):
for j in range(po.shape[1]):
print po[i,j]
print phio[i,j]
Z[i,j] = Emag(po[i,j], phio[i,j], 0)
print Z[i,j]
Below is the response
%run C:/Users/Nigel/Desktop/Nigel07112014.py
[[ 0.14142136 0.16007811 0.18027756] <--- po
[ 0.16007811 0.1767767 0.19525624]
[ 0.18027756 0.19525624 0.21213203]]
[[ 0.78539816 0.89605538 0.98279372] <--- phio
[ 0.67474094 0.78539816 0.87605805]
[ 0.5880026 0.69473828 0.78539816]]
0.141421356237 <--- po[0,0]
0.785398163397 <--- phio[0,0]
732.46186213 <--- Z[0,0]
0.160078105936 <--- po[0,1]
0.896055384571 <--- phio[0,1]
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
It seems that for the second element of the meshgrid, the computer has trouble producing an output for Z. However, when I execute Emag for the second elements of po and phio separately - it works just fine (see below):
Emag(0.160078105936, 0.896055384571, 0)
> 589.541876301
which should be the second element of the Z meshgrid.
So where's it going wrong? I'll paste the much longer error message if requested
Full error message below:
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
C:\Users\Nigel\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195 else:
196 filename = fname
--> 197 exec compile(scripttext, filename, 'exec') in glob, loc
198 else:
199 def execfile(fname, *where):
C:\Users\Nigel\Desktop\Nigel07112014.py in <module>()
220 print po[i,j]
221 print phio[i,j]
--> 222 Z[i,j] = Emag(po[i,j], phio[i,j], 0)
223 print Z[i,j]
224
C:\Users\Nigel\Desktop\Nigel07112014.py in Emag(p, phi, z)
149 def Emag(p, phi, z):
150 if z >=0:
--> 151 VE1p = (p * (z-z0) * ((MuZ)/(4 * pi * Eps0 * Eps1)) * exp(j * k1 * R0 / (R0**3)) * ((3/(R0**2))- (3 * j * k1 / R0)-(k1**2)) ) - ( j * ((MuZ)/(4 * pi * Eps0 * Eps1)) * INTEGRATEZI( lambda kp: J1(kp * p) * A1(kp) * kp * k1z(kp) * (exp(1j * k1z(kp) * (z + z0))) ) )
152
153 VE1phi = 0
OverflowError: math range error
Change exp( j ...
in your Emag
to exp( 1j ...
if you're dealing with exponentials of imaginary argument. When it's executed inside the loop j
can receive bad values from j
the index of the loop.
Should I be right with respect to imaginary arguments, I would also triple check the results of your Emag
function.
这篇关于Numpy在meshgrid中迭代计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!