SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

vcat does not handle numbers correctly

Open vtjnash opened this issue 2 years ago • 3 comments

julia> vcat(1, sparse([1 2]))
ERROR: DimensionMismatch: tried to assign 1-element array to 1×2 destination
Stacktrace:
  [1] throw_setindex_mismatch(X::SparseVector{Int64, Int64}, I::Tuple{Int64, Int64})
    @ Base ./indices.jl:193
  [2] setindex_shape_check
    @ ./indices.jl:248 [inlined]
  [3] setindex!(A::SparseMatrixCSC{Int64, Int64}, V::SparseVector{Int64, Int64}, Ix::UnitRange{Int64}, Jx::UnitRange{Int64})
    @ SparseArrays ~/julia/usr/share/julia/stdlib/v1.10/SparseArrays/src/sparsematrix.jl:3287
  [4] __cat_offset1!
    @ ./abstractarray.jl:1814 [inlined]
  [5] __cat_offset!
    @ ./abstractarray.jl:1804 [inlined]
  [6] __cat
    @ ./abstractarray.jl:1800 [inlined]
  [7] _cat_t
    @ ./abstractarray.jl:1793 [inlined]
  [8] _cat
    @ ~/julia/usr/share/julia/stdlib/v1.10/SparseArrays/src/sparsevector.jl:1205 [inlined]
  [9] cat
    @ ./abstractarray.jl:1982 [inlined]
 [10] vcat(::Int64, ::SparseMatrixCSC{Int64, Int64})
    @ SparseArrays ~/julia/usr/share/julia/stdlib/v1.10/SparseArrays/src/sparsevector.jl:1217
 [11] top-level scope
    @ REPL[5]:1

julia> vcat(1, [1 2])
2×2 Matrix{Int64}:
 1  1
 1  2

vtjnash avatar Apr 11 '23 14:04 vtjnash

Seems like the other way around. Base isn't handling it correctly, is it? Why should a number be repeated implicitly? I'd expect to see the same as

julia> vcat([1;;], [1 2])
ERROR: ArgumentError: number of columns of each array must match (got (1, 2))

EDIT: I'm surprised to see that this implicit repetition mechanism is present since at least v1.0. I was not aware of this behavior, at all!

dkarrasch avatar Apr 11 '23 16:04 dkarrasch

For the record: It started to change behavior in v1.8.

dkarrasch avatar Apr 11 '23 16:04 dkarrasch

Honestly, I believe this is a very old "bug" in Julia Base. Guess the respective results!

julia> vcat(1, [1 2]) # works

julia> vcat([1 2], 3) # throws!

julia> vcat(1, [1 2], 3) # throws!

So, the "expansion" of numbers works only when the number is in the first place, and in no other!

dkarrasch avatar Apr 11 '23 16:04 dkarrasch