SundanceExprWithChildren.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_EXPRWITHCHILDREN_H
00043 #define SUNDANCE_EXPRWITHCHILDREN_H
00044 
00045 #include "SundanceDefs.hpp"
00046 #include "SundanceEvaluatableExpr.hpp"
00047 #include "SundanceCombinatorialUtils.hpp"
00048 #include "SundanceScalarExpr.hpp"
00049 #include "SundanceExpr.hpp"
00050 
00051 namespace Sundance
00052 {
00053 using namespace Sundance;
00054 using namespace Teuchos;
00055 
00056 
00057 /** 
00058  * ExprWithChildren is a base class for any evaluatable expression
00059  * that has child nodes, for example, sums and unary operators.
00060  * ExprWithChildren adds nothing new to the expr interface, but 
00061  * provides some common utilities for getting children
00062  * and recursing to children.
00063  */
00064 class ExprWithChildren : public virtual EvaluatableExpr
00065 {
00066 public:
00067   /** construct with a list of child operands */
00068   ExprWithChildren(const Array<RCP<ScalarExpr> >& children);
00069 
00070   /** virtual dtor */
00071   virtual ~ExprWithChildren() {;}
00072 
00073   /**
00074    * Do preprocessing to set up sparse evaluation in the given region 
00075    */
00076   virtual void setupEval(const EvalContext& context) const ;
00077 
00078   /** Determine whether this expression is constant. It will
00079    * be constant if all children are constant. */
00080   virtual bool isConstant() const ;
00081 
00082   /** Return the number of children */
00083   int numChildren() const {return children_.size();}
00084           
00085   /** downcast the i-th to an evaluatable expr */
00086   const EvaluatableExpr* evaluatableChild(int i) const ;
00087 
00088   /** downcast the i-th to a scalar expr */
00089   const ScalarExpr* scalarChild(int i) const 
00090     {return children_[i].get();}
00091 
00092   /** Get a handle to the i-th child */
00093   Expr child(int i) const {return Expr::handle(children_[i]);}
00094 
00095 
00096   /**
00097    * Find the maximum differentiation order acting on discrete
00098    * functions in this expression. 
00099    */
00100   virtual int maxDiffOrderOnDiscreteFunctions() const ;
00101 
00102   /**
00103    * Indicate whether this expression contains discrete functions.
00104    */
00105   virtual bool hasDiscreteFunctions() const ;
00106 
00107   /** */
00108   virtual Set<MultipleDeriv> 
00109   internalFindW(int order, const EvalContext& context) const ;
00110 
00111   /** */
00112   virtual Set<MultipleDeriv> 
00113   internalFindC(int order, const EvalContext& context) const ;
00114           
00115   /** */
00116   virtual Set<MultipleDeriv> 
00117   internalFindV(int order, const EvalContext& context) const ;
00118 
00119   /** */
00120   virtual void displayNonzeros(std::ostream& os, 
00121     const EvalContext& context) const ;
00122 
00123   /** */
00124   const Set<MultiSet<int> >& findQ_W(int order, 
00125     const EvalContext& context) const ;
00126 
00127   /** */
00128   const Set<MultiSet<int> >& findQ_C(int order, 
00129     const EvalContext& context) const ;
00130 
00131   /** */
00132   const Set<MultiSet<int> >& findQ_V(int order, 
00133     const EvalContext& context) const ;
00134 
00135   /** */
00136   virtual Set<MultiSet<int> > 
00137   internalFindQ_W(int order, 
00138     const EvalContext& context) const ;
00139 
00140   /** */
00141   virtual Set<MultiSet<int> > 
00142   internalFindQ_V(int order, 
00143     const EvalContext& context) const ;
00144 
00145   /** */
00146   virtual Set<MultiSet<int> > 
00147   internalFindQ_C(int order, 
00148     const EvalContext& context) const ;
00149 
00150   /** */
00151   const Set<MultiSet<int> >& getI_N() const ;
00152 
00153   /** */
00154   Set<MultiSet<int> > indexSetProduct(const Set<MultiSet<int> >& a,
00155     const Set<MultiSet<int> >& b) const ;
00156           
00157   /** Return true if any child returns true. The sum expression
00158    * will override this requiring all children to return true */
00159   virtual bool everyTermHasTestFunctions() const ;
00160 
00161   /** Test whether this expr contains a test function. 
00162    * If any child contains a test, return true. */
00163   virtual bool hasTestFunctions() const ;
00164 
00165   /** Test whether this expr contains an unknown function. 
00166    * If any child contains an unknown, return true. */
00167   virtual bool hasUnkFunctions() const ;
00168 
00169   /** */
00170   virtual void showSparsity(std::ostream& os, 
00171     const EvalContext& context) const ;
00172 
00173   /** */
00174   virtual void getUnknowns(Set<int>& unkID, Array<Expr>& unks) const ;
00175 
00176   /** */
00177   virtual void getTests(Set<int>& varID, Array<Expr>& vars) const ;
00178 
00179           
00180   /** */
00181   virtual int countNodes() const ;
00182 
00183   /** */
00184   virtual bool isLinear() const {return false;}
00185 
00186   /** */
00187   virtual bool isProduct() const {return false;}
00188 
00189   /** Indicate whether the expression is independent of the given 
00190    * functions */
00191   virtual bool isIndependentOf(const Expr& u) const ;
00192 
00193 
00194   /** */
00195   Set<MultiSet<int> > subsetContainingIndex(const Set<MultiSet<int> >& s,
00196     int index) const ;
00197 
00198   /** */
00199   virtual RCP<Array<Set<MultipleDeriv> > > 
00200   internalDetermineR(const EvalContext& context,
00201     const Array<Set<MultipleDeriv> >& RInput) const ;
00202 
00203   /** Determine whether the given child is needed to compute derivatives
00204    * of the given order */
00205   bool childIsRequired(int childIndex, int diffOrder,
00206     const EvalContext& context) const ;
00207 
00208 
00209 
00210   /** */
00211   Set<MultipleDeriv> product(const Array<int>& J, const Array<int>& K,
00212     DerivSubsetSpecifier dss,
00213     const EvalContext& context) const ;
00214 
00215   /** Append to the set of func IDs present in this expression. */
00216   virtual void accumulateFuncSet(Set<int>& funcIDs, 
00217     const Set<int>& activeSet) const ;
00218 
00219   /** */
00220   virtual void registerSpatialDerivs(const EvalContext& context, 
00221     const Set<MultiIndex>& miSet) const ;
00222 
00223   /** Ordering operator for use in transforming exprs to standard form */
00224   virtual bool lessThan(const ScalarExpr* other) const ;
00225 
00226 private:
00227   Array<RCP<ScalarExpr> > children_;
00228 
00229   static Map<int, Set<MultiSet<int> > >& cachedI_N()
00230     {static Map<int, Set<MultiSet<int> > > rtn; return rtn;}
00231 
00232   mutable Array<Map<EvalContext, Set<MultiSet<int> > > > contextToQWMap_;
00233   mutable Array<Map<EvalContext, Set<MultiSet<int> > > > contextToQVMap_;
00234   mutable Array<Map<EvalContext, Set<MultiSet<int> > > > contextToQCMap_;
00235 };          
00236 
00237       
00238 
00239 /** \relates ExprWithChildren */
00240 Array<Array<std::pair<int, Array<MultipleDeriv> > > > chainRuleDerivsOfArgs(int nArgs,
00241   const MultiSet<int>& bSet,
00242   const MultipleDeriv& c);
00243 
00244 /**  \relates ExprWithChildren */
00245 Array<Array<Array<int> > > bStructure(const Array<int>& b,
00246   const Array<Array<int> >& tmp);
00247 
00248 /**  \relates ExprWithChildren 
00249  * Return the set of (k,l) tuples appearing in the Constantine
00250  * and Savits formulation of the multivariable, multiargument
00251  * chain rule. 
00252  * \param s 
00253  * \param lambda
00254  * \param nu
00255  */
00256 Array<OrderedPair<Array<MultiSet<int> >, Array<MultipleDeriv> > >
00257 chainRuleTerms(int s, 
00258   const MultiSet<int>& lambda,
00259   const MultipleDeriv& nu) ;
00260       
00261 
00262 /** Return all subsets of a multiset. */
00263 Set<MultipleDeriv> multisetSubsets(const MultipleDeriv& x);
00264 
00265 /** Return the multiplicity of a chain rule term */
00266 int chainRuleMultiplicity(const MultipleDeriv& nu,
00267   const Array<MultiSet<int> >& K,
00268   const Array<MultipleDeriv>& L);
00269 }
00270 
00271 #endif

Site Contact