Skip to content

Commit 4e92e2d

Browse files
authored
Merge pull request #132 from haampie/fix-cg-test
Simplify CG test
2 parents ae5a3b3 + b8e2ed4 commit 4e92e2d

File tree

3 files changed

+45
-70
lines changed

3 files changed

+45
-70
lines changed

test/cg.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ using LinearMaps
44

55
srand(1234321)
66

7-
include("getDivGrad.jl")
7+
include("poisson_matrix.jl")
88

99
facts("cg") do
1010

1111
context("Small full system") do
12-
N=10
13-
A = randn(N,N)
14-
A = A'*A
12+
N = 10
13+
A = randn(N, N)
14+
A = A' * A
1515
rhs = randn(N)
1616
tol = 1e-12
1717

18-
x = cg(A,rhs;tol=tol, maxiter=2*N)
19-
@fact norm(A*x - rhs) --> less_than(cond(A)*tol)
18+
x = cg(A, rhs; tol=tol, maxiter=2N)
19+
@fact norm(A*x - rhs) --> less_than(cond(A) * tol)
2020

21-
x,ch = cg(A,rhs;tol=tol, maxiter=2*N, log=true)
22-
@fact norm(A*x - rhs) --> less_than(cond(A)*tol)
21+
x,ch = cg(A, rhs; tol=tol, maxiter=2N, log=true)
22+
@fact norm(A * x - rhs) --> less_than(cond(A) * tol)
2323
@fact ch.isconverged --> true
2424

2525
# If you start from the exact solution, you should converge immediately
26-
x2, ch2 = cg!(A\rhs, A, rhs; tol=tol*10, log=true)
26+
x2, ch2 = cg!(A \ rhs, A, rhs; tol=10tol, log=true)
2727
@fact niters(ch2) --> less_than_or_equal(1)
2828
@fact nprods(ch2) --> less_than_or_equal(2)
2929

3030
# Test with cholfact should converge immediately
3131
F = cholfact(A)
32-
x2,ch2 = cg(A, rhs; Pl=F, log=true)
32+
x2, ch2 = cg(A, rhs; Pl=F, log=true)
3333
@fact niters(ch2) --> less_than_or_equal(2)
3434
@fact nprods(ch2) --> less_than_or_equal(2)
3535

@@ -39,45 +39,45 @@ context("Small full system") do
3939
end
4040

4141
context("Sparse Laplacian") do
42-
A = getDivGrad(32,32,32)
42+
A = poisson_matrix(Float64, 32, 3)
4343
L = tril(A)
4444
D = diag(A)
4545
U = triu(A)
46-
JAC(x) = D.\x
47-
SGS(x) = L\(D.*(U\x))
46+
JAC(x) = D .\ x
47+
SGS(x) = L \ (D .* (U \ x))
4848

49-
rhs = randn(size(A,2))
50-
rhs/= norm(rhs)
49+
rhs = randn(size(A, 2))
50+
rhs /= norm(rhs)
5151
tol = 1e-5
5252

5353
context("matrix") do
54-
xCG = cg(A,rhs;tol=tol,maxiter=100)
55-
xJAC = cg(A,rhs;Pl=JAC,tol=tol,maxiter=100)
56-
xSGS = cg(A,rhs;Pl=SGS,tol=tol,maxiter=100)
57-
@fact norm(A*xCG - rhs) --> less_than_or_equal(tol)
58-
@fact norm(A*xSGS - rhs) --> less_than_or_equal(tol)
59-
@fact norm(A*xJAC - rhs) --> less_than_or_equal(tol)
54+
xCG = cg(A, rhs; tol=tol, maxiter=100)
55+
xJAC = cg(A, rhs; Pl=JAC, tol=tol, maxiter=100)
56+
xSGS = cg(A, rhs; Pl=SGS, tol=tol, maxiter=100)
57+
@fact norm(A * xCG - rhs) --> less_than_or_equal(tol)
58+
@fact norm(A * xSGS - rhs) --> less_than_or_equal(tol)
59+
@fact norm(A * xJAC - rhs) --> less_than_or_equal(tol)
6060
end
6161

6262
Af = LinearMap(A)
6363
context("function") do
64-
xCG = cg(Af,rhs;tol=tol,maxiter=100)
65-
xJAC = cg(Af,rhs;Pl=JAC,tol=tol,maxiter=100)
66-
xSGS = cg(Af,rhs;Pl=SGS,tol=tol,maxiter=100)
67-
@fact norm(A*xCG - rhs) --> less_than_or_equal(tol)
68-
@fact norm(A*xSGS - rhs) --> less_than_or_equal(tol)
69-
@fact norm(A*xJAC - rhs) --> less_than_or_equal(tol)
64+
xCG = cg(Af, rhs; tol=tol, maxiter=100)
65+
xJAC = cg(Af, rhs; Pl=JAC, tol=tol, maxiter=100)
66+
xSGS = cg(Af, rhs; Pl=SGS, tol=tol, maxiter=100)
67+
@fact norm(A * xCG - rhs) --> less_than_or_equal(tol)
68+
@fact norm(A * xSGS - rhs) --> less_than_or_equal(tol)
69+
@fact norm(A * xJAC - rhs) --> less_than_or_equal(tol)
7070
end
7171

7272
context("function with specified starting guess") do
7373
tol = 1e-4
7474
x0 = randn(size(rhs))
75-
xCG, hCG = cg!(copy(x0),Af,rhs;Pl=identity,tol=tol,maxiter=100, log=true)
76-
xJAC, hJAC = cg!(copy(x0),Af,rhs;Pl=JAC,tol=tol,maxiter=100, log=true)
77-
xSGS, hSGS = cg!(copy(x0),Af,rhs;Pl=SGS,tol=tol,maxiter=100, log=true)
78-
@fact norm(A*xCG - rhs) --> less_than_or_equal(tol)
79-
@fact norm(A*xSGS - rhs) --> less_than_or_equal(tol)
80-
@fact norm(A*xJAC - rhs) --> less_than_or_equal(tol)
75+
xCG, hCG = cg!(copy(x0), Af, rhs; Pl=identity, tol=tol, maxiter=100, log=true)
76+
xJAC, hJAC = cg!(copy(x0), Af, rhs; Pl=JAC, tol=tol, maxiter=100, log=true)
77+
xSGS, hSGS = cg!(copy(x0), Af, rhs; Pl=SGS, tol=tol, maxiter=100, log=true)
78+
@fact norm(A * xCG - rhs) --> less_than_or_equal(tol)
79+
@fact norm(A * xSGS - rhs) --> less_than_or_equal(tol)
80+
@fact norm(A * xJAC - rhs) --> less_than_or_equal(tol)
8181

8282
iterCG = niters(hCG)
8383
iterJAC = niters(hJAC)

test/getDivGrad.jl

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/poisson_matrix.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function poisson_matrix{T}(::Type{T}, n, dims)
2+
D = second_order_central_diff(T, n);
3+
A = copy(D);
4+
5+
for idx = 2 : dims
6+
A = kron(A, speye(n)) + kron(speye(size(A, 1)), D);
7+
end
8+
9+
A
10+
end
11+
12+
second_order_central_diff{T}(::Type{T}, dim) = convert(SparseMatrixCSC{T, Int}, SymTridiagonal(fill(2 * one(T), dim), fill(-one(T), dim - 1)))

0 commit comments

Comments
 (0)