# Avoiding the suppression of messages by NIntegrate

Consider the following code:

test[x_] := If[1 < x < 2, Message[Test::message]; x^2, x];
NIntegrate[test[x], {x, 0, 3}]


5.33333333333334

I.e. it gives me the result, suppressing all messages emitted by test. OTOH, this code does emit the messages:

N@Sum[test[x] 1/1000, {x, 0, 3, 1/1000}]


General::stop: Further output of Test::message will be suppressed during this calculation.

5.3338335

I'd really like to receive the messages from the integrand, because in Mathematica 11 they allow to see stack trace. How can I stop NIntegrate from suppressing the messages?

• Do you want to trace your function or NIntegrate? Jun 2, 2017 at 13:44
• @Shadowray I want to trace my function, and only if it generates a message. Jun 2, 2017 at 14:00

I finally found a workaround to the message suppression that still allows the subdivision of the interval of integration $[0,3]$ at $x = 1, 2$. You have to protect the message-generating code sufficiently deep so that the symbolic processing takes care of the singularities. Then inside a ?NumericQ protected function, turn on messages again by resetting $Messages. ClearAll[test, ftrue]; With[{$m = $Messages}, ftrue[x_?NumericQ] := Block[{$Messages = $m}, Message[Test::message]; x^2]; ]; test[x_] := If[1 < x < 2, ftrue[x], x]; NIntegrate[test[x], {x, 0, 3}]  Message::msgl: $MessageList $MessageList not a list; reset to {}. Test::message: -- Message text not found -- Test::message: -- Message text not found -- Test::message: -- Message text not found -- General::stop: Further output of Test::message will be suppressed during this calculation. Out= 5.33333 The extra message There's an extraneous Message::msgl that is the fault of NIntegrate. (It doesn't seem to matter or wasn't detected, since messages are turned off.) It seems easy enough just to ignore it, but if that's unacceptable, you could reset $MessageList yourself, or Quiet the message:

If[! ListQ[$MessageList],$MessageList = {}];  (* OR *)
Quiet[Message[Test::message], Message::msgl];


Using this definition of ftrue, you can see the message generation, even though the messages themselves are not printed:

ftrue[x_?NumericQ] := (
If[Length@$MessageList < 4, Print[$MessageList]];
Message[Test::message]; x^2);

NIntegrate[test[x], {x, 0, 3}]


$MessageList {Message::msgl,Test::message} {Message::msgl,Test::message,Test::message} Out= 5.33333 A glimpse at the symbolic processing It may be of interest to inspect how NIntegrate breaks down the integrand. With the OP's test[], it looks like the following. We get three integration regions NIntegrate[test[x], {x, 0, 3}, IntegrationMonitor :> ((regions = #) &), MaxRecursion -> 0]; Column[regions /. r_NIntegrateGeneralRule :> Short[r], Dividers -> All] With my version of test[], the x^2 in the first region in the table (from 1 to 2) is replaced with ftrue[x]. The answer below is for the first version of the question: I'd really like to receive the messages from the integrand. How can I stop NIntegrate from suppressing the messages? There are several reasons for this behavior. Generally speaking, NIntegrate pre-evaluates and localizes the integrands in order to facilitate different symbolic pre-processing and numerics algorithms. If we redefine the integrand with Piecewise we will get the message once, when PiecewiseNIntegrate does the preprocessing: In:= ClearAll[Test, test] test[x_] := Piecewise[{{Message[Test::message]; x^2, 1 < x < 2}}, x]; NIntegrate[test[x], {x, 0, 3}] During evaluation of In:= Test::message: -- Message text not found -- Out= 5.33333  Further, we can prevent the symbolic processing with _?NumericQ and use Print or Echo. (Note that the integral converges too slowly.) ClearAll[Test, test] test[x_?NumericQ] := If[1 < x < 2, (Echo[Row[{Test::message, " : ", x}]]; x^2), x]; NIntegrate[test[x], {x, 0, 3}, PrecisionGoal -> 2] (* Test::message : 1.08055 Test::message : 1.5 Test::message : 1.91945 ... 5.34841 *)  I am not sure what kind of testing you want to do. You might consider using EvaluationMonitor or IntegrationMonitor instead. • The way with Piecewise defies the purpose of this check: the message is emitted when test[x] is called with a symbolic x. And the way with Echo doesn't allow me to see Stack Trace, unlike messages (new feature of Mathematica 11). Jun 2, 2017 at 13:23 • Right. As I mentioned in my answer you might be better off using EvaluationMonitor. Jun 2, 2017 at 13:37 Updated to issue a General::stop message Maybe the following does what you want? handler[Hold[Message[Test::message], True]] := With[ {stop = Flatten@Position[$MessageList, HoldForm[General::stop]]},

Which[
FreeQ[$MessageList[[stop-1]], HoldForm[Test::message]], CellPrint @ Cell[ BoxData[InternalMessageTemplate[Test, "message", "Test message", StandardForm]], "Message","MSG" ]; If[Count[$MessageList, HoldForm[Test::message]] > 2,
CellPrint @ Cell[
BoxData[InternalMessageTemplate[General, "stop", StringForm[General::stop, HoldForm[Test::message]], StandardForm]],
"Message", "MSG"
]
]
]
]

test[x_] := If[1<x<2, Message[Test::message];x^2, x];

InternalHandlerBlock[{"Message", handler}, NIntegrate[test[x], {x,0,3}]]


Test::message: Test message

5.33333

• Are you sure this works? The function is called 11 times between x == 1 and x == 2, but I only get one message. Shouldn't there be 3, followed by a General::stop? Jun 2, 2017 at 19:19
• @MichaelE2 Do you have lingering definitions, e.g., something like test[x_?NumericQ] := ..? Jun 2, 2017 at 19:46
• Also, some more work is needed to enable General::stop to do its thing. Jun 2, 2017 at 19:47
• No, I don't think so. Just did it again with a fresh kernel: i.stack.imgur.com/Y1L14.png Jun 2, 2017 at 21:19
• @MichaelE2 test is only called once between 1 and 2. test[x] evaluates to If[1<x<2, Message[Test::message];x^2, x] but then stops because x is not numeric. Then, the NIntegrate piecewise code analyzes the If object and converts it into a Piecewise object (which is when the one message gets generated). Then, NIntegrate uses the Piecewise object instead of test. Jun 2, 2017 at 21:29
{"ft_lang_label":"__label__en","ft_lang_prob":0.63657767,"math_prob":0.7208809,"size":2117,"snap":"2023-40-2023-50","text_gpt3_token_len":587,"char_repetition_ratio":0.18031235,"word_repetition_ratio":0.05782313,"special_character_ratio":0.2838923,"punctuation_ratio":0.25,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96102184,"pos_list":[0,1,2],"im_url_duplicate_count":[null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-10T10:13:28Z\",\"WARC-Record-ID\":\"<urn:uuid:5369ba1a-d72b-4199-b573-1b87d093e254>\",\"Content-Length\":\"198217\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:50efc9ac-5056-4016-a91a-bd220014d087>\",\"WARC-Concurrent-To\":\"<urn:uuid:b1942c80-5bb0-4e83-90b8-d9f84a0aa236>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/147368/avoiding-the-suppression-of-messages-by-nintegrate\",\"WARC-Payload-Digest\":\"sha1:NO6IV3FS3OXN3WJSBUSKO52FVR7WNGJH\",\"WARC-Block-Digest\":\"sha1:SIAL4U2HMV5NDE6SJEML227ZJSD2DJOL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679101779.95_warc_CC-MAIN-20231210092457-20231210122457-00421.warc.gz\"}"}