1
- using Test, LinearMaps, LinearAlgebra, SparseArrays, BenchmarkTools, InteractiveUtils
1
+ using Test, LinearMaps, LinearAlgebra, SparseArrays, InteractiveUtils
2
2
using LinearMaps: FiveArg
3
3
4
4
@testset " block maps" begin
5
5
@testset " hcat" begin
6
- for elty in (Float32, ComplexF64), n2 = (0 , 20 )
7
- A11 = rand (elty, 10 , 10 )
8
- A12 = rand (elty, 10 , n2)
9
- v = rand (elty, 10 )
6
+ m = 3
7
+ n = 4
8
+ for elty in (Float32, ComplexF64), n2 in (0 , 2 )
9
+ A11 = rand (elty, m, n)
10
+ A12 = rand (elty, m, n2)
11
+ a = rand (elty, m)
10
12
L = @inferred hcat (LinearMap (A11), LinearMap (A12))
11
13
@test L. maps isa Tuple
12
14
Lv = @inferred LinearMaps. BlockMap {elty} ([LinearMap (A11), LinearMap (A12)], (2 ,))
13
15
@test Lv. maps isa Vector
14
16
@test L == Lv == LinearMaps. BlockMap ([LinearMap (A11), LinearMap (A12)], (2 ,))
15
- @test occursin (" 10 ×$(10 + n2) LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
17
+ @test occursin (" $m ×$(n + n2) LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
16
18
@test @inferred (LinearMaps. MulStyle (L)) === FiveArg ()
17
19
@test L isa LinearMaps. BlockMap{elty}
18
20
if elty <: Complex
19
21
@test_throws ErrorException LinearMaps. BlockMap {Float64} ((LinearMap (A11), LinearMap (A12)), (2 ,))
20
22
end
21
23
A = [A11 A12]
22
- x = rand (10 + n2)
24
+ x = rand (n + n2)
23
25
@test size (L) == size (A) == size (Lv)
24
- @test Matrix (L) ≈ A ≈ Matrix (Lv)
26
+ @test Matrix (L) == A == Matrix (Lv)
25
27
@test L * x ≈ A * x ≈ Lv * x
26
28
L = @inferred hcat (LinearMap (A11), LinearMap (A12), LinearMap (A11))
27
29
A = [A11 A12 A11]
28
- @test Matrix (L) ≈ A
29
- A = [I I I A11 A11 A11 v ]
30
+ @test Matrix (L) == A
31
+ A = [I I I A11 A11 A11 a ]
30
32
@test (@which [A11 A11 A11]). module != LinearMaps
31
33
@test (@which [I I I A11 A11 A11]). module != LinearMaps
32
34
@test (@which hcat (I, I, I)). module != LinearMaps
33
35
@test (@which hcat (I, I, I, LinearMap (A11), A11, A11)). module == LinearMaps
34
- maps = @inferred LinearMaps. promote_to_lmaps (ntuple (i-> 10 , 7 ), 1 , 1 , I, I, I, LinearMap (A11), A11, A11, v )
36
+ maps = @inferred LinearMaps. promote_to_lmaps (ntuple (i-> m , 7 ), 1 , 1 , I, I, I, LinearMap (A11), A11, A11, a )
35
37
@inferred LinearMaps. rowcolranges (maps, (7 ,))
36
- L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, v )
37
- @test L == [I I I LinearMap (A11) LinearMap (A11) LinearMap (A11) LinearMap (v )]
38
- x = rand (elty, 61 )
38
+ L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, a )
39
+ @test L == [I I I LinearMap (A11) LinearMap (A11) LinearMap (A11) LinearMap (a )]
40
+ x = ones (elty, size (L, 2 ) )
39
41
@test L isa LinearMaps. BlockMap{elty}
40
42
@test L * x ≈ A * x
41
- L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, v, v, v, v)
42
- @test occursin (" 10×64 LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
43
- L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, v, v, v, v, v, v, v)
44
- @test occursin (" 10×67 LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
45
- A11 = rand (elty, 11 , 10 )
46
- A12 = rand (elty, 10 , n2)
47
- @test_throws DimensionMismatch hcat (LinearMap (A11), LinearMap (A12))
43
+ L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, a, a, a, a)
44
+ @test occursin (" $m ×$(3 m+ 3 n+ 4 ) LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
45
+ L = @inferred hcat (I, I, I, LinearMap (A11), A11, A11, a, a, a, a, a, a, a)
46
+ @test occursin (" $m ×$(3 m+ 3 n+ 7 ) LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
48
47
end
48
+ A11 = zeros (m+ 1 , n)
49
+ A12 = zeros (m, n)
50
+ @test_throws DimensionMismatch hcat (LinearMap (A11), LinearMap (A12))
49
51
end
50
52
51
53
@testset " vcat" begin
54
+ m = 2
55
+ n = 3
52
56
for elty in (Float32, ComplexF64)
53
- A11 = rand (elty, 10 , 10 )
54
- v = rand (elty, 10 )
57
+ A11 = rand (elty, m, n )
58
+ v = rand (elty, n )
55
59
L = @inferred vcat (LinearMap (A11))
56
60
@test L == [LinearMap (A11);]
57
61
@test Matrix (L) ≈ A11
58
- A21 = rand (elty, 20 , 10 )
62
+ A21 = rand (elty, 2 m, n )
59
63
L = @inferred vcat (LinearMap (A11), LinearMap (A21))
60
64
@test L. maps isa Tuple
65
+ @test L isa LinearMaps. BlockMap{elty}
66
+ @test occursin (" $(3 m) ×$n LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
67
+ @test @inferred (LinearMaps. MulStyle (L)) === FiveArg ()
61
68
Lv = LinearMaps. BlockMap {elty} ([LinearMap (A11), LinearMap (A21)], (1 ,1 ))
62
69
@test Lv. maps isa Vector
63
70
@test L == Lv
64
- @test occursin (" 30×10 LinearMaps.BlockMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), L))
65
- @test L isa LinearMaps. BlockMap{elty}
66
- @test @inferred (LinearMaps. MulStyle (L)) === FiveArg ()
67
71
@test (@which [A11; A21]). module != LinearMaps
68
72
A = [A11; A21]
69
- x = rand (10 )
73
+ x = rand (elty, n )
70
74
@test size (L) == size (A)
71
75
@test Matrix (L) == Matrix (Lv) == A
72
76
@test L * x ≈ Lv * x ≈ A * x
73
- A = [I; I; I; A11; A11; A11; v v v v v v v v v v ]
77
+ A = [I; I; I; A11; A11; A11; reduce (hcat, fill (v, n)) ]
74
78
@test (@which [I; I; I; A11; A11; A11; v v v v v v v v v v]). module != LinearMaps
75
- L = @inferred vcat (I, I, I, LinearMap (A11), LinearMap (A11), LinearMap (A11), reduce (hcat, fill (v, 10 )))
76
- @test L == [I; I; I; LinearMap (A11); LinearMap (A11); LinearMap (A11); reduce (hcat, fill (v, 10 ))]
77
- x = rand (elty, 10 )
79
+ L = @inferred vcat (I, I, I, LinearMap (A11), LinearMap (A11), LinearMap (A11), reduce (hcat, fill (v, n)))
80
+ @test L == [I; I; I; LinearMap (A11); LinearMap (A11); LinearMap (A11); reduce (hcat, fill (v, n))]
78
81
@test L isa LinearMaps. BlockMap{elty}
79
82
@test L * x ≈ A * x
80
- A11 = rand (elty, 10 , 11 )
81
- A21 = rand (elty, 20 , 10 )
82
- @test_throws DimensionMismatch vcat (LinearMap (A11), LinearMap (A21))
83
83
end
84
+ A11 = zeros (m, n+ 1 )
85
+ A21 = zeros (2 m, n)
86
+ @test_throws DimensionMismatch vcat (LinearMap (A11), LinearMap (A21))
84
87
end
85
88
86
89
@testset " hvcat" begin
90
+ m1 = 2
91
+ m2 = 3
92
+ n = 3
87
93
for elty in (Float32, ComplexF64)
88
- A11 = rand (elty, 10 , 10 )
89
- A12 = rand (elty, 10 , 20 )
90
- A21 = rand (elty, 20 , 10 )
91
- A22 = rand (elty, 20 , 20 )
94
+ A11 = rand (elty, m1, m1 )
95
+ A12 = ones (elty, m1, m2 )
96
+ A21 = rand (elty, m2, m1 )
97
+ A22 = ones (elty, m2, m2 )
92
98
A = [A11 A12; A21 A22]
93
99
@test (@which [A11 A12; A21 A22]). module != LinearMaps
94
100
@inferred hvcat ((2 ,2 ), LinearMap (A11), LinearMap (A12), LinearMap (A21), LinearMap (A22))
@@ -99,7 +105,7 @@ using LinearMaps: FiveArg
99
105
@test @inferred (LinearMaps. MulStyle (L)) === FiveArg ()
100
106
@test @inferred ! issymmetric (L)
101
107
@test @inferred ! ishermitian (L)
102
- x = rand (30 )
108
+ x = rand (m1 + m2 )
103
109
@test L isa LinearMaps. BlockMap{elty}
104
110
@test size (L) == size (A)
105
111
@test L * x ≈ Lv * x ≈ A * x
@@ -110,32 +116,32 @@ using LinearMaps: FiveArg
110
116
@inferred hvcat ((2 ,2 ), I, LinearMap (A12), LinearMap (A21), I)
111
117
L = @inferred hvcat ((2 ,2 ), I, LinearMap (A12), LinearMap (A21), I)
112
118
@test L isa LinearMaps. BlockMap{elty}
113
- @test size (L) == (30 , 30 )
119
+ @test size (L) == (m1 + m2, m1 + m2 )
114
120
@test Matrix (L) ≈ A
115
121
@test L * x ≈ A * x
116
122
y = randn (elty, size (L, 1 ))
117
123
for α in (0 , 1 , rand (elty)), β in (0 , 1 , rand (elty))
118
124
@test mul! (copy (y), L, x, α, β) ≈ y* β .+ A* x* α
119
125
end
120
- X = rand (elty, 30 , 10 )
121
- Y = randn (elty, size (L, 1 ), 10 )
126
+ X = rand (elty, m1 + m2, n )
127
+ Y = randn (elty, size (L, 1 ), n )
122
128
for α in (0 , 1 , rand (elty)), β in (0 , 1 , rand (elty))
123
129
@test mul! (copy (Y), L, X, α, β) ≈ Y* β .+ A* X* α
124
130
end
125
- A = rand (elty, 10 , 10 ); LA = LinearMap (A)
126
- B = rand (elty, 20 , 30 ); LB = LinearMap (B)
131
+ A = ones (elty, m1, m1 ); LA = LinearMap (A)
132
+ B = zeros (elty, m2, 3 m1 ); LB = LinearMap (B)
127
133
@test [LA LA LA; LB] isa LinearMaps. BlockMap{elty}
128
134
@test Matrix ([LA LA LA; LB]) ≈ [A A A; B]
129
135
@test [LB; LA LA LA] isa LinearMaps. BlockMap{elty}
130
136
@test Matrix ([LB; LA LA LA]) ≈ [B; A A A]
131
137
@test [I; LA LA LA] isa LinearMaps. BlockMap{elty}
132
138
@test Matrix ([I; LA LA LA]) ≈ [I; A A A]
133
- A12 = LinearMap (rand (elty, 10 , 21 ))
134
- A21 = LinearMap (rand (elty, 20 , 10 ))
135
- @test_throws DimensionMismatch A = [I A12; A21 I]
136
- @test_throws DimensionMismatch A = [I A21; A12 I]
137
- @test_throws DimensionMismatch A = [A12 A12; A21 A21]
138
- @test_throws DimensionMismatch A = [A12 A21; A12 A21]
139
+ A12 = LinearMap (zeros (elty, m1, m2 + 1 ))
140
+ A21 = LinearMap (zeros (elty, m2, m1 ))
141
+ @test_throws DimensionMismatch [I A12; A21 I]
142
+ @test_throws DimensionMismatch [I A21; A12 I]
143
+ @test_throws DimensionMismatch [A12 A12; A21 A21]
144
+ @test_throws DimensionMismatch [A12 A21; A12 A21]
139
145
140
146
# basic test of "misaligned" blocks
141
147
M = ones (elty, 3 , 2 ) # non-square
@@ -199,7 +205,7 @@ using LinearMaps: FiveArg
199
205
200
206
@testset " block diagonal maps" begin
201
207
for elty in (Float32, ComplexF64)
202
- m = 5 ; n = 6
208
+ m = 2 ; n = 3
203
209
M1 = 10 * (1 : m) .+ (1 : (n+ 1 ))' ; L1 = LinearMap (M1)
204
210
M2 = randn (elty, m, n+ 2 ); L2 = LinearMap (M2)
205
211
M3 = randn (elty, m, n+ 3 ); L3 = LinearMap (M3)
@@ -217,7 +223,7 @@ using LinearMaps: FiveArg
217
223
Bdv = @inferred LinearMaps. BlockDiagonalMap {elty} ([L1, L2, L3, L2, L1])
218
224
@test Bdv. maps isa Vector
219
225
@test @inferred (LinearMaps. MulStyle (Bd)) === FiveArg ()
220
- @test occursin (" 25×39 LinearMaps.BlockDiagonalMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), Bd))
226
+ @test occursin (" $( 5 m) × $( 5 n + 9 ) LinearMaps.BlockDiagonalMap{$elty }" , sprint ((t, s) -> show (t, " text/plain" , s), Bd))
221
227
@test Matrix (Bd) == Md
222
228
@test convert (AbstractMatrix, Bd) isa SparseMatrixCSC
223
229
@test sparse (Bd) == Md
@@ -227,7 +233,7 @@ using LinearMaps: FiveArg
227
233
@test_throws ArgumentError cat (L1, L2, L3, L2, L1; dims= (2 ,2 ))
228
234
@test Bd == Bdv == Bd2
229
235
@test Bd == blockdiag (L1, M2, M3, M2, M1)
230
- @test size (Bd) == (25 , 39 )
236
+ @test size (Bd) == (5 m, 5 n + 9 )
231
237
@test ! issymmetric (Bd)
232
238
@test ! ishermitian (Bd)
233
239
@test (@inferred Bd * x) ≈ Bdv * x ≈ Md * x
@@ -240,8 +246,8 @@ using LinearMaps: FiveArg
240
246
@test mul! (copy (y), Bd, x, α, β) ≈ y* β .+ Md* x* α
241
247
@test mul! (copy (y), Bdv, x, α, β) ≈ y* β .+ Md* x* α
242
248
end
243
- X = randn (elty, size (Md, 2 ), 10 )
244
- Y = randn (elty, size (Md, 1 ), 10 )
249
+ X = randn (elty, size (Md, 2 ), 3 )
250
+ Y = randn (elty, size (Md, 1 ), 3 )
245
251
for α in (0 , 1 , rand (elty)), β in (0 , 1 , rand (elty))
246
252
@test mul! (copy (Y), Bd, X, α, β) ≈ Y* β .+ Md* X* α
247
253
@test mul! (copy (Y), Bdv, X, α, β) ≈ Y* β .+ Md* X* α
@@ -250,7 +256,7 @@ using LinearMaps: FiveArg
250
256
end
251
257
252
258
@testset " function block map" begin
253
- N = 100
259
+ N = 5
254
260
T = ComplexF64
255
261
CS! = LinearMap {T} (cumsum!,
256
262
(y, x) -> (copyto! (y, x); reverse! (cumsum! (y, reverse! (y)))), N;
@@ -272,8 +278,11 @@ using LinearMaps: FiveArg
272
278
@test mul! (copy (v), transform (L), u, α, β) ≈ transform (M)* u* α + v* β
273
279
@test mul! (copy (v), transform (LinearMap (L)), u, α, β) ≈ transform (M)* u* α + v* β
274
280
@test mul! (copy (v), LinearMap (transform (L)), u, α, β) ≈ transform (M)* u* α + v* β
275
- bmap = @benchmarkable mul! ($ (copy (v)), $ (transform (L)), $ u, $ α, $ β)
276
- transform != adjoint && @test run (bmap, samples= 3 ). memory < 2 sizeof (u)
281
+ if transform != adjoint
282
+ transL = transform (L)
283
+ alloc = @allocated similar (v)
284
+ @test (@allocated mul! (v, transL, u, α, β)) <= alloc broken = (L == L2 && α != false )
285
+ end
277
286
end
278
287
end
279
288
end
0 commit comments