Sacado_Fad_SFad.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_Fad_SFad.hpp,v 1.14 2007/11/14 00:18:18 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Fad_SFad.hpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 //
00031 // The forward-mode AD classes in Sacado are a derivative work of the
00032 // expression template classes in the Fad package by Nicolas Di Cesare.  
00033 // The following banner is included in the original Fad source code:
00034 //
00035 // ************ DO NOT REMOVE THIS BANNER ****************
00036 //
00037 //  Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
00038 //  http://www.ann.jussieu.fr/~dicesare
00039 //
00040 //            CEMRACS 98 : C++ courses, 
00041 //         templates : new C++ techniques 
00042 //            for scientific computing 
00043 // 
00044 //********************************************************
00045 //
00046 //  A short implementation ( not all operators and 
00047 //  functions are overloaded ) of 1st order Automatic
00048 //  Differentiation in forward mode (FAD) using
00049 //  EXPRESSION TEMPLATES.
00050 //
00051 //********************************************************
00052 // @HEADER
00053 
00054 #ifndef SACADO_FAD_SFAD_HPP
00055 #define SACADO_FAD_SFAD_HPP
00056 
00057 #include "Sacado_Fad_SFadTraits.hpp"
00058 #include "Sacado_Fad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 
00061 namespace Sacado {
00062 
00064   namespace Fad {
00065 
00067     template <typename T, int Num> 
00068     struct SFadExprTag {};
00069 
00077     template <typename T, int Num> 
00078     class Expr< SFadExprTag<T,Num> > {
00079 
00080     public:
00081 
00083       typedef T value_type;
00084 
00089 
00091       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00092 
00094 
00097       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00098 
00100 
00103       Expr(const int sz, const T & x);
00104 
00106 
00111       Expr(const int sz, const int i, const T & x);
00112 
00114       Expr(const Expr& x);
00115 
00117       template <typename S> Expr(const Expr<S>& x);
00118 
00120       ~Expr() {}
00121 
00123 
00129       void diff(const int ith, const int n);
00130 
00132 
00136       void resize(int sz);
00137 
00139       void zero() { ss_array<T>::zero(dx_, Num); }
00140 
00142 
00147 
00149       const T& val() const { return val_;}
00150 
00152       T& val() { return val_;}
00153 
00155 
00160 
00162       int size() const { return Num;}
00163 
00165       bool hasFastAccess() const { return true; }
00166 
00168       bool isPassive() const { return false; }
00169       
00171       void setIsConstant(bool is_const) {}
00172 
00174       const T* dx() const { return &(dx_[0]);}
00175 
00177       const T& dx(int i) const { return dx_[i]; }
00178     
00180       T& fastAccessDx(int i) { return dx_[i];}
00181 
00183       const T& fastAccessDx(int i) const { return dx_[i];}
00184     
00186 
00191 
00193       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00194 
00196       Expr< SFadExprTag<T,Num> >& 
00197       operator=(const Expr< SFadExprTag<T,Num> >& x);
00198 
00200       template <typename S> 
00201       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00202 
00204 
00209 
00211       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00212 
00214       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00215 
00217       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00218 
00220       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00221 
00223       template <typename S> 
00224       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00225 
00227       template <typename S> 
00228       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00229   
00231       template <typename S> 
00232       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00233 
00235       template <typename S> 
00236       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00237 
00239 
00240     protected:
00241 
00243       T val_;
00244 
00246       T dx_[Num];
00247 
00248     }; // class Expr<SFadExprTag>
00249 
00251 
00267     template <typename ValueT, int Num,
00268         typename ScalarT = typename ScalarValueType<ValueT>::type >
00269     class SFad : 
00270       public Expr< SFadExprTag<ValueT,Num > > {
00271 
00272     public:
00273 
00278 
00280 
00283       SFad() : 
00284   Expr< SFadExprTag< ValueT,Num > >() {}
00285 
00287 
00290       SFad(const ValueT & x) : 
00291   Expr< SFadExprTag< ValueT,Num > >(x) {}
00292 
00294 
00297       SFad(const ScalarT& x) : 
00298   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00299 
00301 
00304       SFad(const int sz, const ValueT & x) : 
00305   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00306 
00308 
00313       SFad(const int sz, const int i, const ValueT & x) : 
00314   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00315 
00317       SFad(const SFad& x) : 
00318   Expr< SFadExprTag< ValueT,Num > >(x) {}
00319 
00321       template <typename S> SFad(const Expr<S>& x) : 
00322   Expr< SFadExprTag< ValueT,Num > >(x) {}
00323 
00325 
00327       ~SFad() {}
00328 
00330       SFad& operator=(const ValueT& val) {
00331   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00332   return *this;
00333       }
00334 
00336       SFad& operator=(const ScalarT& val) {
00337   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(val));
00338   return *this;
00339       }
00340 
00342       SFad& operator=(const SFad& x) {
00343   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00344   return *this;
00345       }
00346 
00348       template <typename S> SFad& operator=(const Expr<S>& x) 
00349       {
00350   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00351   return *this;
00352       }
00353 
00354     }; // class SFad<ValueT,Num,ScalarT>
00355 
00357 
00362     template <typename ValueT, int Num>
00363     class SFad<ValueT,Num,ValueT> : 
00364       public Expr< SFadExprTag<ValueT,Num >  >{
00365 
00366     public:
00367 
00372 
00374 
00377       SFad() : Expr< SFadExprTag< ValueT,Num > >() {}
00378 
00380 
00383       SFad(const ValueT & x) : 
00384   Expr< SFadExprTag< ValueT,Num > >(x) {}
00385 
00387 
00390       SFad(const int sz, const ValueT & x) : 
00391   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00392 
00394 
00399       SFad(const int sz, const int i, const ValueT & x) : 
00400   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00401 
00403       SFad(const SFad& x) : 
00404   Expr< SFadExprTag< ValueT,Num > >(x) {}
00405 
00407       template <typename S> SFad(const Expr<S>& x) : 
00408   Expr< SFadExprTag< ValueT,Num > >(x) {}
00409 
00411 
00413       ~SFad() {}
00414 
00416       SFad& operator=(const ValueT& val) {
00417   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00418   return *this;
00419       }
00420 
00422       SFad& operator=(const SFad& x) {
00423   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00424   return *this;
00425       }
00426 
00428       template <typename S> SFad& operator=(const Expr<S>& x) 
00429       {
00430   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00431   return *this;
00432       }
00433 
00434     }; // class SFad<ValueT,Num>
00435 
00436   } // namespace Fad
00437 
00438 } // namespace Sacado
00439 
00440 #include "Sacado_Fad_SFadImp.hpp"
00441 #include "Sacado_Fad_Ops.hpp"
00442 
00443 #endif // SACADO_FAD_SFAD_HPP

Generated on Wed May 12 21:59:04 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7