-
Notifications
You must be signed in to change notification settings - Fork 13
/
linear_combinations_of_operators_test.jl
114 lines (85 loc) · 3.09 KB
/
linear_combinations_of_operators_test.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Test
using LinearAlgebra
using SummationByPartsOperators
for T in (Float32, Float64)
xmin = zero(T)
xmax = one(T)
N = 51
D₁ = periodic_derivative_operator(1, 4, xmin, xmax, N)
D₂ = periodic_derivative_operator(2, 4, xmin, xmax, N)
D₃ = periodic_derivative_operator(3, 4, xmin, xmax, N)
x = grid(D₁)
u = sinpi.(x)
dest1 = fill(zero(eltype(u)), length(u))
dest2 = fill(zero(eltype(u)), length(u))
dest3 = fill(zero(eltype(u)), length(u))
sum_12 = D₁ + D₂
sum_123 = D₁ + D₂ + D₃
sum_12_3 = sum_12 + D₃
sum_3_12 = D₃ + sum_12
for compact in (true, false)
show(IOContext(devnull, :compact=>compact), sum_12)
show(IOContext(devnull, :compact=>compact), sum_123)
show(IOContext(devnull, :compact=>compact), sum_12_3)
show(IOContext(devnull, :compact=>compact), sum_3_12)
end
@test grid(sum_12) == x
# Compare mul! with β=0 and mul! without β
mul!(dest1, sum_12, u, one(T), zero(T))
mul!(dest2, sum_12, u, one(T))
@test all(i->dest1[i] ≈ dest2[i], eachindex(u))
# Test commutativity
mul!(dest1, sum_123, u, T(2))
mul!(dest2, sum_12_3, u, T(2))
mul!(dest3, sum_3_12, u, T(2))
@test all(i->dest1[i] ≈ dest2[i], eachindex(u))
@test all(i->dest1[i] ≈ dest3[i], eachindex(u))
@testset "Differences" begin
diff = D₁ - D₂
@test diff * u ≈ D₁ * u - D₂ * u
combi = (D₁ - D₂) + D₂
@test combi * u ≈ D₁ * u
combi = D₂ + (D₁ - D₂)
@test combi * u ≈ D₁ * u
combi = (D₁ + D₂) - D₂
@test combi * u ≈ D₁ * u
combi = D₂ - (D₁ + D₂)
@test combi * u ≈ -(D₁ * u)
combi = +D₁
@test combi * u ≈ D₁ * u
combi = -D₁
@test combi * u ≈ -(D₁ * u)
combi = 2 * D₁
@test combi * u ≈ 2 * (D₁ * u)
combi = D₁ * 2
@test combi * u ≈ 2 * (D₁ * u)
combi = +(D₁ - D₂)
@test combi * u ≈ D₁ * u - D₂ * u
combi = -(D₁ - D₂)
@test combi * u ≈ -(D₁ * u) + D₂ * u
combi = (D₁ - D₂) + (-D₁ + D₂)
@test norm(combi * u) < N * eps(T)
combi = (D₁ - D₂) - (D₁ - D₂)
@test norm(combi * u) < N * eps(T)
end
@testset "Products and quotients" begin
combi = (1//2) * D₁
@test combi * u ≈ (D₁ * u) / 2
combi = D₁ * (1//2)
@test combi * u ≈ (D₁ * u) / 2
combi = (1//2) * (D₁ + D₂)
@test combi * u ≈ (D₁ * u + D₂ * u) / 2
combi = (D₁ + D₂) * (1//2)
@test combi * u ≈ (D₁ * u + D₂ * u) / 2
combi = D₁ / 2
@test combi * u ≈ (D₁ * u) / 2
combi = 2 \ D₁
@test combi * u ≈ (D₁ * u) / 2
combi = (D₁ + D₂) / 2
@test combi * u ≈ (D₁ * u + D₂ * u) / 2
combi = 2 \ (D₁ + D₂)
@test combi * u ≈ (D₁ * u + D₂ * u) / 2
combi = 2 * (D₁ + D₂) / 2
@test combi * u ≈ D₁ * u + D₂ * u
end
end