31 #ifndef __TSG_ONE_DIMENSIONAL_WRAPPER_HPP
32 #define __TSG_ONE_DIMENSIONAL_WRAPPER_HPP
69 isNonNested(OneDimensionalMeta::isNonNested(crule)),
70 num_levels(max_level + 1),
72 num_points(num_levels),
73 pntr(num_levels + 1, 0),
80 std::string message =
"ERROR: custom-tabulated rule needed with levels ";
81 message += std::to_string(num_levels);
82 message +=
", but only ";
84 message +=
" are provided.";
85 throw std::runtime_error(message);
90 for(
int l=0; l<num_levels; l++){
92 pntr[l+1] = pntr[l] + num_points[l];
95 for(
int l=0; l<num_levels; l++){
97 pntr[l+1] = pntr[l] + num_points[l];
100 int num_total = pntr[num_levels];
103 indx.reserve(num_total);
104 unique.reserve(num_total);
106 for(
int l=0; l<num_levels; l++){
107 int n = num_points[l];
128 for(
auto x : nodes[l]){
130 for(
int j=0; j<(int) unique.size(); j++){
138 point = (int) (unique.size() - 1);
140 indx.push_back(point);
144 unique.shrink_to_fit();
147 for(
int l=0; l<num_levels; l++){
148 int n = num_points[l];
150 double *x = nodes[l].data();
151 for(
int i=0; i<n; i++){
153 for(
int j=0; j<i; j++){
156 for(
int j=i+1; j<n; j++){
172 std::string message =
"ERROR: gauss-patterson rule needed with level ";
173 message += std::to_string(max_level);
174 message +=
", but only ";
176 message +=
" are hardcoded.";
177 throw std::runtime_error(message);
182 for(
int l=0; l<num_levels; l++){
183 weights[l].resize(num_points[l]);
184 for(
int i=0; i<num_points[l]; i++){
206 for(
int l=0; l<num_levels; l++){
207 int n = num_points[l];
208 weights[l].resize(n);
210 for(
int i=0; i<n; i++){
212 for(
int j=0; j<i; j++){
213 c /= (unique[i] - unique[j]);
215 for(
int j=i+1; j<n; j++){
216 c /= (unique[i] - unique[j]);
219 c /= (unique[i] - 1.0) * (unique[i] + 1.0);
226 for(
int l=0; l<num_levels; l++){
227 for(
int i=0; i<num_points[l]; i++){
232 for(
int l=0; l<num_levels; l++){
233 for(
int i=0; i<num_points[l]; i++){
238 for(
int l=0; l<num_levels; l++){
239 for(
int i=0; i<num_points[l]; i++){
247 int n = 1 + num_points[max_level] / 2;
248 std::vector<double> lag_x, lag_w;
250 for(
int l=0; l<num_levels; l++){
251 std::fill(weights[l].begin(), weights[l].end(), 0.0);
252 int npl = num_points[l];
253 std::vector<double> v(npl);
254 for(
int i=0; i<n; i++){
256 for(
int j=0; j<npl-1; j++){
257 v[j+1] = (lag_x[i] - unique[j]) * v[j];
259 v[npl-1] *= coeff[l][npl-1];
261 for(
int j=npl-2; j>=0; j--){
262 s *= (lag_x[i] - unique[j+1]);
263 v[j] *= s * coeff[l][j];
265 for(
int j=0; j<npl; j++){
266 weights[l][j] += lag_w[i] * v[j];
286 while(point >= num_points[l]) l++;
289 std::vector<int> getLevels(std::vector<int>
const &point){
290 std::vector<int> result(point.size());
291 for(
size_t i=0; i<point.size(); i++) result[i] =
getLevel(point[i]);
298 double getNode(
int j)
const{
return unique[j]; }
300 double getWeight(
int level,
int j)
const{
return weights[level][j]; }
303 const double*
getNodes(
int level)
const{
return (isNonNested) ? nodes[level].data() : unique.data(); }
316 const std::vector<double>&
getUnique()
const{
return unique; }
325 std::vector<int> offsets(num_points.size());
327 for(
size_t i=1; i<num_points.size(); i++)
328 offsets[i] = offsets[i-1] + num_points[i-1];
337 std::vector<int> num_points;
338 std::vector<int> pntr;
339 std::vector<int> indx;
341 std::vector<std::vector<double>> weights;
342 std::vector<std::vector<double>> nodes;
343 std::vector<double> unique;
345 std::vector<std::vector<double>> coeff;
Class providing manipulation of custom tabulated rules, file I/O and structured access to the points,...
Definition: tsgCoreOneDimensional.hpp:66
int getNumPoints(int level) const
Returns the number of points associated with the selected level.
Definition: tsgCoreOneDimensional.hpp:94
int getNumLevels() const
Returns the number of loaded levels.
Definition: tsgCoreOneDimensional.hpp:92
void getWeightsNodes(int level, std::vector< double > &w, std::vector< double > &x) const
Get the points x and quadrature weights w associated with the rule at the level.
A class to cache one dimensional rules, nodes, weight, meta-data, etc.
Definition: tsgOneDimensionalWrapper.hpp:53
std::vector< double > getAllCoeff() const
Return all coefficients concatenated per level.
Definition: tsgOneDimensionalWrapper.hpp:320
int getLevel(int point)
Returns the first level associated with this point (nested rules only!).
Definition: tsgOneDimensionalWrapper.hpp:283
std::vector< double > getAllNodes() const
Return all nodes concatenated per level.
Definition: tsgOneDimensionalWrapper.hpp:318
OneDimensionalWrapper(int max_level, TypeOneDRule crule, double alpha, double beta)
Overload that skips the custom rule altogether, more convenient in Fourier grids.
Definition: tsgOneDimensionalWrapper.hpp:275
const double * getCoefficients(int level) const
Get an array of the Lagrange coefficients associated with the level, the order matches getNodes().
Definition: tsgOneDimensionalWrapper.hpp:305
const double * getNodes(int level) const
Get an array of all nodes associated with the level (the array is ordered by local index).
Definition: tsgOneDimensionalWrapper.hpp:303
std::vector< int > getOffsetNodesPerLevel() const
Return cumulative points per level.
Definition: tsgOneDimensionalWrapper.hpp:324
const std::vector< double > & getUnique() const
Return reference to all unique nodes.
Definition: tsgOneDimensionalWrapper.hpp:316
double getWeight(int level, int j) const
Get the quadrature weight of the j-th node on the level (for non-nested rules, using index local to t...
Definition: tsgOneDimensionalWrapper.hpp:300
OneDimensionalWrapper(const CustomTabulated &custom, int max_level, TypeOneDRule crule, double alpha, double beta)
Load the cache.
Definition: tsgOneDimensionalWrapper.hpp:68
int getNumLevels() const
Get the number of cached levels.
Definition: tsgOneDimensionalWrapper.hpp:313
int getNumNodes() const
Get number of loaded nodes.
Definition: tsgOneDimensionalWrapper.hpp:296
TypeOneDRule getRule() const
Get the loaded rule.
Definition: tsgOneDimensionalWrapper.hpp:311
const std::vector< int > & getNumNodesPerLevel() const
Return reference to the number of nodes per level.
Definition: tsgOneDimensionalWrapper.hpp:322
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
double getNode(int j) const
Get the canonical coordinate of the node with global index j.
Definition: tsgOneDimensionalWrapper.hpp:298
const std::vector< int > & getPointsCount() const
Get a reference to the offsets of all point counts used by the CacheLagrange class.
Definition: tsgOneDimensionalWrapper.hpp:308
~OneDimensionalWrapper()=default
Default destructor.
int getNumPoints(int level) const
Get the number of points for the level, can only query loaded levels.
Definition: tsgOneDimensionalWrapper.hpp:279
OneDimensionalWrapper()
Default constructor, create an emptry cache.
Definition: tsgOneDimensionalWrapper.hpp:56
Rule with hard-corded tabulated points and weights.
Definition: tsgHardCodedTabulatedRules.hpp:64
double getWeight(int level, int point) const
Return the quadrature weight for level and given point.
std::vector< double > getNodes(int level) const
Returns the nodes for the level, note that the nodes are nested.
static int getNumLevels()
Return the number of hard-coded levels.
Definition: tsgHardCodedTabulatedRules.hpp:72
TypeOneDRule
Used to specify the one dimensional family of rules that induces the sparse grid.
Definition: tsgEnumerates.hpp:285
@ rule_minlebesgue
A greedy sequence rule with nodes added to minimize the Lebesgue constant.
Definition: tsgEnumerates.hpp:321
@ rule_gaussgegenbauer
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:343
@ rule_gausshermite
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:355
@ rule_gausslegendre
Non-nested rule but optimized for integration.
Definition: tsgEnumerates.hpp:329
@ rule_fejer2
Similar to rule_clenshawcurtis but with nodes strictly in the interior.
Definition: tsgEnumerates.hpp:293
@ rule_leja
Classic sequence rule, moderate Lebesgue constant growth (empirical result only).
Definition: tsgEnumerates.hpp:299
@ rule_rlejashifteddouble
Same as rule_rlejashifted but doubling the number of nodes per level, which reduced the Lebesgue cons...
Definition: tsgEnumerates.hpp:315
@ rule_gausslaguerreodd
Same as rule_gausslaguerre but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:353
@ rule_chebyshevodd
Same as rule_chebyshev but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:297
@ rule_minlebesgueodd
Same as rule_minlebesgue but using only odd levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:323
@ rule_gausslegendreodd
Same as rule_gausslegendre but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:331
@ rule_gausschebyshev1
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:335
@ rule_gausslaguerre
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:351
@ rule_mindelta
A greedy sequence rule with nodes added to minimize the norm of the surplus operator.
Definition: tsgEnumerates.hpp:325
@ rule_gaussgegenbauerodd
Same as rule_gaussgegenbauer but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:345
@ rule_customtabulated
User provided rule, nodes and weights must be provided with a separate file.
Definition: tsgEnumerates.hpp:359
@ rule_gausshermiteodd
Same as rule_gausshermite but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:357
@ rule_clenshawcurtis
Classic nested rule using Chebyshev nodes with very low Lebesgue constant.
Definition: tsgEnumerates.hpp:289
@ rule_gausschebyshev2odd
Same as rule_gausschebyshev2 but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:341
@ rule_maxlebesgueodd
Same as rule_maxlebesgue but using only odd levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:319
@ rule_maxlebesgue
A greedy sequence rule with nodes placed at the maximum of the Lebesgue function.
Definition: tsgEnumerates.hpp:317
@ rule_clenshawcurtis0
Same as rule_clenshawcurtis but with modified basis that assumes the model is zero at the boundary.
Definition: tsgEnumerates.hpp:291
@ rule_none
Null rule, should never be used as input (default rule for an empty grid).
Definition: tsgEnumerates.hpp:287
@ rule_chebyshev
Using Chebyshev nodes with very low Lebesgue constant and slow node growth, but non-nested.
Definition: tsgEnumerates.hpp:295
@ rule_gausschebyshev1odd
Same as rule_gausschebyshev1 but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:337
@ rule_rlejashifted
Similar sequence to rule_rleja but with nodes strictly in the interior.
Definition: tsgEnumerates.hpp:311
@ rule_gausschebyshev2
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:339
@ rule_mindeltaodd
Same as rule_mindelta but using only odd levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:327
@ rule_rlejadouble4
Using rule_rleja nodes but doubling the nodes every 4 levels, reduces the Lebesgue constant.
Definition: tsgEnumerates.hpp:307
@ rule_gaussjacobiodd
Same as rule_gaussjacobi but using only odd levels, partially mitigates the non-nested issues.
Definition: tsgEnumerates.hpp:349
@ rule_lejaodd
Same as rule_leja but using only odd levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:301
@ rule_rlejaodd
Same as rule_rleja but using only odd levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:309
@ rule_rlejadouble2
Using rule_rleja nodes but doubling the nodes every 2 levels, reduces the Lebesgue constant.
Definition: tsgEnumerates.hpp:305
@ rule_rlejashiftedeven
Same as rule_rlejashifted but using only even levels, quadrature is more stable.
Definition: tsgEnumerates.hpp:313
@ rule_gausspatterson
Nested rule that is optimized for integration, probably the best integration rule in more than 2 dime...
Definition: tsgEnumerates.hpp:333
@ rule_rleja
Classic sequence rule based on complex analysis, moderate Lebesgue constant growth (theoretically pro...
Definition: tsgEnumerates.hpp:303
@ rule_gaussjacobi
Non-nested rule optimized for integral of the form .
Definition: tsgEnumerates.hpp:347
constexpr double num_tol
Numerical tolerance for various algorithms.
Definition: tsgMathUtils.hpp:132
std::vector< T > mergeVectors(std::vector< std::vector< T >> const &vec)
Takes a vector of vectors and returns a single contiguous vector.
Definition: tsgUtils.hpp:105
void getGaussHermite(int m, std::vector< double > &w, std::vector< double > &x, double alpha)
Generate Gauss-Hermite weights w and points x for (input) number of points m, using parameters alpha.
void getChebyshev(int m, std::vector< double > &w, std::vector< double > &x)
Generate Chebyshev weights w and points x for (input) number of points m.
std::vector< double > getRLejaCentered(int n)
Generate the first n R-Leja nodes, starting with 0, 1, -1, ...
void getGaussChebyshev1(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Chebyshev type 1 weights w and points x for (input) number of points m.
double getClenshawCurtisWeightZero(int level, int point)
Return the Clenshaw-Curtis zero-boundary condition weight for the level and node indexed by point.
std::vector< double > getClenshawCurtisNodes(int level)
Generate Clenshaw-Curtis nodes for the level.
std::vector< double > getFourierNodes(int level)
Generate the Fourier nodes for the given level, uniformly distributed points with right-most point om...
double getFejer2Weight(int level, int point)
Return the Fejer type 2 weight for the level and node indexed by point.
std::vector< double > getRLejaShifted(int n)
Generate the first n R-Leja nodes, starting with -0.5, 0.5, ...
void getGaussChebyshev2(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Chebyshev type 2 weights w and points x for (input) number of points m.
void getGaussLegendre(int m, std::vector< double > &w, std::vector< double > &x)
Generate Gauss-Legendre weights w and points x for (input) number of points m.
std::vector< double > getClenshawCurtisNodesZero(int level)
Generate Clenshaw-Curtis zero-boundary condition nodes for the level.
std::vector< double > getFejer2Nodes(int level)
Generate Fejer type 2 nodes for the level.
std::vector< double > getRLeja(int n)
Generate the first n R-Leja nodes, starting with 1, -1, 0, ...
double getClenshawCurtisWeight(int level, int point)
Return the Clenshaw-Curtis weight for the level and node indexed by point.
void getGaussLaguerre(int m, std::vector< double > &w, std::vector< double > &x, double alpha)
Generate Gauss-Laguerre weights w and points x for (input) number of points m, using parameters alpha...
void getGaussJacobi(int m, std::vector< double > &w, std::vector< double > &x, double alpha, double beta)
Generate Gauss-Jacobi weights w and points x for (input) number of points m, using parameters alpha a...
Encapsulates the Tasmanian Sparse Grid module.
Definition: TasmanianSparseGrid.hpp:68
Hard-coded nodes and weights.