Skip to content

Commit 097523e

Browse files
authored
improve PlusOperator performance (#109)
1 parent d12111a commit 097523e

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Operators/general/algebra.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ end
3939

4040
function PlusOperator(ops::Vector)
4141
# calculate bandwidths
42-
b1,b2=-720,-720 # approximates ∞,-∞
43-
for op in ops
44-
br=bandwidths(op)
45-
b1=max(br[1],b1)
46-
b2=max(br[end],b2)
47-
end
42+
almostneginf=-720 # approximates -∞
43+
b1 = mapreduce(first bandwidths, max, ops, init = almostneginf)
44+
b2 = mapreduce(last bandwidths, max, ops, init = almostneginf)
4845
PlusOperator(ops,(b1,b2))
4946
end
5047

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ end
112112
end
113113
end
114114
end
115+
@testset "plus operator" begin
116+
c = [1,2,3]
117+
f = Fun(PointSpace(1:3), c)
118+
M = Multiplication(f)
119+
@testset for t in [1, 3]
120+
op = M + t * M
121+
@test bandwidths(op) == bandwidths(M)
122+
@test coefficients(op * f) == @. (1+t)*c^2
123+
for op2 in Any[M + M + t * M, op + M]
124+
@test bandwidths(op2) == bandwidths(M)
125+
@test coefficients(op2 * f) == @. (2+t)*c^2
126+
end
127+
op3 = op + op
128+
@test bandwidths(op3) == bandwidths(M)
129+
@test coefficients(op3 * f) == @. 2(1+t)*c^2
130+
end
131+
end
115132
end
116133

117134
@time include("ETDRK4Test.jl")

0 commit comments

Comments
 (0)