TSFLinearCombinationDecl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
00005 //                 Copyright (2004) 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 Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 #ifndef TSFLINEARCOMBINATIONDECL_HPP
00030 #define TSFLINEARCOMBINATIONDECL_HPP
00031 
00032 #include "SundanceDefs.hpp"
00033 #include "TSFVectorDecl.hpp"
00034 #include "TSFLinearOperatorDecl.hpp"
00035 #include "Teuchos_ScalarTraits.hpp"
00036 
00037 
00038 
00039 namespace TSFExtendedOps
00040 {
00041 using TSFExtended::Vector;
00042 using TSFExtended::VectorSpace;
00043 using TSFExtended::LinearOperator;
00044 /** 
00045  *
00046  */
00047 template <class Scalar> 
00048 class ConvertibleToVector
00049 {
00050 public:
00051   /** */
00052   virtual ~ConvertibleToVector(){;}
00053 
00054   /** */
00055   virtual Vector<Scalar> eval() const = 0 ;
00056 
00057   /** */
00058   VectorSpace<Scalar> space() const {return eval().space();}
00059 
00060   /** Return the dimension of the vector  */
00061   int dim() const {return eval().dim();} 
00062 
00063   /** 
00064    * Create a new vector that is a copy of this vector 
00065    */
00066   Vector<Scalar> copy() const {return eval().copy();}
00067 
00068   /** 
00069    * Element-by-element product (Matlab dot-star operator)
00070    */
00071   Vector<Scalar> dotStar(const Vector<Scalar>& other) const 
00072     {return eval().dotStar(other);}
00073 
00074   /** 
00075    * Element-by-element division (Matlab dot-slash operator)
00076    */
00077   Vector<Scalar> dotSlash(const Vector<Scalar>& other) const 
00078     {return eval().dotSlash(other);}
00079 
00080   /** 
00081    * Return element-by-element reciprocal as a new vector
00082    */
00083   Vector<Scalar> reciprocal() const {return reciprocal();}
00084 
00085   /** 
00086    * Return element-by-element absolute value as a new vector
00087    */
00088   Vector<Scalar> abs() const {return abs();} 
00089 
00090   /** */
00091   Scalar norm2() const {return eval().norm2();}
00092 
00093   /** */
00094   Scalar norm1() const {return eval().norm1();}
00095 
00096   /** */
00097   Scalar normInf() const {return eval().normInf();}
00098 
00099   /** */
00100   Scalar max() const {return eval().max();}
00101 
00102   /** */
00103   Scalar min() const {return eval().min();}
00104 
00105   /** Return the min element and the corresponding index */
00106   Scalar min(int& index)const {return eval().min(index);}
00107 
00108   /** Return the max element and the corresponding index */
00109   Scalar max(int& index)const {return eval().max(index);}
00110 };
00111 
00112 /** 
00113  * Class OpTimesLC holds an operator times something convertible to a vector
00114  */
00115 template <class Scalar, class Node>
00116 class OpTimesLC : public ConvertibleToVector<Scalar>
00117 {
00118 public:
00119 
00120   /** */
00121   virtual ~OpTimesLC(){;}
00122 
00123   /** */
00124   OpTimesLC(const Scalar& alpha, const Node& x);
00125 
00126   /** */
00127   OpTimesLC(const Scalar& alpha,
00128     const TSFExtended::LinearOperator<Scalar>& op, 
00129     const Node& x);
00130 
00131   /** 
00132    * Evaluate the term into the argument vector, overwriting 
00133    * the previous value of the argument. */
00134   void evalInto(TSFExtended::Vector<Scalar>& result) const ;
00135 
00136   /** Add the term into the argument vector */
00137   void addInto(TSFExtended::Vector<Scalar>& result, 
00138     LCSign sign = LCAdd) const ;
00139 
00140   /** Evaluate the term and return its value */
00141   virtual TSFExtended::Vector<Scalar> eval() const ;
00142 
00143   /** Determine whether this term contains the given vector */
00144   bool containsVector(const Thyra::VectorBase<Scalar>* vec) const ;
00145 
00146   /** */
00147   const LinearOperator<Scalar>& op() const {return op_;}
00148 
00149   /** */
00150   const Scalar& alpha() const {return alpha_;}
00151 
00152   /** */
00153   const Node& node() const {return x_;}
00154 
00155   /** */
00156   Scalar norm2() const {return eval().norm2();}
00157 
00158   /** */
00159   Scalar normInf() const {return eval().normInf();}
00160     
00161 private:
00162   Scalar alpha_;
00163     
00164   TSFExtended::LinearOperator<Scalar> op_;
00165 
00166   Node x_;
00167 
00168   /** */
00169   static Scalar one() {return Teuchos::ScalarTraits<Scalar>::one();}
00170 
00171   /** */
00172   static Scalar zero() {return Teuchos::ScalarTraits<Scalar>::zero();}
00173 };
00174 
00175 
00176 /**
00177  * Class LC2 is a 2-term linear combination
00178  */
00179 template <class Scalar, class Node1, class Node2>
00180 class LC2  : public ConvertibleToVector<Scalar>
00181 {
00182 public:
00183   /** */
00184   virtual ~LC2(){;}
00185 
00186   /** */
00187   LC2(const Node1& x1, const Node2& x2, LCSign sign = LCAdd);
00188 
00189   /** */
00190   void evalInto(TSFExtended::Vector<Scalar>& result) const ;
00191 
00192   /** */
00193   void addInto(TSFExtended::Vector<Scalar>& result, 
00194     LCSign sign = LCAdd) const ;
00195 
00196   /** */
00197   virtual TSFExtended::Vector<Scalar> eval() const ;
00198 
00199   /** */
00200   bool containsVector(const Thyra::VectorBase<Scalar>* vec) const ;
00201 
00202     
00203 private:
00204   Node1 x1_;
00205 
00206   Node2 x2_;
00207 
00208   LCSign sign_;
00209 
00210   /** */
00211   static Scalar one() {return Teuchos::ScalarTraits<Scalar>::one();}
00212 
00213   /** */
00214   static Scalar zero() {return Teuchos::ScalarTraits<Scalar>::zero();}
00215 };
00216 
00217 
00218 }
00219 
00220 namespace TSFExtended
00221 {
00222 using TSFExtendedOps::OpTimesLC;
00223 using TSFExtendedOps::LC2;
00224 using TSFExtendedOps::LCAdd;
00225 using TSFExtendedOps::LCSubtract;
00226 
00227 /* ------------------------ global methods ----------------------- */
00228 
00229 
00230 /*======================================================================
00231  *
00232  *    scalar times vector
00233  *
00234  *======================================================================*/
00235 
00236 /* scalar * vec */
00237 template <class Scalar> 
00238 OpTimesLC<Scalar, Vector<Scalar> > operator*(const Scalar& alpha, 
00239   const Vector<Scalar>& x);
00240 
00241 /* vec * scalar */
00242 template <class Scalar> 
00243 OpTimesLC<Scalar, Vector<Scalar> > operator*(const Vector<Scalar>& x, 
00244   const Scalar& alpha);
00245 
00246 
00247 /*======================================================================
00248  *
00249  *    scalar times OpTimesLC
00250  *
00251  *======================================================================*/
00252 
00253 /* scalar * OpTimesLC */
00254 template <class Scalar, class Node> 
00255 OpTimesLC<Scalar, Node> 
00256 operator*(const Scalar& alpha, 
00257   const OpTimesLC<Scalar, Node>& x);
00258 
00259 /* OpTimesLC * scalar */
00260 template <class Scalar, class Node> 
00261 OpTimesLC<Scalar, Node> 
00262 operator*(const OpTimesLC<Scalar, Node>& x, const Scalar& alpha);
00263 
00264 
00265 /*======================================================================
00266  *
00267  *    scalar times LC2
00268  *
00269  *======================================================================*/
00270 
00271 /* scalar * LC2 */
00272 template <class Scalar, class Node1, class Node2> 
00273 OpTimesLC<Scalar, LC2<Scalar, Node1, Node2> > 
00274 operator*(const Scalar& alpha, 
00275   const LC2<Scalar, Node1, Node2>& x);
00276 
00277 /* LC2 * scalar */
00278 template <class Scalar, class Node1, class Node2> 
00279 OpTimesLC<Scalar, LC2<Scalar, Node1, Node2> > 
00280 operator*(const LC2<Scalar, Node1, Node2>& x, const Scalar& alpha);
00281   
00282 
00283 
00284 /*======================================================================
00285  *
00286  *    operator times [vectors, OpTimesLC, LC2]
00287  *
00288  *======================================================================*/
00289 
00290 /* op * vec */
00291 template <class Scalar>
00292 OpTimesLC<Scalar, Vector<Scalar> > 
00293 operator*(const LinearOperator<Scalar>& op, 
00294   const Vector<Scalar>& x);
00295 
00296 
00297 /* op * OpTimesLC */
00298 template <class Scalar, class Node> 
00299 OpTimesLC<Scalar, Node> 
00300 operator*(const LinearOperator<Scalar>& op, 
00301   const OpTimesLC<Scalar, Node>& x);
00302 
00303 
00304 /* op * LC2 */
00305 template <class Scalar, class Node1, class Node2> 
00306 OpTimesLC<Scalar, LC2<Scalar, Node1, Node2> > 
00307 operator*(const LinearOperator<Scalar>& op, 
00308   const LC2<Scalar, Node1, Node2>& x);
00309 
00310 
00311 /*======================================================================
00312  *
00313  *    add/subtract vector, vector
00314  *
00315  *======================================================================*/
00316   
00317 /* vec + vec */
00318 template <class Scalar> 
00319 LC2<Scalar, Vector<Scalar>, Vector<Scalar> >
00320 operator+(const Vector<Scalar>& x1, 
00321   const Vector<Scalar>& x2);
00322   
00323 /* vec - vec */
00324 template <class Scalar> 
00325 LC2<Scalar, Vector<Scalar>, Vector<Scalar> >
00326 operator-(const Vector<Scalar>& x1, 
00327   const Vector<Scalar>& x2);
00328 
00329 
00330 /*======================================================================
00331  *
00332  *    add/subtract vector, OpTimesLC
00333  *
00334  *======================================================================*/
00335 
00336 
00337 /* vec + OpTimesLC */
00338 template <class Scalar, class Node> 
00339 LC2<Scalar, Vector<Scalar>, OpTimesLC<Scalar, Node> >
00340 operator+(const Vector<Scalar>& x1, 
00341   const OpTimesLC<Scalar, Node>& x2);
00342 
00343 
00344 /* vec - OpTimesLC */
00345 template <class Scalar, class Node> 
00346 LC2<Scalar, Vector<Scalar>, OpTimesLC<Scalar, Node> >
00347 operator-(const Vector<Scalar>& x1, 
00348   const OpTimesLC<Scalar, Node>& x2);
00349 
00350 
00351 /* OpTimesLC + vec */
00352 template <class Scalar, class Node> 
00353 LC2<Scalar, OpTimesLC<Scalar, Node>, Vector<Scalar> >
00354 operator+(const OpTimesLC<Scalar, Node>& x1, 
00355   const Vector<Scalar>& x2);
00356   
00357 /* OpTimesLC - vec */
00358 template <class Scalar, class Node> 
00359 LC2<Scalar, OpTimesLC<Scalar, Node>, Vector<Scalar> >
00360 operator-(const OpTimesLC<Scalar, Node>& x1, 
00361   const Vector<Scalar>& x2);
00362 
00363 
00364   
00365 /*======================================================================
00366  *
00367  *    add/subtract OpTimesLC, OpTimesLC
00368  *
00369  *======================================================================*/
00370   
00371 /* OpTimesLC + OpTimesLC */
00372 template <class Scalar, class Node1, class Node2> 
00373 LC2<Scalar, OpTimesLC<Scalar, Node1>, OpTimesLC<Scalar, Node2> >
00374 operator+(const OpTimesLC<Scalar, Node1>& x1, 
00375   const OpTimesLC<Scalar, Node2>& x2);
00376 
00377   
00378 /* OpTimesLC - OpTimesLC */
00379 template <class Scalar, class Node1, class Node2> 
00380 LC2<Scalar, OpTimesLC<Scalar, Node1>, OpTimesLC<Scalar, Node2> >
00381 operator-(const OpTimesLC<Scalar, Node1>& x1, 
00382   const OpTimesLC<Scalar, Node2>& x2);
00383   
00384 
00385   
00386 /*======================================================================
00387  *
00388  *    add/subtract Vector, LC2
00389  *
00390  *======================================================================*/
00391 
00392   
00393 /* vec + LC2 */
00394 template <class Scalar, class Node1, class Node2> 
00395 LC2<Scalar, Vector<Scalar>, LC2<Scalar, Node1, Node2> >
00396 operator+(const Vector<Scalar>& x1, 
00397   const LC2<Scalar, Node1, Node2>& x2);
00398 
00399 
00400 /* vec - LC2 */
00401 template <class Scalar, class Node1, class Node2> 
00402 LC2<Scalar, Vector<Scalar>, LC2<Scalar, Node1, Node2> >
00403 operator-(const Vector<Scalar>& x1, 
00404   const LC2<Scalar, Node1, Node2>& x2);
00405 
00406 
00407 /* LC2 + vec */
00408 template <class Scalar, class Node1, class Node2> 
00409 LC2<Scalar, LC2<Scalar, Node1, Node2>, Vector<Scalar> >
00410 operator+(const LC2<Scalar, Node1, Node2>& x1, 
00411   const Vector<Scalar>& x2);
00412 
00413 
00414 /* LC2 - vec */
00415 template <class Scalar, class Node1, class Node2> 
00416 LC2<Scalar, LC2<Scalar, Node1, Node2>, Vector<Scalar> >
00417 operator-(const LC2<Scalar, Node1, Node2>& x1, 
00418   const Vector<Scalar>& x2);
00419 
00420 
00421 
00422 /*======================================================================
00423  *
00424  *    add/subtract OpTimesLC, LC2
00425  *
00426  *======================================================================*/
00427 
00428 
00429 /* OpTimesLC + LC2 */
00430 template <class Scalar, class Node0, class Node1, class Node2> 
00431 LC2<Scalar, OpTimesLC<Scalar, Node0>, LC2<Scalar, Node1, Node2> > 
00432 operator+(const OpTimesLC<Scalar, Node0>& x1, 
00433   const LC2<Scalar, Node1, Node2>& x2);
00434 
00435 /* OpTimesLC - LC2 */
00436 template <class Scalar, class Node0, class Node1, class Node2> 
00437 LC2<Scalar, OpTimesLC<Scalar, Node0>, LC2<Scalar, Node1, Node2> > 
00438 operator-(const OpTimesLC<Scalar, Node0>& x1, 
00439   const LC2<Scalar, Node1, Node2>& x2);
00440 
00441 
00442 
00443 /* LC2 + OpTimesLC */
00444 template <class Scalar, class Node1, class Node2, class Node3> 
00445 LC2<Scalar, LC2<Scalar, Node1, Node2>, OpTimesLC<Scalar, Node3> > 
00446 operator+(const LC2<Scalar, Node1, Node2>& x1, 
00447   const OpTimesLC<Scalar, Node3>& x2);
00448 
00449 
00450 /* LC2 - OpTimesLC */
00451 template <class Scalar, class Node1, class Node2, class Node3> 
00452 LC2<Scalar, LC2<Scalar, Node1, Node2>, OpTimesLC<Scalar, Node3> > 
00453 operator-(const LC2<Scalar, Node1, Node2>& x1, 
00454   const OpTimesLC<Scalar, Node3>& x2);
00455 
00456 
00457 
00458 /*======================================================================
00459  *
00460  *    add/subtract LC2, LC2
00461  *
00462  *======================================================================*/
00463   
00464 /* LC2 + LC2 */
00465 template <class Scalar, class Node1, class Node2, 
00466           class Node3, class Node4> 
00467 LC2<Scalar, LC2<Scalar, Node1, Node2>, LC2<Scalar, Node3, Node4> >
00468 operator+(const LC2<Scalar, Node1, Node2>& x1, 
00469   const LC2<Scalar, Node3, Node4>& x2);
00470 
00471 
00472 /* LC2 - LC2 */
00473 template <class Scalar, class Node1, class Node2, 
00474           class Node3, class Node4> 
00475 LC2<Scalar, LC2<Scalar, Node1, Node2>, LC2<Scalar, Node3, Node4> >
00476 operator-(const LC2<Scalar, Node1, Node2>& x1, 
00477   const LC2<Scalar, Node3, Node4>& x2);
00478 
00479 
00480 
00481   
00482 
00483 
00484   
00485 
00486 
00487 }
00488 
00489 
00490 
00491 #endif

Site Contact