Skip to content

Commit 938f368

Browse files
authored
Merge pull request #496 from rfourquet/reduce-vcat
optimize reduce(vcat, vector_of_matrices)
2 parents 338c99f + ff41541 commit 938f368

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/generic/Matrix.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,6 +4797,8 @@ function Base.vcat(A::MatrixElem...)
47974797
return _vcat(A)
47984798
end
47994799

4800+
Base.reduce(::typeof(vcat), A::AbstractVector{<:MatrixElem}) = _vcat(A)
4801+
48004802
function _vcat(A)
48014803
if length(A) == 0
48024804
error("Number of matrices to concatenate must be positive")
@@ -4832,6 +4834,8 @@ function Base.hcat(A::MatrixElem...)
48324834
return _hcat(A)
48334835
end
48344836

4837+
Base.reduce(::typeof(hcat), A::AbstractVector{<:MatrixElem}) = _hcat(A)
4838+
48354839
function _hcat(A)
48364840
if length(A) == 0
48374841
error("Number of matrices to concatenate must be positive")

test/generic/Matrix-test.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,7 @@ end
20062006
0 1 0;
20072007
0 1 2;])
20082008
@test hcat(B, C) == [B C]
2009+
@test hcat(B, C, C, B) == reduce(hcat, [B, C, C, B])
20092010
let BC = hcat([B, C])
20102011
@test size(BC) == (2, 1)
20112012
@test BC[1] == B
@@ -2020,13 +2021,13 @@ end
20202021
0 1;])
20212022

20222023
@test vcat(A, B) == [A; B]
2024+
@test vcat(A, B, B, A) == reduce(vcat, [A, B, B, A])
20232025
let AB = vcat([A, B])
20242026
@test size(AB) == (2,)
20252027
@test AB[1] == A
20262028
@test AB[2] == B
20272029
end
20282030

2029-
20302031
@test [A D; B B C] == matrix(R, [1 2 1 2 3;
20312032
3 4 4 5 6;
20322033
1 2 1 2 0;

0 commit comments

Comments
 (0)