Skip to content

Commit 6472d60

Browse files
authored
Merge pull request #268 from haampie/fix-division-by-zero
Fix division by zero due to exact off-diagonal zeros in Hessenberg matrix
2 parents c8a2ec6 + c50a65a commit 6472d60

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "IterativeSolvers"
22
uuid = "42fd0dbc-a981-5370-80f2-aaf504508153"
3-
version = "0.8.0"
3+
version = "0.8.2"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/gmres.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,14 @@ function gmres!(x, A, b;
205205
end
206206

207207
function update_residual!(r::Residual, arnoldi::ArnoldiDecomp, k::Int)
208-
# Cheaply computes the current residual
209-
r.nullvec[k + 1] = -conj(dot(view(r.nullvec, 1 : k), view(arnoldi.H, 1 : k, k)) / arnoldi.H[k + 1, k])
210-
r.accumulator += abs2(r.nullvec[k + 1])
211-
r.current = r.β / r.accumulator
208+
if iszero(arnoldi.H[k + 1, k])
209+
r.current = zero(r.current)
210+
else
211+
# Cheaply computes the current residual
212+
r.nullvec[k + 1] = -conj(dot(view(r.nullvec, 1 : k), view(arnoldi.H, 1 : k, k)) / arnoldi.H[k + 1, k])
213+
r.accumulator += abs2(r.nullvec[k + 1])
214+
r.current = r.β / r.accumulator
215+
end
212216
end
213217

214218
function init!(arnoldi::ArnoldiDecomp{T}, x, b, Pl, Ax; initially_zero::Bool = false) where {T}

test/gmres.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ end
6262
x = gmres(A, b; tol=tol, maxiter=2000)
6363
@test norm(A * x - b) / norm(b) tol
6464
end
65+
66+
@testset "Off-diagonal in hessenberg matrix exactly zero" begin
67+
A = Matrix(1.0I, 2, 2)
68+
b = [1.0, 2.2]
69+
x = gmres(A, b)
70+
@test all(x .== b)
71+
end
6572
end

0 commit comments

Comments
 (0)