Skip to content

Commit

Permalink
Make hvcats of heterogeneous combinations of dense matrices and vec…
Browse files Browse the repository at this point in the history
…tors yield dense arrays (fixes JuliaLang#17686).
  • Loading branch information
Sacha0 authored and mfasi committed Sep 5, 2016
1 parent 53fca77 commit 0b374fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ vcat{T}(A::Matrix{T}...) = typed_vcat(T, A...)
hcat(A::Union{Matrix, Vector}...) = typed_hcat(promote_eltype(A...), A...)
hcat{T}(A::Union{Matrix{T}, Vector{T}}...) = typed_hcat(T, A...)


vcat(A::Union{Matrix, Vector}...) = typed_vcat(promote_eltype(A...), A...)
vcat{T}(A::Union{Matrix{T}, Vector{T}}...) = typed_vcat(T, A...)

Expand All @@ -699,8 +698,11 @@ hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Vector{T}...) = typed_hvcat(T, rows, xs..
hvcat(rows::Tuple{Vararg{Int}}, xs::Matrix...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Matrix{T}...) = typed_hvcat(T, rows, xs...)

cat(catdims, xs::Union{Matrix,Vector}...) = Base.cat_t(catdims, promote_eltype(xs...), xs...)
cat{T}(catdims, xs::Union{Matrix{T},Vector{T}}...) = Base.cat_t(catdims, T, xs...)
hvcat(rows::Tuple{Vararg{Int}}, xs::Union{Vector,Matrix}...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Union{Vector{T},Matrix{T}}...) = typed_hvcat(T, rows, xs...)

cat(catdims, xs::Union{Vector,Matrix}...) = Base.cat_t(catdims, promote_eltype(xs...), xs...)
cat{T}(catdims, xs::Union{Vector{T},Matrix{T}}...) = Base.cat_t(catdims, T, xs...)

## find ##

Expand Down
22 changes: 22 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1703,3 +1703,25 @@ for op in (:.+, :.*, :.÷, :.%, :.<<, :.>>, :.-, :./, :.\, :.//, :.^)
end

end

# Test that concatenations of dense matrices/vectors yield dense matrices/vectors
let
N = 4
densevec = ones(N)
densemat = diagm(ones(N))
# Test that concatenations of homogeneous pairs of either dense matrices or dense vectors
# (i.e., Matrix-Matrix concatenations, and Vector-Vector concatenations) yield dense arrays
for densearray in (densevec, densemat)
@test isa(vcat(densearray, densearray), Array)
@test isa(hcat(densearray, densearray), Array)
@test isa(hvcat((2,), densearray, densearray), Array)
@test isa(cat((1,2), densearray, densearray), Array)
end
# Test that concatenations of heterogeneous Matrix-Vector pairs yield dense matrices
@test isa(hcat(densemat, densevec), Array)
@test isa(hcat(densevec, densemat), Array)
@test isa(hvcat((2,), densemat, densevec), Array)
@test isa(hvcat((2,), densevec, densemat), Array)
@test isa(cat((1,2), densemat, densevec), Array)
@test isa(cat((1,2), densevec, densemat), Array)
end

0 comments on commit 0b374fb

Please sign in to comment.