JuliaArrays / JuliaArrays/StructArrays.jl

`eltype` not inferred in broadcasting involving sparse arrays

Open
#273 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
402
Forks
49
PR merge metrics
No merged PRs in 30d

Description

julia> using LinearAlgebra, SparseArrays, StructArrays

julia> D = sparse(Diagonal(Float64[1:4;]));

julia> S = StructArray{ComplexF64}((D, D));

julia> ComplexF64.(S)
4×4 SparseMatrixCSC{Any, Int64} with 4 stored entries:
Error showing value of type SparseMatrixCSC{Any, Int64}:
ERROR: MethodError: no method matching zero(::Type{Any})

Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T
   @ Base missing.jl:105
  zero(::Union{Type{P}, P}) where P<:Dates.Period
   @ Dates ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/Dates/src/periods.jl:51
  zero(::AbstractIrrational)
   @ Base irrationals.jl:151
  ...

Stacktrace:
  [1] zero(#unused#::Type{Any})
    @ Base ./missing.jl:106
  [2] getindex
    @ ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/SparseArrays/src/sparsematrix.jl:2504 [inlined]
  [3] isassigned(::SparseMatrixCSC{Any, Int64}, ::Int64, ::Int64)
    @ Base ./abstractarray.jl:606
  [4] alignment(io::IOContext{Base.TTY}, X::AbstractVecOrMat, rows::Vector{Int64}, cols::Vector{Int64}, cols_if_complete::Int64, cols_otherwise::Int64, sep::Int64, ncols::Int64)
    @ Base ./arrayshow.jl:68
  [5] _print_matrix(io::IOContext{Base.TTY}, X::AbstractVecOrMat, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64, rowsA::UnitRange{Int64}, colsA::UnitRange{Int64})
    @ Base ./arrayshow.jl:207
  [6] print_matrix(io::IOContext{Base.TTY}, X::SparseMatrixCSC{Any, Int64}, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64)
    @ Base ./arrayshow.jl:171
  [7] print_matrix
    @ ./arrayshow.jl:171 [inlined]
  [8] print_array(io::IOContext{Base.TTY}, S::SparseMatrixCSC{Any, Int64})
    @ SparseArrays ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/SparseArrays/src/sparsematrix.jl:339
  [9] show(io::IOContext{Base.TTY}, #unused#::MIME{Symbol("text/plain")}, X::SparseMatrixCSC{Any, Int64})
    @ Base ./arrayshow.jl:399
 [10] (::REPL.var"#55#56"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:276
 [11] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:557
 [12] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:262
 [13] display
    @ ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:281 [inlined]
 [14] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [15] #invokelatest#2
    @ ./essentials.jl:816 [inlined]
 [16] invokelatest
    @ ./essentials.jl:813 [inlined]
 [17] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:305
 [18] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:287
 [19] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:557
 [20] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:285
 [21] (::REPL.var"#do_respond#80"{Bool, Bool, REPL.var"#93#103"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:899
 [22] #invokelatest#2
    @ ./essentials.jl:816 [inlined]
 [23] invokelatest
    @ ./essentials.jl:813 [inlined]
 [24] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/LineEdit.jl:2647
 [25] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL ~/packages/julias/julia-1.9/share/julia/stdlib/v1.9/REPL/src/REPL.jl:1300
 [26] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ./task.jl:514

However, this works:

julia> D = Diagonal(Float64[1:4;]);

julia> S = StructArray{ComplexF64}((D, D));

julia> ComplexF64.(S)
4×4 Diagonal{ComplexF64, Vector{ComplexF64}}:
 1.0+1.0im      ⋅          ⋅          ⋅    
     ⋅      2.0+2.0im      ⋅          ⋅    
     ⋅          ⋅      3.0+3.0im      ⋅    
     ⋅          ⋅          ⋅      4.0+4.0im

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with StructArrays, SparseArrays, and the ComplexF64.(S) broadcast shown in the report, then compare it with the working Diagonal case. Trace the broadcast and eltype handling that produces SparseMatrixCSC{Any}; done means the sparse case infers the expected element type and no longer fails when displayed.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.