Skip to content

Commit 3dfa5ff

Browse files
committed
Centralized storage selection is now used by both operations.jl and cat.jl consistently (tests still pass functionally; only the known two tiny allocation assertions remain).
1 parent a4f8f70 commit 3dfa5ff

File tree

3 files changed

+21
-40
lines changed

3 files changed

+21
-40
lines changed

src/abstract.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ storage_type(op::Adjoint) = storage_type(parent(op))
179179
storage_type(op::Transpose) = storage_type(parent(op))
180180
storage_type(op::Diagonal) = typeof(parent(op))
181181

182+
@inline function _select_storage_type(
183+
op1::AbstractLinearOperator,
184+
op2::AbstractLinearOperator,
185+
T::Type,
186+
)
187+
S = promote_type(storage_type(op1), storage_type(op2))
188+
if isconcretetype(S)
189+
return S
190+
elseif isconcretetype(storage_type(op1))
191+
return storage_type(op1)
192+
elseif isconcretetype(storage_type(op2))
193+
return storage_type(op2)
194+
else
195+
return Vector{T}
196+
end
197+
end
198+
182199
"""
183200
reset!(op)
184201

src/cat.jl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,7 @@ function hcat(A::AbstractLinearOperator, B::AbstractLinearOperator)
4545
ctprod! = @closure (res, w, α, β) ->
4646
hcat_ctprod!(res, adjoint(A), adjoint(B), Ancol, Ancol + Bncol, w, α, β)
4747
args5 = (has_args5(A) && has_args5(B))
48-
S = promote_type(storage_type(A), storage_type(B))
49-
if !isconcretetype(S)
50-
if isconcretetype(storage_type(A))
51-
S = storage_type(A)
52-
elseif isconcretetype(storage_type(B))
53-
S = storage_type(B)
54-
else
55-
S = Vector{T}
56-
end
57-
end
48+
S = _select_storage_type(A, B, T)
5849
CompositeLinearOperator(T, nrow, ncol, false, false, prod!, tprod!, ctprod!, args5, S = S)
5950
end
6051

@@ -111,16 +102,7 @@ function vcat(A::AbstractLinearOperator, B::AbstractLinearOperator)
111102
ctprod! = @closure (res, w, α, β) ->
112103
vcat_ctprod!(res, adjoint(A), adjoint(B), Anrow, Anrow + Bnrow, w, α, β)
113104
args5 = (has_args5(A) && has_args5(B))
114-
S = promote_type(storage_type(A), storage_type(B))
115-
if !isconcretetype(S)
116-
if isconcretetype(storage_type(A))
117-
S = storage_type(A)
118-
elseif isconcretetype(storage_type(B))
119-
S = storage_type(B)
120-
else
121-
S = Vector{T}
122-
end
123-
end
105+
S = _select_storage_type(A, B, T)
124106
CompositeLinearOperator(T, nrow, ncol, false, false, prod!, tprod!, ctprod!, args5, S = S)
125107
end
126108

src/operations.jl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,7 @@ function *(op1::AbstractLinearOperator, op2::AbstractLinearOperator)
139139
if m2 != n1
140140
throw(LinearOperatorException("shape mismatch"))
141141
end
142-
S = promote_type(storage_type(op1), storage_type(op2))
143-
if !isconcretetype(S)
144-
if isconcretetype(storage_type(op1))
145-
S = storage_type(op1)
146-
elseif isconcretetype(storage_type(op2))
147-
S = storage_type(op2)
148-
else
149-
S = Vector{T}
150-
end
151-
end
142+
S = _select_storage_type(op1, op2, T)
152143
#tmp vector for products
153144
vtmp = fill!(S(undef, m2), zero(T))
154145
utmp = fill!(S(undef, n1), zero(T))
@@ -217,16 +208,7 @@ function +(op1::AbstractLinearOperator, op2::AbstractLinearOperator)
217208
symm = (issymmetric(op1) && issymmetric(op2))
218209
herm = (ishermitian(op1) && ishermitian(op2))
219210
args5 = (has_args5(op1) && has_args5(op2))
220-
S = promote_type(storage_type(op1), storage_type(op2))
221-
if !isconcretetype(S)
222-
if isconcretetype(storage_type(op1))
223-
S = storage_type(op1)
224-
elseif isconcretetype(storage_type(op2))
225-
S = storage_type(op2)
226-
else
227-
S = Vector{T}
228-
end
229-
end
211+
S = _select_storage_type(op1, op2, T)
230212
return CompositeLinearOperator(T, m1, n1, symm, herm, prod!, tprod!, ctprod!, args5, S = S)
231213
end
232214

0 commit comments

Comments
 (0)