Sacado_Fad_SFadImp.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_Fad_SFadImp.hpp,v 1.7.2.2 2007/08/14 00:19:06 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Fad_SFadImp.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 #include "Sacado_ConfigDefs.h"
00055 
00056 template <typename T, int Num> 
00057 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00058 Expr(const int sz, const T & x) : val_(x)
00059 {
00060 #ifdef SACADO_DEBUG
00061   if (sz != Num)
00062     throw "SFad::SFad() Error:  Supplied derivative dimension does not match compile time length.";
00063 #endif
00064 
00065   ss_array<T>::zero(dx_, Num); 
00066 }
00067 
00068 template <typename T, int Num> 
00069 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00070 Expr(const int sz, const int i, const T & x) : val_(x) 
00071 {
00072 #ifdef SACADO_DEBUG
00073   if (sz != Num)
00074     throw "SFad::SFad() Error:  Supplied derivative dimension does not match compile time length.";
00075   if (i >= Num)
00076     throw "SFad::SFad() Error:  Invalid derivative index.";
00077 #endif
00078 
00079   ss_array<T>::zero(dx_, Num);
00080   dx_[i]=1.; 
00081 }
00082 
00083 template <typename T, int Num> 
00084 template <typename S> 
00085 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00086 Expr(const Expr<S>& x) : val_(x.val())
00087 {
00088 #ifdef SACADO_DEBUG
00089   if (x.size() != Num)
00090     throw "SFad::SFad() Error:  Attempt to assign with incompatible sizes";
00091 #endif
00092 
00093   for(int i=0; i<Num; ++i) 
00094     dx_[i] = x.fastAccessDx(i);
00095 }
00096 
00097 
00098 template <typename T, int Num> 
00099 inline void 
00100 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00101 diff(const int ith, const int n) 
00102 { 
00103 #ifdef SACADO_DEBUG
00104   if (n != Num)
00105     throw "SFad::diff() Error:  Supplied derivative dimension does not match compile time length.";
00106 #endif
00107 
00108   ss_array<T>::zero(dx_, Num);
00109   dx_[ith] = T(1.);
00110 }
00111 
00112 template <typename T, int Num> 
00113 inline void 
00114 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00115 resize(int sz)
00116 {
00117 #ifdef SACADO_DEBUG
00118   if (sz != Num)
00119     throw "SFad::resize() Error:  Cannot resize fixed derivative array dimension";
00120 #endif
00121 }
00122 
00123 template <typename T, int Num> 
00124 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00125 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00126 operator=(const T& val) 
00127 {
00128   val_ = val;
00129   ss_array<T>::zero(dx_, Num);
00130 
00131   return *this;
00132 }
00133 
00134 template <typename T, int Num> 
00135 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00136 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00137 operator=(const Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& x) 
00138 {
00139   // Copy value
00140   val_ = x.val_;
00141 
00142   // Copy dx_
00143   ss_array<T>::copy(x.dx_, dx_, Num);
00144   
00145   return *this;
00146 }
00147 
00148 template <typename T, int Num> 
00149 template <typename S> 
00150 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00151 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00152 operator=(const Expr<S>& x) 
00153 {
00154 #ifdef SACADO_DEBUG
00155   if (x.size() != Num)
00156     throw "SFad::operator=() Error:  Attempt to assign with incompatible sizes";
00157 #endif
00158 
00159   for(int i=0; i<Num; ++i)
00160     dx_[i] = x.fastAccessDx(i);
00161   
00162   val_ = x.val();
00163   
00164   return *this;
00165 }
00166 
00167 template <typename T, int Num> 
00168 inline  Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00169 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00170 operator += (const T& val)
00171 {
00172   val_ += val;
00173 
00174   return *this;
00175 }
00176 
00177 template <typename T, int Num> 
00178 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00179 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00180 operator -= (const T& val)
00181 {
00182   val_ -= val;
00183 
00184   return *this;
00185 }
00186 
00187 template <typename T, int Num> 
00188 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00189 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00190 operator *= (const T& val)
00191 {
00192   val_ *= val;
00193 
00194   for (int i=0; i<Num; ++i)
00195     dx_[i] *= val;
00196 
00197   return *this;
00198 }
00199 
00200 template <typename T, int Num> 
00201 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00202 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00203 operator /= (const T& val)
00204 {
00205   val_ /= val;
00206 
00207   for (int i=0; i<Num; ++i)
00208     dx_[i] /= val;
00209 
00210   return *this;
00211 }
00212 
00213 template <typename T, int Num> 
00214 template <typename S> 
00215 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00216 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00217 operator += (const Sacado::Fad::Expr<S>& x)
00218 {
00219 #ifdef SACADO_DEBUG
00220   if (x.size() != Num)
00221     throw "SFad::operator+=() Error:  Attempt to assign with incompatible sizes";
00222 #endif
00223 
00224   for (int i=0; i<Num; ++i)
00225     dx_[i] += x.fastAccessDx(i);
00226  
00227   val_ += x.val();
00228 
00229   return *this;
00230 }
00231 
00232 template <typename T, int Num> 
00233 template <typename S> 
00234 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00235 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00236 operator -= (const Sacado::Fad::Expr<S>& x)
00237 {
00238 #ifdef SACADO_DEBUG
00239   if (x.size() != Num)
00240     throw "SFad::operator-=() Error:  Attempt to assign with incompatible sizes";
00241 #endif
00242 
00243   for(int i=0; i<Num; ++i)
00244     dx_[i] -= x.fastAccessDx(i);
00245 
00246   val_ -= x.val();
00247 
00248   return *this;
00249 }
00250 
00251 template <typename T, int Num> 
00252 template <typename S> 
00253 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00254 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00255 operator *= (const Sacado::Fad::Expr<S>& x)
00256 {
00257   T xval = x.val();
00258 
00259 #ifdef SACADO_DEBUG
00260   if (x.size() != Num)
00261     throw "SFad::operator*=() Error:  Attempt to assign with incompatible sizes";
00262 #endif
00263 
00264   for(int i=0; i<Num; ++i)
00265     dx_[i] = val_ * x.fastAccessDx(i) + dx_[i] * xval;
00266  
00267   val_ *= xval;
00268 
00269   return *this;
00270 }
00271 
00272 template <typename T, int Num>
00273 template <typename S> 
00274 inline Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >& 
00275 Sacado::Fad::Expr< Sacado::Fad::SFadExprTag<T,Num> >::
00276 operator /= (const Sacado::Fad::Expr<S>& x)
00277 {
00278   T xval = x.val();
00279 
00280 #ifdef SACADO_DEBUG
00281   if (x.size() != Num)
00282     throw "SFad::operator/=() Error:  Attempt to assign with incompatible sizes";
00283 #endif
00284 
00285   for(int i=0; i<Num; ++i)
00286     dx_[i] = ( dx_[i]*xval - val_*x.fastAccessDx(i) )/ (xval*xval);
00287 
00288   val_ /= xval;
00289 
00290   return *this;
00291 }
00292 

Generated on Tue Oct 20 12:55:05 2009 for Sacado Package Browser (Single Doxygen Collection) by doxygen 1.4.7