JuliaLinearAlgebra / IterativeSolvers.jl

Iterative algorithms for solving linear systems, eigensystems, and singular value problems
MIT License
394 stars 106 forks source link

error in CG with Preconditioner #332

Closed ChrisNabold closed 1 year ago

ChrisNabold commented 1 year ago

I tried to run Example 8.8.3 from the book T. A. Driscoll, R. J. Braun Fundamentals of Numerical Computation Julia Edition. Whereas cg without preconditioner seems to work, I receive an error message when using cg with a preconditioner. 2023-02-17_14-53-53 using MAT, LinearAlgebra, SparseArrays, IterativeSolvers, Preconditioners

file = matopen("A.mat"); A = read(file,"A"); close(file); n = size(A,1); @show n,nnz(A) b = ones(n);

M = DiagonalPreconditioner(diag(A));

plain(b) = cg(A,b,maxiter=200,reltol=1.e-4,log=true) time_plain = @elapsed x,hist1 = plain(b) prec(b) = cg(A,b,P1=M,maxiter=200,reltol=1.e-4,log=true) time_prec = @elapsed x,hist2 = prec(b) @show time_plain, time_prec

rr = hist1[:resnorm] plot(0:length(rr),rr/rr[1],yscale=:log10,label="plain") rr = hist2[:resnorm] plot!(0:length(rr),rr/rr[1],yscale=:log10,label="preconditioned") title!("Diagonal preconditioning in CG") 2023-02-17_14-53-53

mohamed82008 commented 1 year ago

prec(b) = cg(A,b,P1=M,maxiter=200,reltol=1.e-4,log=true)

should be Pl not P1