Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hcat/vcat/hvcat bug #885

Closed
andreasvarga opened this issue Nov 7, 2021 · 6 comments
Closed

hcat/vcat/hvcat bug #885

andreasvarga opened this issue Nov 7, 2021 · 6 comments

Comments

@andreasvarga
Copy link
Contributor

The following simple matrix construction fails (joint finding with John Verzani #364):

julia> using LinearAlgebra

julia> [1 2; I]
ERROR: ArgumentError: number of columns of each array must match (got (2, 1))
Stacktrace:
 [1] _typed_vcat(#unused#::Type{Any}, A::Tuple{Matrix{Any}, Matrix{Any}})
   @ Base .\abstractarray.jl:1553
 [2] typed_vcat(::Type{Any}, ::Matrix{Any}, ::Matrix{Any})
   @ Base .\abstractarray.jl:1567
 [3] typed_hvcat(::Type{Any}, ::Tuple{Int64, Int64}, ::Int64, ::Vararg{Any, N} where N)
   @ Base .\abstractarray.jl:1957
 [4] hvcat(::Tuple{Int64, Int64}, ::Int64, ::Vararg{Any, N} where N)
   @ Base .\abstractarray.jl:1932
 [5] top-level scope
   @ REPL[9]:1

but [[1] [2]; I] and [[1 2]; I] work correctly:

julia> [[1] [2]; I]
3×2 Matrix{Int64}:
 1  2
 1  0
 0  1

julia>  [[1 2]; I]
3×2 Matrix{Int64}:
 1  2
 1  0
 0  1

I think, a simple fix would be to replace line 393 of LinearAlgebra.UniformScaling
promote_to_arrays_(n::Int, ::Type, a::Number) = a
with
promote_to_arrays_(n::Int, ::Type, a::Number) = [a]

@ViralBShah
Copy link
Member

Can you submit a PR? Seems like it would be good to fix.

@dkarrasch
Copy link
Member

I think, a simple fix would be to replace line 393 of LinearAlgebra.UniformScaling
promote_to_arrays_(n::Int, ::Type, a::Number) = a
with
promote_to_arrays_(n::Int, ::Type, a::Number) = [a]

That was my very first attempt in the extension of *cat to numbers, but was considered unfortunate because it creates an (unnecessary) array for each number... which gets eventually included in the concatenated matrix. The issue seems to be that the size of the uniform scaling is deduced incorrectly?

@martinholters
Copy link
Member

Fixed on master?

julia> [1 2; I]
3×2 Matrix{Int64}:
 1  2
 1  0
 0  1

@dkarrasch
Copy link
Member

Fixed on master?

julia> [1 2; I]
3×2 Matrix{Int64}:
 1  2
 1  0
 0  1

True. I thought JuliaLang/julia@18f2f9f might have been included earlier, but it's going to be released with v1.8.

@andreasvarga
Copy link
Contributor Author

The following wrong outputs are hopefully also fixed:

julia> [1 I]
1×2 Matrix{Any}:
 1  UniformScaling{Bool}(true)

julia> [1; I]
2-element Vector{Any}:
 1
  UniformScaling{Bool}(true)

julia> [1 1; 1 I]
2×2 Matrix{Any}:
 1  1
 1   UniformScaling{Bool}(true)

@martinholters
Copy link
Member

Yes:

julia> [1 I]
1×2 Matrix{Int64}:
 1  1

julia> [1; I]
2×1 Matrix{Int64}:
 1
 1

julia> [1 1; 1 I]
2×2 Matrix{Int64}:
 1  1
 1  1

@KristofferC KristofferC transferred this issue from JuliaLang/julia Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants