@@ -2,7 +2,7 @@ import Base: iterate
22
33export lalqmr, lalqmr!
44
5- mutable struct LALQMRIterable{T, xT, rT, lanczosT}
5+ mutable struct LALQMRIterable{T, xT, MT, rT, lanczosT}
66 x:: xT
77
88 lanczos:: lanczosT
@@ -11,11 +11,11 @@ mutable struct LALQMRIterable{T, xT, rT, lanczosT}
1111 maxiter:: Int
1212
1313 t:: Vector{T}
14- R:: UpperTriangular {T, Matrix{T}}
14+ R:: LimitedMemoryUpperTriangular {T, Matrix{T}}
1515
1616 G:: Vector{Givens{T}}
1717
18- d:: Matrix{T }
18+ d:: LimitedMemoryMatrix{T, MT }
1919end
2020
2121function lalqmr_iterable! (
@@ -24,6 +24,7 @@ function lalqmr_iterable!(
2424 reltol:: Real = sqrt (eps (real (eltype (b)))),
2525 maxiter:: Int = size (A, 2 ),
2626 initially_zero:: Bool = false ,
27+ max_memory:: Int = 4 ,
2728 kwargs...
2829)
2930 T = eltype (x)
@@ -37,16 +38,17 @@ function lalqmr_iterable!(
3738
3839 lanczos = LookAheadLanczosDecomp (
3940 A, v, w;
40- vw_normalized = true ,
41+ vw_normalized= true ,
42+ max_memory= max_memory,
4143 kwargs...
4244 )
4345
44- R = UpperTriangular ( Matrix {T} (undef, 0 , 0 ) )
46+ R = LimitedMemoryUpperTriangular {T, Matrix{T}} (max_memory )
4547 # Givens rotations
4648 G = Vector {Givens{T}} ()
4749
4850 # Projection operators
49- D = similar (x, size (x , 1 ), 0 )
51+ d = LimitedMemoryMatrix ( similar (x, size (v , 1 ), 0 ), max_memory )
5052
5153 t = Vector {T} (undef, 1 )
5254 t[1 ] = resnorm
@@ -58,7 +60,7 @@ function lalqmr_iterable!(
5860 lanczos, resnorm, tolerance,
5961 maxiter,
6062 t, R,
61- G, D
63+ G, d
6264 )
6365end
6466
@@ -82,9 +84,7 @@ function iterate(q::LALQMRIterable, n::Int=start(q))
8284 gend, r = givens (Llastcol, n, n+ 1 )
8385 push! (q. G, gend)
8486 Llastcol[end - 1 ] = r
85- Llastcol[end ] = 0
86- q. R = UpperTriangular ([[q. R; fill (0 , 1 , n- 1 )] Llastcol[1 : end - 1 ]])
87- @assert q. R[:, end ] ≈ Llastcol[1 : end - 1 ]
87+ _grow_hcat! (q. R, Llastcol[1 : end - 1 ])
8888
8989
9090 # Eq. 6.2, update t
@@ -97,10 +97,12 @@ function iterate(q::LALQMRIterable, n::Int=start(q))
9797 RU = q. R* q. lanczos. U
9898 d = q. lanczos. V[:, end - 1 ]
9999 for i in 1 : size (RU, 1 )- 1
100- axpy! (- RU[i, end ], q. d[:, i], d)
100+ if RU[i, end ] != 0
101+ axpy! (- RU[i, end ], q. d[:, i], d)
102+ end
101103 end
102104 d = d / RU[end , end ]
103- q. d = [q . d d]
105+ hcat! ( q. d, d)
104106
105107 # iterate x_n = x_n-1 + d_n τ_n
106108 axpy! (q. t[end - 1 ], d, q. x)
0 commit comments