# How can I automatically set an NIntegrate call to zero if NIntegrate::slwcon is observed?

This is a generic question about the NIntegrate function. I am attempting to numerically integrate a function which may or may not be highly oscillatory, based on it's inputs. For a certain sets of function inputs I get the NIntegrate::slwcon message. The function does not have any singularities. In the case of the NIntegrate::slwcon message, I am confident that the integral over the desired region is ~0. Is there any way to make sure that as soon as NIntegrate realises that the result is converging slowly, it returns a zero instead of attempting to go through all the recursions?

The function being integrated is essentially a two-dimensional Gaussian, which is modulated by complex exponentials. The oscillating frequency of the exponentials depends upon multiple function inputs. The Gaussian envelope has a maximum of ~10^-101. The FWHM of the Gaussian is ~10^14. Overall the range of NIntegate output can be crudely approximated to be from 0 to 1.

I'm not really concerned with the quality of the result. Three significant digits is good enough for me. I need to significantly speed up the computation. For well behaving inputs, the NIntegrate call is done in 0.04s, however under certain conditions the call executes in ~2s and gives a slightly incorrect result.

My NIntegrate call is already using Method -> "LevinRule" , MaxRecursion -> 10, PrecisionGoal -> 3, AccuracyGoal -> 3.

• You can use something like the technique in this answer: Catch[InternalHandlerBlock[{"Message", If[Last[#], Throw[Indeterminate, "x"]] &}, NIntegrate[Log[x], {x, 0, 1}, Method -> {"Trapezoidal", "SymbolicProcessing" -> 0}]], "x"] – J. M. will be back soon Mar 18 '16 at 23:43
• @J.M. Thank you for the reply, your solution works well. Overall I understand the solution, but not the details. In InternalHandlerBlock[{"Message", If[Last[#], Throw[Indeterminate, "x"]] &}... what is the significance of Last[#] and the If statement? I have not encountered functions of the form InternalHandlerBlock, InternalAddHandler and InternalRemoveHandler before. What do these mean (I could not find any documentation concerning this)? – Gleb Egorov Mar 21 '16 at 18:31
• The functions are undocumented, which is why I linked to an answer explaining how to use these. Note in particular "whenever a message is generated, Hold[message, printed] is passed to all "Message" handler functions where message is the message text and printed is True is the message would be printed, False otherwise." Last[] just extracts the Boolean value of printed` in this case. – J. M. will be back soon Mar 21 '16 at 19:20