-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_factorizations.jl
More file actions
217 lines (187 loc) · 6.33 KB
/
test_factorizations.jl
File metadata and controls
217 lines (187 loc) · 6.33 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using BlockArrays: Block, BlockedMatrix, BlockedVector, blocks, mortar
using BlockSparseArrays: BlockSparseArray, BlockDiagonal, eachblockstoredindex
using MatrixAlgebraKit:
lq_compact,
lq_full,
qr_compact,
qr_full,
svd_compact,
svd_full,
svd_trunc,
truncrank,
trunctol
using LinearAlgebra: LinearAlgebra
using Random: Random
using Test: @inferred, @testset, @test
function test_svd(a, (U, S, Vᴴ); full=false)
# Check that the SVD is correct
(U * S * Vᴴ ≈ a) || return false
(U' * U ≈ LinearAlgebra.I) || return false
(Vᴴ * Vᴴ' ≈ LinearAlgebra.I) || return false
full || return true
# Check factors are unitary
(U * U' ≈ LinearAlgebra.I) || return false
(Vᴴ' * Vᴴ ≈ LinearAlgebra.I) || return false
return true
end
blockszs = (
([2, 2], [2, 2]), ([2, 2], [2, 3]), ([2, 2, 1], [2, 3]), ([2, 3], [2]), ([2], [2, 3])
)
eltypes = (Float32, Float64, ComplexF64)
test_params = Iterators.product(blockszs, eltypes)
# svd_compact!
# ------------
@testset "svd_compact ($m, $n) BlockSparseMatrix{$T}" for ((m, n), T) in test_params
a = BlockSparseArray{T}(undef, m, n)
# test empty matrix
usv_empty = svd_compact(a)
@test test_svd(a, usv_empty)
# test blockdiagonal
for i in LinearAlgebra.diagind(blocks(a))
I = CartesianIndices(blocks(a))[i]
a[Block(I.I...)] = rand(T, size(blocks(a)[i]))
end
usv = svd_compact(a)
@test test_svd(a, usv)
perm = Random.randperm(length(m))
b = a[Block.(perm), Block.(1:length(n))]
usv = svd_compact(b)
@test test_svd(b, usv)
# test permuted blockdiagonal with missing row/col
I_removed = rand(eachblockstoredindex(b))
c = copy(b)
delete!(blocks(c).storage, CartesianIndex(Int.(Tuple(I_removed))))
usv = svd_compact(c)
@test test_svd(c, usv)
end
# svd_full!
# ---------
@testset "svd_full ($m, $n) BlockSparseMatrix{$T}" for ((m, n), T) in test_params
a = BlockSparseArray{T}(undef, m, n)
# test empty matrix
usv_empty = svd_full(a)
@test test_svd(a, usv_empty; full=true)
# test blockdiagonal
for i in LinearAlgebra.diagind(blocks(a))
I = CartesianIndices(blocks(a))[i]
a[Block(I.I...)] = rand(T, size(blocks(a)[i]))
end
usv = svd_full(a)
@test test_svd(a, usv; full=true)
perm = Random.randperm(length(m))
b = a[Block.(perm), Block.(1:length(n))]
usv = svd_full(b)
@test test_svd(b, usv; full=true)
# test permuted blockdiagonal with missing row/col
I_removed = rand(eachblockstoredindex(b))
c = copy(b)
delete!(blocks(c).storage, CartesianIndex(Int.(Tuple(I_removed))))
usv = svd_full(c)
@test test_svd(c, usv; full=true)
end
# svd_trunc!
# ----------
@testset "svd_trunc ($m, $n) BlockSparseMatri{$T}" for ((m, n), T) in test_params
a = BlockSparseArray{T}(undef, m, n)
# test blockdiagonal
for i in LinearAlgebra.diagind(blocks(a))
I = CartesianIndices(blocks(a))[i]
a[Block(I.I...)] = rand(T, size(blocks(a)[i]))
end
minmn = min(size(a)...)
r = max(1, minmn - 2)
trunc = truncrank(r)
U1, S1, V1ᴴ = svd_trunc(a; trunc)
U2, S2, V2ᴴ = svd_trunc(Matrix(a); trunc)
@test size(U1) == size(U2)
@test size(S1) == size(S2)
@test size(V1ᴴ) == size(V2ᴴ)
@test Matrix(U1 * S1 * V1ᴴ) ≈ U2 * S2 * V2ᴴ
@test (U1' * U1 ≈ LinearAlgebra.I)
@test (V1ᴴ * V1ᴴ' ≈ LinearAlgebra.I)
atol = minimum(LinearAlgebra.diag(S1)) + 10 * eps(real(T))
trunc = trunctol(atol)
U1, S1, V1ᴴ = svd_trunc(a; trunc)
U2, S2, V2ᴴ = svd_trunc(Matrix(a); trunc)
@test size(U1) == size(U2)
@test size(S1) == size(S2)
@test size(V1ᴴ) == size(V2ᴴ)
@test Matrix(U1 * S1 * V1ᴴ) ≈ U2 * S2 * V2ᴴ
@test (U1' * U1 ≈ LinearAlgebra.I)
@test (V1ᴴ * V1ᴴ' ≈ LinearAlgebra.I)
# test permuted blockdiagonal
perm = Random.randperm(length(m))
b = a[Block.(perm), Block.(1:length(n))]
for trunc in (truncrank(r), trunctol(atol))
U1, S1, V1ᴴ = svd_trunc(b; trunc)
U2, S2, V2ᴴ = svd_trunc(Matrix(b); trunc)
@test size(U1) == size(U2)
@test size(S1) == size(S2)
@test size(V1ᴴ) == size(V2ᴴ)
@test Matrix(U1 * S1 * V1ᴴ) ≈ U2 * S2 * V2ᴴ
@test (U1' * U1 ≈ LinearAlgebra.I)
@test (V1ᴴ * V1ᴴ' ≈ LinearAlgebra.I)
end
# test permuted blockdiagonal with missing row/col
I_removed = rand(eachblockstoredindex(b))
c = copy(b)
delete!(blocks(c).storage, CartesianIndex(Int.(Tuple(I_removed))))
for trunc in (truncrank(r), trunctol(atol))
U1, S1, V1ᴴ = svd_trunc(c; trunc)
U2, S2, V2ᴴ = svd_trunc(Matrix(c); trunc)
@test size(U1) == size(U2)
@test size(S1) == size(S2)
@test size(V1ᴴ) == size(V2ᴴ)
@test Matrix(U1 * S1 * V1ᴴ) ≈ U2 * S2 * V2ᴴ
@test (U1' * U1 ≈ LinearAlgebra.I)
@test (V1ᴴ * V1ᴴ' ≈ LinearAlgebra.I)
end
end
@testset "qr_compact" for T in (Float32, Float64, ComplexF32, ComplexF64)
for i in [2, 3], j in [2, 3], k in [2, 3], l in [2, 3]
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
A[Block(1, 1)] = randn(T, i, k)
A[Block(2, 2)] = randn(T, j, l)
Q, R = qr_compact(A)
@test Matrix(Q'Q) ≈ LinearAlgebra.I
@test A ≈ Q * R
end
end
@testset "qr_full" for T in (Float32, Float64, ComplexF32, ComplexF64)
for i in [2, 3], j in [2, 3], k in [2, 3], l in [2, 3]
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
A[Block(1, 1)] = randn(T, i, k)
A[Block(2, 2)] = randn(T, j, l)
Q, R = qr_full(A)
Q′, R′ = qr_full(Matrix(A))
@test size(Q) == size(Q′)
@test size(R) == size(R′)
@test Matrix(Q'Q) ≈ LinearAlgebra.I
@test Matrix(Q * Q') ≈ LinearAlgebra.I
@test A ≈ Q * R
end
end
@testset "lq_compact" for T in (Float32, Float64, ComplexF32, ComplexF64)
for i in [2, 3], j in [2, 3], k in [2, 3], l in [2, 3]
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
A[Block(1, 1)] = randn(T, i, k)
A[Block(2, 2)] = randn(T, j, l)
L, Q = lq_compact(A)
@test Matrix(Q * Q') ≈ LinearAlgebra.I
@test A ≈ L * Q
end
end
@testset "lq_full" for T in (Float32, Float64, ComplexF32, ComplexF64)
for i in [2, 3], j in [2, 3], k in [2, 3], l in [2, 3]
A = BlockSparseArray{T}(undef, ([i, j], [k, l]))
A[Block(1, 1)] = randn(T, i, k)
A[Block(2, 2)] = randn(T, j, l)
L, Q = lq_full(A)
L′, Q′ = lq_full(Matrix(A))
@test size(L) == size(L′)
@test size(Q) == size(Q′)
@test Matrix(Q * Q') ≈ LinearAlgebra.I
@test Matrix(Q'Q) ≈ LinearAlgebra.I
@test A ≈ L * Q
end
end