使用ufunc bitwise_xor输入TypeError

dou*_*lix 14 python if-statement numpy typeerror

在我的程序中追踪粒子的路径,我得到以下错误:

Traceback (most recent call last):
  File "C:\Users\Felix\Google Drive\Research\particles.py", line 154, in <module>
    bfield += b_X(r_p(r,pos[2]))*(r_p(r,pos[2])/r)   
   *((r-r_p(r,pos[2]))**2+pos[2]**2)^(-1/2)*np.array
   ([(1-r_p(r,pos[2])/r)*pos[0],(1-r_p(r,pos[2])/r)*pos[1],pos[2]])

TypeError: ufunc 'bitwise_xor' not supported for the input types, 
and the inputs could not be safely coerced to any supported types 
according to the casting rule ''safe''
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到正在发生的事情.我没有xor的任何实例(虽然我想它可能在if/else语句中编码).

Jos*_*del 33

在违规行中,您使用的是^当您想要**将值提升为幂时.Python将此解释为xor:

bfield += b_X(r_p(r,pos[2]))*(r_p(r,pos[2])/r)*((r-r_p(r,pos[2]))**2+
        pos[2]**2)^(-1/2)*np.array([(1-r_p(r,pos[2])/r)*pos[0],
        (1-r_p(r,pos[2])/r)*pos[1],pos[2]])
Run Code Online (Sandbox Code Playgroud)

看到:

http://docs.python.org/2/reference/expressions.html#binary-bitwise-operations


小智 8

在 Python 中使用 ** 而不是 ^ 来求幂。