6a11a63efd743df7fa7a73349de80fff3f488827
1 ## Used throughout. The PCGM uses the infinity norm as the stopping
2 ## condition, so we had better also.
3 max_iterations = 100000;
4 tolerance = 1e-11;
6 ## First a simple example.
7 A = [5,1,2; ...
8 1,6,3; ...
9 2,3,7];
11 M = eye(3);
12 b = [1;2;3];
13 x0 = [1;1;1];
15 cgm = conjugate_gradient_method(A, b, x0, tolerance, max_iterations);
17 M, ...
18 b, ...
19 x0, ...
20 tolerance, ...
21 max_iterations);
22 diff = norm(cgm - pcgm, 'inf');
24 unit_test_equals("PCGM agrees with CGM when M == I", ...
25 true, ...
26 diff < 2*tolerance);
28 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, tolerance, max_iterations);
29 diff = norm(pcgm_simple - pcgm, 'inf');
31 unit_test_equals("PCGM agrees with SimplePCGM when M == I", ...
32 true, ...
33 diff < 2*tolerance);
35 ## Needs to be symmetric!
36 M = [0.97466, 0.24345, 0.54850; ...
37 0.24345, 0.73251, 0.76639; ...
38 0.54850, 0.76639, 1.47581];
41 M, ...
42 b, ...
43 x0, ...
44 tolerance, ...
45 max_iterations);
46 diff = norm(cgm - pcgm, 'inf');
48 unit_test_equals("PCGM agrees with CGM when M != I", ...
49 true, ...
50 diff < 2*tolerance);
53 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, tolerance, max_iterations);
54 diff = norm(pcgm_simple - pcgm, 'inf');
56 unit_test_equals("PCGM agrees with Simple PCGM when M != I", ...
57 true, ...
58 diff < 2*tolerance);
61 # Test again Octave's pcg() function.
62 for n = [ 5, 10, 25, 50, 100 ]
63 A = random_positive_definite_matrix(5, 1000);
64 C = random_positive_definite_matrix(5, 1000);
65 M = C*C';
67 # Assumed by Octave's implementation when you don't supply a
68 # preconditioner.
69 x0 = zeros(5, 1);
70 b = unifrnd(-1000, 1000, 5, 1);
71 [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations, C, C');