Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/lssolve/lsmr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ function lssolve(operator, b, alg::LSMR, λ_::Real = 0)
v = scale(v₀, one(T))
β = norm(u)
S = typeof(β)
tol::S = alg.tol

# Check for early return, before normalising: ‖Aᴴb‖ = α * β vanishes whenever
# `b` or `Aᴴb` does, and then `x = 0` solves the (damped) least-squares problem
absζ̄ = norm(v)
if absζ̄ <= tol
if alg.verbosity > STARTSTOP_LEVEL
@info """LSMR lssolve converged without any iterations:
* ‖b - A * x ‖ = $(normres2string(β))
* ‖[b - A * x; λ * x] ‖ = $(normres2string(β))
* ‖ Aᴴ(b - A x) - λ^2 x ‖ = $(normres2string(absζ̄))
* number of operations = 1"""
end
return (zerovector(v), ConvergenceInfo(1, u, absζ̄, 0, 1))
elseif alg.verbosity >= STARTSTOP_LEVEL
@info "LSMR lssolve starts with convergence measure ‖ Aᴴ(b - A x) - λ^2 x ‖ = $(normres2string(absζ̄))"
end

u = scale!!(u, 1 / β)
v = scale!!(v, 1 / β)
α = norm(v)
Expand Down Expand Up @@ -41,23 +59,8 @@ function lssolve(operator, b, alg::LSMR, λ_::Real = 0)
numiter = 0
numops = 1 # One (adjoint) function application for v
maxiter = alg.maxiter
tol::S = alg.tol
λ::S = convert(S, λ_)

# Check for early return
if absζ̄ < tol
if alg.verbosity > STARTSTOP_LEVEL
@info """LSMR lssolve converged without any iterations:
* ‖b - A * x ‖ = $(normres2string(β))
* ‖[b - A * x; λ * x] ‖ = $(normres2string(β))
* ‖ Aᴴ(b - A x) - λ^2 x ‖ = $(normres2string(absζ̄))
* number of operations = $numops"""
end
return (x, ConvergenceInfo(1, r, absζ̄, numiter, numops))
elseif alg.verbosity >= STARTSTOP_LEVEL
@info "LSMR lssolve starts with convergence measure ‖ Aᴴ(b - A x) - λ^2 x ‖ = $(normres2string(absζ̄))"
end

while true
numiter += 1
Av = apply_normal(operator, v)
Expand Down
20 changes: 20 additions & 0 deletions test/lssolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,23 @@ end
end
end
end
@testset "LSMR with Aᴴb = 0 ($mode)" for mode in (:vector, :inplace, :outplace, :mixed)
scalartypes = mode === :vector ? (Float32, Float64, ComplexF32, ComplexF64) :
(ComplexF64,)
@testset for T in scalartypes
# the last n rows of A vanish, so both b = 0 and b supported on them give Aᴴb = 0
A = vcat(rand(T, (n, n)), zeros(T, (n, n)))
for b in (zeros(T, 2 * n), vcat(zeros(T, n), rand(T, n))), tol in (0, 1.0e-8)
for λ in (0, rand(real(T)))
x, info = @constinferred lssolve(
wrapop(A, Val(mode)), wrapvec(b, Val(mode)),
LSMR(; tol, verbosity = SILENT_LEVEL), λ
)
@test iszero(unwrapvec(x))
@test unwrapvec(info.residual) == b
@test info.converged == 1
@test info.numops == 1
end
end
end
end
Loading