Sacado_CacheFad_GeneralFadImp.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, typename Storage> 
00057 template <typename S> 
00058 inline Sacado::CacheFad::GeneralFad<T,Storage>::GeneralFad(const Expr<S>& x) :
00059   Storage(T(0.))
00060 {
00061   int sz = x.size();
00062 
00063   T xval = x.val(); 
00064 
00065   if (sz != this->size()) 
00066     this->resize(sz);
00067 
00068   if (sz) {
00069     if (x.hasFastAccess())
00070       for(int i=0; i<sz; ++i) 
00071   this->fastAccessDx(i) = x.fastAccessDx(i);
00072     else
00073       for(int i=0; i<sz; ++i) 
00074   this->fastAccessDx(i) = x.dx(i);
00075   }
00076 
00077   this->val() = xval;
00078 }
00079 
00080 
00081 template <typename T, typename Storage> 
00082 inline void 
00083 Sacado::CacheFad::GeneralFad<T,Storage>::diff(const int ith, const int n) 
00084 { 
00085   if (this->size() == 0) 
00086     this->resize(n);
00087 
00088   this->zero();
00089   this->fastAccessDx(ith) = T(1.);
00090 
00091 }
00092 
00093 template <typename T, typename Storage> 
00094 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00095 Sacado::CacheFad::GeneralFad<T,Storage>::operator=(const T& v) 
00096 {
00097   this->val() = v;
00098 
00099   if (this->size()) {
00100     this->zero();    // We need to zero out the array for future resizes
00101     this->resize(0);
00102   }
00103 
00104   return *this;
00105 }
00106 
00107 template <typename T, typename Storage> 
00108 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00109 Sacado::CacheFad::GeneralFad<T,Storage>::operator=(
00110            const Sacado::CacheFad::GeneralFad<T,Storage>& x) 
00111 {
00112   // Copy val_ and dx_
00113   Storage::operator=(x);
00114   
00115   return *this;
00116 }
00117 
00118 template <typename T, typename Storage> 
00119 template <typename S> 
00120 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00121 Sacado::CacheFad::GeneralFad<T,Storage>::operator=(const Expr<S>& x) 
00122 {
00123   int sz = x.size();
00124 
00125   T xval = x.val();
00126 
00127   if (sz != this->size()) 
00128     this->resize(sz);
00129 
00130   if (sz) {
00131     if (x.hasFastAccess())
00132       for(int i=0; i<sz; ++i)
00133   this->fastAccessDx(i) = x.fastAccessDx(i);
00134     else
00135       for(int i=0; i<sz; ++i)
00136   this->fastAccessDx(i) = x.dx(i);
00137   }
00138   
00139   this->val() = xval;
00140   
00141   return *this;
00142 }
00143 
00144 template <typename T, typename Storage> 
00145 inline  Sacado::CacheFad::GeneralFad<T,Storage>& 
00146 Sacado::CacheFad::GeneralFad<T,Storage>::operator += (const T& v)
00147 {
00148   this->val() += v;
00149 
00150   return *this;
00151 }
00152 
00153 template <typename T, typename Storage> 
00154 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00155 Sacado::CacheFad::GeneralFad<T,Storage>::operator -= (const T& v)
00156 {
00157   this->val() -= v;
00158 
00159   return *this;
00160 }
00161 
00162 template <typename T, typename Storage> 
00163 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00164 Sacado::CacheFad::GeneralFad<T,Storage>::operator *= (const T& v)
00165 {
00166   int sz = this->size();
00167 
00168   this->val() *= v;
00169   for (int i=0; i<sz; ++i)
00170     this->fastAccessDx(i) *= v;
00171 
00172   return *this;
00173 }
00174 
00175 template <typename T, typename Storage> 
00176 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00177 Sacado::CacheFad::GeneralFad<T,Storage>::operator /= (const T& v)
00178 {
00179   int sz = this->size();
00180 
00181   this->val() /= v;
00182   for (int i=0; i<sz; ++i)
00183     this->fastAccessDx(i) /= v;
00184 
00185   return *this;
00186 }
00187 
00188 template <typename T, typename Storage> 
00189 inline  Sacado::CacheFad::GeneralFad<T,Storage>& 
00190 Sacado::CacheFad::GeneralFad<T,Storage>::
00191 operator += (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00192 {
00193   this->val() += v;
00194 
00195   return *this;
00196 }
00197 
00198 template <typename T, typename Storage> 
00199 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00200 Sacado::CacheFad::GeneralFad<T,Storage>::
00201 operator -= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00202 {
00203   this->val() -= v;
00204 
00205   return *this;
00206 }
00207 
00208 template <typename T, typename Storage> 
00209 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00210 Sacado::CacheFad::GeneralFad<T,Storage>::
00211 operator *= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00212 {
00213   int sz = this->size();
00214 
00215   this->val() *= v;
00216   for (int i=0; i<sz; ++i)
00217     this->fastAccessDx(i) *= v;
00218 
00219   return *this;
00220 }
00221 
00222 template <typename T, typename Storage> 
00223 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00224 Sacado::CacheFad::GeneralFad<T,Storage>::
00225 operator /= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00226 {
00227   int sz = this->size();
00228 
00229   this->val() /= v;
00230   for (int i=0; i<sz; ++i)
00231     this->fastAccessDx(i) /= v;
00232 
00233   return *this;
00234 }
00235 
00236 template <typename T, typename Storage> 
00237 template <typename S> 
00238 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00239 Sacado::CacheFad::GeneralFad<T,Storage>::operator += (const Sacado::CacheFad::Expr<S>& x)
00240 {
00241   int xsz = x.size(), sz = this->size();
00242 
00243 #ifdef SACADO_DEBUG
00244   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00245     throw "Fad Error:  Attempt to assign with incompatible sizes";
00246 #endif
00247 
00248   T xval = x.val();
00249 
00250   if (xsz) {
00251     if (sz) {
00252       if (x.hasFastAccess())
00253   for (int i=0; i<sz; ++i)
00254     this->fastAccessDx(i) += x.fastAccessDx(i);
00255       else
00256   for (int i=0; i<sz; ++i)
00257     this->fastAccessDx(i) += x.dx(i);
00258     }
00259     else {
00260       this->resize(xsz);
00261       if (x.hasFastAccess())
00262   for (int i=0; i<xsz; ++i)
00263     this->fastAccessDx(i) = x.fastAccessDx(i);
00264       else
00265   for (int i=0; i<xsz; ++i)
00266     this->fastAccessDx(i) = x.dx(i);
00267     }
00268   }
00269 
00270   this->val() += xval;
00271 
00272   return *this;
00273 }
00274 
00275 template <typename T, typename Storage> 
00276 template <typename S> 
00277 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00278 Sacado::CacheFad::GeneralFad<T,Storage>::operator -= (const Sacado::CacheFad::Expr<S>& x)
00279 {
00280   int xsz = x.size(), sz = this->size();
00281 
00282 #ifdef SACADO_DEBUG
00283   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00284     throw "Fad Error:  Attempt to assign with incompatible sizes";
00285 #endif
00286 
00287   T xval = x.val();
00288 
00289   if (xsz) {
00290     if (sz) {
00291       if (x.hasFastAccess())
00292   for(int i=0; i<sz; ++i)
00293     this->fastAccessDx(i) -= x.fastAccessDx(i);
00294       else
00295   for (int i=0; i<sz; ++i)
00296     this->fastAccessDx(i) -= x.dx(i);
00297     }
00298     else {
00299       this->resize(xsz);
00300       if (x.hasFastAccess())
00301   for(int i=0; i<xsz; ++i)
00302     this->fastAccessDx(i) = -x.fastAccessDx(i);
00303       else
00304   for (int i=0; i<xsz; ++i)
00305     this->fastAccessDx(i) = -x.dx(i);
00306     }
00307   }
00308 
00309   this->val() -= xval;
00310 
00311 
00312   return *this;
00313 }
00314 
00315 template <typename T, typename Storage> 
00316 template <typename S> 
00317 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00318 Sacado::CacheFad::GeneralFad<T,Storage>::operator *= (const Sacado::CacheFad::Expr<S>& x)
00319 {
00320   int xsz = x.size(), sz = this->size();
00321   T xval = x.val();
00322 
00323 #ifdef SACADO_DEBUG
00324   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00325     throw "Fad Error:  Attempt to assign with incompatible sizes";
00326 #endif
00327 
00328   if (xsz) {
00329     if (sz) {
00330       if (x.hasFastAccess())
00331   for(int i=0; i<sz; ++i)
00332     this->fastAccessDx(i) = this->val() * x.fastAccessDx(i) + this->fastAccessDx(i) * xval;
00333       else
00334   for (int i=0; i<sz; ++i)
00335     this->fastAccessDx(i) = this->val() * x.dx(i) + this->fastAccessDx(i) * xval;
00336     }
00337     else {
00338       this->resize(xsz);
00339       if (x.hasFastAccess())
00340   for(int i=0; i<xsz; ++i)
00341     this->fastAccessDx(i) = this->val() * x.fastAccessDx(i);
00342       else
00343   for (int i=0; i<xsz; ++i)
00344     this->fastAccessDx(i) = this->val() * x.dx(i);
00345     }
00346   }
00347   else {
00348     if (sz) {
00349       for (int i=0; i<sz; ++i)
00350   this->fastAccessDx(i) *= xval;
00351     }
00352   }
00353 
00354   this->val() *= xval;
00355 
00356   return *this;
00357 }
00358 
00359 template <typename T, typename Storage>
00360 template <typename S> 
00361 inline Sacado::CacheFad::GeneralFad<T,Storage>& 
00362 Sacado::CacheFad::GeneralFad<T,Storage>::operator /= (const Sacado::CacheFad::Expr<S>& x)
00363 {
00364   int xsz = x.size(), sz = this->size();
00365   T xval = x.val();
00366 
00367 #ifdef SACADO_DEBUG
00368   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00369     throw "Fad Error:  Attempt to assign with incompatible sizes";
00370 #endif
00371 
00372   if (xsz) {
00373     if (sz) {
00374       if (x.hasFastAccess())
00375   for(int i=0; i<sz; ++i)
00376     this->fastAccessDx(i) = ( this->fastAccessDx(i)*xval - this->val()*x.fastAccessDx(i) )/ (xval*xval);
00377       else
00378   for (int i=0; i<sz; ++i)
00379     this->fastAccessDx(i) = ( this->fastAccessDx(i)*xval - this->val()*x.dx(i) )/ (xval*xval);
00380     }
00381     else {
00382       this->resize(xsz);
00383       if (x.hasFastAccess())
00384   for(int i=0; i<xsz; ++i)
00385     this->fastAccessDx(i) = - this->val()*x.fastAccessDx(i) / (xval*xval);
00386       else
00387   for (int i=0; i<xsz; ++i)
00388     this->fastAccessDx(i) = -this->val() * x.dx(i) / (xval*xval);
00389     }
00390   }
00391   else {
00392     if (sz) {
00393       for (int i=0; i<sz; ++i)
00394   this->fastAccessDx(i) /= xval;
00395     }
00396   }
00397 
00398   this->val() /= xval;
00399 
00400   return *this;
00401 }
00402 

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