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

BUG: Empty CSR Matrix has multiple indptr instead of 1 #20521

Closed
Ant1ng2 opened this issue Apr 18, 2024 · 4 comments
Closed

BUG: Empty CSR Matrix has multiple indptr instead of 1 #20521

Ant1ng2 opened this issue Apr 18, 2024 · 4 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse

Comments

@Ant1ng2
Copy link

Ant1ng2 commented Apr 18, 2024

Describe your issue.

When creating an empty csr_matrix, instead of indptr returning [0], it returns [0, 0]. This is inconsistent with other formats (i.e. csc_matrix returns [0]).

Reproducing Code Example

from scipy.sparse import csr_matrix, csc_matrix

print(csr_matrix([]).indptr)
# [0, 0]

print(csc_matrix([]).indptr)
# [0]

Error message

N/A

SciPy/NumPy/Python version and system information

1.13.0 1.24.4 sys.version_info(major=3, minor=10, micro=13, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= SANDYBRIDGE MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= SANDYBRIDGE MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld64
    name: clang
    version: 13.0.0
  c++:
    commands: c++
    linker: ld64
    name: clang
    version: 13.0.0
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld64
    name: gcc
    version: 11.3.0
  pythran:
    include directory: <omitted>
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: darwin
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: darwin
Python Information:
  path: <omitted>
  version: '3.10'
@Ant1ng2 Ant1ng2 added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Apr 18, 2024
@perimosocordiae
Copy link
Member

Confirmed that this happens at head. Another reproducer: csr_matrix((1, 0)).indptr

When creating an empty CSR or CSC sparse matrix with a given shape, we initialize indptr to be an all-zero array with (size of major axis + 1) entries. So when you pass an empty list, this results in shape (1, 0), which means our indptr will have size 2. Note that for CSC, the major axis has size 0, and thus its indptr will have size 1.

This behavior is internally consistent, even if it's not intuitive, so I'm inclined to say that this isn't a bug.

@Ant1ng2
Copy link
Author

Ant1ng2 commented Apr 22, 2024

So when you pass an empty list, this results in shape (1, 0), which means our indptr will have size 2. Note that for CSC, the major axis has size 0, and thus its indptr will have size 1.

Is there a reason why an empty matrix would default to (1, 0) instead of (0, 0)? If there is a legitimate reason then I would agree that this wouldn't be a bug.

@perimosocordiae
Copy link
Member

We're following the numpy.matrix convention, which in turn derives from the behavior of atleast_2d:

In [1]: import numpy as np

In [2]: np.matrix([])
Out[2]: matrix([], shape=(1, 0), dtype=float64)

In [3]: np.atleast_2d([])
Out[3]: array([], shape=(1, 0), dtype=float64)

@Ant1ng2
Copy link
Author

Ant1ng2 commented Apr 29, 2024

I see, that makes sense. I'll close this then as not a bug

@Ant1ng2 Ant1ng2 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.sparse
Projects
None yet
Development

No branches or pull requests

4 participants
@perimosocordiae @Ant1ng2 @j-bowhay and others