X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=tests%2Fpreconditioned_conjugate_gradient_method_tests.m;h=6a11a63efd743df7fa7a73349de80fff3f488827;hp=d44ee421570ead5908c49a14a959650c8cb88495;hb=36afdc59f58ac6a8a1fe366e1cde070db24905ac;hpb=84b8fb9002d091f84d0205e923c3989d0138ec9e diff --git a/tests/preconditioned_conjugate_gradient_method_tests.m b/tests/preconditioned_conjugate_gradient_method_tests.m index d44ee42..6a11a63 100644 --- a/tests/preconditioned_conjugate_gradient_method_tests.m +++ b/tests/preconditioned_conjugate_gradient_method_tests.m @@ -1,3 +1,9 @@ +## Used throughout. The PCGM uses the infinity norm as the stopping +## condition, so we had better also. +max_iterations = 100000; +tolerance = 1e-11; + +## First a simple example. A = [5,1,2; ... 1,6,3; ... 2,3,7]; @@ -6,37 +12,70 @@ M = eye(3); b = [1;2;3]; x0 = [1;1;1]; -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); +cgm = conjugate_gradient_method(A, b, x0, tolerance, max_iterations); +pcgm = preconditioned_conjugate_gradient_method(A, ... + M, ... + b, ... + x0, ... + tolerance, ... + max_iterations); +diff = norm(cgm - pcgm, 'inf'); unit_test_equals("PCGM agrees with CGM when M == I", ... true, ... - norm(diff) < 1e-6); + diff < 2*tolerance); -pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000); -diff = norm(pcgm_simple - pcgm); +pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, tolerance, max_iterations); +diff = norm(pcgm_simple - pcgm, 'inf'); unit_test_equals("PCGM agrees with SimplePCGM when M == I", ... true, ... - norm(diff) < 1e-6); + diff < 2*tolerance); ## 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); +pcgm = preconditioned_conjugate_gradient_method(A, ... + M, ... + b, ... + x0, ... + tolerance, ... + max_iterations); +diff = norm(cgm - pcgm, 'inf'); unit_test_equals("PCGM agrees with CGM when M != I", ... true, ... - norm(diff) < 1e-6); + diff < 2*tolerance); -pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, 1e-6, 1000); -diff = norm(pcgm_simple - pcgm); +pcgm_simple = simple_preconditioned_cgm(A, M, b, x0, tolerance, max_iterations); +diff = norm(pcgm_simple - pcgm, 'inf'); unit_test_equals("PCGM agrees with Simple PCGM when M != I", ... true, ... - norm(diff) < 1e-6); + diff < 2*tolerance); + + +# Test again Octave's pcg() function. +for n = [ 5, 10, 25, 50, 100 ] + A = random_positive_definite_matrix(5, 1000); + C = random_positive_definite_matrix(5, 1000); + M = C*C'; + + # 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, 'inf'); + msg = sprintf("Our PCGM agrees with Octave's, n=%d.", n); + unit_test_equals(msg, true, diff < 2*tolerance); +end