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       void zero() { ss_array<T>::zero(dx_, Num); }
00143 
00145       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00146 
00148       bool updateValue() const { return update_val_; }
00149 
00151 
00156 
00158       const T& val() const { return val_;}
00159 
00161       T& val() { return val_;}
00162 
00164 
00169 
00171       int size() const { return Num;}
00172 
00177       int availableSize() const { return Num; }
00178 
00180       bool hasFastAccess() const { return true; }
00181 
00183       bool isPassive() const { return false; }
00184       
00186       void setIsConstant(bool is_const) {}
00187 
00189       const T* dx() const { return &(dx_[0]);}
00190 
00192       const T& dx(int i) const { return dx_[i]; }
00193     
00195       T& fastAccessDx(int i) { return dx_[i];}
00196 
00198       const T& fastAccessDx(int i) const { return dx_[i];}
00199     
00201 
00206 
00208       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00209 
00211       Expr< SFadExprTag<T,Num> >& 
00212       operator=(const Expr< SFadExprTag<T,Num> >& x);
00213 
00215       template <typename S> 
00216       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00217 
00219 
00224 
00226       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00227 
00229       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00230 
00232       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00233 
00235       Expr< SFadExprTag<T,Num> >& operator /= (const T& 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       template <typename S> 
00247       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00248 
00250       template <typename S> 
00251       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00252 
00254 
00255     protected:
00256 
00258       T val_;
00259 
00261       T dx_[Num];
00262 
00264       bool update_val_;
00265 
00266     }; // class Expr<SFadExprTag>
00267 
00269 
00275     template <typename ValueT, int Num>
00276     class SFad : 
00277       public Expr< SFadExprTag<ValueT,Num > > {
00278 
00279     public:
00280 
00282       typedef typename ScalarType<ValueT>::type ScalarT;
00283 
00285       template <typename T> 
00286       struct apply {
00287   typedef SFad<T,Num> type;
00288       };
00289 
00294 
00296 
00299       SFad() : 
00300   Expr< SFadExprTag< ValueT,Num > >() {}
00301 
00303 
00306       SFad(const ValueT & x) : 
00307   Expr< SFadExprTag< ValueT,Num > >(x) {}
00308 
00310 
00314       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00315   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00316 
00318 
00321       SFad(const int sz, const ValueT & x) : 
00322   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00323 
00325 
00330       SFad(const int sz, const int i, const ValueT & x) : 
00331   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00332 
00334       SFad(const SFad& x) : 
00335   Expr< SFadExprTag< ValueT,Num > >(x) {}
00336 
00338       template <typename S> SFad(const Expr<S>& x) : 
00339   Expr< SFadExprTag< ValueT,Num > >(x) {}
00340 
00342 
00344       ~SFad() {}
00345 
00347       SFad& operator=(const ValueT& v) {
00348   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00349   return *this;
00350       }
00351 
00353 
00356       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00357   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00358   return *this;
00359       }
00360 
00362       SFad& operator=(const SFad& x) {
00363   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00364   return *this;
00365       }
00366 
00368       template <typename S> SFad& operator=(const Expr<S>& x) 
00369       {
00370   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00371   return *this;
00372       }
00373 
00374     }; // class SFad<ValueT,Num>
00375 
00376   } // namespace Fad
00377 
00378 } // namespace Sacado
00379 
00380 #include "Sacado_Fad_SFadImp.hpp"
00381 #include "Sacado_Fad_Ops.hpp"
00382 
00383 #endif // SACADO_FAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines