]> gitweb.michael.orlitzky.com - octave.git/blobdiff - tests/preconditioned_conjugate_gradient_method_tests.m
Test our (P)CGM implementation against Octave's.
[octave.git] / tests / preconditioned_conjugate_gradient_method_tests.m
index c58eb55edb206042c80477deb3ad41d964133334..2c8ff84044fb9c3ffce03259a3d6af2cb4265366 100644 (file)
@@ -3,12 +3,9 @@ A = [5,1,2; ...
      2,3,7];
 
 M = eye(3);
-
 b = [1;2;3];
-
 x0 = [1;1;1];
 
-## Solved over the rationals.
 cgm  = conjugate_gradient_method(A, b, x0, 1e-6, 1000);
 pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
 diff = norm(cgm - pcgm);
@@ -17,6 +14,12 @@ unit_test_equals("PCGM agrees with CGM when M == I", ...
                 true, ...
                 norm(diff) < 1e-6);
 
+pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
+diff = norm(pcgm_simple - pcgm);
+
+unit_test_equals("PCGM agrees with SimplePCGM when M == I", ...
+                true, ...
+                norm(diff) < 1e-6);
 
 ## Needs to be symmetric!
 M = [0.97466, 0.24345, 0.54850; ...
@@ -29,3 +32,37 @@ diff = norm(cgm - pcgm);
 unit_test_equals("PCGM agrees with CGM when M != I", ...
                 true, ...
                 norm(diff) < 1e-6);
+
+
+pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000);
+diff = norm(pcgm_simple - pcgm);
+
+unit_test_equals("PCGM agrees with Simple PCGM when M != I", ...
+                true, ...
+                norm(diff) < 1e-6);
+
+
+# Test again Octave's pcg() function.
+max_iterations = 100000;
+tolerance = 1e-11;
+C = random_positive_definite_matrix(5, 1000);
+M = C*C';
+
+for n = [ 5, 10, 25, 50, 100 ]
+  A = random_positive_definite_matrix(5, 1000);
+
+  # Assumed by Octave's implementation when you don't supply a
+  # preconditioner.
+  x0 = zeros(5, 1);
+  b  = unifrnd(-1000, 1000, 5, 1);
+  [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance, max_iterations, C, C');
+  [x, k] = preconditioned_conjugate_gradient_method(A,
+                                                   M,
+                                                   b,
+                                                   x0,
+                                                   tolerance,
+                                                   max_iterations);
+  diff = norm(o_x - x);
+  msg = sprintf("Our PCGM agrees with Octave's, n=%d.", n);
+  unit_test_equals(msg, true, norm(diff) < 1e-10);
+end