pratyushadk opened a new pull request, #50907:
URL: https://github.com/apache/arrow/pull/50907
### Rationale for this change
Calling `SparseCSRMatrix::Make` or `SparseCSCMatrix::Make` on a 1D dense
tensor currently returns a `NotImplemented` error ("TODO for ndim <= 1"). The
CSR/CSC representation for a 1D vector is well-defined though: it is equivalent
to a single-row sparse matrix where `indptr = [0, nnz]`, `indices` holds the
positions of non-zero values, and `values` holds the corresponding values. The
COO format already handles 1D tensors, so this fills a straightforward gap.
### What changes are included in this PR?
`cpp/src/arrow/tensor/csx_converter.cc`
The `Convert()` method in `SparseCSXMatrixConverter` now handles three cases
cleanly. `ndim == 0` returns `Status::Invalid` since scalars have no meaningful
sparse representation. `ndim == 1` takes a new path that does a single linear
scan of the vector, writes `indptr = [0, nnz]`, and records the positions and
values of non-zeros. `ndim == 2` is the existing path and is completely
unchanged.
The reverse path (`MakeTensorFromSparseCSXMatrix`) had a hardcoded
`shape[1]` access which would be out of bounds for a 1D tensor. That is fixed
to use `shape.size() > 1 ? shape[1] : shape[0]`.
`cpp/src/arrow/sparse_tensor_test.cc`
Three new tests added to the existing `TestSparseCSRMatrix` fixture covering
the 1D case.
### Are these changes tested?
Yes. Three new tests cover the main conversion path, the all-zero edge case,
and a full round-trip from dense to sparse and back. All 153 tests in the
sparse tensor test suite passed locally with no regressions.
### Are there any user-facing changes?
`SparseCSRMatrix::Make` and `SparseCSCMatrix::Make` now accept 1D tensors
instead of returning `NotImplemented`. There are no breaking changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]