Sacado Package Browser (Single Doxygen Collection) Version of the Day
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.)), update_val_(true) { 
00093   ss_array<T>::zero(dx_, Num); }
00094 
00096 
00099       Expr(const T & x) : val_(x), update_val_(true)  { 
00100   ss_array<T>::zero(dx_, Num); }
00101 
00103 
00106       Expr(const int sz, const T & x);
00107 
00109 
00114       Expr(const int sz, const int i, const T & x);
00115 
00117       Expr(const Expr& x);
00118 
00120       template <typename S> Expr(const Expr<S>& x);
00121 
00123       ~Expr() {}
00124 
00126 
00132       void diff(const int ith, const int n);
00133 
00135 
00139       void resize(int sz);
00140 
00142 
00146       void expand(int sz) { resize(sz); }
00147 
00149       void zero() { ss_array<T>::zero(dx_, Num); }
00150 
00152       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00153 
00155       bool updateValue() const { return update_val_; }
00156 
00158       template <typename S>
00159       bool isEqualTo(const Expr<S>& x) const {
00160   typedef IsEqual<value_type> IE;
00161   if (x.size() != this->size()) return false;
00162   bool eq = IE::eval(x.val(), this->val());
00163   for (int i=0; i<this->size(); i++)
00164     eq = eq && IE::eval(x.dx(i), this->dx(i));
00165   return eq;
00166       }
00167 
00169 
00174 
00176       const T& val() const { return val_;}
00177 
00179       T& val() { return val_;}
00180 
00182 
00187 
00189       int size() const { return Num;}
00190 
00195       int availableSize() const { return Num; }
00196 
00198       bool hasFastAccess() const { return true; }
00199 
00201       bool isPassive() const { return false; }
00202       
00204       void setIsConstant(bool is_const) {}
00205 
00207       const T* dx() const { return &(dx_[0]);}
00208 
00210       const T& dx(int i) const { return dx_[i]; }
00211     
00213       T& fastAccessDx(int i) { return dx_[i];}
00214 
00216       const T& fastAccessDx(int i) const { return dx_[i];}
00217     
00219 
00224 
00226       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00227 
00229       Expr< SFadExprTag<T,Num> >& 
00230       operator=(const Expr< SFadExprTag<T,Num> >& x);
00231 
00233       template <typename S> 
00234       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00235 
00237 
00242 
00244       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00245 
00247       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00248 
00250       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00251 
00253       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00254 
00256       template <typename S> 
00257       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00258 
00260       template <typename S> 
00261       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00262   
00264       template <typename S> 
00265       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00266 
00268       template <typename S> 
00269       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00270 
00272 
00273     protected:
00274 
00276       T val_;
00277 
00279       T dx_[Num];
00280 
00282       bool update_val_;
00283 
00284     }; // class Expr<SFadExprTag>
00285 
00287 
00293     template <typename ValueT, int Num>
00294     class SFad : 
00295       public Expr< SFadExprTag<ValueT,Num > > {
00296 
00297     public:
00298 
00300       typedef typename ScalarType<ValueT>::type ScalarT;
00301 
00303       template <typename T> 
00304       struct apply {
00305   typedef SFad<T,Num> type;
00306       };
00307 
00312 
00314 
00317       SFad() : 
00318   Expr< SFadExprTag< ValueT,Num > >() {}
00319 
00321 
00324       SFad(const ValueT & x) : 
00325   Expr< SFadExprTag< ValueT,Num > >(x) {}
00326 
00328 
00332       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00333   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00334 
00336 
00339       SFad(const int sz, const ValueT & x) : 
00340   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00341 
00343 
00348       SFad(const int sz, const int i, const ValueT & x) : 
00349   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00350 
00352       SFad(const SFad& x) : 
00353   Expr< SFadExprTag< ValueT,Num > >(x) {}
00354 
00356       template <typename S> SFad(const Expr<S>& x) : 
00357   Expr< SFadExprTag< ValueT,Num > >(x) {}
00358 
00360 
00362       ~SFad() {}
00363 
00365       SFad& operator=(const ValueT& v) {
00366   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00367   return *this;
00368       }
00369 
00371 
00374       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00375   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00376   return *this;
00377       }
00378 
00380       SFad& operator=(const SFad& x) {
00381   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00382   return *this;
00383       }
00384 
00386       template <typename S> SFad& operator=(const Expr<S>& x) 
00387       {
00388   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00389   return *this;
00390       }
00391 
00392     }; // class SFad<ValueT,Num>
00393 
00394   } // namespace Fad
00395 
00396 } // namespace Sacado
00397 
00398 #include "Sacado_Fad_SFadImp.hpp"
00399 #include "Sacado_Fad_Ops.hpp"
00400 
00401 #endif // SACADO_FAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines