Skip to content

Commit fa377a4

Browse files
haampieandreasnoack
authored andcommitted
Fix off-by-one error in iteration count of IDR(s) (#158)
* Fix off-by-one error in iteration count * Add test for # of iterations in IDR(s)
1 parent c2a6698 commit fa377a4

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/idrs.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function idrs_method!(log::ConvergenceHistory, X, op, args, C::T,
5151
verbose && @printf("=== idrs ===\n%4s\t%7s\n","iter","resnorm")
5252
R = C - op(X, args...)::T
5353
normR = vecnorm(R)
54-
iter = 0
54+
iter = 1
5555

5656
if smoothing
5757
X_s = copy(X)
@@ -76,7 +76,7 @@ function idrs_method!(log::ConvergenceHistory, X, op, args, C::T,
7676
c = zeros(eltype(C),s)
7777

7878
om::eltype(C) = 1
79-
while normR > tol && iter < maxiter
79+
while normR > tol && iter maxiter
8080
for i in 1:s,
8181
f[i] = vecdot(P[i], R)
8282
end
@@ -144,16 +144,15 @@ function idrs_method!(log::ConvergenceHistory, X, op, args, C::T,
144144
end
145145
push!(log, :resnorm, normR)
146146
verbose && @printf("%3d\t%1.2e\n",iter,normR)
147-
iter += 1
148-
if normR < tol || iter > maxiter
147+
if normR < tol || iter == maxiter
149148
shrink!(log)
150149
setconv(log, 0<=normR<tol)
151150
return X
152151
end
153152
if k < s
154153
f[k+1:s] = f[k+1:s] - beta*M[k+1:s,k]
155154
end
156-
155+
iter += 1
157156
end
158157

159158
# Now we have sufficient vectors in G_j to compute residual in G_j+1

test/idrs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ end
3535
@test history.isconverged
3636
@test norm(A * x - b) / norm(b) tol
3737
end
38+
39+
@testset "Maximum number of iterations" begin
40+
x, history = idrs(rand(5, 5), rand(5), log=true, maxiter=2)
41+
@test history.iters == 2
42+
@test length(history[:resnorm]) == 2
43+
end
3844
end

0 commit comments

Comments
 (0)