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?

  • 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$ – J. M.'s torpor Jul 26 '16 at 15:25

I ran a simple scenario:

func[x_] := x^3

Output: {0.000019737327747406418`, Null}

Output: {9.47391731875508`*^-6, Null}

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

Output: {0.005909355927573481`, Null}

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.

  • $\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$ – J. M.'s torpor Jul 26 '16 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$ – basementDweller Jul 26 '16 at 18:10
  • $\begingroup$ @basementDweller Is accuracy at all a concern, or only speed? $\endgroup$ – Michael E2 Jul 27 '16 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$ – basementDweller Jul 27 '16 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 Jul 27 '16 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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