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);
13 unit_test_equals("PCGM agrees with CGM when M == I", ...
17 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
18 diff = norm(pcgm_simple - pcgm);
20 unit_test_equals("PCGM agrees with SimplePCGM when M == I", ...
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];
29 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
30 diff = norm(cgm - pcgm);
32 unit_test_equals("PCGM agrees with CGM when M != I", ...
37 pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
38 diff = norm(pcgm_simple - pcgm);
40 unit_test_equals("PCGM agrees with Simple PCGM when M != I", ...
45 # Test again Octave's pcg() function.
46 max_iterations = 100000;
48 C = random_positive_definite_matrix(5, 1000);
51 for n = [ 5, 10, 25, 50, 100 ]
52 A = random_positive_definite_matrix(5, 1000);
54 # Assumed by Octave's implementation when you don't supply a
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,
66 msg = sprintf("Our PCGM agrees with Octave's, n=%d.", n);
67 unit_test_equals(msg, true, norm(diff) < 1e-10);