Skip to content

Commit

Permalink
Ensure that all-zeros rhs gives all-zeros LHS
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 20, 2017
1 parent b41b9cc commit a13f0da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/cg.jl
Expand Up @@ -7,14 +7,18 @@ export cg, cg!
cg(A, b; kwargs...) = cg!(zerox(A,b), A, b; kwargs...)

function cg!(x, A, b;
tol::Real=size(A,2)*eps(), maxiter::Integer=size(A,2),
plot=false, log::Bool=false, kwargs...
)
tol::Real=size(A,2)*eps(), maxiter::Integer=size(A,2),
plot=false, log::Bool=false, kwargs...
)
(plot & !log) && error("Can't plot when log keyword is false")
K = KrylovSubspace(A, length(b), 1, Vector{Adivtype(A,b)}[])
init!(K, x)
history = ConvergenceHistory(partial=!log)
history[:tol] = tol
if all(el->el==0, b)
fill!(x, zero(eltype(x)))
return log ? (x, history) : x
end
reserve!(history,:resnorm, maxiter)
cg_method!(history, x, K, b; tol=tol, maxiter=maxiter, kwargs...)
(plot || log) && shrink!(history)
Expand Down
6 changes: 5 additions & 1 deletion test/cg.jl
Expand Up @@ -28,7 +28,11 @@ context("Small full system") do
F = cholfact(A)
x2,ch2 = cg(A, rhs; Pl=F, log=true)
@fact niters(ch2) --> less_than_or_equal(2)
@fact nprods(ch2) --> less_than_or_equal(2)
@fact nprods(ch2) --> less_than_or_equal(2)

# All-zeros rhs should give all-zeros lhs
x0 = cg(A, zeros(N))
@fact x0 --> zeros(N)
end

context("Sparse Laplacian") do
Expand Down

0 comments on commit a13f0da

Please sign in to comment.