ConstrainedOptPack_MatrixSymPosDefLBFGS.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 //
00029 // Let's define a compact representation for the matrix B^{k} and
00030 // its inverse H^{k} = inv(B^{k}).
00031 //
00032 // Bk = (1/gk)*I - [ (1/gk)*S  Y ] * inv( [ (1/gk)*S'S   L ]     [ (1/gk)*S' ]
00033 //                                        [    L'       -D ] ) * [    Y'     ]
00034 //                                        \________________/
00035 //                                                Q
00036 //
00037 // Hk = gk*I + [ S  gk*Y ] * [ inv(R')*(D+gk*Y'Y)*inv(R)     -inv(R') ] * [   S'  ]
00038 //                           [            -inv(R)                0    ]   [ gk*Y' ]
00039 //
00040 // where:
00041 //
00042 // gk = gamma_k <: R
00043 //
00044 //      [ s^{1}'*y^{1}  s^{1}'*y^{2}  ...   s^{1}'*y^{m}  ]
00045 //  S'Y = [ s^{2}'*y^{1}  s^{2}'*y^{2}  ...   s^{2}'*y^{m}  ] <: R^(m x m)
00046 //      [ .       .           .       ]
00047 //      [ s^{m}'*y^{1}  s^{m}'*y^{2}  ...   s^{m}'*y^{m}  ]
00048 // 
00049 //      [ s^{1}'*y^{1}  0       ...   0       ]
00050 //  D =   [ 0       s^{2}'*y^{2}  ...   0       ] <: R^(m x m)
00051 //      [ .       .           .       ]
00052 //      [ 0       0       ...   s^{m}'*y^{m}  ]
00053 //
00054 //  R = upper triangular part of S'Y
00055 // 
00056 //  L = lower tirangular part of S'Y with zeros on the diagonal
00057 //
00058 
00059 #include <assert.h>
00060 
00061 #include <typeinfo>
00062 
00063 #include "ConstrainedOptPack_MatrixSymPosDefLBFGS.hpp"
00064 #include "AbstractLinAlgPack_BFGS_helpers.hpp"
00065 #include "AbstractLinAlgPack_VectorDenseEncap.hpp"
00066 #include "AbstractLinAlgPack_MatrixOpOut.hpp"
00067 #include "AbstractLinAlgPack_VectorStdOps.hpp"
00068 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00069 #include "DenseLinAlgPack_LinAlgOpPack.hpp"
00070 #include "DenseLinAlgPack_DMatrixOut.hpp"
00071 #include "DenseLinAlgLAPack.hpp"
00072 #include "Teuchos_Workspace.hpp"
00073 #include "Teuchos_TestForException.hpp"
00074 
00075 namespace {
00076 
00077   using DenseLinAlgPack::DVectorSlice;
00078   using DenseLinAlgPack::DMatrixSlice;
00079 
00087   void comp_Cb( const DMatrixSlice& Lb, const DVectorSlice& Db_diag
00088     , DMatrixSlice* Cb );
00089 
00090 } // end namespace
00091 
00092 namespace ConstrainedOptPack {
00093 
00094 // /////////////////////////////////
00095 // Inline private member functions
00096 
00097 inline
00098 const DMatrixSliceTri MatrixSymPosDefLBFGS::R() const
00099 {
00100   return DenseLinAlgPack::tri( STY_(1,m_bar_,1,m_bar_), BLAS_Cpp::upper, BLAS_Cpp::nonunit );
00101 }
00102 
00103 inline
00104 const DMatrixSliceTri MatrixSymPosDefLBFGS::Lb() const
00105 {
00106   return DenseLinAlgPack::tri( STY_(2,m_bar_,1,m_bar_-1), BLAS_Cpp::lower, BLAS_Cpp::nonunit );
00107 }
00108 
00109 inline
00110 DMatrixSlice MatrixSymPosDefLBFGS::STY()
00111 {
00112   return STY_(1,m_bar_,1,m_bar_);
00113 }
00114 
00115 inline
00116 const DMatrixSlice MatrixSymPosDefLBFGS::STY() const
00117 {
00118   return STY_(1,m_bar_,1,m_bar_);
00119 }
00120 
00121 inline
00122 DMatrixSliceSym MatrixSymPosDefLBFGS::STS()
00123 {
00124   return DenseLinAlgPack::nonconst_sym( STSYTY_(2,m_bar_+1,1,m_bar_),BLAS_Cpp::lower );
00125 }
00126 
00127 inline
00128 const DMatrixSliceSym MatrixSymPosDefLBFGS::STS() const
00129 {
00130   return DenseLinAlgPack::sym( STSYTY_(2,m_bar_+1,1,m_bar_),BLAS_Cpp::lower );
00131 }
00132 
00133 inline
00134 DMatrixSliceSym MatrixSymPosDefLBFGS::YTY()
00135 {
00136   return DenseLinAlgPack::nonconst_sym( STSYTY_(1,m_bar_,2,m_bar_+1),BLAS_Cpp::upper );
00137 }
00138 
00139 inline
00140 const DMatrixSliceSym MatrixSymPosDefLBFGS::YTY() const
00141 {
00142   return DenseLinAlgPack::sym( STSYTY_(1,m_bar_,2,m_bar_+1),BLAS_Cpp::upper );
00143 }
00144 
00145 // ///////////////////////
00146 // Nonlinined functions
00147 
00148 MatrixSymPosDefLBFGS::MatrixSymPosDefLBFGS(
00149     size_type   m
00150   ,bool       maintain_original
00151   ,bool       maintain_inverse
00152   ,bool       auto_rescaling
00153   )
00154 {
00155   initial_setup(m,maintain_original,maintain_inverse,auto_rescaling);
00156 }
00157 
00158 void MatrixSymPosDefLBFGS::initial_setup(
00159     size_type   m
00160   ,bool       maintain_original
00161   ,bool       maintain_inverse
00162   ,bool       auto_rescaling
00163   )
00164 {
00165   // Validate input
00166   TEST_FOR_EXCEPTION(
00167     !maintain_original && !maintain_inverse, std::invalid_argument
00168     ,"MatrixSymPosDefLBFGS::initial_setup(...) : "
00169     "Error, both maintain_original and maintain_inverse can not both be false!" );
00170   TEST_FOR_EXCEPTION(
00171     m < 1, std::invalid_argument
00172     ,"MatrixSymPosDefLBFGS::set_num_updates_stored(m) : "
00173     "Error, the number of storage locations must be > 0" );
00174   vec_spc_           = Teuchos::null;
00175   maintain_original_ = maintain_original;
00176   maintain_inverse_  = maintain_inverse;
00177   m_                 = m;
00178   n_                 = 0; // make uninitialized
00179   num_secant_updates_= 0;
00180 }
00181 
00182 // Overridden from MatrixOp
00183 
00184 const VectorSpace& MatrixSymPosDefLBFGS::space_cols() const
00185 {
00186   return *vec_spc_;
00187 }
00188 
00189 std::ostream& MatrixSymPosDefLBFGS::output(std::ostream& out) const
00190 {
00191   assert_initialized();
00192   out << "\n*** Limited Memory BFGS matrix\n";
00193   out << "\nConversion to dense =\n";
00194   MatrixOp::output(out);
00195   out << "\nStored quantities\n"
00196     << "\nn       = " << n_
00197     << "\nm       = " << m_
00198     << "\nm_bar   = " << m_bar_
00199     << "\ngamma_k = " << gamma_k_ << std::endl;
00200   if( m_bar_ ) {
00201     out << "\nS =\n" << *S()
00202       << "\nY =\n" << *Y()
00203       << "\nS'Y =\n" << STY_(1,m_bar_,1,m_bar_)
00204       << "\nlower(S'S) \\ zero diagonal \\ upper(Y'Y) =\n"
00205         << STSYTY_(1,m_bar_+1,1,m_bar_+1)
00206       << "\nQ updated? = " << Q_updated_ << std::endl;
00207     if(Q_updated_)
00208       out << "\nCholesky of schur complement of Q, QJ =\n" << QJ_(1,m_bar_,1,m_bar_);
00209   }
00210   return out;
00211 }
00212 
00213 MatrixOp& MatrixSymPosDefLBFGS::operator=(const MatrixOp& mwo)
00214 { 
00215   const MatrixSymPosDefLBFGS *p_m = dynamic_cast<const MatrixSymPosDefLBFGS*>(&mwo);
00216   if(p_m) {
00217     if( p_m == this ) return *this; // assignment to self
00218     // Important: Assign all members here.
00219     auto_rescaling_      = p_m->auto_rescaling_;
00220     maintain_original_   = p_m->maintain_original_;
00221     original_is_updated_ = p_m->original_is_updated_;
00222     maintain_inverse_    = p_m->maintain_inverse_;
00223     inverse_is_updated_  = p_m->inverse_is_updated_;
00224     vec_spc_             = p_m->vec_spc_.get() ? p_m->vec_spc_->clone() : Teuchos::null;
00225     n_               = p_m->n_;
00226     m_               = p_m->m_;
00227     m_bar_             = p_m->m_bar_;
00228     num_secant_updates_  = p_m->num_secant_updates_;
00229     gamma_k_           = p_m->gamma_k_;
00230     S_               = p_m->S_;
00231     Y_               = p_m->Y_;
00232     STY_             = p_m->STY_;
00233     STSYTY_            = p_m->STSYTY_;
00234     Q_updated_           = p_m->Q_updated_;
00235     QJ_              = p_m->QJ_;
00236   }
00237   else {
00238     TEST_FOR_EXCEPTION(
00239       true,std::invalid_argument
00240       ,"MatrixSymPosDefLBFGS::operator=(const MatrixOp& mwo) : Error, "
00241       "The concrete type of mwo \'" << typeid(mwo).name() << "\' is not "
00242       "as subclass of MatrixSymPosDefLBFGS as required" );
00243   }
00244   return *this;
00245 }
00246 
00247 // Level-2 BLAS
00248 
00249 void MatrixSymPosDefLBFGS::Vp_StMtV(
00250     VectorMutable* y, value_type alpha, BLAS_Cpp::Transp trans_rhs1
00251   , const Vector& x, value_type beta
00252   ) const
00253 {
00254   using AbstractLinAlgPack::Vt_S;
00255   using AbstractLinAlgPack::Vp_StV;
00256   using AbstractLinAlgPack::Vp_StMtV;
00257   using LinAlgOpPack::V_StMtV;
00258   using LinAlgOpPack::V_MtV;
00259   typedef VectorDenseEncap         vde;
00260   typedef VectorDenseMutableEncap  vdme;
00261   using Teuchos::Workspace;
00262   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00263 
00264   assert_initialized();
00265 
00266   assert( original_is_updated_ ); // For now just always update
00267 
00268   // y = b*y + Bk * x
00269   //
00270   // y = b*y + (1/gk)*x - [ (1/gk)*S  Y ] * inv(Q) * [ (1/gk)*S' ] * x
00271   //                                                 [     Y'    ]
00272   // Perform the following operations (in order):
00273   //
00274   // y = b*y
00275   //
00276   // y += (1/gk)*x
00277   //
00278   // t1 = [ (1/gk)*S'*x ]   <: R^(2*m)
00279   //    [      Y'*x   ]
00280   //
00281   // t2 = inv(Q) * t1     <: R^(2*m)
00282   //
00283   // y += -(1/gk) * S * t2(1:m)
00284   //
00285   // y += -1.0 * Y * t2(m+1,2m)
00286 
00287   const value_type
00288     invgk = 1.0 / gamma_k_;
00289 
00290   // y = b*y
00291   Vt_S( y, beta );
00292 
00293   // y += (1/gk)*x
00294   Vp_StV( y, invgk, x );
00295 
00296   if( !m_bar_ )
00297     return; // No updates have been added yet.
00298 
00299   const multi_vec_ptr_t
00300     S = this->S(),
00301     Y = this->Y();
00302 
00303   // Get workspace
00304 
00305   const size_type
00306     mb = m_bar_;
00307 
00308   Workspace<value_type>  t1_ws(wss,2*mb);
00309   DVectorSlice                 t1(&t1_ws[0],t1_ws.size());
00310   Workspace<value_type>  t2_ws(wss,2*mb);
00311   DVectorSlice                 t2(&t2_ws[0],t2_ws.size());
00312 
00313   VectorSpace::vec_mut_ptr_t
00314     t = S->space_rows().create_member();
00315 
00316   // t1 = [ (1/gk)*S'*x ]
00317   //    [      Y'*x   ]
00318 
00319   V_StMtV( t.get(), invgk, *S, BLAS_Cpp::trans, x );
00320   t1(1,mb) = vde(*t)();
00321   V_MtV( t.get(), *Y, BLAS_Cpp::trans, x );
00322   t1(mb+1,2*mb) = vde(*t)();
00323 
00324   // t2 = inv(Q) * t1
00325   V_invQtV( &t2, t1 );
00326 
00327   // y += -(1/gk) * S * t2(1:m)
00328   (vdme(*t)() = t2(1,mb));
00329   Vp_StMtV( y, -invgk, *S, BLAS_Cpp::no_trans, *t );
00330 
00331   // y += -1.0 * Y * t2(m+1,2m
00332   (vdme(*t)() = t2(mb+1,2*mb));
00333   Vp_StMtV( y, -1.0, *Y, BLAS_Cpp::no_trans, *t );
00334 
00335 }
00336 
00337 // Overridden from MatrixOpNonsing
00338 
00339 void MatrixSymPosDefLBFGS::V_InvMtV(
00340   VectorMutable* y, BLAS_Cpp::Transp trans_rhs1
00341   , const Vector& x
00342   ) const
00343 {
00344   using AbstractLinAlgPack::Vp_StMtV;
00345   using DenseLinAlgPack::V_InvMtV;
00346   using LinAlgOpPack::V_mV;
00347   using LinAlgOpPack::V_StV;
00348   using LinAlgOpPack::Vp_V;
00349   using LinAlgOpPack::V_MtV;
00350   using LinAlgOpPack::V_StMtV;
00351   using LinAlgOpPack::Vp_MtV;
00352   using DenseLinAlgPack::Vp_StMtV;
00353   typedef VectorDenseEncap         vde;
00354   typedef VectorDenseMutableEncap  vdme;
00355   using Teuchos::Workspace;
00356   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00357 
00358   assert_initialized();
00359 
00360   assert( inverse_is_updated_ ); // For now just always update
00361 
00362   // y = inv(Bk) * x = Hk * x
00363   //
00364   // = gk*x + [S gk*Y] * [ inv(R')*(D+gk*Y'Y)*inv(R)     -inv(R') ] * [   S'  ] * x
00365   //                     [            -inv(R)                0    ]   [ gk*Y' ]
00366   //
00367   // Perform in the following (in order):
00368   //
00369   // y = gk*x
00370   //
00371   // t1 = [   S'*x  ]         <: R^(2*m)
00372   //      [ gk*Y'*x ]
00373   //
00374   // t2 = inv(R) * t1(1:m)      <: R^(m)
00375   //
00376   // t3 = - inv(R') * t1(m+1,2*m)   <: R^(m)
00377   //
00378   // t4 = gk * Y'Y * t2       <: R^(m)
00379   //
00380   // t4 += D*t2
00381   //
00382   // t5 = inv(R') * t4        <: R^(m)
00383   //
00384   // t5 += t3
00385   //
00386   // y += S*t5
00387   //
00388   // y += -gk*Y*t2
00389 
00390   // y = gk*x
00391   V_StV( y, gamma_k_, x );
00392 
00393   const size_type
00394     mb = m_bar_;  
00395   
00396   if( !mb )
00397     return; // No updates have been performed.
00398 
00399   const multi_vec_ptr_t
00400     S = this->S(),
00401     Y = this->Y();
00402 
00403   // Get workspace
00404 
00405   Workspace<value_type>    t1_ws(wss,2*mb);
00406   DVectorSlice                   t1(&t1_ws[0],t1_ws.size());
00407   Workspace<value_type>    t2_ws(wss,mb);
00408   DVectorSlice                   t2(&t2_ws[0],t2_ws.size());
00409   Workspace<value_type>    t3_ws(wss,mb);
00410   DVectorSlice                   t3(&t3_ws[0],t3_ws.size());
00411   Workspace<value_type>    t4_ws(wss,mb);
00412   DVectorSlice                   t4(&t4_ws[0],t4_ws.size());
00413   Workspace<value_type>    t5_ws(wss,mb);
00414   DVectorSlice                   t5(&t5_ws[0],t5_ws.size());
00415 
00416   VectorSpace::vec_mut_ptr_t
00417     t = S->space_rows().create_member();
00418 
00419   const DMatrixSliceTri
00420     &R = this->R();
00421 
00422   const DMatrixSliceSym
00423     &YTY = this->YTY();
00424 
00425   // t1 = [   S'*x  ]
00426   //      [ gk*Y'*x ]
00427   V_MtV( t.get(), *S, BLAS_Cpp::trans, x );
00428   t1(1,mb) = vde(*t)();
00429   V_StMtV( t.get(), gamma_k_, *Y, BLAS_Cpp::trans, x );
00430   t1(mb+1,2*mb) = vde(*t)();
00431 
00432   // t2 = inv(R) * t1(1:m)
00433   V_InvMtV( &t2, R, BLAS_Cpp::no_trans, t1(1,mb) );
00434 
00435   // t3 = - inv(R') * t1(m+1,2*m)
00436   V_mV( &t3, t1(mb+1,2*mb) );
00437   V_InvMtV( &t3, R, BLAS_Cpp::trans, t3 );
00438 
00439   // t4 = gk * Y'Y * t2
00440   V_StMtV( &t4, gamma_k_, YTY, BLAS_Cpp::no_trans, t2 );
00441 
00442   // t4 += D*t2
00443   Vp_DtV( &t4, t2 );
00444 
00445   // t5 = inv(R') * t4
00446   V_InvMtV( &t5, R, BLAS_Cpp::trans, t4 );
00447 
00448   // t5 += t3
00449   Vp_V( &t5, t3 );
00450 
00451   // y += S*t5
00452   (vdme(*t)() = t5);
00453   Vp_MtV( y, *S, BLAS_Cpp::no_trans, *t );
00454 
00455   // y += -gk*Y*t2
00456   (vdme(*t)() = t2);
00457   Vp_StMtV( y, -gamma_k_, *Y, BLAS_Cpp::no_trans, *t );
00458 
00459 }
00460 
00461 // Overridden from MatrixSymSecant
00462 
00463 void MatrixSymPosDefLBFGS::init_identity( const VectorSpace& space_diag, value_type alpha )
00464 {
00465   // Validate input
00466   TEST_FOR_EXCEPTION(
00467     alpha <= 0.0, std::invalid_argument
00468     ,"MatrixSymPosDefLBFGS::init_identity(n,alpha) : Error, "
00469     "alpha = " << alpha << " <= 0 is not allowed!" );
00470   
00471   // Set the vector space
00472   vec_spc_ = space_diag.clone();
00473   vec_spc_.get();
00474 
00475   // Set storage
00476   S_ = vec_spc_->create_members(m_);
00477   Y_ = vec_spc_->create_members(m_);
00478   assert(S_.get());
00479   assert(Y_.get());
00480   STY_.resize( m_, m_ );
00481   STSYTY_.resize( m_+1, m_+1 );
00482   STSYTY_.diag(0) = 0.0;
00483 
00484   gamma_k_ = 1.0/alpha;
00485 
00486   // Initialize counters
00487   m_bar_  = 0;
00488 
00489   n_ = vec_spc_->dim();        // initialized;
00490   original_is_updated_ = true; // This will never change for now
00491   inverse_is_updated_  = true; // This will never change for now
00492   num_secant_updates_  = 0;    // reset this to zero
00493 }
00494 
00495 void MatrixSymPosDefLBFGS::init_diagonal(const Vector& diag)
00496 {
00497   init_identity( diag.space(), diag.norm_inf() );
00498 }
00499 
00500 void MatrixSymPosDefLBFGS::secant_update(
00501   VectorMutable* s, VectorMutable* y, VectorMutable* Bs
00502   )
00503 {
00504   using AbstractLinAlgPack::BFGS_sTy_suff_p_d;
00505   using AbstractLinAlgPack::dot;
00506   using LinAlgOpPack::V_MtV;
00507   using Teuchos::Workspace;
00508   Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00509 
00510   assert_initialized();
00511 
00512   // Check skipping the BFGS update
00513   const value_type
00514     sTy       = dot(*s,*y);
00515   std::ostringstream omsg;
00516   if( !BFGS_sTy_suff_p_d(*s,*y,&sTy,&omsg,"MatrixSymPosDefLBFGS::secant_update(...)" ) ) {
00517     throw UpdateSkippedException( omsg.str() ); 
00518   }
00519 
00520   try {
00521 
00522   // Update counters
00523   if( m_bar_ == m_ ) {
00524     // We are at the end of the storage so remove the oldest stored update
00525     // and move updates to make room for the new update.  This has to be done for the
00526     // the matrix to behave properly
00527     {for( size_type k = 1; k <= m_-1; ++k ) {
00528       S_->col(k) = S_->col(k+1);                            // Shift S.col() to the left
00529       Y_->col(k) = Y_->col(k+1);                            // Shift Y.col() to the left
00530       STY_.col(k)(1,m_-1) = STY_.col(k+1)(2,m_);            // Move submatrix STY(2,m-1,2,m-1) up and left
00531       STSYTY_.col(k)(k+1,m_) = STSYTY_.col(k+1)(k+2,m_+1);  // Move triangular submatrix STS(2,m-1,2,m-1) up and left
00532       STSYTY_.col(k+1)(1,k) = STSYTY_.col(k+2)(2,k+1);      // Move triangular submatrix YTY(2,m-1,2,m-1) up and left
00533     }}
00534     // ToDo: Create an abstract interface, call it MultiVectorShiftVecs, to rearrange S and Y all at once.
00535     // This will be important for parallel performance.
00536   }
00537   else {
00538     m_bar_++;
00539   }
00540   // Set the update vectors
00541   *S_->col(m_bar_) = *s;
00542   *Y_->col(m_bar_) = *y;
00543 
00544   // /////////////////////////////////////////////////////////////////////////////////////
00545   // Update S'Y
00546   //
00547   // Update the row and column m_bar
00548   //
00549   //  S'Y = 
00550   //
00551   //  [ s(1)'*y(1)    ...   s(1)'*y(m_bar)    ...   s(1)'*y(m_bar)    ]
00552   //  [ .           .             .         ] row
00553   //  [ s(m_bar)'*y(1)  ...   s(m_bar)'*y(m_bar)  ...   s(m_bar)'*y(m_bar)  ] m_bar
00554   //  [ .           .             .         ]
00555   //  [ s(m_bar)'*y(1)  ...   s(m_bar)'*y(m_bar)  ...   s(m_bar)'*y(m_bar)  ]
00556   //
00557   //                col m_bar
00558   //
00559   // Therefore we set:
00560   //  (S'Y)(:,m_bar) =  S'*y(m_bar)
00561   //  (S'Y)(m_bar,:) =  s(m_bar)'*Y
00562 
00563   const multi_vec_ptr_t
00564     S = this->S(),
00565     Y = this->Y();
00566 
00567   VectorSpace::vec_mut_ptr_t
00568     t = S->space_rows().create_member();  // temporary workspace
00569 
00570   //  (S'Y)(:,m_bar) =  S'*y(m_bar)
00571   V_MtV( t.get(), *S, BLAS_Cpp::trans, *y );
00572   STY_.col(m_bar_)(1,m_bar_) = VectorDenseEncap(*t)();
00573 
00574   //  (S'Y)(m_bar,:)' =  Y'*s(m_bar)
00575   V_MtV( t.get(), *Y, BLAS_Cpp::trans, *s );
00576   STY_.row(m_bar_)(1,m_bar_) = VectorDenseEncap(*t)();
00577 
00578   // /////////////////////////////////////////////////////////////////
00579   // Update S'S
00580   //
00581   //  S'S = 
00582   //
00583   //  [ s(1)'*s(1)    ...   symmetric         symmetric     ]
00584   //  [ .           .             .         ] row
00585   //  [ s(m_bar)'*s(1)  ...   s(m_bar)'*s(m_bar)  ...   symmetric     ] m_bar
00586   //  [ .           .             .         ]
00587   //  [ s(m_bar)'*s(1)  ...   s(m_bar)'*s(m_bar)  ...   s(m_bar)'*s(m_bar)  ]
00588   //
00589   //                col m_bar
00590   //
00591   // Here we will update the lower triangular part of S'S.  To do this we
00592   // only need to compute:
00593   //    t = S'*s(m_bar) = { s(m_bar)' * [ s(1),..,s(m_bar),..,s(m_bar) ]  }'
00594   // then set the appropriate rows and columns of S'S.
00595 
00596   Workspace<value_type>   work_ws(wss,m_bar_);
00597   DVectorSlice                  work(&work_ws[0],work_ws.size());
00598 
00599   // work = S'*s(m_bar)
00600   V_MtV( t.get(), *S, BLAS_Cpp::trans, *s );
00601   work = VectorDenseEncap(*t)();
00602 
00603   // Set row elements
00604   STSYTY_.row(m_bar_+1)(1,m_bar_) = work;
00605   // Set column elements
00606   STSYTY_.col(m_bar_)(m_bar_+1,m_bar_+1) = work(m_bar_,m_bar_);
00607 
00608   // /////////////////////////////////////////////////////////////////////////////////////
00609   // Update Y'Y
00610   //
00611   // Update the row and column m_bar
00612   //
00613   //  Y'Y = 
00614   //
00615   //  [ y(1)'*y(1)    ...   y(1)'*y(m_bar)    ...   y(1)'*y(m_bar)    ]
00616   //  [ .           .             .         ] row
00617   //  [ symmetric   ...   y(m_bar)'*y(m_bar)  ...   y(m_bar)'*y(m_bar)  ] m_bar
00618   //  [ .           .             .         ]
00619   //  [ symmetric   ...   symmetric     ...   y(m_bar)'*y(m_bar)  ]
00620   //
00621   //                col m_bar
00622   //
00623   // Here we will update the upper triangular part of Y'Y.  To do this we
00624   // only need to compute:
00625   //    t = Y'*y(m_bar) = { y(m_bar)' * [ y(1),..,y(m_bar),..,y(m_bar) ]  }'
00626   // then set the appropriate rows and columns of Y'Y.
00627 
00628   // work = Y'*y(m_bar)
00629   V_MtV( t.get(), *Y, BLAS_Cpp::trans, *y );
00630   work = VectorDenseEncap(*t)();
00631 
00632   // Set row elements
00633   STSYTY_.col(m_bar_+1)(1,m_bar_) = work;
00634   // Set column elements
00635   STSYTY_.row(m_bar_)(m_bar_+1,m_bar_+1) = work(m_bar_,m_bar_);
00636 
00637   // /////////////////////////////
00638   // Update gamma_k
00639 
00640   // gamma_k = s'*y / y'*y
00641   if(auto_rescaling_)
00642     gamma_k_ = STY_(m_bar_,m_bar_) / STSYTY_(m_bar_,m_bar_+1);
00643 
00644   // We do not initially update Q unless we have to form a matrix-vector
00645   // product later.
00646   
00647   Q_updated_ = false;
00648   num_secant_updates_++;
00649 
00650   } //  end try
00651   catch(...) {
00652     // If we throw any exception the we should make the matrix uninitialized
00653     // so that we do not leave this object in an inconsistant state.
00654     n_ = 0;
00655     throw;
00656   }
00657 
00658 }
00659 
00660 // Private member functions
00661 
00662 void MatrixSymPosDefLBFGS::Vp_DtV( DVectorSlice* y, const DVectorSlice& x ) const
00663 {
00664   DenseLinAlgPack::Vp_MtV_assert_sizes(
00665     y->dim(), m_bar_, m_bar_, BLAS_Cpp::no_trans, x.dim() );
00666 
00667   DVectorSlice::const_iterator
00668     d_itr = STY_.diag(0).begin(),
00669     x_itr = x.begin();
00670   DVectorSlice::iterator
00671     y_itr = y->begin();
00672 
00673   while( y_itr != y->end() )
00674     *y_itr++ += (*d_itr++) * (*x_itr++);    
00675 }
00676 
00677 //
00678 // We need to update the factorizations to solve for:
00679 //
00680 // x = inv(Q) * y   =>   Q * x = y
00681 //
00682 //  [ (1/gk)*S'S   L  ] * [ x1 ] = [ y1 ]
00683 //  [      L'   -D  ]   [ x2 ]   [ y2 ]
00684 //
00685 // We will solve the above system using a schur complement:
00686 //
00687 // C = (1/gk)*S'S + L*inv(D)*L'
00688 //
00689 // According to the referenced paper, C is p.d. so:
00690 //
00691 // C = J*J'
00692 //
00693 // We then compute the solution as:
00694 //
00695 // x1 = inv(C) * ( y1 + L*inv(D)*y2 )
00696 // x2 = - inv(D) * ( y2 - L'*x1 )
00697 //
00698 // Therefore we will just update the factorization C = J*J'
00699 // where the factor J is stored in QJ_.
00700 //
00701 
00702 void MatrixSymPosDefLBFGS::update_Q() const
00703 {
00704   using DenseLinAlgPack::tri;
00705   using DenseLinAlgPack::tri_ele;
00706   using DenseLinAlgPack::Mp_StM;
00707 
00708   //
00709   // We need update the factorizations to solve for:
00710   //
00711   // x = inv(Q) * y
00712   //
00713   //  [ y1 ]  = [ (1/gk)*S'S   L  ] * [ x1 ]
00714   //  [ y2 ]    [      L'   -D  ]   [ x2 ]
00715   //
00716   // We will solve the above system using the schur complement:
00717   //
00718   // C = (1/gk)*S'S + L*inv(D)*L'
00719   //
00720   // According to the referenced paper, C is p.d. so:
00721   //
00722   // C = J*J'
00723   //
00724   // We then compute the solution as:
00725   //
00726   // x1 = inv(C) * ( y1 + L*inv(D)*y2 )
00727   // x2 = - inv(D) * ( y2 - L'*x1 )
00728   //
00729   // Therefore we will just update the factorization C = J*J'
00730   //
00731 
00732   // Form the upper triangular part of C which will become J
00733   // which we are using storage of QJ
00734 
00735   if( QJ_.rows() < m_ )
00736     QJ_.resize( m_, m_ );
00737 
00738   const size_type
00739     mb = m_bar_;
00740 
00741   DMatrixSlice
00742     C = QJ_(1,mb,1,mb);
00743 
00744   // C = L * inv(D) * L'
00745   //
00746   // Here L is a strictly lower triangular (zero diagonal) matrix where:
00747   //
00748   // L = [ 0  0 ]
00749   //     [ Lb 0 ]
00750   //
00751   // Lb is lower triangular (nonzero diagonal)
00752   //
00753   // Therefore we compute L*inv(D)*L' as:
00754   //
00755   // C = [ 0  0 ] * [ Db  0  ] * [ 0  Lb' ]
00756   //     [ Lb 0 ]   [ 0   db ]   [ 0   0  ]
00757   //
00758   //   = [ 0  0  ] = [ 0      0     ]
00759   //     [ 0  Cb ]   [ 0  Lb*Db*Lb' ]
00760   //
00761   // We need to compute the upper triangular part of Cb.
00762 
00763   C.row(1) = 0.0;
00764   if( mb > 1 )
00765     comp_Cb( STY_(2,mb,1,mb-1), STY_.diag(0)(1,mb-1), &C(2,mb,2,mb) );
00766 
00767   // C += (1/gk)*S'S
00768 
00769   const DMatrixSliceSym &STS = this->STS();
00770   Mp_StM( &C, (1/gamma_k_), tri( STS.gms(), STS.uplo(), BLAS_Cpp::nonunit )
00771     , BLAS_Cpp::trans );
00772 
00773   // Now perform a cholesky factorization of C
00774   // After this factorization the upper triangular part of QJ
00775   // (through C) will contain the cholesky factor.
00776 
00777   DMatrixSliceTriEle C_upper = tri_ele( C, BLAS_Cpp::upper );
00778   try {
00779     DenseLinAlgLAPack::potrf( &C_upper );
00780   }
00781   catch( const DenseLinAlgLAPack::FactorizationException &fe ) {
00782     TEST_FOR_EXCEPTION(
00783       true, UpdateFailedException
00784       ,"Error, the factorization of Q which should be s.p.d. failed with"
00785       " the error message: {" << fe.what() << "}";
00786       );
00787   }
00788 
00789   Q_updated_ = true;
00790 }
00791 
00792 void MatrixSymPosDefLBFGS::V_invQtV( DVectorSlice* x, const DVectorSlice& y ) const
00793 {
00794   using DenseLinAlgPack::sym;
00795   using DenseLinAlgPack::tri;
00796   using DenseLinAlgPack::Vp_StV;
00797   using DenseLinAlgPack::V_InvMtV;
00798 
00799   using LinAlgOpPack::Vp_V;
00800   using LinAlgOpPack::V_MtV;
00801 
00802 
00803   // Solve the system 
00804   //
00805   // Q * x = y
00806   //
00807   // Using the schur complement factorization as described above.
00808 
00809   const size_type
00810     mb = m_bar_;
00811 
00812   if(!Q_updated_) {
00813     update_Q();
00814   }
00815 
00816   DVectorSlice
00817     x1 = (*x)(1,mb),
00818     x2 = (*x)(mb+1,2*mb);
00819 
00820   const DVectorSlice
00821     y1 = y(1,mb),
00822     y2 = y(mb+1,2*mb);
00823 
00824   // //////////////////////////////////////
00825   // x1 = inv(C) * ( y1 + L*inv(D)*y2 )
00826   //     = inv(J'*J) * r
00827   //     = inv(J) * inv(J') * r
00828 
00829   { // x1 = inv(D) * y2
00830     DVectorSlice::const_iterator
00831       d_itr = STY_.diag(0).begin(),
00832       y2_itr = y2.begin();
00833     DVectorSlice::iterator
00834       x1_itr = x1.begin();
00835     while( x1_itr != x1.end() )
00836       *x1_itr++ = *y2_itr++ / *d_itr++;
00837   }
00838 
00839   // x1 = L * x1
00840   //
00841   //    = [ 0  0 ] * [ x1(1:mb-1) ]
00842   //      [ Lb 0 ]   [ x1(mb)     ]
00843   //
00844   //    = [ 0             ]
00845   //      [ Lb*x1(1:mb-1) ]
00846   //
00847   if( mb > 1 ) {
00848     // x1(2,mb) = x1(1,mb-1) ( copy from mb-1 to mb then mb-2 to mb-1
00849     // etc. so that we don't overwrite the elements we need to copy ).
00850     DVectorSlice
00851       x1a = x1(1,mb-1),
00852       x1b = x1(2,mb);
00853     std::copy( x1a.rbegin(), x1a.rend(), x1b.rbegin() );
00854     V_MtV( &x1b, Lb(), BLAS_Cpp::no_trans, x1b );
00855   }
00856   x1(1) = 0.0;
00857 
00858   // r = x1 += y1
00859   Vp_V( &x1, y1 );
00860 
00861   // x1 = inv(J') * r
00862   const DMatrixSliceTri J = tri( QJ_(1,mb,1,mb), BLAS_Cpp::upper, BLAS_Cpp::nonunit );
00863   V_InvMtV( &x1, J, BLAS_Cpp::trans, x1 );
00864 
00865   // x1 = inv(J) * x1
00866   V_InvMtV( &x1, J, BLAS_Cpp::no_trans, x1 );
00867 
00868   // /////////////////////////////////////
00869   // x2 = inv(D) * ( - y2 + L'*x1 )
00870 
00871   // x2 = L'*x1
00872   //
00873   //    = [ 0  Lb' ] * [ x1(1)    ]
00874   //      [ 0  0   ]   [ x1(2,mb) ]
00875   //
00876   //    = [ Lb'*x1(2,mb) ]
00877   //      [      0       ]
00878   //
00879   if( mb > 1 ) {
00880     V_MtV( &x2(1,mb-1), Lb(), BLAS_Cpp::trans, x1(2,mb) );
00881   }
00882   x2(mb) = 0.0;
00883 
00884   // x2 += -y2
00885   Vp_StV( &x2, -1.0, y2 );
00886 
00887   // x2 = inv(D) * x2
00888   {
00889     DVectorSlice::const_iterator
00890       d_itr = STY_.diag(0).begin();
00891     DVectorSlice::iterator
00892       x2_itr = x2.begin();
00893     while( x2_itr != x2.end() )
00894       *x2_itr++ /= *d_itr++;
00895   }
00896 }
00897 
00898 void MatrixSymPosDefLBFGS::assert_initialized() const
00899 {
00900   if(!n_)
00901     throw std::logic_error( "MatrixSymPosDefLBFGS::assert_initialized() : "
00902       "Error, matrix not initialized" );
00903 }
00904 
00905 } // end namespace ConstrainedOptPack 
00906 
00907 namespace {
00908 
00909 void comp_Cb(
00910   const DMatrixSlice& Lb, const DVectorSlice& Db_diag
00911   ,DMatrixSlice* Cb
00912   )
00913 {
00914   // Lb * inv(Db) * Lb =
00915   //
00916   // [ l(1,1)           ]   [ dd(1)         ]   [ l(1,1)  l(2,1)  ... l(p,1)  ]
00917   // [ l(2,1) l(2,2)        ]   [   dd(2)     ]   [     l(2,2)  ... l(p,2)  ]
00918   // [ .    .        .      ] * [     .     ] * [         . .   ]
00919   // [ l(p,1) l(p,2)  ... l(p,p)  ]   [       dd(p) ]   [           l(p,p)  ]
00920   //
00921   //
00922   // [ l(1,1)*dd(1)*l(1,1)  l(1,1)*dd(1)*l(2,1)             ...   l(1,1)*dd(1)*l(p,1)             ]
00923   // [ symmetric        l(2,1)*dd(1)*l(2,1) + l(2,2)*dd(2)*l(2,2) ...   l(2,1)*dd(1)*l(p,1) + l(2,2)*dd(2)*l(p,2) ]   
00924   // [ .            .                     ...
00925   // [ symmetric        symmetric                 ...   sum( l(p,i)*dd(i)*l(p,i), i=1,..,p )    ]
00926   //
00927   // Therefore we can express the upper triangular elemetns of Cb as:
00928   //
00929   // Cb(i,j) = sum( l(i,k)*dd(k)*l(j,k), k = 1,..,i )
00930 
00931   typedef DenseLinAlgPack::size_type size_type;
00932   typedef DenseLinAlgPack::value_type value_type;
00933 
00934   assert( Lb.rows() == Cb->rows() && Cb->rows() == Db_diag.dim() ); // only a local error!
00935 
00936   const size_type p = Db_diag.dim();
00937 
00938   for( size_type i = 1; i <= p; ++i ) {
00939     for( size_type j = i; j <= p; ++j ) {
00940       value_type &c = (*Cb)(i,j) = 0.0;
00941       for( size_type k = 1; k <= i; ++k ) {
00942         c += Lb(i,k) * Lb(j,k) / Db_diag(k);
00943       }
00944     }
00945   }
00946 
00947   // ToDo: Make the above operation more efficent if needed! (i.e. write
00948   // it in fortran or something?).
00949 }
00950 
00951 } // end namespace

Generated on Thu Sep 18 12:34:15 2008 for ConstrainedOptPack: C++ Tools for Constrained (and Unconstrained) Optimization by doxygen 1.3.9.1