-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Request: Allow arbitrary array type for nzval of sparse matrix #30173
Conversation
I think it would be better to have this capability as a separate package, and making changes to |
Maybe this could be part of the |
How about introducing
Thanks for considering this. I appreciate if |
It seems that for the purposes of what you are trying here, what we want to do is widen the nzval to be an AbstractVector as much as possible throughout the sparse codebase. |
Hmm... yes, but that's exactly what my PR does. I thought you preferred to do this in a separate package? Just to be clear, I wasn't suggesting to add the capabilities of |
If we want to add a new type, like |
We still need some change in SparseArrays.jl for downstream packages to extend sparse matrix, right? Let me know what is the best approach:
(I know you dismissed 1, but I'm listing it for completeness) Approach 1 and 2 are almost equivalent (1 can be made almost non-breaking). Approach 3 is slightly more challenging but it's (probably) completely non-breaking and more powerful. For example, maybe you can implement a different behavior in an external package for the case Approach 2 is the only one that does not introduce a new type. However, it breaks the following code:
|
What about a case where the |
That's approach 2 I suggested, right? As I mentioned, it breaks the code using
The aim of this PR is to allow arbitrary vector type, not the element type (which can already be anything; see below [1]). So, I believe [1] julia/stdlib/SparseArrays/src/sparsematrix.jl Lines 16 to 31 in 8fa0645
julia> SparseMatrixCSC(3, 3, [1, 2, 2, 2], [1], [:hello])
3×3 SparseMatrixCSC{Symbol,Int64} with 1 stored entry:
[1, 1] = :hello |
Codecov Report
@@ Coverage Diff @@
## master #30173 +/- ##
==========================================
+ Coverage 39.12% 94.83% +55.7%
==========================================
Files 344 327 -17
Lines 61276 54302 -6974
==========================================
+ Hits 23977 51496 +27519
+ Misses 37299 2806 -34493
Continue to review full report at Codecov.
|
We definitely do not want to introduce new types in the sparse matrix code in stdlib. If anything we need to work towards making such things possible outside of stdlib, and refactor stuff in stdlib that might be getting in the way. |
Could you please re-read my previous comments and reconsider. I've been trying to convince you that the whole purpose of this PR and other variants I proposed are exactly for "making such things possible outside of stdlib" and not implementing any extra features for end-users. I appreciate if you point out why my proposals are not aligned to your point or any other unclear points. It also would be great if you assign other reviewers as well. |
Unfortunately I can't assign reviewers - we can ping people and see who is interested. I will re-review the whole thread myself. |
Is it possible to rebase this PR and retrigger the tests with the new CI? |
Thank you for taking the time to reconsider this. |
Can we not change SparseMatrixCSC fields to be AbstractVectors? |
I think that would mean to add |
I think using interface functions to access the internals of the data structure is actually a good thing. |
Thank you. So, I guess creating a patch with |
Yes, that sounds like the right direction to me. The challenge of course is that we can only know if it feels right after a couple of attempts. Please open a new PR for that, so we have this one to refer to as well. Let's use the full name |
Thanks. I understand that there is always a possibility that PR is rejected. I'll do it once I have a long enough chunk of time. I'll use |
I really do want to try get it in. Looking forward. |
I will close this one since we want to take a different approach. Let's do the new approach as a new PR - and we will still have this one to refer to for comparison. |
I request a generalization of
SparseMatrixCSC
such thatnzval
can be an arbitrary vector type, not just aVector
. This let us compose the sparse matrix type with custom array types. For example, the following code works with this PR:Achieving similar effect with current
SparseMatrixCSC
is hard. For example, here is my attempt to implement very small subset of mapped sparse array based onMappedArrays
. It took > 30 lines of code and yet have fewer features than the matrixM1
above (e.g., efficientmul!
). Doing something like constantnzval
as inM3
with currentSparseMatrixCSC
would be hard. I can imagine doing so with singleton number-likestruct
but that would be very awkward and inefficient.Of course, new feature shouldn't break old ones. I tired to make
SparseMatrixCSC
is backward compatible as much as possible. In fact, this PR so far only changedstdlib/SparseArrays/src
except for two lines of introspection-based code instdlib/SparseArrays/test
. (Of course, once the direction of this PR is approved I will add the tests for the generalized interface.) Since the tests forSuiteSparse
pass and it usesSparseMatrixCSC
heavily, I think the approach in this PR seems to be working. I also assumed thatshow
is part of API (otherwise doctest would break) and defined backward compatibleshow(::IO, ::Type{<:SparseMatrixCSC})
.The main change in this PR is the new
struct
which I tentatively callGenericMatrixCSC
. This is just a simple generalization ofSparseMatrixCSC
:SparseMatrixCSC
is redefined asOther than that, the PR is mostly just simple renames although the diff look large.
An alternative approach would be to define all CSC matrix code based on an abstract type and interface (e.g.,
nonzeros
) add addGenericMatrixCSC
while keepingSparseMatrixCSC
untouched. The disadvantage is that we need to maintain two very similar structs. I thinkSparseMatrixCSC
would have coded likeGenericMatrixCSC
if the ability to use arbitrary array type fornzval
was foreseen. If that's the case using concretestruct
as in this PR is more natural. Furthermore, this approach let us succinctly construct sparse matrices usingSetfield.@set
(as in the code above).Needless to say, the implementation detail is not important. I can switch to abstract type approach if that's required.
Relevant PRs: #25118, #25151