Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_Tay_CacheTaylor.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 // @HEADER
00031 
00032 #ifndef SACADO_TAY_CACHETAYLOR_HPP
00033 #define SACADO_TAY_CACHETAYLOR_HPP
00034 
00035 #include <valarray>
00036 
00037 #include "Sacado_Tay_CacheTaylorExpr.hpp"
00038 
00039 // forward decalarations
00040 namespace Sacado {
00041   namespace Tay {
00042     template <class ExprT> class UnaryPlusOp;
00043     template <class ExprT> class UnaryMinusOp;
00044   }
00045 }
00046 
00047 namespace Sacado {
00048 
00049   namespace Tay {
00050 
00052 
00057     template <typename T> 
00058     class CacheTaylorImplementation {
00059 
00060     public:
00061 
00063       typedef T value_type;
00064 
00066       CacheTaylorImplementation() : coeff_(T(0.),1) {}
00067 
00069 
00072       CacheTaylorImplementation(const T& x) : coeff_(x,1) {}
00073 
00075 
00078       CacheTaylorImplementation(unsigned int d, const T & x) : 
00079   coeff_(T(0.),d+1) {
00080   coeff_[0] = x;
00081       }
00082 
00084       CacheTaylorImplementation(const CacheTaylorImplementation& x) : 
00085         coeff_(x.coeff_) {}
00086 
00088       ~CacheTaylorImplementation() {}
00089 
00091 
00095       void resize(unsigned int d, bool keep_coeffs) {
00096   if (keep_coeffs)
00097     resizeCoeffs(d);
00098   else
00099     coeff_.resize(d+1, T(0.));
00100       }
00101 
00106 
00108       const T& val() const { return coeff_[0];}
00109 
00111       T& val() { return coeff_[0];}
00112 
00114 
00119 
00121       unsigned int degree() const { return coeff_.size()-1;}
00122 
00124       bool hasFastAccess(unsigned int d) const { return coeff_.size()>=d+1;}
00125 
00127       const std::valarray<T>& coeff() const { return coeff_;}
00128 
00130       const T coeff(unsigned int i) const { 
00131   T tmp= i<coeff_.size() ? coeff_[i]:T(0.); return tmp;}
00132 
00134       T coeff(unsigned int i) { 
00135   T tmp= i<coeff_.size() ? coeff_[i]:T(0.); return tmp;}
00136     
00138       T& fastAccessCoeff(unsigned int i) { return coeff_[i];}
00139 
00141       const T& fastAccessCoeff(unsigned int i) const { return coeff_[i];}
00142 
00144       void allocateCache(unsigned int d) const {}
00145 
00147       template <typename S>
00148       bool isEqualTo(const Expr<S>& x) const {
00149   typedef IsEqual<value_type> IE;
00150   if (x.degree() != this->degree()) return false;
00151   bool eq = true;
00152   for (unsigned int i=0; i<=this->degree(); i++)
00153     eq = eq && IE::eval(x.coeff(i), this->coeff(i));
00154   return eq;
00155       }
00156     
00158 
00159     protected:
00160 
00162       void resizeCoeffs(unsigned int dnew) {
00163   std::valarray<T> tmp = coeff_;
00164   unsigned int sz = coeff_.size();
00165   coeff_.resize(dnew+1,T(0.));
00166   if (sz > dnew+1) {
00167     std::slice s(0,dnew+1,1);
00168     coeff_ = tmp[s];
00169   }
00170   else {
00171     std::slice s(0,sz,1);
00172     coeff_[s] = tmp;
00173   }
00174       }
00175 
00176     protected:
00177 
00179       std::valarray<T> coeff_;
00180 
00181     }; // class CacheTaylorImplementation
00182 
00184 
00187     template <typename T> 
00188     class Expr< CacheTaylorImplementation<T> > : 
00189       public CacheTaylorImplementation<T> {
00190 
00191     public:
00192 
00194       Expr() : CacheTaylorImplementation<T>() {}
00195 
00197 
00200       Expr(const T & x) : CacheTaylorImplementation<T>(x) {}
00201 
00203 
00206       Expr(unsigned int d, const T & x) : CacheTaylorImplementation<T>(d,x) {}
00207 
00209       Expr(const Expr& x) : CacheTaylorImplementation<T>(x) {}
00210 
00211     }; // class Expr< CacheTaylorImplementation<T> >
00212 
00214 
00218     template <typename T>
00219     class CacheTaylor : public Expr< CacheTaylorImplementation<T> > {
00220 
00221     public:
00222 
00224       typedef T value_type;
00225 
00227       typedef typename ScalarType<T>::type scalar_type;
00228 
00230       template <typename U> 
00231       struct apply {
00232   typedef CacheTaylor<U> type;
00233       };
00234 
00239 
00241       CacheTaylor() : Expr< CacheTaylorImplementation<T> >() {}
00242 
00244 
00247       CacheTaylor(const T & x) : Expr< CacheTaylorImplementation<T> >(x) {}
00248 
00250 
00254       CacheTaylor(const typename dummy<value_type,scalar_type>::type& x) :
00255   Expr< CacheTaylorImplementation<T> >(value_type(x)) {}
00256 
00258 
00261       CacheTaylor(unsigned int d, const T & x) : 
00262   Expr< CacheTaylorImplementation<T> >(d,x) {}
00263 
00265       CacheTaylor(const CacheTaylor& x) : Expr< CacheTaylorImplementation<T> >(x) {}
00266 
00268       template <typename S> CacheTaylor(const Expr<S>& x);
00269 
00271 
00273       ~CacheTaylor() {}
00274 
00279 
00281       CacheTaylor<T>& operator=(const T& v);
00282 
00284 
00288       CacheTaylor<T>& 
00289       operator=(const typename dummy<value_type,scalar_type>::type& val) {
00290   return operator=(value_type(val));
00291       }
00292 
00294       CacheTaylor<T>& operator=(const CacheTaylor<T>& x);
00295 
00297       template <typename S> CacheTaylor<T>& operator=(const Expr<S>& x); 
00298 
00300 
00305 
00307       inline Expr< UnaryExpr< CacheTaylor<T>, UnaryPlusOp > >
00308       operator + () const {
00309   typedef UnaryExpr< CacheTaylor<T>, UnaryPlusOp > expr_t;
00310   return Expr<expr_t>(expr_t(*this));
00311       }
00312 
00314       inline Expr< UnaryExpr< CacheTaylor<T>, UnaryMinusOp > >
00315       operator - () const {
00316   typedef UnaryExpr< CacheTaylor<T>, UnaryMinusOp > expr_t;
00317   return Expr<expr_t>(expr_t(*this));
00318       }
00319 
00321       CacheTaylor<T>& operator += (const T& x);
00322 
00324       CacheTaylor<T>& operator -= (const T& x);
00325 
00327       CacheTaylor<T>& operator *= (const T& x);
00328 
00330       CacheTaylor<T>& operator /= (const T& x);
00331 
00333       template <typename S> CacheTaylor<T>& operator += (const S& x);
00334 
00336       template <typename S> CacheTaylor<T>& operator -= (const S& x);
00337   
00339       template <typename S> CacheTaylor<T>& operator *= (const S& x);
00340 
00342       template <typename S> CacheTaylor<T>& operator /= (const S& x);
00343 
00345 
00346     }; // class CacheTaylor<T>
00347 
00348   } // namespace Tay
00349 
00350 } // namespace Sacado
00351 
00352 #include "Sacado_Tay_CacheTaylorTraits.hpp"
00353 #include "Sacado_Tay_CacheTaylorImp.hpp"
00354 #include "Sacado_Tay_CacheTaylorOps.hpp"
00355 
00356 #endif // SACADO_TAYLOR_CACHETAYLOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines