X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=optimization%2Fpreconditioned_conjugate_gradient_method.m;h=63943482c8c6dd1b47d916d9bdaf400521d6b992;hp=af70af5f14e90b819f7ef2c6b1dc980877a62d7e;hb=84b8fb9002d091f84d0205e923c3989d0138ec9e;hpb=af2083885af78b1290c21f2852c6fdba25820918 diff --git a/optimization/preconditioned_conjugate_gradient_method.m b/optimization/preconditioned_conjugate_gradient_method.m index af70af5..6394348 100644 --- a/optimization/preconditioned_conjugate_gradient_method.m +++ b/optimization/preconditioned_conjugate_gradient_method.m @@ -13,8 +13,9 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, % % min [phi(x) = (1/2)* + ] % - % using the preconditioned conjugate gradient method (14.54 in - % Guler). + % using the preconditioned conjugate gradient method (14.56 in + % Guler). If ``M`` is the identity matrix, we use the slightly + % faster implementation in conjugate_gradient_method.m. % % INPUT: % @@ -23,7 +24,8 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, % % - ``M`` -- The preconditioning matrix. If the actual matrix used % to precondition ``Q`` is called ``C``, i.e. ``C^(-1) * Q * - % C^(-T) == \bar{Q}``, then M=CC^T. + % C^(-T) == \bar{Q}``, then M=CC^T. However the matrix ``C`` is + % never itself needed. This is explained in Guler, section 14.9. % % - ``b`` -- The right-hand-side of the system to solve. % @@ -46,21 +48,51 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, % % All vectors are assumed to be *column* vectors. % + % The cited algorithm contains a typo; in "The Preconditioned + % Conjugate-Gradient Method", we are supposed to define + % d_{0} = -z_{0}, not -r_{0} as written. + % % REFERENCES: % % 1. Guler, Osman. Foundations of Optimization. New York, Springer, % 2010. % - Ct = chol(M); - C = Ct'; - C_inv = inv(C); - Ct_inv = inv(Ct); + % Set k=0 first, that way the references to xk,rk,zk,dk which + % immediately follow correspond to x0,r0,z0,d0 respectively. + k = 0; + + xk = x0; + rk = Q*xk - b; + zk = M \ rk; + dk = -zk; + + for k = [ 0 : max_iterations ] + if (norm(rk) < tolerance) + x = xk; + return; + end + + % Used twice, avoid recomputation. + rkzk = rk' * zk; + + % The term alpha_k*dk appears twice, but so does Q*dk. We can't + % do them both, so we precompute the more expensive operation. + Qdk = Q * dk; + + alpha_k = rkzk/(dk' * Qdk); + x_next = xk + (alpha_k * dk); + r_next = rk + (alpha_k * Qdk); + z_next = M \ r_next; + beta_next = (r_next' * z_next)/rkzk; + d_next = -z_next + beta_next*dk; - Q_bar = C_inv * Q * Ct_inv; - b_bar = C_inv * b; + k = k + 1; + xk = x_next; + rk = r_next; + zk = z_next; + dk = d_next; + end - % The solution to Q_bar*x_bar == b_bar is x_bar = Ct*x. - [x_bar, k] = conjugate_gradient_method(Q_bar, b_bar, x0, tolerance, max_iterations); - x = Ct_inv * x_bar; + x = xk; end