Sacado_Fad_SFad.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
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 #include "Sacado_dummy_arg.hpp"
00061 
00062 namespace Sacado {
00063 
00065   namespace Fad {
00066 
00068     template <typename T, int Num> 
00069     struct SFadExprTag {};
00070 
00078     template <typename T, int Num> 
00079     class Expr< SFadExprTag<T,Num> > {
00080 
00081     public:
00082 
00084       typedef T value_type;
00085 
00090 
00092       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00093 
00095 
00098       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00099 
00101 
00104       Expr(const int sz, const T & x);
00105 
00107 
00112       Expr(const int sz, const int i, const T & x);
00113 
00115       Expr(const Expr& x);
00116 
00118       template <typename S> Expr(const Expr<S>& x);
00119 
00121       ~Expr() {}
00122 
00124 
00130       void diff(const int ith, const int n);
00131 
00133 
00137       void resize(int sz);
00138 
00140       void zero() { ss_array<T>::zero(dx_, Num); }
00141 
00143 
00148 
00150       const T& val() const { return val_;}
00151 
00153       T& val() { return val_;}
00154 
00156 
00161 
00163       int size() const { return Num;}
00164 
00169       int availableSize() const { return Num; }
00170 
00172       bool hasFastAccess() const { return true; }
00173 
00175       bool isPassive() const { return false; }
00176       
00178       void setIsConstant(bool is_const) {}
00179 
00181       const T* dx() const { return &(dx_[0]);}
00182 
00184       const T& dx(int i) const { return dx_[i]; }
00185     
00187       T& fastAccessDx(int i) { return dx_[i];}
00188 
00190       const T& fastAccessDx(int i) const { return dx_[i];}
00191     
00193 
00198 
00200       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00201 
00203       Expr< SFadExprTag<T,Num> >& 
00204       operator=(const Expr< SFadExprTag<T,Num> >& x);
00205 
00207       template <typename S> 
00208       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00209 
00211 
00216 
00218       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00219 
00221       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00222 
00224       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00225 
00227       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00228 
00230       template <typename S> 
00231       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00232 
00234       template <typename S> 
00235       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00236   
00238       template <typename S> 
00239       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00240 
00242       template <typename S> 
00243       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00244 
00246 
00247     protected:
00248 
00250       T val_;
00251 
00253       T dx_[Num];
00254 
00255     }; // class Expr<SFadExprTag>
00256 
00258 
00264     template <typename ValueT, int Num>
00265     class SFad : 
00266       public Expr< SFadExprTag<ValueT,Num > > {
00267 
00268     public:
00269 
00271       typedef typename ScalarType<ValueT>::type ScalarT;
00272 
00274       template <typename T> 
00275       struct apply {
00276   typedef SFad<T,Num> type;
00277       };
00278 
00283 
00285 
00288       SFad() : 
00289   Expr< SFadExprTag< ValueT,Num > >() {}
00290 
00292 
00295       SFad(const ValueT & x) : 
00296   Expr< SFadExprTag< ValueT,Num > >(x) {}
00297 
00299 
00303       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00304   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00305 
00307 
00310       SFad(const int sz, const ValueT & x) : 
00311   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00312 
00314 
00319       SFad(const int sz, const int i, const ValueT & x) : 
00320   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00321 
00323       SFad(const SFad& x) : 
00324   Expr< SFadExprTag< ValueT,Num > >(x) {}
00325 
00327       template <typename S> SFad(const Expr<S>& x) : 
00328   Expr< SFadExprTag< ValueT,Num > >(x) {}
00329 
00331 
00333       ~SFad() {}
00334 
00336       SFad& operator=(const ValueT& v) {
00337   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00338   return *this;
00339       }
00340 
00342 
00345       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00346   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00347   return *this;
00348       }
00349 
00351       SFad& operator=(const SFad& x) {
00352   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00353   return *this;
00354       }
00355 
00357       template <typename S> SFad& operator=(const Expr<S>& x) 
00358       {
00359   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00360   return *this;
00361       }
00362 
00363     }; // class SFad<ValueT,Num>
00364 
00365   } // namespace Fad
00366 
00367 } // namespace Sacado
00368 
00369 #include "Sacado_Fad_SFadImp.hpp"
00370 #include "Sacado_Fad_Ops.hpp"
00371 
00372 #endif // SACADO_FAD_SFAD_HPP

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