SundanceChainRuleSum.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_CHAINRULESUM_H
00043 #define SUNDANCE_CHAINRULESUM_H
00044 
00045 #include "SundanceDefs.hpp"
00046 #include "SundanceEvalManager.hpp"
00047 #include "SundanceEvaluator.hpp"
00048 #include "Teuchos_Array.hpp"
00049 
00050 namespace Sundance 
00051 {
00052 
00053 using namespace Teuchos;
00054 
00055 
00056 /** */
00057 class IndexPair 
00058 {
00059 public:
00060   /** */
00061   IndexPair(int argIndex, int valueIndex)
00062     : argIndex_(argIndex), valueIndex_(valueIndex) {;}
00063       
00064   /** */
00065   int argIndex() const {return argIndex_;}
00066       
00067   /** */
00068   int valueIndex() const {return valueIndex_;}
00069       
00070 private:
00071   int argIndex_;
00072   int valueIndex_;
00073 };
00074 
00075 /** */
00076 class DerivProduct
00077 {
00078 public:
00079   /** */
00080   DerivProduct() : coeff_(1.0), constants_(), variables_() {}
00081   /** */
00082   DerivProduct(const double& coeff) : coeff_(coeff), constants_(), variables_() {}
00083 
00084   /** */
00085   void addConstantFactor(const IndexPair& p) {constants_.append(p);}
00086 
00087   /** */
00088   void addVariableFactor(const IndexPair& p) {variables_.append(p);}
00089 
00090   /** */
00091   bool isConstant() const {return numVariables()==0;}
00092 
00093   /** */
00094   int numConstants() const {return constants_.size();}
00095 
00096   /** */
00097   int numVariables() const {return variables_.size();}
00098 
00099   /** */
00100   const double& coeff() const {return coeff_;}
00101 
00102   /** */
00103   const IndexPair& constant(int i) const {return constants_[i];}
00104 
00105   /** */
00106   const IndexPair& variable(int i) const {return variables_[i];}
00107         
00108 private:
00109 
00110   double coeff_;
00111 
00112   Array<IndexPair> constants_;
00113 
00114   Array<IndexPair> variables_;
00115 };
00116 
00117 
00118 /** */
00119 class ChainRuleSum : public ObjectWithClassVerbosity<Evaluator>
00120 {
00121 public:
00122   /** */
00123   ChainRuleSum(const MultipleDeriv& md, 
00124     int resultIndex,
00125     bool resultIsConstant);
00126 
00127   /** */
00128   void addTerm(int argDerivIndex, 
00129     bool argDerivIsConstant,
00130     const Array<DerivProduct>& sum);
00131 
00132 
00133   /** */
00134   void evalConstant(const EvalManager& mgr,
00135     const Array<RCP<Array<double> > >& constantArgResults,
00136     const Array<double>& constantArgDerivs,
00137     double& constResult) const ;
00138 
00139   /** */
00140   void evalVar(const EvalManager& mgr,
00141     const Array<RCP<Array<double> > >& constantArgResults,
00142     const Array<RCP<Array<RCP<EvalVector> > > > & vArgResults,
00143     const Array<double>& constantArgDerivs,
00144     const Array<RCP<EvalVector> >& varArgDerivs,
00145     RCP<EvalVector>& varResult) const ;
00146 
00147   /** */
00148   int resultIndex() const {return resultIndex_;}
00149 
00150   /** */
00151   bool resultIsConstant() const {return resultIsConstant_;}
00152 
00153   /** */
00154   int numTerms() const {return terms_.size();}
00155 
00156   /** */
00157   bool argDerivIsConstant(int i) const {return argDerivIsConstant_[i];}
00158 
00159   /** */
00160   int argDerivIndex(int i) const {return argDerivIndex_[i];}
00161 
00162   /** */
00163   const Array<DerivProduct>& terms(int i) const {return terms_[i];}
00164 
00165   /** */
00166   const MultipleDeriv& deriv() const {return md_;}
00167 
00168 private:
00169   MultipleDeriv md_;
00170   int resultIndex_;
00171   bool resultIsConstant_;
00172 
00173   Array<int> argDerivIndex_;
00174   Array<int> argDerivIsConstant_;
00175   Array<Array<DerivProduct> > terms_;
00176 };
00177 
00178 }
00179                
00180 #endif

Site Contact