Test our (P)CGM implementation against Octave's.
[octave.git] / tests / preconditioned_conjugate_gradient_method_tests.m
1 A = [5,1,2; ...
2 1,6,3; ...
3 2,3,7];
4
5 M = eye(3);
6 b = [1;2;3];
7 x0 = [1;1;1];
8
9 cgm = conjugate_gradient_method(A, b, x0, 1e-6, 1000);
10 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
11 diff = norm(cgm - pcgm);
12
13 unit_test_equals("PCGM agrees with CGM when M == I", ...
14 true, ...
15 norm(diff) < 1e-6);
16
17 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
18 diff = norm(pcgm_simple - pcgm);
19
20 unit_test_equals("PCGM agrees with SimplePCGM when M == I", ...
21 true, ...
22 norm(diff) < 1e-6);
23
24 ## Needs to be symmetric!
25 M = [0.97466, 0.24345, 0.54850; ...
26 0.24345, 0.73251, 0.76639; ...
27 0.54850, 0.76639, 1.47581];
28
29 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
30 diff = norm(cgm - pcgm);
31
32 unit_test_equals("PCGM agrees with CGM when M != I", ...
33 true, ...
34 norm(diff) < 1e-6);
35
36
37 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
38 diff = norm(pcgm_simple - pcgm);
39
40 unit_test_equals("PCGM agrees with Simple PCGM when M != I", ...
41 true, ...
42 norm(diff) < 1e-6);
43
44
45 # Test again Octave's pcg() function.
46 max_iterations = 100000;
47 tolerance = 1e-11;
48 C = random_positive_definite_matrix(5, 1000);
49 M = C*C';
50
51 for n = [ 5, 10, 25, 50, 100 ]
52 A = random_positive_definite_matrix(5, 1000);
53
54 # Assumed by Octave's implementation when you don't supply a
55 # preconditioner.
56 x0 = zeros(5, 1);
57 b = unifrnd(-1000, 1000, 5, 1);
58 [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations, C, C');
59 [x, k] = preconditioned_conjugate_gradient_method(A,
60 M,
61 b,
62 x0,
63 tolerance,
64 max_iterations);
65 diff = norm(o_x - x);
66 msg = sprintf("Our PCGM agrees with Octave's, n=%d.", n);
67 unit_test_equals(msg, true, norm(diff) < 1e-10);
68 end