`SparseArray` types

SparseMatrixCSC currently has a documented type definition of:

SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}

but, if you try to actually make one, you will find in practice that it only has exactly one of the types:

SparseMatrixCSC{Tv<:Union{Number, Missing},Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}
SparseMatrixCSC{Tv<:Union{UniformScaling{N} where N, Missing},Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}
SparseMatrixCSC{Tv<:Union{CartesianIndex{N} where N, Missing},Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}

that is, unless you don’t have any empty indices.

now, why is this? it’s because when we’re printing it out, we first find the textual width of zero(::Type{Tv}), and then put a which is the same length as that in the array.
look at how w i d e :

julia> A = sparse([3], [4], [CartesianIndex((1,2,3))])
3×4 SparseMatrixCSC{CartesianIndex{3}, Int64} with 1 stored entry:
            ⋅                        ⋅                        ⋅                        ⋅
            ⋅                        ⋅                        ⋅                        ⋅
            ⋅                        ⋅                        ⋅             CartesianIndex(1, 2, 3)

the reason I found this was because I was trying to dissect the awful way it interacts with Base printing functions for #62362. I even had a commit message ready for them: stop cannibalizing unexported base printing functions \n\n stahp it. doe normaal.. (I’m warning you, LinearAlgebra, you’re next ^^)




now, there are two paths forward, which is why I am asking over here.

  1. keep the type of SparseMatrixCSC the same, and fix the printing
  2. restrict the type of SparseMatrixCSC to SparseMatrixCSC{Tv<:Union{Number, Missing},Ti<:Integer} <: AbstractSparseMatrixCSC{Tv,Ti}

the second option here probably has compilation benefits, and right now you can make that change without it being breaking. idk much about all that, so I’m throwing this question out to the community. I’d say it’s extremely unlikely that someone is actually using UniformScaling or CartesianIndex SparseMatrixCSC’s because you have to be extra careful that the types actually make it all the way through:

sparse([2, 4], [2, 1], [6I, I]) # fails, has to be: sparse([2, 4], [2, 1], UniformScaling{Int64}[6I, I])
sparse([3,4], [4,3], [CartesianIndex((1,2,3)), CartesianIndex((1,2,3,5))]) # fails: they have to be the same dimensionality

ok, after looking at some discussions / issues (here, #512), I like option #2. it sounds like a bunch of things assume that it’s using a numeric type; since I’m fixing the output funkyness, it would be better to error on instantiation than randomly at some point while you’re using them.

a quick aside; someone who cares about static arrays should give that repo a little love. the sparsematrix.jl file is almost 5k lines…

For information, sparse matrices and the sparsematrix.jl source file are part of the Sparse Arrays julia standard library, nothing related to static arrays, which usually refers to the StaticArrays.jl package.

Also, I believe that the OP is a barely edited (and not synthetised) copy paste of a coding agent summary, with not even a question asked/issue stated. This is not compliant with the forum’s guidelines, I doubt you would get any help from the community if you don’t make the effort of following the guidelines and minimizing OP length…

as someone who can’t stand working with ai, I really dislike the idea that my writing apparently sounds like ai now? I’m pretty particular with my word choice, and have yet to find an ai that gives output I’d be willing to express myself with.

I was specifically trying for a lighter tone since this is a discussion board; maybe that’s what went wrong. I can see how the asides can be taken as ai-y; I don’t really use SparseMatrix and kept seeing more and more annoying things it’s doing so I was getting pretty frustrated. the whole /the reason I found this/ paragraph was prob unneeded.

I’m pretty surprised I didn’t notice the typo in the title, that’s pretty bad. I wouldn’t want to read a post that gets the entire title wrong either.

as for the question actually asked, it was if people care about being able to put non numeric types into a SparseArray. you can see I got the answer I was looking for in #735: “it’s more effort than it’s worth”. I was just trying to ask the question in a discussion board first but apparently failed at that.

at the point I replied to myself I was basically done with this post, and then I realized interacting with it actually brought it back up to the top of latest. prob should have just deleted instead

Apologies, I got this impression because the reason for the post appears late (when you mention #62362), and quite after the “now, why is this?” question which I assume refers to the printing issue, but that cannot be understood when reading the question, so I found the post confusing.

no, you’re good. I was prob being lazy, I mean how on earth did I miss the title…

I wasn’t trying to accuse you for calling me an ai, I was just disappointed in my own writing in that it could get taken as ai. usually I feel I put more care into what I’m saying