]> gitweb.michael.orlitzky.com - octave.git/blobdiff - tests/preconditioned_conjugate_gradient_method_tests.m
Add the first working version of the preconditioned CGM.
[octave.git] / tests / preconditioned_conjugate_gradient_method_tests.m
diff --git a/tests/preconditioned_conjugate_gradient_method_tests.m b/tests/preconditioned_conjugate_gradient_method_tests.m
new file mode 100644 (file)
index 0000000..c58eb55
--- /dev/null
@@ -0,0 +1,31 @@
+A = [5,1,2; ...
+     1,6,3; ...
+     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);
+
+unit_test_equals("PCGM agrees with CGM when M == I", ...
+                true, ...
+                norm(diff) < 1e-6);
+
+
+## Needs to be symmetric!
+M = [0.97466, 0.24345, 0.54850; ...
+     0.24345, 0.73251, 0.76639; ...
+     0.54850, 0.76639, 1.47581];
+
+pcgm = preconditioned_conjugate_gradient_method(A, M, b, x0, 1e-6, 1000);
+diff = norm(cgm - pcgm);
+
+unit_test_equals("PCGM agrees with CGM when M != I", ...
+                true, ...
+                norm(diff) < 1e-6);