Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_CacheFad_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_CACHEFAD_SFAD_HPP
00055 #define SACADO_CACHEFAD_SFAD_HPP
00056 
00057 #include "Sacado_CacheFad_SFadTraits.hpp"
00058 #include "Sacado_CacheFad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 #include "Sacado_dummy_arg.hpp"
00061 
00062 namespace Sacado {
00063 
00065   namespace CacheFad {
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 
00087       typedef typename ScalarType<T>::type scalar_type;
00088 
00093 
00095       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00096 
00098 
00101       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00102 
00104 
00107       Expr(const int sz, const T & x);
00108 
00110 
00115       Expr(const int sz, const int i, const T & x);
00116 
00118       Expr(const Expr& x);
00119 
00121       template <typename S> Expr(const Expr<S>& x);
00122 
00124       ~Expr() {}
00125 
00127 
00133       void diff(const int ith, const int n);
00134 
00136 
00140       void resize(int sz);
00141 
00143       void zero() { ss_array<T>::zero(dx_, Num); }
00144 
00146 
00151 
00153       const T& val() const { return val_;}
00154 
00156       T& val() { return val_;}
00157 
00159 
00164 
00166       int size() const { return Num;}
00167 
00172       int availableSize() const { return Num; }
00173 
00175       bool hasFastAccess() const { return true; }
00176 
00178       bool isPassive() const { return false; }
00179       
00181       void setIsConstant(bool is_const) {}
00182 
00184       const T* dx() const { return &(dx_[0]);}
00185 
00187       const T& dx(int i) const { return dx_[i]; }
00188     
00190       T& fastAccessDx(int i) { return dx_[i];}
00191 
00193       const T& fastAccessDx(int i) const { return dx_[i];}
00194     
00196 
00201 
00203       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00204 
00206       Expr< SFadExprTag<T,Num> >& 
00207       operator=(const Expr< SFadExprTag<T,Num> >& x);
00208 
00210       template <typename S> 
00211       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00212 
00214 
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       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00231 
00233       template <typename S> 
00234       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00235 
00237       template <typename S> 
00238       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00239   
00241       template <typename S> 
00242       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00243 
00245       template <typename S> 
00246       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00247 
00249 
00250     protected:
00251 
00253       T val_;
00254 
00256       T dx_[Num];
00257 
00258     }; // class Expr<SFadExprTag>
00259 
00273     template <typename ValueT, int Num>
00274     class SFad : 
00275       public Expr< SFadExprTag<ValueT,Num > > {
00276 
00277     public:
00278 
00280       typedef typename ScalarType<ValueT>::type ScalarT;
00281 
00283       template <typename T> 
00284       struct apply {
00285   typedef SFad<T,Num> type;
00286       };
00287 
00292 
00294 
00297       SFad() : 
00298   Expr< SFadExprTag< ValueT,Num > >() {}
00299 
00301 
00304       SFad(const ValueT & x) : 
00305   Expr< SFadExprTag< ValueT,Num > >(x) {}
00306 
00308 
00312       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00313   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00314 
00316 
00319       SFad(const int sz, const ValueT & x) : 
00320   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00321 
00323 
00328       SFad(const int sz, const int i, const ValueT & x) : 
00329   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00330 
00332       SFad(const SFad& x) : 
00333   Expr< SFadExprTag< ValueT,Num > >(x) {}
00334 
00336       template <typename S> SFad(const Expr<S>& x) : 
00337   Expr< SFadExprTag< ValueT,Num > >(x) {}
00338 
00340 
00342       ~SFad() {}
00343 
00345       SFad& operator=(const ValueT& v) {
00346   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00347   return *this;
00348       }
00349 
00351 
00354       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00355   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00356   return *this;
00357       }
00358 
00360       SFad& operator=(const SFad& x) {
00361   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00362   return *this;
00363       }
00364 
00366       template <typename S> SFad& operator=(const Expr<S>& x) 
00367       {
00368   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00369   return *this;
00370       }
00371 
00372     }; // class SFad<ValueT,Num>
00373 
00374   } // namespace CacheFad
00375 
00376 } // namespace Sacado
00377 
00378 #include "Sacado_CacheFad_SFadImp.hpp"
00379 #include "Sacado_CacheFad_Ops.hpp"
00380 
00381 #endif // SACADO_CACHEFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines