vectorterm.hh

Go to the documentation of this file.
00001 
00002 /*************************************************************************
00003 
00004 Copyright Rice University, 2004.
00005 All rights reserved.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, provided that the above copyright notice(s) and this
00013 permission notice appear in all copies of the Software and that both the
00014 above copyright notice(s) and this permission notice appear in supporting
00015 documentation.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00020 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00021 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00022 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00023 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00024 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00025 THIS SOFTWARE.
00026 
00027 Except as contained in this notice, the name of a copyright holder shall
00028 not be used in advertising or otherwise to promote the sale, use or other
00029 dealings in this Software without prior written authorization of the
00030 copyright holder.
00031 
00032 **************************************************************************/
00033 
00034 #ifndef __RVLALG_VECTOR_TERMINATOR
00035 #define __RVLALG_VECTOR_TERMINATOR
00036 
00037 #include "functional.hh"
00038 #include "ls.hh"
00039 #include "alg.hh"
00040 
00047 namespace RVLAlg {
00048   using namespace RVL;
00055 template <class Scalar>
00056 class UnaryThresholdTerminator: public Terminator {
00057   
00058 public:
00059   UnaryThresholdTerminator( FunctionObjectScalarRedn<Scalar>& tf, 
00060                 Vector<Scalar> & tx,
00061                 Scalar ttol) 
00062     : f(tf), x(tx), tol(ttol) {}
00063 
00064   virtual bool query() { 
00065     x.eval(f);
00066     Scalar temp = f.getValue();
00067     return (temp < tol);
00068   }
00069 
00070 protected:
00071   FunctionObjectScalarRedn<Scalar> & f;
00072   Vector<Scalar> & x;
00073   Scalar tol;
00074   
00075 };
00076 
00083 template <class Scalar>
00084 class BinaryThresholdTerminator: public Terminator {
00085   
00086 public:
00087   BinaryThresholdTerminator( FunctionObjectScalarRedn<Scalar>& tf, 
00088                  Vector<Scalar> & tx,
00089                  Vector<Scalar> & ty,
00090                  Scalar ttol) 
00091     : f(tf), x(tx), y(ty), tol(ttol) {}
00092 
00093   virtual bool query() { 
00094     Scalar temp;
00095     x.eval(f, y);
00096     temp = f.getValue();
00097     return (temp < tol);
00098   }
00099 
00100 protected:
00101   FunctionObjectScalarRedn<Scalar> & f;
00102   Vector<Scalar> & x;
00103   Vector<Scalar> & y;
00104   Scalar tol;  
00105 };
00106 
00113 template <class Scalar>
00114 class TernaryThresholdTerminator: public Terminator {
00115   
00116 public:
00117   TernaryThresholdTerminator( FunctionObjectScalarRedn<Scalar>& tf, 
00118                   Vector<Scalar> & tx,
00119                   Vector<Scalar> & ty,
00120                   Vector<Scalar> & tz,
00121                   Scalar ttol) 
00122     : f(tf), x(tx), y(ty), z(tz), tol(ttol) {}
00123 
00124   virtual bool query() { 
00125     Scalar temp;
00126     x.eval(f,y,z);
00127     temp = f.getValue();
00128     return (temp < tol);
00129   }
00130 
00131 protected:
00132   FunctionObjectScalarRedn<Scalar> & f;
00133   Vector<Scalar> & x;
00134   Vector<Scalar> & y;
00135   Vector<Scalar> & z;
00136   Scalar tol;
00137 };
00138 
00144 template< class Scalar >
00145 class NormThresholdTerminator: public Terminator {
00146 public:
00147   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00148 
00149   NormThresholdTerminator( const Vector<Scalar> & tx, NormRetType ttol): x(tx), tol(ttol) {}
00150 
00151   virtual bool query() {
00152     return (x.norm() < tol);
00153   }
00154 
00155 protected:
00156   const Vector<Scalar> & x;
00157   NormRetType tol;
00158 };
00159 
00167 template< class Scalar >
00168 class Norm2ThresholdTerminator: public Terminator {
00169 public:
00170   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00171 
00172   Norm2ThresholdTerminator( Vector<Scalar> & tx, NormRetType ttol): x(tx), tol(ttol) {}
00173 
00174   virtual bool query() {
00175     return (x.norm2() < tol);
00176   }
00177 
00178 protected:
00179   Vector<Scalar> & x;
00180   NormRetType tol;
00181 };
00182 
00189 template< class Scalar >
00190 class DiffThresholdTerminator: public Terminator {
00191 public:
00192   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00193 
00194   DiffThresholdTerminator( Vector<Scalar> & tx, Vector<Scalar> & ty, NormRetType ttol)
00195     : x(tx), y(ty), tol(ttol) {
00196     if( ! x.inSameSpace(y) ) {
00197       RVLException e; e << "Error in DiffThresholdTerminator constructor: Vectors not in same space.";
00198       throw e;
00199     }
00200   }
00201 
00202   virtual bool query() {
00203     Vector<Scalar> temp(x.getSpace());
00204     temp.lincomb(1.0, x, -1.0, y);
00205     return (temp.norm() < tol);
00206   }
00207 
00208 protected:
00209   Vector<Scalar> & x;
00210   Vector<Scalar> & y;
00211   NormRetType tol;
00212 };
00213 
00214 
00223 template< class Scalar >
00224 class Diff2ThresholdTerminator: public Terminator {
00225 public:
00226   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00227 
00228   Diff2ThresholdTerminator( Vector<Scalar> & tx, Vector<Scalar> & ty, NormRetType ttol)
00229     : x(tx), y(ty), tol(ttol) {
00230     if( ! x.inSameSpace(y) ) {
00231       RVLException e; e << "Error in DiffThresholdTerminator constructor: Vectors not in same space.";
00232       throw e;
00233     }
00234   }
00235 
00236   virtual bool query() {
00237     Vector<Scalar> temp(x.getSpace());
00238     temp.lincomb(1.0, x, -1.0, y);
00239     return (temp.norm2() < tol);
00240   }
00241 
00242 protected:
00243   Vector<Scalar> & x;
00244   Vector<Scalar> & y;
00245   NormRetType tol;
00246 };
00247 
00253 template< class Scalar >
00254 class IPThresholdTerminator: public Terminator {
00255 public:
00256   IPThresholdTerminator( Vector<Scalar> & tx, Vector<Scalar> & ty, Scalar ttol)
00257     : x(tx), y(ty), tol(ttol) {
00258     if( ! x.inSameSpace(y) ) {
00259     RVLException e; e << "Error in DiffThresholdTerminator constructor: Vectors not in same space.";
00260       throw e;
00261     }
00262   }
00263 
00264   virtual bool query() {
00265     Scalar temp = x.inner(y);
00266     return (temp < tol);
00267   }
00268 
00269 protected:
00270   Vector<Scalar> & x;
00271   Vector<Scalar> & y;
00272   Scalar tol;
00273 };
00274 
00280 template< class Scalar >
00281 class AbsIPThresholdTerminator: public Terminator {
00282 public:
00283   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00284 
00285   AbsIPThresholdTerminator( Vector<Scalar> & tx, Vector<Scalar> & ty, NormRetType ttol)
00286     : x(tx), y(ty), tol(ttol) {
00287     if( ! x.inSameSpace(y) ) {
00288       RVLException e; e << "Error in DiffThresholdTerminator constructor: Vectors not in same space.";
00289       throw e;
00290     }
00291   }
00292   ~AbsIPThresholdTerminator(){}
00293 
00294   virtual bool query() {
00295     Scalar temp = x.inner(y);
00296     return (fabs(temp) < tol);
00297   }
00298 
00299 protected:
00300   Vector<Scalar> & x;
00301   Vector<Scalar> & y;
00302   NormRetType tol;
00303 };
00304 
00312 template <class Scalar> 
00313 class NormGradientTerminator: public Terminator {
00314 public:
00315   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;  
00316 
00324   NormGradientTerminator( Vector<Scalar> & x, 
00325               Functional<Scalar> & f, 
00326               NormRetType tol) 
00327     : tol_(tol*tol), fx_(f,x)
00328   {}
00329 
00331   bool query() {
00332     return( fx_.getGradient().norm2() <= tol_ );
00333   }
00334 
00335 protected:
00336   NormRetType tol_; 
00337   FunctionalEvaluation<Scalar>  fx_;
00338 };
00339 
00340 
00356 template< class Scalar >
00357 class DiffBallProjTerminator: public Terminator {
00358 public:
00359   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00360 
00361   DiffBallProjTerminator( Vector<Scalar> const & tx, Vector<Scalar> & ty, NormRetType _maxstep, ostream & _str = cout)
00362     : x(tx), y(ty), maxstep(_maxstep), str(_str), res(false) {
00363     if( ! x.inSameSpace(y) ) {
00364       RVLException e; e << "Error in DiffBallProjTerminator constructor: Vectors not in same space.";
00365       throw e;
00366     }
00367   }
00368 
00369   virtual bool query() {
00370     Vector<Scalar> temp(x.getSpace());
00371     temp.copy(y);
00372     temp.linComb(-1.0,x);
00373     NormRetType tn = temp.norm();
00374     res = (tn > maxstep);
00375     if (res) {
00376       NormRetType scfac=ScalarFieldTraits<NormRetType>::One();
00377       if (ProtectedDivision(maxstep,tn,scfac)) {
00378     RVLException e; 
00379     e<<"Error: zerodivide in DiffBalProjTerminator::query\n";
00380     e<<"maxstep = "<<maxstep<<"\n";
00381     e<<"temp.norm() = "<<tn<<"\n";
00382     throw e;
00383       }
00384       
00385       temp.scale((ScalarFieldTraits<NormRetType>::One() - 100*numeric_limits<NormRetType>::epsilon())*scfac);
00386       y.copy(x);
00387       y.linComb(1.0,temp);
00388     }
00389     return res;
00390   }
00391 
00393   bool static_query() { return res; }
00394 
00395 protected:
00396   Vector<Scalar> const & x;
00397   Vector<Scalar> & y;
00398   NormRetType maxstep;
00399   bool res;
00400   ostream & str;
00401 };
00402 
00420 template< class Scalar >
00421 class BallProjTerminator: public Terminator {
00422 public:
00423   typedef typename ScalarFieldTraits<Scalar>::AbsType NormRetType;
00424 
00425   BallProjTerminator(Vector<Scalar> & ty, NormRetType _maxstep,
00426              ostream & _str = cout)
00427     : y(ty), maxstep(_maxstep), queryres(false), str(_str) {}
00428 
00429   virtual bool query() {
00430     if (queryres) return true;
00431     NormRetType tn = y.norm();
00432     bool res = (tn > maxstep);
00433     if (res) {
00434       NormRetType scfac=ScalarFieldTraits<NormRetType>::One();
00435       if (ProtectedDivision(maxstep,tn,scfac)) {
00436     RVLException e; 
00437     e<<"Error: zerodivide in DiffBalProjTerminator::query\n";
00438     e<<"maxstep = "<<maxstep<<"\n";
00439     e<<"temp.norm() = "<<tn<<"\n";
00440     throw e;
00441       }
00442       y.scale((ScalarFieldTraits<NormRetType>::One() - 100*numeric_limits<NormRetType>::epsilon())*scfac);
00443       str<<"RVLAlg::BallProjTerminator::query: trust region truncation applied\n";
00444       str<<"  untruncated solution norm = "<<tn<<" trust radius = "<<maxstep<<"\n";      
00445     }
00446     queryres=res;
00447     return res;
00448   }
00449 
00450 protected:
00451   Vector<Scalar> & y;
00452   NormRetType maxstep;
00453   mutable bool queryres;
00454   ostream & str;
00455 };
00456 
00457 
00458 }
00459 
00460 #endif

Generated on 5 Jan 2017 for RvlAlg by  doxygen 1.4.7