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 
00094 
00096       const T& val() const { return coeff_[0];}
00097 
00099       T& val() { return coeff_[0];}
00100 
00102 
00107 
00109       unsigned int degree() const { return coeff_.size()-1;}
00110 
00112       bool hasFastAccess(unsigned int d) const { return coeff_.size()>=d+1;}
00113 
00115       const std::valarray<T>& coeff() const { return coeff_;}
00116 
00118       const T coeff(unsigned int i) const { 
00119   T tmp= i<coeff_.size() ? coeff_[i]:T(0.); return tmp;}
00120 
00122       T coeff(unsigned int i) { 
00123   T tmp= i<coeff_.size() ? coeff_[i]:T(0.); return tmp;}
00124     
00126       T& fastAccessCoeff(unsigned int i) { return coeff_[i];}
00127 
00129       T fastAccessCoeff(unsigned int i) const { return coeff_[i];}
00130 
00132       void allocateCache(unsigned int d) const {}
00133     
00135 
00136     protected:
00137 
00139       void resizeCoeffs(unsigned int dnew) {
00140   std::valarray<T> tmp = coeff_;
00141   std::slice s(0,coeff_.size(),1);
00142   coeff_.resize(dnew+1,T(0.));
00143   coeff_[s] = tmp;
00144       }
00145 
00146     protected:
00147 
00149       std::valarray<T> coeff_;
00150 
00151     }; // class CacheTaylorImplementation
00152 
00154 
00157     template <typename T> 
00158     class Expr< CacheTaylorImplementation<T> > : 
00159       public CacheTaylorImplementation<T> {
00160 
00161     public:
00162 
00164       Expr() : CacheTaylorImplementation<T>() {}
00165 
00167 
00170       Expr(const T & x) : CacheTaylorImplementation<T>(x) {}
00171 
00173 
00176       Expr(unsigned int d, const T & x) : CacheTaylorImplementation<T>(d,x) {}
00177 
00179       Expr(const Expr& x) : CacheTaylorImplementation<T>(x) {}
00180 
00181     }; // class Expr< CacheTaylorImplementation<T> >
00182 
00184 
00188     template <typename T>
00189     class CacheTaylor : public Expr< CacheTaylorImplementation<T> > {
00190 
00191     public:
00192 
00194       template <typename U> 
00195       struct apply {
00196   typedef CacheTaylor<U> type;
00197       };
00198 
00203 
00205       CacheTaylor() : Expr< CacheTaylorImplementation<T> >() {}
00206 
00208 
00211       CacheTaylor(const T & x) : Expr< CacheTaylorImplementation<T> >(x) {}
00212 
00214 
00217       CacheTaylor(unsigned int d, const T & x) : 
00218   Expr< CacheTaylorImplementation<T> >(d,x) {}
00219 
00221       CacheTaylor(const CacheTaylor& x) : Expr< CacheTaylorImplementation<T> >(x) {}
00222 
00224       template <typename S> CacheTaylor(const Expr<S>& x);
00225 
00227 
00229       ~CacheTaylor() {}
00230 
00235 
00237       CacheTaylor<T>& operator=(const T& v);
00238 
00240       CacheTaylor<T>& operator=(const CacheTaylor<T>& x);
00241 
00243       template <typename S> CacheTaylor<T>& operator=(const Expr<S>& x); 
00244 
00246 
00251 
00253       inline Expr< UnaryExpr< CacheTaylor<T>, UnaryPlusOp > >
00254       operator + () const {
00255   typedef UnaryExpr< CacheTaylor<T>, UnaryPlusOp > expr_t;
00256   return Expr<expr_t>(expr_t(*this));
00257       }
00258 
00260       inline Expr< UnaryExpr< CacheTaylor<T>, UnaryMinusOp > >
00261       operator - () const {
00262   typedef UnaryExpr< CacheTaylor<T>, UnaryMinusOp > expr_t;
00263   return Expr<expr_t>(expr_t(*this));
00264       }
00265 
00267       CacheTaylor<T>& operator += (const T& x);
00268 
00270       CacheTaylor<T>& operator -= (const T& x);
00271 
00273       CacheTaylor<T>& operator *= (const T& x);
00274 
00276       CacheTaylor<T>& operator /= (const T& x);
00277 
00279       template <typename S> CacheTaylor<T>& operator += (const S& x);
00280 
00282       template <typename S> CacheTaylor<T>& operator -= (const S& x);
00283   
00285       template <typename S> CacheTaylor<T>& operator *= (const S& x);
00286 
00288       template <typename S> CacheTaylor<T>& operator /= (const S& x);
00289 
00291 
00292     }; // class CacheTaylor<T>
00293 
00294   } // namespace Tay
00295 
00296 } // namespace Sacado
00297 
00298 #include "Sacado_Tay_CacheTaylorTraits.hpp"
00299 #include "Sacado_Tay_CacheTaylorImp.hpp"
00300 #include "Sacado_Tay_CacheTaylorOps.hpp"
00301 
00302 #endif // SACADO_TAYLOR_CACHETAYLOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines