]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Test our (P)CGM implementation against Octave's.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 22 Mar 2013 07:48:56 +0000 (03:48 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 22 Mar 2013 07:48:56 +0000 (03:48 -0400)
tests/conjugate_gradient_method_tests.m
tests/preconditioned_conjugate_gradient_method_tests.m

index 59e137aa0c37964dfa543d55e5a08633455a5ce4..e04b2e2a0e9a28afab3a0f083872303775471100 100644 (file)
@@ -14,3 +14,23 @@ diff = norm(actual - expected);
 unit_test_equals("CGM works on an example", ...
                 true, ...
                 norm(diff) < 1e-6);
+
+
+# Let's test Octave's pcg() against our method on some easy matrices.
+max_iterations = 100000;
+tolerance = 1e-11;
+
+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);
+  [x, k] = conjugate_gradient_method(A, b, x0, tolerance, max_iterations);
+
+  diff = norm(o_x - x);
+  msg = sprintf("Our CGM agrees with Octave's, n=%d.", n);
+  unit_test_equals(msg, true, norm(diff) < 1e-10);
+end
index d44ee421570ead5908c49a14a959650c8cb88495..2c8ff84044fb9c3ffce03259a3d6af2cb4265366 100644 (file)
@@ -40,3 +40,29 @@ 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