matrixconcatenationjuliatoeplitz

Cannot concatenate Toeplitz matrices in Julia 1.8.5


I can do the following fine before my last update to Julia 1.8.5. Now I get an error message. I can concatenate normal (randomly generated) matrices OK.

julia> using ToeplitzMatrices

julia> B1 = Toeplitz([1, 2, 4], [1, 3, 5])
3×3 Toeplitz{Int64, Vector{Int64}, Vector{Int64}}:
 1  3  5
 2  1  3
 4  2  1

julia> B2 = B1 .+ 3
3×3 Matrix{Int64}:
 4  6  8
 5  4  6
 7  5  4

julia> hcat(B1, B2)
ERROR: CanonicalIndexError: setindex! not defined for Toeplitz{Int64, Vector{Int64}, Vector{Int64}}
Stacktrace:
  [1] error_if_canonical_setindex(::IndexCartesian, ::Toeplitz{Int64, Vector{Int64}, Vector{Int64}}, ::Int64, ::Int64)
    @ Base ./abstractarray.jl:1354
  [2] setindex!
    @ ./abstractarray.jl:1343 [inlined]
  [3] macro expansion
    @ ./multidimensional.jl:946 [inlined]
  [4] macro expansion
    @ ./cartesian.jl:64 [inlined]
  [5] _unsafe_setindex!(::IndexCartesian, ::Toeplitz{Int64, Vector{Int64}, Vector{Int64}}, ::Toeplitz{Int64, Vector{Int64}, Vector{Int64}}, ::Base.Slice{Base.OneTo{Int64}}, ::UnitRange{Int64})
    @ Base ./multidimensional.jl:941
  [6] _setindex!
    @ ./multidimensional.jl:930 [inlined]
  [7] setindex!
    @ ./abstractarray.jl:1344 [inlined]
  [8] _typed_hcat(#unused#::Type{Int64}, A::Tuple{Toeplitz{Int64, Vector{Int64}, Vector{Int64}}, Matrix{Int64}})
    @ Base ./abstractarray.jl:1618
  [9] typed_hcat
    @ ./abstractarray.jl:1586 [inlined]
 [10] hcat(::Toeplitz{Int64, Vector{Int64}, Vector{Int64}}, ::Matrix{Int64})
    @ Base ./abstractarray.jl:1589
 [11] top-level scope
    @ REPL[4]:1

I expect the output to be

1 3 5 11 13 15 2 1 3 12 11 13 4 2 1 14 12 11


Solution

  • Okay sorry @PaSTE and @TDTL for doubting the existence of the error. The reason for it is in ToeplitzMatrices version 0.8.0 (I had 0.7.1 installed).

    Specifically, the addition of a specialized definition for similar(...) for Toeplitz matrices is breaking this, as the resulting matrix isn't actually a Toeplitz matrix (while the new similar definition returns a Toeplitz matrix).

    Solutions:

    1. revert to 0.7.1 (by add ToeplitzMatrices@0.7.1 in pkg prompt and restart), or
    2. use hcat(collect(B1), B2)

    An issue about this glitch should be noted for the developer of the package. Not sure what the correct fix is, as it might be nice to have compatible Toeplits matrices hcated well, or to use similar to create a new Toeplitz matrix.