Python ufunc bitwise_xor 的类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22725421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:29:56  来源:igfitidea点击:

TypeError with ufunc bitwise_xor

pythonif-statementnumpytypeerror

提问by doublefelix

In my program which traces out the path of a particle, I get the following error:

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

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''

I can't seem to find what's going on. I don't have any instances of xor (although I suppose it might be encoded in an if/else statement).

我似乎无法找到发生了什么。我没有任何 xor 实例(尽管我认为它可能被编码在 if/else 语句中)。

采纳答案by JoshAdel

In the offending line you are using a ^when you want a **to raise a value to a power. Python interprets this as an xor:

在违规行中,^当您希望 a**将值提高到幂时,您正在使用 a 。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]])

See:

看:

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

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