Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9184781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:08:16+00:00 2026-06-17T19:08:16+00:00

I have a set of sparse matrices filled with boolean values that I need

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:08:17+00:00Added an answer on June 17, 2026 at 7:08 pm

    Logical operations are not supported for sparse matrices, but converting back to a ‘bool’ is not all that expensive. Actually, if using LIL format matrices, the conversion may appear to take negative time due to performance fluctuations:

    a = scipy.sparse.rand(10000, 10000, density=0.001, format='lil').astype('bool')
    b = scipy.sparse.rand(10000, 10000, density=0.001, format='lil').astype('bool')
    
    In [2]: %timeit a+b
    10 loops, best of 3: 61.2 ms per loop
    
    In [3]: %timeit (a+b).astype('bool')
    10 loops, best of 3: 60.4 ms per loop
    

    You may have noticed that your LIL matrices were converted to CSR format before adding them together, look at the return format. If you had already been using CSR format to begin with, then the conversion overhead becomes more noticeable:

    In [14]: %timeit a+b
    100 loops, best of 3: 2.28 ms per loop
    
    In [15]: %timeit (a+b).astype(bool)
    100 loops, best of 3: 2.96 ms per loop
    

    CSR (and CSC) matrices have a data attribute which is a 1D array that holds the actual non-zero entries of the sparse matrix, so the cost of recasting your sparse matrix will depend on the number of non-zero entries of your matrix, not its size:

    a = scipy.sparse.rand(10000, 10000, density=0.0005, format='csr').astype('int8')
    b = scipy.sparse.rand(1000, 1000, density=0.5, format='csr').astype('int8')
    
    In [4]: %timeit a.astype('bool') # a is 10,000x10,000 with 50,000 non-zero entries
    10000 loops, best of 3: 93.3 us per loop
    
    In [5]: %timeit b.astype('bool') # b is 1,000x1,000 with 500,000 non-zero entries
    1000 loops, best of 3: 1.7 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have set up an app that is registered for remote notifications. - (void)application:(UIApplication*)application
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
I have a sparse matrix that represents a 3D rectangular space. Along some of
Assuming that we have a set of elements and want to store them in
I have a set of 200M documents I need to index. Every document has
We have an application that stores a sparse matrix. This matrix has entries that
I have created an arff file for a data set that I would like
I have a specific need to have a particularly large number of sparse indexes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.