MUQ  0.4.3
MultiIndexSet.h
Go to the documentation of this file.
1 #ifndef MULTIINDEXSET_H_
2 #define MULTIINDEXSET_H_
3 
4 #include <vector>
5 #include <memory>
6 #include <set>
7 #include <map>
8 
10 
12 
13 namespace muq{
14  namespace Utilities{
15 
16  class H5Object;
17  class MultiIndexSet;
18  class MultiIndexFactory;
19 
20 
21  std::shared_ptr<MultiIndexSet> operator+=( std::shared_ptr<MultiIndexSet> x,
22  std::shared_ptr<MultiIndexSet> y);
23 
24  std::shared_ptr<MultiIndexSet> operator+=( std::shared_ptr<MultiIndexSet> x,
25  std::shared_ptr<MultiIndex> y);
26 
66 
67  friend class MultiIndexFactory;
68 
69  public:
70 
71  MultiIndexSet(const unsigned dimIn,
72  std::shared_ptr<MultiIndexLimiter> limiterIn = std::make_shared<NoLimiter>());
73 
75  static std::shared_ptr<MultiIndexSet> CloneExisting(std::shared_ptr<MultiIndexSet> const& original);
76 
78  virtual ~MultiIndexSet() = default;
79 
85  virtual void SetLimiter(std::shared_ptr<MultiIndexLimiter> const& limiterIn);
86 
88  virtual std::shared_ptr<MultiIndexLimiter> GetLimiter() const{return limiter;};
89 
90  // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
91  // * * * FIXED COMPONENTS
92  // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
93 
97  virtual std::vector<std::shared_ptr<MultiIndex>> GetAllMultiIndices() const {return allMultis;};
98 
99 
107  virtual std::shared_ptr<MultiIndex> const& IndexToMulti(unsigned activeIndex) const{return allMultis.at(active2global.at(activeIndex));};
108 
109 
116  virtual int MultiToIndex(std::shared_ptr<MultiIndex> const& input) const;
117 
119  virtual unsigned int GetMultiLength() const{return dim;};
120 
128  virtual Eigen::VectorXi GetMaxOrders() const{return maxOrders;};
129 
135  virtual std::shared_ptr<MultiIndex> const& at(int activeIndex){return IndexToMulti(activeIndex);}
136 
142  virtual std::shared_ptr<MultiIndex> operator[](int activeIndex){return allMultis[active2global[activeIndex]]; };
143 
148  virtual unsigned int Size() const{return active2global.size();};
149 
150  // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
151  // * * * ADAPTIVE COMPONENTS
152  // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
153 
161  virtual MultiIndexSet& operator+=(const MultiIndexSet &rhs);
162 
171  virtual MultiIndexSet& operator+=(std::shared_ptr<MultiIndex> const& rhs);
172 
178  virtual int Union(const MultiIndexSet &rhs);
179 
187  virtual void Activate(std::shared_ptr<MultiIndex> const& multiIndex);
188 
200  virtual int AddActive(std::shared_ptr<MultiIndex> const& newNode);
201 
210  virtual std::vector<unsigned> Expand(unsigned int activeIndex);
211 
220  virtual std::vector<unsigned> ForciblyExpand(unsigned int const activeIndex);
221 
230  virtual std::vector<unsigned> ForciblyActivate(std::shared_ptr<MultiIndex> const& multiIndex);
231 
236  virtual std::vector<std::shared_ptr<MultiIndex>> GetAdmissibleForwardNeighbors(unsigned int activeIndex);
237 
242  virtual std::vector<unsigned int> GetFrontier() const;
243 
247  virtual std::vector<unsigned int> GetStrictFrontier() const;
248 
253  virtual std::vector<unsigned int> GetBackwardNeighbors(unsigned int activeIndex) const;
254 
256  virtual std::vector<unsigned int> GetBackwardNeighbors(std::shared_ptr<MultiIndex> const& multiIndex) const;
257 
258  //*********************************************************
259  //Testing properties of an index/multiIndex
260  //*********************************************************
261 
263  virtual bool IsAdmissible(std::shared_ptr<MultiIndex> const& multiIndex) const;
264 
266  virtual bool IsExpandable(unsigned int activeIndex) const;
267 
269  virtual bool IsActive(std::shared_ptr<MultiIndex> const& multiIndex) const;
270 
272  virtual unsigned int NumActiveForward(unsigned int activeInd) const;
273 
275  virtual unsigned int NumForward(unsigned int activeInd) const;
276 
287  void ToHDF5(std::string filename, std::string dsetName="/multiindices") const;
288 
308  void ToHDF5(muq::Utilities::H5Object &group, std::string dsetName="multiindices") const;
309 
320  static std::shared_ptr<MultiIndexSet> FromHDF5(std::string filename, std::string dsetName="/multiiindices");
321 
328  static std::shared_ptr<MultiIndexSet> FromHDF5(muq::Utilities::H5Object &dset);
329 
330  protected:
331 
332  int AddInactive(std::shared_ptr<MultiIndex> const& newNode);
333 
334  virtual bool IsAdmissible(unsigned int globalIndex) const;
335  virtual bool IsActive(unsigned int globalIndex) const;
336 
337  void AddForwardNeighbors(unsigned int globalIndex, bool addInactive);
338  void AddBackwardNeighbors(unsigned int globalIndex, bool addInactive);
339 
340  void Activate(int globalIndex);
341  void ForciblyActivate(int localIndex, std::vector<unsigned int> &newInds);
342 
343  // Maps the active index to an entry in allMultis
344  std::vector<unsigned> active2global;
345 
346  // Maps a global index to an active index. Non-active values are -1
347  std::vector<int> global2active;
348 
349  // a vector of sets for the input and output edges.
350  std::vector<std::set<int>> outEdges; // edges going out of each multi
351  std::vector<std::set<int>> inEdges; // edges coming in to each multi
352 
353  Eigen::VectorXi maxOrders; // the maximum order in each dimension
354 
355  // the dimension (i.e., number of components) in each multi index
356  unsigned int dim;
357 
358  // A vector of both active and admissable multiindices. Global index.
359  std::vector<std::shared_ptr<MultiIndex>> allMultis;
360 
361  // store a MultiIndexLimiter that will tell us the feasible set (i.e. simplex, exp limited, etc...)
362  std::shared_ptr<MultiIndexLimiter> limiter;
363 
364  private:
365 
366  int AddMulti(std::shared_ptr<MultiIndex> const& newMulti);
367 
368  std::map<std::shared_ptr<MultiIndex>, unsigned int, MultiPtrComp> multi2global; // map from a multiindex to an integer
369 
370  MultiIndexSet() = default;
371 
372  }; // class MultiIndexSet
373 
374  } // namespace Utilities
375 } // namespace muq
376 
377 
378 
379 
380 #endif // MULTIINDEXSET_H_
A factory class with static methods for generating MultiIndexSets.
A class for holding, sorting, and adapting sets of multiindices.
Definition: MultiIndexSet.h:65
std::vector< std::shared_ptr< MultiIndex > > allMultis
virtual bool IsActive(std::shared_ptr< MultiIndex > const &multiIndex) const
Return true if the multiIndex is active.
void ForciblyActivate(int localIndex, std::vector< unsigned int > &newInds)
virtual bool IsExpandable(unsigned int activeIndex) const
Return true if one of the forward neighbors of index is admissible but not active.
virtual MultiIndexSet & operator+=(const MultiIndexSet &rhs)
Add another set of multiindices to this one.
virtual std::vector< unsigned > Expand(unsigned int activeIndex)
virtual ~MultiIndexSet()=default
virtual unsigned int Size() const
std::shared_ptr< MultiIndexLimiter > limiter
static std::shared_ptr< MultiIndexSet > FromHDF5(std::string filename, std::string dsetName="/multiiindices")
Loads a multiindex set from an HDF5 file.
static std::shared_ptr< MultiIndexSet > CloneExisting(std::shared_ptr< MultiIndexSet > const &original)
NOTE: does not perform a deep copy of the multiindices themselves, only pointers to the multiindices.
virtual std::vector< std::shared_ptr< MultiIndex > > GetAllMultiIndices() const
Definition: MultiIndexSet.h:97
virtual int Union(const MultiIndexSet &rhs)
Add all terms in rhs to this instance.
virtual unsigned int NumActiveForward(unsigned int activeInd) const
Returns the number of active forward neighbors.
virtual void SetLimiter(std::shared_ptr< MultiIndexLimiter > const &limiterIn)
virtual unsigned int NumForward(unsigned int activeInd) const
Returns the number of forward neighbors (active or inactive)
std::vector< std::set< int > > inEdges
virtual std::vector< unsigned int > GetStrictFrontier() const
virtual std::vector< unsigned int > GetFrontier() const
virtual Eigen::VectorXi GetMaxOrders() const
virtual unsigned int GetMultiLength() const
void AddBackwardNeighbors(unsigned int globalIndex, bool addInactive)
std::vector< std::set< int > > outEdges
std::map< std::shared_ptr< MultiIndex >, unsigned int, MultiPtrComp > multi2global
std::vector< int > global2active
virtual std::vector< unsigned > ForciblyActivate(std::shared_ptr< MultiIndex > const &multiIndex)
virtual int MultiToIndex(std::shared_ptr< MultiIndex > const &input) const
virtual void Activate(std::shared_ptr< MultiIndex > const &multiIndex)
void ToHDF5(std::string filename, std::string dsetName="/multiindices") const
Saves the multiindex set to a group in an HDF5 file.
int AddInactive(std::shared_ptr< MultiIndex > const &newNode)
int AddMulti(std::shared_ptr< MultiIndex > const &newMulti)
void AddForwardNeighbors(unsigned int globalIndex, bool addInactive)
virtual bool IsAdmissible(std::shared_ptr< MultiIndex > const &multiIndex) const
Determines whether the input multiIndex is currently admissible.
virtual std::shared_ptr< MultiIndex > const & at(int activeIndex)
virtual std::vector< unsigned > ForciblyExpand(unsigned int const activeIndex)
virtual int AddActive(std::shared_ptr< MultiIndex > const &newNode)
virtual std::shared_ptr< MultiIndex > operator[](int activeIndex)
virtual std::shared_ptr< MultiIndex > const & IndexToMulti(unsigned activeIndex) const
virtual std::vector< std::shared_ptr< MultiIndex > > GetAdmissibleForwardNeighbors(unsigned int activeIndex)
virtual std::vector< unsigned int > GetBackwardNeighbors(unsigned int activeIndex) const
std::vector< unsigned > active2global
virtual std::shared_ptr< MultiIndexLimiter > GetLimiter() const
Definition: MultiIndexSet.h:88
std::shared_ptr< MultiIndexSet > operator+=(std::shared_ptr< MultiIndexSet > x, std::shared_ptr< MultiIndexSet > y)