using MatrixMarket, SuiteSparseMatrixCollection
using Krylov, LinearOperators
using LinearAlgebra, Printf

ssmc = ssmc_db(verbose=false)
matrix = ssmc_matrices(ssmc, "HB", "illc1850")
path = fetch_ssmc(matrix, format="MM")

A = MatrixMarket.mmread(joinpath(path[1], "$(matrix.name[1]).mtx"))
b = MatrixMarket.mmread(joinpath(path[1], "$(matrix.name[1])_b.mtx"))[:]
(m, n) = size(A)
@printf("System size: %d rows and %d columns\n", m, n)

# Define a regularization parameter.
λ = 1.0e-3

(x, stats) = lsmr(A, b, λ=λ, atol=0.0, btol=0.0)
show(stats)
resid = norm(A' * (A * x - b) + λ * x) / norm(b)
@printf("LSMR: Relative residual: %8.1e\n", resid)
@printf("LSMR: ‖x‖: %8.1e\n", norm(x))
 Downloading artifact: HB/illc1850.MM
 Downloading artifact: HB/illc1850.MM
System size: 1850 rows and 712 columns
Lsmr stats
 niter: 1089
 solved: true
 inconsistent: true
 residuals: []
 Aresiduals: []
 residual: 16.205302719154798
 Aresidual: 0.001744668542011466
 κ₂(A): 67.13806544631052
 ‖A‖F: 45.016285250483314
 xNorm: 16060.854604524206
 status: truncated forward error small enough
LSMR: Relative residual:  2.4e-03
LSMR: ‖x‖:  1.6e+04