SundanceChainRuleSum.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCE_CHAINRULESUM_H
00032 #define SUNDANCE_CHAINRULESUM_H
00033 
00034 #include "SundanceDefs.hpp"
00035 #include "SundanceEvalManager.hpp"
00036 #include "SundanceEvaluator.hpp"
00037 #include "Teuchos_Array.hpp"
00038 
00039 namespace Sundance 
00040 {
00041 
00042 using namespace Teuchos;
00043 
00044 
00045 /** */
00046 class IndexPair 
00047 {
00048 public:
00049   /** */
00050   IndexPair(int argIndex, int valueIndex)
00051     : argIndex_(argIndex), valueIndex_(valueIndex) {;}
00052       
00053   /** */
00054   int argIndex() const {return argIndex_;}
00055       
00056   /** */
00057   int valueIndex() const {return valueIndex_;}
00058       
00059 private:
00060   int argIndex_;
00061   int valueIndex_;
00062 };
00063 
00064 /** */
00065 class DerivProduct
00066 {
00067 public:
00068   /** */
00069   DerivProduct() : coeff_(1.0), constants_(), variables_() {}
00070   /** */
00071   DerivProduct(const double& coeff) : coeff_(coeff), constants_(), variables_() {}
00072 
00073   /** */
00074   void addConstantFactor(const IndexPair& p) {constants_.append(p);}
00075 
00076   /** */
00077   void addVariableFactor(const IndexPair& p) {variables_.append(p);}
00078 
00079   /** */
00080   bool isConstant() const {return numVariables()==0;}
00081 
00082   /** */
00083   int numConstants() const {return constants_.size();}
00084 
00085   /** */
00086   int numVariables() const {return variables_.size();}
00087 
00088   /** */
00089   const double& coeff() const {return coeff_;}
00090 
00091   /** */
00092   const IndexPair& constant(int i) const {return constants_[i];}
00093 
00094   /** */
00095   const IndexPair& variable(int i) const {return variables_[i];}
00096         
00097 private:
00098 
00099   double coeff_;
00100 
00101   Array<IndexPair> constants_;
00102 
00103   Array<IndexPair> variables_;
00104 };
00105 
00106 
00107 /** */
00108 class ChainRuleSum : public ObjectWithClassVerbosity<Evaluator>
00109 {
00110 public:
00111   /** */
00112   ChainRuleSum(const MultipleDeriv& md, 
00113     int resultIndex,
00114     bool resultIsConstant);
00115 
00116   /** */
00117   void addTerm(int argDerivIndex, 
00118     bool argDerivIsConstant,
00119     const Array<DerivProduct>& sum);
00120 
00121 
00122   /** */
00123   void evalConstant(const EvalManager& mgr,
00124     const Array<RCP<Array<double> > >& constantArgResults,
00125     const Array<double>& constantArgDerivs,
00126     double& constResult) const ;
00127 
00128   /** */
00129   void evalVar(const EvalManager& mgr,
00130     const Array<RCP<Array<double> > >& constantArgResults,
00131     const Array<RCP<Array<RCP<EvalVector> > > > & vArgResults,
00132     const Array<double>& constantArgDerivs,
00133     const Array<RCP<EvalVector> >& varArgDerivs,
00134     RCP<EvalVector>& varResult) const ;
00135 
00136   /** */
00137   int resultIndex() const {return resultIndex_;}
00138 
00139   /** */
00140   bool resultIsConstant() const {return resultIsConstant_;}
00141 
00142   /** */
00143   int numTerms() const {return terms_.size();}
00144 
00145   /** */
00146   bool argDerivIsConstant(int i) const {return argDerivIsConstant_[i];}
00147 
00148   /** */
00149   int argDerivIndex(int i) const {return argDerivIndex_[i];}
00150 
00151   /** */
00152   const Array<DerivProduct>& terms(int i) const {return terms_[i];}
00153 
00154   /** */
00155   const MultipleDeriv& deriv() const {return md_;}
00156 
00157 private:
00158   MultipleDeriv md_;
00159   int resultIndex_;
00160   bool resultIsConstant_;
00161 
00162   Array<int> argDerivIndex_;
00163   Array<int> argDerivIsConstant_;
00164   Array<Array<DerivProduct> > terms_;
00165 };
00166 
00167 }
00168                
00169 #endif

Site Contact