Skip to content

Commit a084156

Browse files
author
Pawel Latawiec
committed
Fine-grained testing of LAL process
1 parent 9976395 commit a084156

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

src/lal.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Printf
12
import Base: iterate
23
import LinearAlgebra: UpperTriangular
34

@@ -335,7 +336,7 @@ function _update_VW_sequence!(ld)
335336
# Alg. 5.2.16
336337
_update_Flastcol!(ld)
337338
# Alg. 5.2.17
338-
innerv = inner_ok && _is_singular(ld.D)
339+
innerv = inner_ok && _is_singular(ld.D[ld.nl[ld.l]:end, ld.nl[ld.l]:end])
339340
# Alg. 5.2.18
340341
_update_L!(ld, innerv)
341342
# Alg. 5.2.19

test/lal.jl

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Test
66
# Equation references and identities from:
77
# Freund, R. W., & Nachtigal, N. M. (1994). An Implementation of the QMR Method Based on Coupled Two-Term Recurrences. SIAM Journal on Scientific Computing, 15(2), 313–337. https://doi.org/10.1137/0915022
88

9-
function _iterate_and_test_intermediates(ld)
9+
function _iterate_and_collect_lal_intermediates(ld)
1010
# iterates through ld and collects the intermediate matrices by appending the last
1111
# row and column to the matrix being built
1212
# returns a NamedTuple with the same names as `ld`
@@ -52,16 +52,13 @@ function _iterate_and_test_intermediates(ld)
5252
= [F̃ ld.F̃lastcol; (transpose(F * Γ[1:end-1, 1:end-1]) / Γ[1:end-1, 1:end-1])[end:end, :]]
5353
U = [U ld.U[1:end-1, end]; ld.U[end:end, :]]
5454
L = [L ld.L[1:end-1, end]; ld.L[end:end, :]]
55-
56-
results = (P=P, Q=Q, W=W, V=V, γ=γ, Γ=Γ, D=D, E=E, F=F, F̃=F̃, U=U, L=L, n=ld.n, A=ld.A, At=ld.At)
57-
_test_lal_identities(results)
5855
end
5956
end
6057

6158
return (P=P, Q=Q, W=W, V=V, γ=γ, Γ=Γ, D=D, E=E, F=F, F̃=F̃, U=U, L=L, n=ld.n, A=ld.A, At=ld.At)
6259
end
6360

64-
function _test_lal_identities(ld)
61+
function test_lal_identities(ld)
6562
# Note: V, W, Γ are all at n+1, but the values at n are use for some identities
6663
Vn = ld.V[:, 1:end-1]
6764
Wn = ld.W[:, 1:end-1]
@@ -121,7 +118,7 @@ function _test_lal_identities(ld)
121118
@test all([norm(ld.W[:, i]) for i in axes(ld.W, 2)] .≈ 1)
122119
end
123120

124-
function test_regular_identities(ld, log; early_exit=false)
121+
function test_regular_lal_identities(ld, log; early_exit=false)
125122
# If all regular vectors, expect:
126123
# Eq. 3.22, U is upper triangular (bi-diagonal)
127124
@test tril(ld.U, 1) triu(ld.U)
@@ -143,15 +140,15 @@ end
143140
v = rand(5)
144141
w = rand(5)
145142
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false)
146-
ld_results = _iterate_and_test_intermediates(ld)
143+
ld_results = _iterate_and_collect_lal_intermediates(ld)
147144

148145
@test ld.n == 1
149146
@test ld_results.V v
150147
@test ld_results.W w
151148
@test isempty(ld_results.P)
152149
@test isempty(ld_results.Q)
153150

154-
test_regular_identities(ld_results, ld.log; early_exit=true)
151+
test_regular_lal_identities(ld_results, ld.log; early_exit=true)
155152
end
156153

157154
@testset "Dense Hermitian Matrix" begin
@@ -162,20 +159,64 @@ end
162159
w = fill(0.0im, 10)
163160
w[1:2] .= 1.0
164161
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false)
165-
ld_results = _iterate_and_test_intermediates(ld)
166-
test_regular_identities(ld_results, ld.log)
162+
ld_results = _iterate_and_collect_lal_intermediates(ld)
163+
164+
test_lal_identities(ld_results)
165+
test_regular_lal_identities(ld_results, ld.log)
167166
end
168167

169168

170169
@testset "Sparse Tri-diagonal Matrix" begin
171170
A = Tridiagonal(-ones(9), 2*ones(10), -ones(9))
172-
v = fill(0.0im, 10)
173-
v[1] = 1.0
174-
w = fill(0.0im, 10)
175-
w[1] = 1.0
176-
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false)
177-
ld_results = _iterate_and_test_intermediates(ld)
178-
test_regular_identities(ld_results, ld.log)
171+
v = fill(0.0, 10)
172+
w = fill(0.0, 10)
173+
@testset "Regular Construction" begin
174+
fill!(v, 0)
175+
fill!(w, 0)
176+
v[1] = 1.0
177+
w[1] = 1.0
178+
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false)
179+
ld_results = _iterate_and_collect_lal_intermediates(ld)
180+
181+
test_lal_identities(ld_results)
182+
test_regular_lal_identities(ld_results, ld.log)
183+
end
184+
@testset "Size 2 V-W Blocks" begin
185+
fill!(v, 0)
186+
fill!(w, 0)
187+
v[1] = 1.0
188+
w[2] = 1.0
189+
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false, max_block_size=2)
190+
ld_results = _iterate_and_collect_lal_intermediates(ld)
191+
192+
test_lal_identities(ld_results)
193+
@test ld.log.VW_block_count[2] == 5
194+
@test ld.log.PQ_block_count[1] == 10
195+
end
196+
@testset "Size 3 V-W Blocks, size 2 P-Q Blocks" begin
197+
fill!(v, 0)
198+
fill!(w, 0)
199+
v[1] = 1.0
200+
w[3] = 1.0
201+
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false, max_block_size=3)
202+
ld_results = _iterate_and_collect_lal_intermediates(ld)
203+
204+
test_lal_identities(ld_results)
205+
@test ld.log.VW_block_count[3] == 3
206+
@test ld.log.PQ_block_count[2] == 3
207+
@test ld.log.PQ_block_count[1] == 3
208+
end
209+
@testset "Regular V-W, immediate P-Q Block" begin
210+
# v, w chosen s.t (w, v) ≠ 0 and (q, Ap) == (w, Av) == 0
211+
fill!(v, 0)
212+
fill!(w, 0)
213+
v[1] = 1.0
214+
w[1:2] .= [1.0; 2.0]
215+
ld = IS.LookAheadLanczosDecomp(A, v, w; log=true, vw_normalized=false)
216+
ld_results = _iterate_and_collect_lal_intermediates(ld)
217+
218+
test_lal_identities(ld_results)
219+
end
179220
end
180221

181222
@testset "Cyclic Circulant Matrix" begin
@@ -200,9 +241,10 @@ end
200241
Z13' B3 I3 Z34
201242
Z14' Z24' B4 I4
202243
]
203-
v = [ones(size(I1, 1)); fill(0.0, size(Ac, 1) - size(I1, 1))]
204-
w = [ones(size(I1, 1)); fill(0.0, size(Ac, 1) - size(I1, 1))]
244+
v = [rand(size(I1, 1)); fill(0.0, size(Ac, 1) - size(I1, 1))]
245+
w = [rand(size(I1, 1)); fill(0.0, size(Ac, 1) - size(I1, 1))]
205246
ld = IS.LookAheadLanczosDecomp(Ac, v, w; log=true, vw_normalized=false, max_block_size=8, verbose=true)
206-
ld_results = _iterate_and_test_intermediates(ld)
247+
ld_results = _iterate_and_collect_lal_intermediates(ld)
248+
test_lal_identities(ld_results)
207249
end
208250

0 commit comments

Comments
 (0)