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

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