@@ -2,8 +2,17 @@ using IterativeSolvers
22using LinearMaps
33using Base. Test
44
5+ import Base. A_ldiv_B!
6+
57include (" laplace_matrix.jl" )
68
9+ struct JacobiPrec
10+ diagonal
11+ end
12+
13+
14+ A_ldiv_B! (y, P:: JacobiPrec , x) = y .= x ./ P. diagonal
15+
716@testset " Conjugate Gradients" begin
817
918srand (1234321 )
@@ -40,48 +49,35 @@ srand(1234321)
4049end
4150
4251@testset " Sparse Laplacian" begin
43- A = laplace_matrix (Float64, 10 , 3 )
44- L = tril (A)
45- D = diag (A)
46- U = triu (A)
47-
48- JAC (x) = D .\ x
49- SGS (x) = L \ (D .* (U \ x))
52+ A = laplace_matrix (Float64, 10 , 2 )
53+ P = JacobiPrec (diag (A))
5054
5155 rhs = randn (size (A, 2 ))
52- rhs /= norm (rhs)
56+ scale! ( rhs, inv ( norm (rhs)) )
5357 tol = 1e-5
5458
5559 @testset " Matrix" begin
5660 xCG = cg (A, rhs; tol= tol, maxiter= 100 )
57- xJAC = cg (A, rhs; Pl= JAC, tol= tol, maxiter= 100 )
58- xSGS = cg (A, rhs; Pl= SGS, tol= tol, maxiter= 100 )
61+ xJAC = cg (A, rhs; Pl= P, tol= tol, maxiter= 100 )
5962 @test norm (A * xCG - rhs) ≤ tol
60- @test norm (A * xSGS - rhs) ≤ tol
6163 @test norm (A * xJAC - rhs) ≤ tol
6264 end
6365
6466 Af = LinearMap (A)
6567 @testset " Function" begin
6668 xCG = cg (Af, rhs; tol= tol, maxiter= 100 )
67- xJAC = cg (Af, rhs; Pl= JAC, tol= tol, maxiter= 100 )
68- xSGS = cg (Af, rhs; Pl= SGS, tol= tol, maxiter= 100 )
69+ xJAC = cg (Af, rhs; Pl= P, tol= tol, maxiter= 100 )
6970 @test norm (A * xCG - rhs) ≤ tol
70- @test norm (A * xSGS - rhs) ≤ tol
7171 @test norm (A * xJAC - rhs) ≤ tol
7272 end
7373
7474 @testset " Function with specified starting guess" begin
75- tol = 1e-4
7675 x0 = randn (size (rhs))
7776 xCG, hCG = cg! (copy (x0), Af, rhs; tol= tol, maxiter= 100 , log= true )
78- xJAC, hJAC = cg! (copy (x0), Af, rhs; Pl= JAC, tol= tol, maxiter= 100 , log= true )
79- xSGS, hSGS = cg! (copy (x0), Af, rhs; Pl= SGS, tol= tol, maxiter= 100 , log= true )
77+ xJAC, hJAC = cg! (copy (x0), Af, rhs; Pl= P, tol= tol, maxiter= 100 , log= true )
8078 @test norm (A * xCG - rhs) ≤ tol
81- @test norm (A * xSGS - rhs) ≤ tol
8279 @test norm (A * xJAC - rhs) ≤ tol
8380 @test niters (hJAC) == niters (hCG)
84- @test niters (hSGS) ≤ niters (hJAC)
8581 end
8682end
8783
0 commit comments