Sparse Grids Example 10: local polynomial vs. wavelet grids.
Wavelet basis has a number of desirable properties compared to the simple local polynomials, most notably, the basis coefficients are much sharper estimates of the local approximation error. In an adaptive refinement context, this often leads to a decrease in the total number of nodes. However, the wavelets also have large Lebesgue constant especially around the boundary, which can have the converse effect.
#endif
cout << "\n---------------------------------------------------------------------------------------------------\n";
cout << std::scientific; cout.precision(4);
cout << "Example 10: comparison between local polynomial and wavelet grids\n\n";
int const num_inputs = 2;
int const num_test_points = 1000;
std::vector<double> test_points(num_test_points * num_inputs);
std::minstd_rand park_miller(42);
std::uniform_real_distribution<double> domain(-1.0, 1.0);
for(auto &t : test_points) t = domain(park_miller);
std::function<void(double const x[], double y[], size_t)> model)->
double{
std::vector<double> grid_result;
double err = 0.0;
for(int i=0; i<num_test_points; i++){
double model_result;
model(&test_points[i*num_inputs], &model_result, 0);
err = std::max(err, std::abs(grid_result[i] - model_result));
}
return err;
};
auto sharp_model = [](double const x[], double y[], size_t)->
void{ y[0] = x[0] / (1.0 + 100.0 * std::exp(-10.0 * x[1])); };
int const order = 1;
cout << "Using batch refinement:\n"
<< setw(22) << "polynomial" << setw(22) << "wavelet\n"
<< setw(8) << "points" << setw(14) << "error"
<< setw(8) << "points" << setw(14) << "error\n";
double const tolerance = 1.E-5;
while((grid_poly.getNumNeeded() > 0) || (grid_wavelet.getNumNeeded() > 0)){
cout << setw(8) << grid_poly.getNumLoaded()
<< setw(14) << get_error(grid_poly, sharp_model)
<< setw(8) << grid_wavelet.getNumLoaded()
<< setw(14) << get_error(grid_wavelet, sharp_model) << "\n";
}
cout << "\n";
#ifndef __TASMANIAN_DOXYGEN_SKIP
The master-class that represents an instance of a Tasmanian sparse grid.
Definition: TasmanianSparseGrid.hpp:293
void evaluateBatch(std::vector< FloatType > const &x, std::vector< FloatType > &y) const
Computes the value of the interpolant (or point-wise approximation) for a batch of points.
@ rule_localp
Nested rule with a hierarchy of uniformly distributed nodes and functions with compact support.
Definition: tsgEnumerates.hpp:362
@ refine_fds
Anisotropic refinement adding children only if the parents are already included.
Definition: tsgEnumerates.hpp:433
void loadNeededValues(std::function< void(double const x[], double y[], size_t thread_id)> model, TasmanianSparseGrid &grid, size_t num_threads)
Loads the current grid with model values, does not perform any refinement.
Definition: tsgLoadNeededValues.hpp:104
TasmanianSparseGrid makeWaveletGrid(int dimensions, int outputs, int depth, int order=1, std::vector< int > const &level_limits=std::vector< int >())
Factory method, creates a new grid and calls TasmanianSparseGrid::makeWaveletGrid().
Definition: TasmanianSparseGrid.hpp:2314
TasmanianSparseGrid makeLocalPolynomialGrid(int dimensions, int outputs, int depth, int order=1, TypeOneDRule rule=rule_localp, std::vector< int > const &level_limits=std::vector< int >())
Factory method, creates a new grid and calls TasmanianSparseGrid::makeLocalPolynomialGrid().
Definition: TasmanianSparseGrid.hpp:2301