I have a function (or rather many functions) defined over an n-dimensional grid. The grid points will often but not always be evenly spaced. I want to compute integrals of these functions numerically.
My question now is what the best approach to this is. In 1-D I can always works with the trapezoid rule, e.g.:
chapeaux[
grid_List?(VectorQ[#, Internal`RealValuedNumericQ] &),
f_List?(VectorQ[#, Internal`RealValuedNumericQ] &)
] :=
Module[
{
diffs = Differences[grid],
means = MovingAverage[f, 2]
},
diffs.means
];
And here are some simple tests:
gigi = Range[-10., 10., .1];
grigri = Sort@BlockRandom[RandomReal[{-10. , 10.}, 100*Length@gigi]];
sinf = Sin@gigi;
sinfr = Sin@grigri;
NIntegrate[Sin[x], {x, -10, 10}] // RepeatedTiming
{0.0049, 0.}
chapeaux[gigi, sinf] // RepeatedTiming
{0.000066, -8.32667*10^-17}
chapeaux[grigri, sinfr] // RepeatedTiming
{0.00070, 0.00164231}
harm = gigi^2;
harmr = grigri^2;
NIntegrate[x^2, {x, -10, 10}] // RepeatedTiming
{0.0034, 666.667}
chapeaux[gigi, harm] // RepeatedTiming
{0.000068, 666.7}
chapeaux[grigri, harmr] // RepeatedTiming
{0.000694, 666.281}
And sure in the unstructured case I need about 100X as many points to even get okay-ish accuracy, but that's not the end of the world, so now the question is how best to do this or something similar in n-dimensions. Of course, the functions I'm using have no analytic analog and so the best I can do is NIntegrate the Interpolation, but I'm hoping to do this faster and cleaner.
At some level this is mostly a math question, of course, but I figured it's worth asking here before implementing as the accumulated math knowledge here is much greater than my own.
NIntegrateover anElementMeshas done here, does a simple integration rule without adaptive refinement. – Michael E2 Jan 07 '19 at 22:40NIntegratedo it all for me but I might need to this to be fast in the future. – b3m2a1 Jan 07 '19 at 22:42Interpolationor is that basically a necessity in methods like this? – b3m2a1 Jan 07 '19 at 22:43emifn = ElementMeshInterpolation[{mesh}, values], also discussed in this tutorial. I think somehow you have to triangulate/mesh the domain;ElementMeshInterpolationwould add relatively little time to that. ForNIntegrate, it's somewhat fast because there's no error estimation, symbolic processing, or recursive refinement. Not sure how it compares with a packed-array direct implementation of an integration rule. – Michael E2 Jan 07 '19 at 23:05