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

Site Contact