Comparing two scipy.sparse matrices in python

I am working on a program that deals with large networks, and therefore I have to use sparse matrices (preferred scipy.sparse.csr). Now I would like to write a function that takes two sparse logic matrices A and B and returns B without those entries that are set to A. Here is an example of pseudocode.

def f(A, B):
    return B and (not A)

How can this be done using scipy.sparse matrices?

+4
source share
1 answer

Here is one way to implement your function:

def f(a, b):
    return b - b.multiply(a)

b.multiply(a)- actually an elemental operation and.

Here is an example. aand bare sparse matrices:

In [134]: b.A
Out[134]: array([[False, False,  True,  True]], dtype=bool)

In [135]: a.A
Out[135]: array([[False,  True, False,  True]], dtype=bool)

In [136]: f(a,b).A
Out[136]: array([[False, False,  True, False]], dtype=bool)
+2
source

All Articles