SundanceChainRuleEvaluator.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef SUNDANCE_CHAINRULEEVALUATOR_H
00043 #define SUNDANCE_CHAINRULEEVALUATOR_H
00044 
00045 #include "SundanceDefs.hpp"
00046 #include "SundanceSubtypeEvaluator.hpp"
00047 #include "SundanceExprWithChildren.hpp"
00048 #include "SundanceChainRuleSum.hpp"
00049 
00050 namespace Sundance 
00051 {
00052 /** 
00053  *
00054  */
00055 class ChainRuleEvaluator : public SubtypeEvaluator<ExprWithChildren>
00056 {
00057 public:
00058 
00059   /** */
00060   ChainRuleEvaluator(const ExprWithChildren* expr, 
00061     const EvalContext& context);
00062 
00063   /** */
00064   virtual ~ChainRuleEvaluator(){;}
00065 
00066   /** */
00067   virtual void internalEval(const EvalManager& mgr,
00068     Array<double>& constantResults,
00069     Array<RCP<EvalVector> >& vectorResults) const ;
00070 
00071   /** */
00072   int numChildren() const {return childEvaluators_.size();}
00073 
00074   /** */
00075   int constArgDerivIndex(const MultiSet<int>& df) const ;
00076 
00077   /** */
00078   int varArgDerivIndex(const MultiSet<int>& df) const ;
00079 
00080   /** */
00081   TEUCHOS_TIMER(chainRuleEvalTimer, "chain rule evaluation");
00082 
00083 
00084   /** */
00085   const Array<Array<int> >& nComps(int N, int n) const ;
00086 
00087   /** */
00088   void resetNumCalls() const ;
00089 
00090   /** 
00091    * Evaluate the derivatives of the expression with respect to
00092    * the arguments.
00093    *
00094    * \param mgr Manager for this evaluation step
00095    *
00096    * \param constDerivsOfArgs Constant values and functional
00097    * derivatives of arguments.  The outer array index is over
00098    * arguments. The inner array index is over functional
00099    * derivatives of that argument.
00100    *
00101    * \param varDerivsOfArgs Variable values and functional
00102    * derivatives of arguments.  The outer array index is over
00103    * arguments. The inner array index is over functional
00104    * derivatives of that argument.
00105    *
00106    * \param constArgDerivs Constant-valued derivatives of expr wrt
00107    * arguments.
00108    *
00109    * \param varArgDerivs Variable-valued derivatives of expr wrt
00110    * arguments.
00111    */
00112 
00113   virtual void evalArgDerivs(const EvalManager& mgr,
00114     const Array<RCP<Array<double> > >& constDerivsOfArgs,
00115     const Array<RCP<Array<RCP<EvalVector> > > >& varDerivOfArgs,
00116     Array<double>& constArgDerivs,
00117     Array<RCP<EvalVector> >& varArgDerivs) const = 0 ;
00118 
00119 
00120   static Set<MultiSet<MultipleDeriv> > chainRuleBins(const MultipleDeriv& d,
00121     const MultiSet<int>& q);
00122       
00123 protected:
00124   /** The init() function should be called from the derived class ctors */
00125   void init(const ExprWithChildren* expr, 
00126     const EvalContext& context);
00127 
00128   /** */
00129   void addConstArgDeriv(const MultiSet<int>& df, int index);
00130 
00131   /** */
00132   void addVarArgDeriv(const MultiSet<int>& df, int index);
00133 
00134   /** */
00135   const Evaluator* childEvaluator(int i) const {return childEvaluators_[i].get();}
00136 
00137   /** */
00138   const SparsitySuperset* childSparsity(int i) const {return childSparsity_[i].get();}
00139 
00140   static MultipleDeriv makeMD(const Array<Deriv>& d) ;
00141 
00142   /** Returns the binomial coefficient */
00143   double choose(int N, int n) const ;
00144       
00145   /** Returns the factorial of n */
00146   double fact(int n) const ;
00147 
00148 
00149   /** Returns the stirling number of the second kind */
00150   double stirling2(int n, int k) const ;
00151 
00152   /** */
00153   int derivComboMultiplicity(const MultiSet<MultipleDeriv>& b) const ;
00154 
00155 
00156 private:
00157 
00158       
00159   Array<RCP<ChainRuleSum> > expansions_;
00160 
00161   Array<RCP<Evaluator> > childEvaluators_;
00162 
00163   Array<RCP<SparsitySuperset> > childSparsity_;
00164 
00165   Map<MultiSet<int>, int> constArgDerivMap_;
00166 
00167   Map<MultiSet<int>, int> varArgDerivMap_;
00168 
00169   int zerothDerivResultIndex_;
00170 
00171   bool zerothDerivIsConstant_;
00172 
00173   static Map<OrderedPair<int, int>, Array<Array<int> > >& compMap() ;
00174 };
00175 
00176 /** */
00177 MultipleDeriv makeDeriv(const Expr& a);
00178 
00179 /** */
00180 MultipleDeriv makeDeriv(const Expr& a, const Expr& b);
00181 
00182 /** */
00183 MultipleDeriv makeDeriv(const Expr& a, const Expr& b, const Expr& c);
00184 
00185     
00186 }
00187 
00188 
00189 #endif

Site Contact