Skip to content

Commit 55cdd2f

Browse files
authored
Retain Vector{Operator{T}} in convert (#292)
* retain Vector{Operator{T}} in convert * Add tests * Fix scope issue
1 parent 213b887 commit 55cdd2f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.7.40"
3+
version = "0.7.41"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Operators/general/algebra.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ for (OP,mn) in ((:colstart,:min),(:colstop,:max),(:rowstart,:min),(:rowstop,:max
4747
end
4848
end
4949

50+
# We assume that a Vector{Operator{T}} occurs when incompatible operators are added
51+
# in this case, we may retain the container
52+
_convertops(::Type{Operator{T}}, ops) where {T} =
53+
map(x -> strictconvert(Operator{T}, x), ops)
54+
_convertops(::Type{Operator{T}}, ops::Vector{Operator{S}}) where {T,S} =
55+
Operator{T}[strictconvert(Operator{T}, op) for op in ops]
5056
function convert(::Type{Operator{T}}, P::PlusOperator) where T
5157
if T==eltype(P)
5258
P
5359
else
5460
ops = P.ops
55-
PlusOperator(ops isa AbstractVector{<:Operator{T}} ? ops :
56-
map(x -> strictconvert(Operator{T}, x), ops),
57-
P.bandwidths,P.sz)::Operator{T}
61+
PlusOperator(eltype(ops) <: Operator{T} ? ops :
62+
_convertops(Operator{T}, ops),
63+
P.bandwidths, P.sz)::Operator{T}
5864
end
5965
end
6066

@@ -272,8 +278,8 @@ function convert(::Type{Operator{T}},P::TimesOperator) where T
272278
P
273279
else
274280
ops = P.ops
275-
TimesOperator(ops isa AbstractVector{<:Operator{T}} ? ops :
276-
map(x -> strictconvert(Operator{T}, x), ops) ,
281+
TimesOperator(eltype(ops) <: Operator{T} ? ops :
282+
_convertops(Operator{T}, ops) ,
277283
bandwidths(P), size(P))
278284
end
279285
end

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,22 @@ end
235235
@test (@inferred Z + Z + Z) == Z
236236
@test (@inferred Z + Z + Z + Z) == Z
237237

238-
@inferred (() -> (D = Derivative(); D + D))()
238+
@inferred (() -> (local D = Derivative(); D + D))()
239+
240+
A = @inferred M + M
241+
@test A * f 2 * (M * f)
242+
M = Multiplication(f, space(f))
243+
A = M + M
244+
@test A * f 2 * (M * f)
245+
B = @inferred convert(Operator{ComplexF64}, A)
246+
@test eltype(B) == ComplexF64
247+
@test B * f 2 * (M * f)
248+
249+
C = @inferred 2M
250+
D = M + C
251+
E = @inferred convert(Operator{ComplexF64}, D)
252+
@test eltype(E) == ComplexF64
253+
@test E * f 3 * (M * f)
239254
end
240255
end
241256

0 commit comments

Comments
 (0)