Error showing value of type SparseMatrixCSC

It appears that there is a valid SparseMatrixCSC, but cannot be shown.

julia> using SparseArrays

julia> A = [rand(-9:9, 2) for i in 1:2, j in 1:2]
2×2 Matrix{Vector{Int64}}:
 [-6, -1]  [-7, -6]
 [5, 5]    [0, -1]

julia> S = sparse(A)
2×2 SparseMatrixCSC{Vector{Int64}, Int64} with 4 stored entries:
 [-6, -1]  [-7, -6]
 [5, 5]    [0, -1]

julia> I, J, V = findnz(S); pop!.([I, J, V]);

julia> @info "see" I J V
┌ Info: see
│   I =
│    3-element Vector{Int64}:
│     1
│     2
│     1
│   J =
│    3-element Vector{Int64}:
│     1
│     1
│     2
│   V =
│    3-element Vector{Vector{Int64}}:
│     [-6, -1]
│     [5, 5]
└     [-7, -6]

julia> S_revised = sparse(I, J, V)
2×2 SparseMatrixCSC{Vector{Int64}, Int64} with 3 stored entries:
Error showing value of type SparseMatrixCSC{Vector{Int64}, Int64}:

SYSTEM (REPL): showing an error caused an error
ERROR: MethodError: no method matching zero(::Type{Vector{Int64}})
The function `zero` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Missing})
   @ Base missing.jl:104
  zero(::Missing)
   @ Base missing.jl:101
  ...

Stacktrace:
  [1] print_response(errio::IO, response::Any, backend::Union{…}, show_value::Bool, have_color::Bool, specialdisplay::Union{…})
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:446
  [2] (::REPL.var"#70#71"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:405
  [3] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:678
  [4] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:403
  [5] (::REPL.var"#do_respond#100"{Bool, Bool, REPL.var"#116#134"{…}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:1035
  [6] #invokelatest#2
    @ .\essentials.jl:1055 [inlined]
  [7] invokelatest
    @ .\essentials.jl:1052 [inlined]
  [8] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\LineEdit.jl:2755
  [9] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:1506
 [10] (::REPL.var"#79#85"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL C:\Users\34682\.julia\juliaup\julia-1.11.5+0.x64.w64.mingw32\share\julia\stdlib\v1.11\REPL\src\REPL.jl:497
Some type information was truncated. Use `show(err)` to see complete types.
1 Like

Long story short: the concept of a sparse matrix only makes sense when the element type has a uniquely defined zero. Vector{Int} doesn’t have this because it doesn’t encode the length of the vector in the type. If you want a sparse array where the elements are vectors of a particular length, you should use StaticArrays.jl, which does encode the length in the type. Here’s what your example would look like:

julia> using SparseArrays, StaticArrays

julia> A = [@SVector rand(-9:9, 2) for i in 1:2, j in 1:2]
2×2 Matrix{SVector{2, Int64}}:
 [-1, -1]  [-7, -2]
 [-4, 8]   [-8, 3]

julia> S = sparse(A)
2×2 SparseMatrixCSC{SVector{2, Int64}, Int64} with 4 stored entries:
 [-1, -1]  [-7, -2]
 [-4, 8]   [-8, 3]

julia> I, J, V = findnz(S); pop!.([I, J, V]);

julia> sparse(I, J, V)
2×2 SparseMatrixCSC{SVector{2, Int64}, Int64} with 3 stored entries:
 [-1, -1]  [-7, -2]
 [-4, 8]     ⋅

Longer explanation: This is more fundamental than a display problem. It’s a problem determining what the value of a structural zero should be in this matrix.

julia> using SparseArrays

julia> A = [rand(-9:9, 2) for i in 1:2, j in 1:2]
2×2 Matrix{Vector{Int64}}:
 [1, -9]  [6, -8]
 [5, 6]   [-3, 4]

julia> S = sparse(A)
2×2 SparseMatrixCSC{Vector{Int64}, Int64} with 4 stored entries:
 [1, -9]  [6, -8]
 [5, 6]   [-3, 4]

julia> I, J, V = findnz(S); pop!.([I, J, V]);

julia> S2 = sparse(I, J, V);

julia> S2[2, 2]
ERROR: MethodError: no method matching zero(::Type{Vector{Int64}})
[...]

I assume you want S2[2, 2] == [0, 0], but the problem is that the lengths of vectors aren’t encoded in their type, so there’s no way to know that you want [0, 0] and not [0] or [0, 0, 0, 0, 0, 0].

For a starker illustration of the problem, consider the following: what should S2[2, 2] return here?

julia> M = Matrix{Vector{Int64}}(undef, 2, 2);

julia> for i in 1:4
           M[i] = [i for _ in 1:i]
       end

julia> S = sparse(M)
2×2 SparseMatrixCSC{Vector{Int64}, Int64} with 4 stored entries:
 [1]     [3, 3, 3]
 [2, 2]  [4, 4, 4, 4]

julia> I, J, V = findnz(S); pop!.([I, J, V]);

julia> S2 = sparse(I, J, V);

julia> S2[2, 2]
ERROR: MethodError: no method matching zero(::Type{Vector{Int64}})
[...]
3 Likes

Found a relevant issue and offered my perspective (which is that this should error on construction since the arrays are unusable anyway): Sparse array of string types · Issue #512 · JuliaSparse/SparseArrays.jl · GitHub