From 0b374fbde031bd1195b0128257bdecf3f2a56ea0 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Thu, 28 Jul 2016 16:03:54 -0700 Subject: [PATCH] Make `hvcat`s of heterogeneous combinations of dense matrices and vectors yield dense arrays (fixes #17686). --- base/array.jl | 8 +++++--- test/arrayops.jl | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/base/array.jl b/base/array.jl index 4fc67b12ba356..89bec1acb5ee9 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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...) @@ -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 ## diff --git a/test/arrayops.jl b/test/arrayops.jl index 60cd54e437f50..92549881f665b 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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