31#ifndef __TSG_CACHE_LAGRANGE_HPP
32#define __TSG_CACHE_LAGRANGE_HPP
44#include "tsgGridCore.hpp"
75 CacheLagrange(
int num_dimensions,
const std::vector<int> &max_levels,
const OneDimensionalWrapper &rule,
const double x[]) :
76 cache(std::vector<std::vector<T>>(num_dimensions, std::vector<T>())), offsets(rule.getPointsCount()){
77 for(
int dim=0; dim<num_dimensions; dim++){
78 cache[dim].resize(offsets[max_levels[dim] + 1]);
79 for(
int level=0; level <= max_levels[dim]; level++)
80 cacheLevel(level, x[dim], rule, &(cache[dim][offsets[level]]));
84 ~CacheLagrange() =
default;
87 static void cacheLevel(
int level,
double x,
const OneDimensionalWrapper &rule, T *cc){
88 const double *nodes = rule.getNodes(level);
89 const double *coeff = rule.getCoefficients(level);
90 int num_points = rule.getNumPoints(level);
94 for(
int j=0; j<num_points-1; j++){
99 cc[num_points-1] *= c * coeff[num_points-1];
100 for(
int j=num_points-2; j>=0; j--){
101 c *= (x - nodes[j+1]);
102 cc[j] *= c * coeff[j];
107 T getLagrange(
int dimension,
int level,
int local)
const{
108 return cache[dimension][offsets[level] + local];
120 std::vector<std::vector<T>> cache;
122 std::vector<int> offsets;
134class CacheLagrangeDerivative {
148 CacheLagrangeDerivative(
int num_dimensions,
const std::vector<int> &max_levels,
const OneDimensionalWrapper &rule,
const double x[]) :
149 cache(std::vector<std::vector<T>>(num_dimensions, std::vector<T>())), offsets(rule.getPointsCount()) {
150 for(
int dim=0; dim<num_dimensions; dim++){
151 cache[dim].resize(offsets[max_levels[dim] + 1]);
152 for(
int level=0; level <= max_levels[dim]; level++)
153 cacheDerivativeLevel(level, x[dim], rule, &(cache[dim][offsets[level]]));
157 ~CacheLagrangeDerivative() =
default;
160 static void cacheDerivativeLevel(
int level,
double x,
const OneDimensionalWrapper &rule, T *cc){
170 const double *nodes = rule.getNodes(level);
171 const double *coeff = rule.getCoefficients(level);
172 int num_points = rule.getNumPoints(level);
174 std::vector<T> aux_f(num_points), aux_g(num_points);
177 aux_g[num_points-1] = 1.0;
178 for(
int i=1; i<num_points; i++) {
179 aux_f[i] = aux_f[i-1] * (x - nodes[i-1]);
180 aux_g[num_points-1-i] = aux_g[num_points-i] * (x - nodes[num_points-i]);
181 cc[i] = aux_f[i-1] + (x - nodes[i-1]) * cc[i-1];
183 cc[num_points-1] *= coeff[num_points-1];
185 for(
int i=num_points-2; i>=0; i--) {
186 diff_gj = aux_g[i+1] + diff_gj * (x - nodes[i+1]);
187 cc[i] = coeff[i] * (cc[i] * aux_g[i] + aux_f[i] * diff_gj);
192 T getLagrangeDerivative(
int dimension,
int level,
int local)
const {
193 return cache[dimension][offsets[level] + local];
198 std::vector<std::vector<T>> cache;
200 std::vector<int> offsets;
@ rule_clenshawcurtis0
Same as rule_clenshawcurtis but with modified basis that assumes the model is zero at the boundary.
Definition tsgEnumerates.hpp:291
Encapsulates the Tasmanian Sparse Grid module.
Definition TasmanianSparseGrid.hpp:68