Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError with ufunc bitwise_xor

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).

like image 897
doublefelix Avatar asked Mar 29 '14 00:03

doublefelix


2 Answers

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:

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

like image 169
JoshAdel Avatar answered Oct 14 '22 11:10

JoshAdel


Use ** rather than ^ for raising a value to a power in python.

like image 4
Motahare Avatar answered Oct 14 '22 12:10

Motahare