Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra converting in coo_matrix.multiply() when multiplying vector #10055

Open
gan-lin opened this issue Apr 12, 2019 · 1 comment
Open

Extra converting in coo_matrix.multiply() when multiplying vector #10055

gan-lin opened this issue Apr 12, 2019 · 1 comment

Comments

@gan-lin
Copy link

gan-lin commented Apr 12, 2019

coo_matrix.multiply() method is inherited from spmatrix, which convert self to csr format before calculating.

# in base.py
class spmatrix(object):
    # ...
    def multiply(self, other):
        return self.tocsr().multiply(other)

However, in csr_matrix.multiply(), when multiplying a dense vector, it's done by converting to coo_matrix first. It's inherited from _cs_matrix.

# in compressed.py
class _cs_matrix(_data_matrix, _minmax_mixin, IndexMixin):
    # ...
    def multiply(self, other):
        # ...
        ret = self.tocoo()
        # ...
        # Sparse matrix times dense row vector.
        elif other.shape[0] == 1 and self.shape[1] == other.shape[1]:
            data = np.multiply(ret.data, other[:, ret.col].ravel())
        # Sparse matrix times dense column vector.
        elif other.shape[1] == 1 and self.shape[0] == other.shape[0]:
            data = np.multiply(ret.data, other[ret.row].ravel())
        # ...

That is to say, coo_matrix.multiply() should be overwritten to avoid these two time-consuming and meaningless converting.

Scipy/Numpy/Python version information:

scipy:1.2.1

numpy:1.16.2

python:3.6.7

@gan-lin
Copy link
Author

gan-lin commented Apr 12, 2019

Sorry, there is one thing I ignored previously. When there are duplicate elements, the behavior will be different. Converting to csr will first sum them, but directly multiplying will keep them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants