31 #ifndef __TSG_INDEX_MANIPULATOR_HPP
32 #define __TSG_INDEX_MANIPULATOR_HPP
70 namespace MultiIndexManipulations{
80 size_t num_dimensions = num_entries.size();
82 for(
auto &l : num_entries) num_total *= l;
84 auto iter = indexes.rbegin();
85 for(
int i=num_total-1; i>=0; i--){
87 auto l = num_entries.rbegin();
89 for(
size_t j = 0; j<num_dimensions; j++){
104 size_t c = num_dimensions -1;
106 std::vector<int> root(num_dimensions, 0);
107 std::vector<int> indexes;
108 while(is_in || (c > 0)){
110 indexes.insert(indexes.end(), root.begin(), root.end());
111 c = num_dimensions-1;
114 std::fill(root.begin() + c, root.end(), 0);
117 is_in = inside(root);
143 if (weights.empty())
linear = std::vector<int>(num_dimensions, 1);
145 if (!weights.empty()){
149 if (weights.empty()){
150 curved = std::vector<double>(num_dimensions, 0.0);
152 linear = std::vector<int>(weights.begin(), weights.begin() + num_dimensions);
153 curved = std::vector<double>(num_dimensions);
154 std::transform(weights.begin() + num_dimensions, weights.end(),
curved.begin(), [&](
int i)->double{ return (double) i; });
157 if (weights.empty()){
158 curved = std::vector<double>(num_dimensions, 1.0);
160 linear = std::vector<int>(num_dimensions, 1);
161 double exponent_normalization = (double) *std::min_element(weights.begin(), weights.end());
162 curved = std::vector<double>(num_dimensions);
163 std::transform(weights.begin(), weights.end(),
curved.begin(),
164 [&](
int i)->double{ return ((double) i) / exponent_normalization; });
171 for(
size_t i=0; i<
linear.size(); i++)
200 template<
typename CacheType, TypeDepth contour,
bool isotropic>
203 std::vector<std::vector<CacheType>> cache(num_dimensions);
204 std::vector<int> exactness_cache;
208 for(
auto &vec : cache) vec.reserve((
size_t) offset);
209 exactness_cache.resize((
size_t) offset);
210 exactness_cache[0] = 0;
211 for(
int i=1; i<offset; i++)
212 exactness_cache[i] = 1 + rule_exactness(i - 1);
214 exactness_cache.push_back(0);
217 for(
size_t j=0; j<num_dimensions; j++){
218 int wl = weights.
linear[j];
223 cache[j].push_back(w);
226 if (!isotropic && (i >= exactness_cache.size()))
227 exactness_cache.push_back(1 + rule_exactness((
int)(i - 1)));
229 int e = exactness_cache[i];
232 w = (CacheType)(wl * e);
234 w = (CacheType)(wl * e) + (CacheType)(wc * std::log1p((CacheType)e));
236 w = (CacheType)(pow((CacheType) (1 + e), wc));
239 cache[j].push_back(w);
241 }
while( (!isotropic && (std::ceil(w) <= (CacheType) offset)) || (isotropic && (i + 1 < (
size_t) offset)) );
253 template<
typename CacheType, TypeDepth contour>
254 inline CacheType
getIndexWeight(
int const index[], std::vector<std::vector<CacheType>>
const &cache){
256 for(
size_t j=0; j<cache.size(); j++)
258 w *= cache[j][index[j]];
260 w += cache[j][index[j]];
274 std::vector<int>
const &anisotropic_weights, std::vector<int>
const &level_limits);
357 template<
bool nested>
360 std::vector<int> num_points(num_dimensions);
362 for(
size_t j=0; j<num_dimensions; j++) num_points[j] = wrapper.
getNumPoints(levels[j]);
363 for(
auto n : num_points) num_total *= n;
365 std::vector<int> refs(num_total);
366 std::vector<int> p(num_dimensions);
368 for(
int i=0; i<num_total; i++){
370 auto n = num_points.rbegin();
371 for(
int j=(
int) num_dimensions-1; j>=0; j--){
372 p[j] = (nested) ? t % *n : wrapper.
getPointIndex(levels[j], t % *n);
398 size_t nz_weights = 0;
399 for(
auto w: weights)
if (w != 0) nz_weights++;
401 std::vector<int> indexes(nz_weights * num_dimensions);
403 auto iter = indexes.begin();
404 auto iset = mset.
begin();
405 for(
auto w: weights){
407 std::copy_n(iset, num_dimensions, iter);
408 std::advance(iter, num_dimensions);
410 std::advance(iset, num_dimensions);
424 active_w = std::vector<int>();
426 for(
auto w : tensors_w)
if (w != 0) active_w.push_back(w);
451 std::copy(point.begin(), point.end(), scratch.begin());
452 for(
int &d : scratch){
455 if (mset.
missing(scratch))
return false;
473 if (current.
empty()){
474 if (candidates.
missing(std::vector<int>(num_dimensions, 0))){
477 result =
MultiIndexSet(num_dimensions, std::vector<int>(num_dimensions, 0));
481 std::vector<int> scratch(num_dimensions);
487 if (!result.
empty()) total += result;
489 std::vector<int> kid(total.
getIndex(i), total.
getIndex(i) + num_dimensions);
499 if (loopon) result += update;
513 template<
bool limited>
517 std::vector<int> scratch(num_dimensions);
520 std::vector<int> kid(t, t + num_dimensions);
521 auto ilimit = level_limits.begin();
527 if ((*ilimit == -1) || (k <= *ilimit))
567 template<
class IndexList,
class RuleLike,
class OutputIteratorLike>
568 OutputIteratorLike
indexesToNodes(IndexList
const &list, RuleLike
const &rule, OutputIteratorLike nodes){
569 return std::transform(list.begin(), list.end(), nodes, [&](
int i)->double{ return rule.getNode(i); });
576 template<
class IteratorLike,
class RuleLike,
class OutputIteratorLike>
577 OutputIteratorLike
indexesToNodes(IteratorLike ibegin,
size_t num_entries, RuleLike
const &rule, OutputIteratorLike nodes){
578 return std::transform(ibegin, ibegin + num_entries, nodes, [&](
int i)->
double{
return rule.getNode(i); });
585 template<
class IndexList,
class RuleLike>
587 std::vector<double> result(std::distance(list.begin(), list.end()));
596 template<
class IteratorLike,
class RuleLike>
597 std::vector<double>
getIndexesToNodes(IteratorLike ibegin,
size_t num_entries, RuleLike
const &rule){
598 std::vector<double> result(num_entries);
612 MultiIndexSet const &points, std::vector<double>
const &coefficients,
double tol);
Generic 2D data structure divided into contiguous strips of fixed length (similar to a matrix).
Definition: tsgIndexSets.hpp:104
void appendStrip(typename std::vector< T >::const_iterator const &x)
Uses std::vector::insert to append the data.
Definition: tsgIndexSets.hpp:179
int getNumStrips() const
Returns the number of strips.
Definition: tsgIndexSets.hpp:149
Class that stores multi-indexes in sorted (lexicographical) order.
Definition: tsgIndexSets.hpp:234
size_t getNumDimensions() const
Returns the number of dimensions.
Definition: tsgIndexSets.hpp:263
int getSlot(const int *p) const
Returns the slot containing index p, returns -1 if not found.
bool missing(const std::vector< int > &p) const
Returns true if p is missing from the set, false otherwise.
Definition: tsgIndexSets.hpp:291
const int * getIndex(int i) const
Returns the i-th index of the set, useful to loop over all indexes or to cross reference with values.
Definition: tsgIndexSets.hpp:294
int getNumIndexes() const
Returns the number of indexes.
Definition: tsgIndexSets.hpp:265
bool empty() const
Returns true if there are no multi-indexes in the set, false otherwise.
Definition: tsgIndexSets.hpp:260
std::vector< int >::const_iterator begin() const
Returns a const iterator to the beginning of the internal data.
Definition: tsgIndexSets.hpp:277
A class to cache one dimensional rules, nodes, weight, meta-data, etc.
Definition: tsgOneDimensionalWrapper.hpp:53
int getPointIndex(int level, int j) const
Get the global index of point j on level (used only by non-nested rules).
Definition: tsgOneDimensionalWrapper.hpp:281
int getNumPoints(int level) const
Get the number of points for the level, can only query loaded levels.
Definition: tsgOneDimensionalWrapper.hpp:279
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition: tsgEnumerates.hpp:285
TypeDepth
Used by Global Sequence and Fourier grids, indicates the selection criteria.
Definition: tsgEnumerates.hpp:203
@ type_curved
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:213
@ type_level
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:209
@ type_hyperbolic
Ignoring the polynomial space, use rules with index .
Definition: tsgEnumerates.hpp:217
MultiIndexSet generateFullTensorSet(std::vector< int > const &num_entries)
Create a full-tensor multi-index set with num_entries in each direction.
Definition: tsgIndexManipulator.hpp:79
std::vector< int > computeTensorWeights(MultiIndexSet const &mset)
Computes the weights for the tensor linear combination, mset must be a lower complete set.
Definition: tsgIndexManipulator.cpp:545
std::vector< int > referencePoints(const int levels[], const OneDimensionalWrapper &wrapper, const MultiIndexSet &points)
Find the indexes of all points associated with the tensor with levels within the global points set.
Definition: tsgIndexManipulator.hpp:358
OutputIteratorLike indexesToNodes(IndexList const &list, RuleLike const &rule, OutputIteratorLike nodes)
Converts int-indexes to double-valued abscissas using the provided rule.
Definition: tsgIndexManipulator.hpp:568
MultiIndexSet createActiveTensors(const MultiIndexSet &mset, const std::vector< int > &weights)
Creates a set containing only the entries of mset that have non-zero weights.
Definition: tsgIndexManipulator.hpp:396
MultiIndexSet selectFlaggedChildren(const MultiIndexSet &mset, const std::vector< bool > &flagged, const std::vector< int > &level_limits)
Using the flagged map, create a set with the flagged children of mset but only if they obey the level...
Definition: tsgIndexManipulator.cpp:336
MultiIndexSet getLargestCompletion(MultiIndexSet const ¤t, MultiIndexSet const &candidates)
Return the largest subset of candidates such that adding it to current will preserve lower completene...
Definition: tsgIndexManipulator.hpp:469
MultiIndexSet generateLowerMultiIndexSet(size_t num_dimensions, std::function< bool(const std::vector< int > &index)> inside)
Generate the multi-index with entries satisfying the inside(), assumes that inside() defines a lower-...
Definition: tsgIndexManipulator.hpp:103
MultiIndexSet addExclusiveChildren(const MultiIndexSet &tensors, const MultiIndexSet &exclude, const std::vector< int > level_limits)
For a set of tensors create an mset that contain the children of indexes in tensors that are missing ...
Definition: tsgIndexManipulator.hpp:514
std::vector< double > getIndexesToNodes(IndexList const &list, RuleLike const &rule)
Overload that returns the result in a vector.
Definition: tsgIndexManipulator.hpp:586
std::vector< int > inferAnisotropicWeights(AccelerationContext const *acceleration, TypeOneDRule rule, TypeDepth depth, MultiIndexSet const &points, std::vector< double > const &coefficients, double tol)
Overload that returns the result in a vector.
Definition: tsgIndexManipulator.cpp:601
std::vector< int > getMaxIndexes(const MultiIndexSet &mset)
Returns a vector with the maximum index in each dimension.
Definition: tsgIndexManipulator.cpp:293
std::vector< int > computeLevels(MultiIndexSet const &mset)
Returns a vector that is the sum of entries of each multi-index in the set.
Definition: tsgIndexManipulator.cpp:280
void completeSetToLower(MultiIndexSet &set)
Expand the set with the minimum number of multi-indexes that will result in a lower complete set....
Definition: tsgIndexManipulator.cpp:113
void computeActiveTensorsWeights(MultiIndexSet const &tensors, MultiIndexSet &active_tensors, std::vector< int > &active_w)
Uses the computeTensorWeights() and createActiveTensors() to extract the active tensors and the activ...
Definition: tsgIndexManipulator.hpp:420
MultiIndexSet generateNonNestedPoints(const MultiIndexSet &tensors, const OneDimensionalWrapper &wrapper)
Converts a set of non-nested active tensors to the actual points, the wrapper gives mapping between l...
Definition: tsgIndexManipulator.cpp:457
MultiIndexSet createPolynomialSpace(const MultiIndexSet &tensors, std::function< int(int)> exactness)
For a set of tensors compute the corresponding polynomial space assuming the 1D rules have given exac...
Definition: tsgIndexManipulator.cpp:584
MultiIndexSet generateNestedPoints(const MultiIndexSet &tensors, std::function< int(int)> getNumPoints)
Converts a set of nested tensors to the actual points, where each level has getNumPoints() in each di...
Definition: tsgIndexManipulator.cpp:416
bool isLowerComplete(std::vector< int > const &point, MultiIndexSet const &mset, std::vector< int > &scratch)
Assuming that mset is lower complete, return true if adding the point will preserve completeness.
Definition: tsgIndexManipulator.hpp:450
Data2D< int > computeDAGup(MultiIndexSet const &mset)
Returns a Data2D structure where each strip holds the slot-index of the parents of indexes in mset (f...
Definition: tsgIndexManipulator.cpp:317
void resortIndexes(const MultiIndexSet &iset, std::vector< std::vector< int >> &map, std::vector< std::vector< int >> &lines1d)
Creates a map with sorted indexes dimension by dimension.
Definition: tsgIndexManipulator.cpp:489
size_t size_mult(IntA a, IntB b)
Converts two integer-like variables to size_t and returns the product..
Definition: tsgUtils.hpp:82
CacheType getIndexWeight(int const index[], std::vector< std::vector< CacheType >> const &cache)
Returns the weight for the multi-index using the cache, assuming level contour.
Definition: tsgIndexManipulator.hpp:254
MultiIndexSet selectTensors(size_t num_dimensions, int offset, TypeDepth type, std::function< int(int i)> rule_exactness, std::vector< int > const &anisotropic_weights, std::vector< int > const &level_limits)
Generate a lower complete multi-index set that satisfies the given properties.
Definition: tsgIndexManipulator.cpp:240
std::vector< std::vector< CacheType > > generateLevelWeightsCache(ProperWeights const &weights, std::function< int(int i)> rule_exactness, int offset)
Generate weights cache for the given parameters.
Definition: tsgIndexManipulator.hpp:201
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Wrapper class around GPU device ID, acceleration type and GpuEngine.
Definition: tsgAcceleratedDataStructures.hpp:576
Holds anisotropic weights in a usable format.
Definition: tsgIndexManipulator.hpp:139
std::vector< double > curved
The curved components of the weights type_curved contours; a scaled vector of exponents for type_hype...
Definition: tsgIndexManipulator.hpp:185
bool provenLower() const
Return true if the combination of weights and contour is guaranteed to give a lower-complete set.
Definition: tsgIndexManipulator.hpp:169
ProperWeights(size_t num_dimensions, TypeDepth type, std::vector< int > const &weights)
Interpret the single vector and form the proper weights, e.g., use defaults, scale hyperbolic weights...
Definition: tsgIndexManipulator.hpp:141
TypeDepth contour
Always equal to type_level, type_curved or type_hyperbolic, indicates the general contour and how the...
Definition: tsgIndexManipulator.hpp:181
size_t getNumDimensions() const
Return the number of dimensions.
Definition: tsgIndexManipulator.hpp:178
std::vector< int > linear
The linear components of the weights for type_level and type_curved contours; a vector of ones for ty...
Definition: tsgIndexManipulator.hpp:183
int minLinear() const
Return the smallest linear weight, used for normalization of the offset.
Definition: tsgIndexManipulator.hpp:176
Data structures for storing multi-indexes and floating point values.
Cache for one dimensional rules.