Skip to content

Commit fb282c9

Browse files
authored
Merge pull request #790 from ssclift/master
Expose getDifferentiationWeights to Python
2 parents 0ec86c6 + d69b01a commit fb282c9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

InterfacePython/TasmanianSG.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,30 @@ def getInterpolationWeightsBatch(self, llfX):
10831083
np.ctypeslib.as_ctypes(aWeights.reshape([iNumX * iNumPoints])))
10841084
return aWeights
10851085

1086+
def getDifferentiationWeights(self, lfX):
1087+
'''
1088+
returns the differentiation weights associated with the points
1089+
in getPoints()
1090+
1091+
lfX: a 1-D numpy.ndarray with length iDimensions
1092+
the entries indicate the points for evaluating the weights
1093+
1094+
output: a 1-D numpy.ndarray of length getNumPoints()
1095+
the order of the weights matches the order in getPoints()
1096+
1097+
TODO: This, and the C++ call, would probably benefit from using a
1098+
sparse matrix data structure.
1099+
'''
1100+
iNumX = len(lfX)
1101+
if (iNumX != self.getNumDimensions()):
1102+
raise TasmanianInputError("lfX", "ERROR: len(lfX) should equal {0:1d} instead it equals {1:1d}".format(self.getNumDimensions(), iNumX))
1103+
iNumPoints = self.getNumPoints()
1104+
if (iNumPoints == 0):
1105+
return np.empty([0,0], np.float64)
1106+
aWeights = np.empty([iNumPoints * iNumX], np.float64)
1107+
pLibTSG.tsgGetDifferentiationWeightsStatic(self.pGrid, np.ctypeslib.as_ctypes(lfX), np.ctypeslib.as_ctypes(aWeights))
1108+
return aWeights.reshape([iNumPoints, iNumX])
1109+
10861110
def loadNeededValues(self, llfVals):
10871111
'''
10881112
loads the values of the target function at the needed points

SparseGrids/TasmanianSparseGrid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ void tsgGetQuadratureWeightsStatic(void *grid, double *weights);
8080
double* tsgGetQuadratureWeights(void *grid);
8181
void tsgGetInterpolationWeightsStatic(void *grid, const double *x, double *weights);
8282
double* tsgGetInterpolationWeights(void *grid, const double *x);
83+
void tsgGetDifferentiationWeightsStatic(void *grid, const double *x, double *weights);
84+
double* tsgGetDifferentiationWeights(void *grid, const double *x);
8385
void tsgLoadNeededPoints(void *grid, const double *vals);
8486
void tsgLoadNeededValues(void *grid, const double *vals);
8587
const double* tsgGetLoadedValues(void *grid);

SparseGrids/TasmanianSparseGridWrapC.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,24 @@ double* tsgGetQuadratureWeights(void *grid){
202202
((TasmanianSparseGrid*) grid)->getQuadratureWeights(w);
203203
return w;
204204
}
205+
205206
void tsgGetInterpolationWeightsStatic(void *grid, const double *x, double *weights){ ((TasmanianSparseGrid*) grid)->getInterpolationWeights(x, weights); }
206207
double* tsgGetInterpolationWeights(void *grid, const double *x){
207208
double *w = (double*) malloc(((TasmanianSparseGrid*) grid)->getNumPoints() * sizeof(double));
208209
((TasmanianSparseGrid*) grid)->getInterpolationWeights(x, w);
209210
return w;
210211
}
211212

213+
void tsgGetDifferentiationWeightsStatic(void *grid, const double *x, double *weights){ ((TasmanianSparseGrid*) grid)->getDifferentiationWeights(x, weights); }
214+
double* tsgGetDifferentiationWeights(void *grid, const double *x){
215+
double *w = (double*) malloc( ((TasmanianSparseGrid*) grid)->getNumPoints()
216+
* ((TasmanianSparseGrid*) grid)->getNumDimensions()
217+
* sizeof(double)
218+
);
219+
((TasmanianSparseGrid*) grid)->getDifferentiationWeights(x, w);
220+
return w;
221+
}
222+
212223
void tsgLoadNeededPoints(void *grid, const double *vals){ ((TasmanianSparseGrid*) grid)->loadNeededPoints(vals); }
213224

214225
void tsgLoadNeededValues(void *grid, const double *vals){ ((TasmanianSparseGrid*) grid)->loadNeededValues(vals); }

0 commit comments

Comments
 (0)