I have a set of sparse matrices filled with boolean values that I need to perform logical operations on (mostly element-wise OR).
as in numpy, summing matrices with dtype='bool' gives the element-wise OR, however there's a nasty side-effect:
>>> from scipy import sparse
>>> [a,b] = [sparse.rand(5,5,density=0.1,format='lil').astype('bool')
... for x in range(2)]
>>> b
<5x5 sparse matrix of type '<class 'numpy.bool_'>'
with 2 stored elements in LInked List format>
>>> a+b
<5x5 sparse matrix of type '<class 'numpy.int8'>'
with 4 stored elements in Compressed Sparse Row format>
The data type gets changed to 'int8', which causes problems for future operations. This could be gotten around with by saying:
(a+b).astype('bool')
But I get the impression that all this type changing would cause a performance hit.
Why is the dtype of the result different from the operands?
And is there a better way to do logical operations on sparse matrices in python?