1
+ module BandedMatrixFactorizationTests
2
+ using MatrixFactorizations, LinearAlgebra, BandedMatrices, Test
3
+
4
+ @testset " QL tests" begin
5
+ for T in (Float64,ComplexF64,Float32,ComplexF32)
6
+ A= brand (T,10 ,10 ,3 ,2 )
7
+ Q,L= ql (A)
8
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
9
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
10
+ @test Matrix (Q)* Matrix (L) ≈ A
11
+ b= rand (T,10 )
12
+ @test mul! (similar (b),Q,mul! (similar (b),Q' ,b)) ≈ b
13
+ for j= 1 : size (A,2 )
14
+ @test Q' * A[:,j] ≈ L[:,j]
15
+ end
16
+
17
+ A= brand (T,14 ,10 ,3 ,2 )
18
+ Q,L= ql (A)
19
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
20
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
21
+ @test_broken Matrix (Q)* Matrix (L) ≈ A
22
+
23
+ for k= 1 : size (A,1 ),j= 1 : size (A,2 )
24
+ @test Q[k,j] ≈ Matrix (Q)[k,j]
25
+ end
26
+
27
+ A= brand (T,10 ,14 ,3 ,2 )
28
+ Q,L= ql (A)
29
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
30
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
31
+ @test Matrix (Q)* Matrix (L) ≈ A
32
+
33
+ for k= 1 : size (Q,1 ),j= 1 : size (Q,2 )
34
+ @test Q[k,j] ≈ Matrix (Q)[k,j]
35
+ end
36
+
37
+ A= brand (T,10 ,14 ,3 ,6 )
38
+ Q,L= ql (A)
39
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
40
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
41
+ @test Matrix (Q)* Matrix (L) ≈ A
42
+
43
+ for k= 1 : size (Q,1 ),j= 1 : size (Q,2 )
44
+ @test Q[k,j] ≈ Matrix (Q)[k,j]
45
+ end
46
+
47
+ A= brand (T,100 ,100 ,3 ,4 )
48
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
49
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
50
+ b= rand (T,100 )
51
+ @test ql (A)\ b ≈ Matrix (A)\ b
52
+ b= rand (T,100 ,2 )
53
+ @test ql (A)\ b ≈ Matrix (A)\ b
54
+ @test_throws DimensionMismatch ql (A) \ randn (3 )
55
+ @test_throws DimensionMismatch ql (A). Q' randn (3 )
56
+
57
+ A= brand (T,102 ,100 ,3 ,4 )
58
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
59
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
60
+ b= rand (T,102 )
61
+ @test_broken ql (A)\ b ≈ Matrix (A)\ b
62
+ b= rand (T,102 ,2 )
63
+ @test_broken ql (A)\ b ≈ Matrix (A)\ b
64
+ @test_throws DimensionMismatch ql (A) \ randn (3 )
65
+ @test_throws DimensionMismatch ql (A). Q' randn (3 )
66
+
67
+ A= brand (T,100 ,102 ,3 ,4 )
68
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
69
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
70
+ b= rand (T,100 )
71
+ @test_broken ql (A)\ b ≈ Matrix (A)\ b
72
+
73
+ A = LinearAlgebra. Tridiagonal (randn (T,99 ), randn (T,100 ), randn (T,99 ))
74
+ @test ql (A). factors ≈ ql! (Matrix (A)). factors
75
+ @test ql (A). τ ≈ ql! (Matrix (A)). τ
76
+ b= rand (T,100 )
77
+ @test ql (A)\ b ≈ Matrix (A)\ b
78
+ b= rand (T,100 ,2 )
79
+ @test ql (A)\ b ≈ Matrix (A)\ b
80
+ @test_throws DimensionMismatch ql (A) \ randn (3 )
81
+ @test_throws DimensionMismatch ql (A). Q' randn (3 )
82
+ end
83
+
84
+ @testset " lmul!/rmul!" begin
85
+ for T in (Float32, Float64, ComplexF32, ComplexF64)
86
+ A = brand (T,100 ,100 ,3 ,4 )
87
+ Q,R = qr (A)
88
+ x = randn (T,100 )
89
+ b = randn (T,100 ,2 )
90
+ @test lmul! (Q, copy (x)) ≈ Matrix (Q)* x
91
+ @test lmul! (Q, copy (b)) ≈ Matrix (Q)* b
92
+ @test lmul! (Q' , copy (x)) ≈ Matrix (Q)' * x
93
+ @test lmul! (Q' , copy (b)) ≈ Matrix (Q)' * b
94
+ c = randn (T,2 ,100 )
95
+ @test rmul! (copy (c), Q) ≈ c* Matrix (Q)
96
+ @test rmul! (copy (c), Q' ) ≈ c* Matrix (Q' )
97
+
98
+ A = brand (T,100 ,100 ,3 ,4 )
99
+ Q,L = ql (A)
100
+ x = randn (T,100 )
101
+ b = randn (T,100 ,2 )
102
+ @test lmul! (Q, copy (x)) ≈ Matrix (Q)* x
103
+ @test lmul! (Q, copy (b)) ≈ Matrix (Q)* b
104
+ @test lmul! (Q' , copy (x)) ≈ Matrix (Q)' * x
105
+ @test lmul! (Q' , copy (b)) ≈ Matrix (Q)' * b
106
+ c = randn (T,2 ,100 )
107
+ @test rmul! (copy (c), Q) ≈ c* Matrix (Q)
108
+ @test rmul! (copy (c), Q' ) ≈ c* Matrix (Q' )
109
+ end
110
+ end
111
+
112
+ @testset " Mixed types" begin
113
+ A= brand (10 ,10 ,3 ,2 )
114
+ b= rand (ComplexF64,10 )
115
+ Q,L= ql (A)
116
+ @test L\ (Q' * b) ≈ ql (A)\ b ≈ Matrix (A)\ b
117
+ @test Q* L ≈ A
118
+
119
+ A= brand (ComplexF64,10 ,10 ,3 ,2 )
120
+ b= rand (10 )
121
+ Q,L= ql (A)
122
+ @test Q* L ≈ A
123
+ @test L\ (Q' * b) ≈ ql (A)\ b ≈ Matrix (A)\ b
124
+
125
+ A = BandedMatrix {Int} (undef, (2 ,1 ), (4 ,4 ))
126
+ A. data .= 1 : length (A. data)
127
+ Q, L = ql (A)
128
+ @test Q* L ≈ A
129
+ end
130
+ end
131
+ end
0 commit comments