Tag Info

Hot answers tagged

31

I found a robust solution described in this MathGroup message by Maxim Rytin: messageHandler = If[Last[#], Abort[]] & Internal`AddHandler["Message", messageHandler] This will abort the computation whenever a message would be printed. It can be turned off using Internal`RemoveHandler["Message", messageHandler] Alternatively this can be ...


26

Solutions to algebraic or transcendental equations are expressed in terms of Root objects whenever it is impossible to find explicit solutions. In general there is no way express roots of 5-th (or higher) order polynomials in terms of radicals. However even higher order algebraic equations can be solved explicitly if an associated Galois group is solvable. ...


24

Use Reduce[(1/x) Cosh[x/2] == Sqrt[2], x, Reals] or Solve[(1/x) Cosh[x/2] == Sqrt[2], x, Reals] the latter yields {{x -> Root[{-E^(-(#1/2)) - E^(#1/2) + 2 Sqrt[2] #1 &, 0.75858229952537718426}]}, {x -> Root[{-E^(-(#1/2)) - E^(#1/2) + 2 Sqrt[2] #1 &, 5.4693513860610533998}]}} For transcendental equations you may get with Reduce ...


21

Here is my proposal for tagging messages with (the value of) an arbitrary expression at the time of message generation. The tag is placed inside the the message itself. ClearAll[withTaggedMsg] SetAttributes[withTaggedMsg, HoldAll] withTaggedMsg[exp_, label_: "When"] := Function[, Internal`InheritedBlock[{MessagePacket}, Unprotect @ ...


19

If you look carefully, you'll notice that the usage messages of package functions are nicely formatted. Notice the nice italicised and subscripted $x_1$. If you actually look in the package, you'll find a usage message that doesn't have any formatting at all, and even differs from the version ?DelaunayTriangulation gives us. ...


16

I cannot seem to make it do exactly what you want do to how messages are created, but here is a serviceable alternative using $MessagePrePrint. $MessagePrePrint formats the variables specified in the message string, and in your example, the message has the form General::indet = "Indeterminate expression `1` encountered." where the `1` will be replaced by ...


13

It is actually straightforward. You use Messages[symbol] to get the list, e.g. Power::infy (* trigger loading the message *) Messages[Power] (* {HoldPattern[Power::infy] :> "Infinite expression `1` encountered."} *) then, as it is a list of replacement rules, you can simply do Power::infy /. Messages[Power] (* "Infinite expression `1` encountered." *) ...


12

What about something like this? Function[i, {i, ParallelEvaluate[i]};, HoldFirst][ Unprotect[Message, Check, Quiet]; Module[{$guardMes = True, $guardChck = True, $guardQuiet = True}, Message[args___ /; $guardMes] := Block[{$guardMes = False}, Message[args]; If[Head[First@{args}] =!= $Off, Abort[]]; ]; Quiet[args___ /; $guardQuiet] := ...


12

rcollyer has a nice solution. Here's another possibility using Check and printing the list of messages generated at the current evaluation. Quiet@Block[{$OldMessages = 0}, Do[Check[#^#/# &@Mod[i, 2], Print@StringForm["At i=``, ``", i, $MessageList[[$OldMessages + 1 ;;]]]; $OldMessages = Length@$MessageList;], ...


11

Available messages The message ::argx is one of the general messages intended for use with any function. These have the special property of being called for any symbol used (placed left of ::): Message[foo::"argx", "foo", 2, 3] foo::argx: foo called with 2 arguments; 1 argument is expected. >> Use Messages[General] to see a list of these messages. ...


11

I agree completely with J.M., Quiet is the answer. Implementing WithOff using Quiet is (as I'm sure you know) trivial. Here it is, just for fun: ClearAll[WithOff] SetAttributes[WithOff, HoldAll]; WithOff[msg_, expr_] := Quiet[expr, {msg}]; WithOff[Pattern::patv, rule = (f[x_Integer | {x__Integer}] :> g[x])]; rule2 = x_[x__] :> x;


10

You can do something like this: resetMessages[symbol_] := With[{mysymbol = symbol}, Unprotect[$MessageList]; $MessageList = DeleteCases[$MessageList, HoldForm[MessageName[mysymbol, _]]]; Protect[$MessageList];] And you will have to call it after each function... Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; \ Exp[a, b]; ...


9

As acl points out, this post shows you how to setup error highlighting for invalid number of arguments. Coming to the actual error messages used, there are three built-in messages attached to General, that can be used for your own functions as well. These are argx, argrx and argt: General::argx (* "`1` called with `2` arguments; 1 argument is expected." *) ...


8

New Method FJRA pointed out that my original method will fail in certain cases. Here is what I hope is a more robust approach: Unprotect[Message, $MessageList] Message[args___] /; ! TrueQ[$msgClear] := Block[{$msgClear = True}, $MessageList = DeleteCases[$MessageList, HoldForm[Power::infy]]; Message[args] ] Now the specified message will print ...


8

The easiest and, so far, the best solution I have found is the following: (* Put the following two lines at the top of every notebook. *) messageHandler = If[Last[#], Interrupt[]] & ; Internal`AddHandler["Message", messageHandler]; The above code is slightly modified from Szabolcs's solution at the beginning of this thread. I changed Abort to ...


8

You can define the function: messageIsOn[msg_]:=Head[msg]===String Which yields True if the message is on. Than do e.g: msgStatus=messageIsOn[Pattern::patv] If[msgStatus, Off[Pattern::patv]] < some calculation suppressing message Pattern::patv > (* Restore the message status *) If[msgStatus, On[Pattern::patv]]


6

First, note that turning off messages is technically not the same thing as not printing them. You can avoid printing messages by removing the output channel they're being sent to: $Messages = {} Restore the previous behaviour using $Messages = $Output, provided that you haven't changed $Output. But this won't turn messages off, it will only avoid ...


6

You do it exactly as you would do it on the main kernel. Following example which throws a message when it divides by zero: ParallelMap[1/# &, Mod[Range[30], 3]] Some people prefer to switch specific messages off beforehand. This can be done with ParallelEvaluate[Off[Power::infy]] ParallelMap[1/# &, Mod[Range[30], 3]] Or you use Quiet as ...


6

Following R.M's suggestion, and shamelessly lifting code from the Wizard’s fine answer there, you can use Stack[] and get the following: SetAttributes[withTaggedMsg, HoldAll] withTaggedMsg[] := Function[, Internal`InheritedBlock[{MessagePacket}, Unprotect[MessagePacket]; MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] := ...


6

There were some attempts on that in this discussion. I also have this functionality in my debug function posted here


5

Why don't you use Check. You could combine it with Reap/Sow or Throw/Catch. Here's an example that counts divisions by zero. SeedRandom[1]; list = RandomInteger[{-2, 2}, {100}]; Quiet[results = Reap[Table[Check[1/x, Sow["bad news"]], {x, list}]]]; Length[results[[2, 1]]] Of course, since the second argument of Check can be anything, you could arrange ...


5

How about doing something as simple as sym1::msg = "I am feeling grumpy."; sym2::msg = sym3::msg = sym1::msg; Then Do[With[{ff = f}, Message[MessageName[ff, "msg"]]], {f, {sym1, sym2, sym3}}] Outputs sym1::msg: I am feeling grumpy. sym2::msg: I am feeling grumpy. sym3::msg: I am feeling grumpy.


4

The following expression should result in an error: First@Cases[NotebookGet[EvaluationNotebook[]][[1]], Cell[___, CellTags -> "MyCode", ___]]] because Cases, by default, operates at level {1} and in a notebook's expression, CellTags will never be at level {1}. Thus, Cases returns {} and First throws an error. The solution here, is to use level ...


3

It seems to me that there's a better approach, but one way is to define your own DownValue for this particular message. For example: Unprotect[Message]; Message[NIntegrate::maxp, its_, int_, err_] := Sow[err] Then NIntegrate[Sin[x]/Sqrt[x], {x, 0, 100}, Method -> "MonteCarlo", PrecisionGoal -> 6] // Reap (* Out: {1.07721, {{0.0761274}}} *)


3

First, I put $t = \sin x - \cos x$, eq1 = (3 - Cos[4x]) ( Sin[x] - Cos[x]) - 2 == 0; eq2 = t == Sin[x] - Cos[x]; Eliminate[ TrigExpand[ {eq1, eq2}], x] I receive 2 t - 2 t^3 + t^5 == 1 And then, I solve Solve[ 2 t - 2 t^3 + t^5 == 1, Reals] finally Reduce[ -Cos[x] + Sin[x] == 1, x, Reals] (C[1] ∈ Integers && x == π/2 + 2 π C[1]) || (C[1] ∈ ...


3

It's a bug: there are more than one call to Message[FindRoot::bbrac], and some of them are suppressed, but only if Quiet is used. Here's a way to check suggested by Szabolcs: messageHandler = Print[{##}] &; Internal`AddHandler["Message", messageHandler]; Quiet@FindRoot[x == 1, {x, 0, 0.5}, Method -> "Brent"] <...> ...


2

This "answer", if it can be considered an answer, consists of a mixture of comments and suggestions. Aside from dealing with levels as rm-rf has shown in another answer, one probably ought to deal with the possibility of Cases returning {}, even if it should not happen. How to deal with it is something you should decide. Here's a way to return Null, if ...



Only top voted, non community-wiki answers of a minimum length are eligible