10
$\begingroup$

I noticed that when I call Integrate for the first time, on an integral that I know will take longer than the time limit I give it, it will abort for the first time, as expected when wrapped by TimeConstrained. But on the third try (not the second!), it will return the antiderivative very quickly within the same time limit.

This tells me that Integrate seems to be loading things or caching things each time it is called?

Is it possible to somehow start each call of Integrate fresh, as the first call, but without restarting the kernel? Here is an MWE. Run the following from a fresh kernel:

res = AbsoluteTiming[ TimeConstrained[Integrate[
       (Sqrt[1 - x]*x*(1 + x)^(2/3))/(-((1 - x)^(5/6)*(1 + x)^(1/3)) +
         (1 - x)^(2/3)*Sqrt[1 + x]), x], 500 $TimeUnit]]

Here is a screen shot

Mathematica graphics

An interesting thing is that I needed 3 calls to get an answer. You might have to adjust the multiplier on $TimeUnits on your PC. For me, 500 seems to work just right for this illustration.

Using 11.1 on Windows 7, 64 bit.

I am trying to get predictable results each time, and would like the next integral calls not to be affected by the previous integral calls made in terms of timing.

$\endgroup$
  • 2
    $\begingroup$ Have you tried ClearSystemCache["Symbolic"]? $\endgroup$ – J. M. will be back soon Mar 30 '17 at 5:10
  • $\begingroup$ @J.M. Thanks! I did not know about this function. I just tried it and it seems to do the trick! I need to read more about this function. $\endgroup$ – Nasser Mar 30 '17 at 5:11
  • $\begingroup$ Related: (1115), (91816), (135781) $\endgroup$ – Mr.Wizard Mar 30 '17 at 5:49
13
$\begingroup$

In general, whenever you suspect that Mathematica's internal caching is affecting your results, it is a good idea to run ClearSystemCache[] first. If more specificity is necessary, one can try ClearSystemCache["Numeric"] for numeric computations, or ClearSystemCache["Symbolic"] for symbolic computations.

For more control of how caching is done for various objects, you can look at SystemOptions["CacheOptions"]. In particular, you can set SetSystemOptions["CacheOptions" -> {"Symbolic" -> {"Cache" -> False}}] if you want no caching of symbolic results during your entire session.

$\endgroup$

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.