Sacado_Fad_GeneralFadImp.hpp

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

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