3
$\begingroup$

This is just a general question about how InterpolatingFunctions work:

If I have a complicated function that I don't want to have to evaluate many times over, does turning it into an InterpolatingFunction make it behave more like a lookup table, or will every call to an InterpolatingFunction still have to do a complicated calculation?

Does adding NumericQ to the argument of this function have an effect on this aspect of its behavior?

$\endgroup$
1
  • 1
    $\begingroup$ "...does turning it into an InterpolatingFunction make it behave more like a lookup table?" - more or less; the function will use piecewise polynomials to "connect the dots". $\endgroup$ Commented Jul 26, 2016 at 15:25

1 Answer 1

1
$\begingroup$

I ran a simple scenario:

func[x_] := x^3

AbsoluteTiming[func[192999292992292992];]
Output: {0.000019737327747406418`, Null}

AbsoluteTiming[func[155];]
Output: {9.47391731875508`*^-6, Null}

pts = Table[{i, func[i]}, {i, 1, 200}];
intfunc = Interpolation[pts];

AbsoluteTiming[intfunc[192999292992292992];]
Output: {0.005909355927573481`, Null}

AbsoluteTiming[intfunc[155];]
Output: {0.00004500110726408663`, Null}

So as you can see, using the original function should take a shorter time, especially when the Interpolation has to extrapolate data.

Of course, I don't know how complex your function is, so try running an AbsoluteTiming on both cases if you're curious.

$\endgroup$
5
  • $\begingroup$ Well sure, for a simple polynomial function, Interpolation[] is slow; that's not what it's really intended for. More illustrative would be to use a (complicated) transcendental function. $\endgroup$ Commented Jul 26, 2016 at 17:47
  • $\begingroup$ The function has a polynomial in the numerator and the denominator is a sum of terms that have polynomial exponents. The InterpolatingFunction appears to be about 50 times faster. Thanks! $\endgroup$ Commented Jul 26, 2016 at 18:10
  • $\begingroup$ @basementDweller Is accuracy at all a concern, or only speed? $\endgroup$
    – Michael E2
    Commented Jul 27, 2016 at 2:36
  • $\begingroup$ @MichaelE2 Right now speed is more important, but obviously I don't want to scrap accuracy altogether...Why do you ask? $\endgroup$ Commented Jul 27, 2016 at 17:34
  • $\begingroup$ @basementDweller InterpolatingFunction will usually be fast, slowing down somewhat as the InterpolationOrder (degree of the polynomials used) increases and as the number of interpolation nodes/points increases, but one does not usually need to sacrifice accuracy for speed here. There are a few methods for constructing an IF, depending on the function, FunctionInterpolation[] and NDSolve[] being common. See, for instance, this, this, or this. $\endgroup$
    – Michael E2
    Commented Jul 27, 2016 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.