1 ## Used throughout. The PCGM uses the infinity norm as the stopping
2 ## condition, so we had better also.
3 max_iterations = 100000;
6 ## First a simple example.
15 cgm = conjugate_gradient_method(A, b, x0, tolerance, max_iterations);
16 pcgm = preconditioned_conjugate_gradient_method(A, ...
22 diff = norm(cgm - pcgm, 'inf');
24 unit_test_equals("PCGM agrees with CGM when M == I", ...
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", ...
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];
40 pcgm = preconditioned_conjugate_gradient_method(A, ...
46 diff = norm(cgm - pcgm, 'inf');
48 unit_test_equals("PCGM agrees with CGM when M != I", ...
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", ...
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);
67 # Assumed by Octave's implementation when you don't supply a
70 b = unifrnd(-1000, 1000, 5, 1);
71 [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations, C, C');
72 [x, k] = preconditioned_conjugate_gradient_method(A,
78 diff = norm(o_x - x, 'inf');
79 msg = sprintf("Our PCGM agrees with Octave's, n=%d.", n);
80 unit_test_equals(msg, true, diff < 2*tolerance);