From: Michael Orlitzky Date: Tue, 26 Mar 2013 00:58:54 +0000 (-0400) Subject: Mangle the PCGM tests, commit something that just might work even when we fail to... X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=commitdiff_plain;h=606266fd6248aa04a256a3fe23e98a83b1397c9e;hp=c3e7feb9a20fe34085814a2d32a9878e0b069ec6 Mangle the PCGM tests, commit something that just might work even when we fail to converge. --- diff --git a/tests/preconditioned_conjugate_gradient_method_tests.m b/tests/preconditioned_conjugate_gradient_method_tests.m index 6a11a63..a27d3e2 100644 --- a/tests/preconditioned_conjugate_gradient_method_tests.m +++ b/tests/preconditioned_conjugate_gradient_method_tests.m @@ -1,7 +1,7 @@ ## Used throughout. The PCGM uses the infinity norm as the stopping ## condition, so we had better also. -max_iterations = 100000; -tolerance = 1e-11; +max_iterations = 10000; +tolerance = 1e-10; ## First a simple example. A = [5,1,2; ... @@ -60,22 +60,35 @@ unit_test_equals("PCGM agrees with Simple PCGM when M != I", ... # 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'; + A = random_positive_definite_matrix(n, 100); + + # Use the cholesky factorization as a preconditioner. + Ct = perturb(chol(A)); + C = Ct'; + M = Ct*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'); + x0 = zeros(n, 1); + b = unifrnd(-100, 100, n, 1); + g = @(x) A*x - b; + + ## pcg() stops when the /relative/ norm falls below tolerance. To + ## eliminate the relativity, we divide the tolerance by the + ## quantity that pcg() will divide by. + [o_x, o_flag, o_relres, o_iter] = pcg(A, b, tolerance/norm(g(x0)), ... + max_iterations, C, C'); [x, k] = preconditioned_conjugate_gradient_method(A, M, - b, - x0, - tolerance, - max_iterations); + 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); + ## There's no good way to choose the tolerance here, since each + ## individual algorithm terminates based on the (2,infinity)-norm of + ## the gradient. So we use two orders of magnitude. + unit_test_equals(msg, true, diff <= sqrt(tolerance)); end