Skip to content

Commit ddb518a

Browse files
committed
improve and simplify [h/v]cat
1 parent 559dd24 commit ddb518a

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

src/blockmap.jl

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,13 @@ function Base.hcat(As::Union{LinearMap, UniformScaling, AbstractVecOrMat}...)
8484
T = promote_type(map(eltype, As)...)
8585
nbc = length(As)
8686

87-
nrows = -1
8887
# find first non-UniformScaling to detect number of rows
89-
for A in As
90-
if !(A isa UniformScaling)
91-
nrows = size(A, 1)
92-
break
93-
end
94-
end
95-
@assert nrows != -1
88+
j = findfirst(x -> !isa(x, UniformScaling), As)
9689
# this should not happen, function should only be called with at least one LinearMap
97-
return BlockMap{T}(promote_to_lmaps(ntuple(i->nrows, nbc), 1, 1, As...), (nbc,))
90+
@assert !isnothing(j)
91+
nrows = size(As[j], 1)
92+
93+
return BlockMap{T}(promote_to_lmaps(ntuple(_ -> nrows, Val(nbc)), 1, 1, As...), (nbc,))
9894
end
9995

10096
############
@@ -126,18 +122,14 @@ function Base.vcat(As::Union{LinearMap,UniformScaling,AbstractVecOrMat}...)
126122
T = promote_type(map(eltype, As)...)
127123
nbr = length(As)
128124

129-
ncols = -1
130-
# find first non-UniformScaling to detect number of columns
131-
for A in As
132-
if !(A isa UniformScaling)
133-
ncols = size(A, 2)
134-
break
135-
end
136-
end
137-
@assert ncols != -1
125+
# find first non-UniformScaling to detect number of rows
126+
j = findfirst(x -> !isa(x, UniformScaling), As)
138127
# this should not happen, function should only be called with at least one LinearMap
139-
rows = ntuple(i->1, nbr)
140-
return BlockMap{T}(promote_to_lmaps(ntuple(i->ncols, nbr), 1, 2, As...), rows)
128+
@assert !isnothing(j)
129+
ncols = size(As[j], 2)
130+
131+
rows = ntuple(i->1, Val(nbr))
132+
return BlockMap{T}(promote_to_lmaps(ntuple(_ -> ncols, Val(nbr)), 1, 2, As...), rows)
141133
end
142134

143135
############

0 commit comments

Comments
 (0)