Boolean operation of a Boolean variable on a symbol produces TypeError
, but the reverse has no problem:
>>> from sympy import *
>>> x = Symbol('x', bool=True)
>>> x ^ True
Not(x)
>>> True ^ x
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
True ^ x
TypeError: unsupported operand type(s) for ^: 'bool' and 'Symbol'
I can do try-catch:
try :
print True ^ x
except TypeError:
print x ^ True
Not(x)
But, for my present task, it is impossible to implement this with try-except
as I have to deal with ~200 symbols. How can I achieve this?
numpy.array
,S
, with both symbols and Booleans arbitrarily mixed. I have operations like:S[15] ^ (S[19] & S[72]) ^ S[112]
. Now, without knowing which one is Boolean and which one is symbol, I can not do try-except things, as number of such try-except blocks will grow exponentially. – Shend[(15, 19, 73, 112), ...]
this won't work if its not always the same operations. You could also use a list of expressions['S[15] ^ (S[19] & S[72]) ^ S[112]', ...]
, andeval
them inside a try-except. I wouldn't do that unless I have no other solution though. – Ozan