Tag Info

Hot answers tagged

27

While I wait for better answers from some very knowledgeable people in the matter on the site, I'll write what I'm thinking... I think that most of your problems are due to lack of practice with functional thinking rather than lack of debugability itself. I think one that on the contrary, one of the advantages of programming functionally is that the state ...


25

While I agree that the debugging tools could have been better developed by now, let me just throw in a few notes and links. Function chaining (f[g[h[...]]]): I'd argue that this is a good thing. Why: Functions return expressions, which are immutable. You don't introduce as much state (or at all), as in imperative languages. This makes it easier to debug ...


16

Two of the most common error messages that users encounter when working with parts of lists are Part::partd and Part::partw (look up Message for the error message syntax). Both of these are because the user is trying to access an invalid part of the expression (the "object" referred to in the error message), but there's a subtle difference between the two: ...


14

QuantityForm (and some other formatting functions) issues messages at typesetting instead of evaluation, and Trace is generating output that is in an unevaluated state, which QuantityForm isn't expecting. Here's a couple of similar examples: Trace[Block[{form = "LongForm"}, QuantityForm[Quantity[1, "Meters"], form]]] Trace[Block[{digits = 3}, ...


11

Here's another, reliable way: messages = {} clearMessages[] := messages = {} collectMessages[m_] := AppendTo[messages, m] Internal`AddHandler["Message", collectMessages] Then do clearMessages[] 1/0; 0/0; messages Internal`RemoveHandler["Message", collectMessages] Reference and details: How to abort on any message generated?


10

The function Shuffle is not defined. If you define it (say, replace it with RandomSample) it works. Apparently, Rotate in the latter part of the code is being applied to the output of a function that uses edgeNoise which, in turn, (because Shuffle is undefined) is producing the error message you are seeing. To replicate what is happening in a simple setting ...


10

You could name the patterns DefFn[f_[args___], body_] := f[s : PatternSequence[args]] := WithStackFrame[{f, {s}}, body];


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

The documentation for $Messages clearly states: $Messages gives the list of files and pipes to which message output is sent. Therefore Block[{$Messages = {stream}}, ... ] is the correct syntax.


6

Simply you could use $MessagePrePrint to get the "fillers" and $MessageList as you did to get the message name they belong to: $MessagePrePrint = Sow; Reap[ Module[{}, 1/0; 0^0]; $MessageList ] {{Power::infy,Power::indet},{{1/0,0^0}}} For complete control you could go low-level and intercept MessagePacket as I did for: Prepend Information to Warning ...


5

You need a parser for the argument patterns. I wrote a simplistic one for this answer. I will reproduce it here to keep things self-contained: splitHeldSequence[Hold[seq___], f_: Hold] := List @@ Map[f, Hold[seq]]; getFunArguments[Verbatim[HoldPattern][Verbatim[Condition][f_[args___], test_]]] := getFunArguments[HoldPattern[f[args]]]; ...


5

What you look for is the function Check which will give you the possibility to implement what you ask for in several variants, the most simple probably be this: success=Check[Import["test1.txt", "Table"];True, False] See the documentation of Check for more details...


4

I would do this: DefFn[f_[args___], body_] := lhs : f[args] := WithStackFrame[lhs, body]; Then make WithStackFrame HoldFirst and do de-structuring there. For example: SetAttributes[WithStackFrame, HoldFirst] WithStackFrame[f_[args___], expr_] := Print[{f, {args}}]; If for some reason this were unacceptable I would do: DefFn[f_[args___], body_] := ...


4

NDSolve has already detected the largest such intervals for you, which is why the resulting InterpolatingFunctions have restricted domains. You can use InterpolatingFunctionDomain to extract those domains. I'd do something like so Clear[x1, x2, y] eqn = {x1'[t] == -x1[t]^2 - x2[t] + y[t]^3, x2'[t] == x1[t] - x2[t] + x1[t]^2 x2[t]^2, y'[t] == x2[t]^2 ...


3

Do[ Check[{i/i, 1/(i - 5)}, Print@i], {i, -10, 10}] // Quiet 0 5


3

You could always capture the information directly, myMessageList = {}; Internal`InheritedBlock[{Message, $InMsg = False}, Unprotect[Message]; Message[msg_, vars___] /; ! $InMsg := Block[{$InMsg = True}, AppendTo[myMessageList, {msg, vars}]; Message[msg, vars] ]; (* code to run *) Module[{}, 1/0; 0^0] ]; myMessageList (* ...


2

You can use Messages[foo] to get the text of any message. With that, we can proceed as follows to extract the text of the messages that were last generated: Module[{}, 1/0;0^0]; msg = $MessageList; (* last errors *) With[{messages = ReleaseHold@ DeleteDuplicates[# /. HoldPattern@MessageName[s_, _] :> Messages@s] &}, # /. messages@#] ...


1

Another approach: a = Table[{i/i, 1/(i - 5)}, {i, -5, 5}] // Quiet {{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, -(1/7)}, {1, -(1/ 6)}, {Indeterminate, -(1/5)}, {1, -(1/4)}, {1, -(1/3)}, {1, -(1/ 2)}, {1, -1}, {1, ComplexInfinity}} DeleteCases[a, {___, Indeterminate | ComplexInfinity, ___}] {{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, ...


1

<< ..>> is called a Skeleton. It is is used to show you an abbreviated version of the actual offending line. You can find it in Short and Shallow. Short[(x + y)^30 // Expand] You can see the skeleton right there in the middle. The error message probably comes from an illegal construction like: (a + b)[x_] := 2 SetDelayed::write: Tag Plus in ...


1

On my system, which is Ubuntu Linux 12.04 (64bit), this does not happen. I used the following code, which extends your example into a SSCCE (Short, Self Contained, Correct (Compilable), Example). The C code can be found on pastebin and you might want to adapt the setting for "TargetDirectory" and "Libraries": << CCompilerDriver` src = ...


1

This seems to work for me: thisstep = 0; laststep = 0; eqn = {x1'[t] == -x1[t]^2 - x2[t] + y[t]^3, x2'[t] == x1[t] - x2[t] + x1[t]^2 x2[t]^2, y'[t] == x2[t]^2 + y[t] + x1[t]^2 y[t]^2 + 0.5` y[t]^3, x1[0] == 1, x2[0] == 1, y[0] == 1}; {tmin, tmax} = {-1,1}; First@NDSolve[eqn, {x1[t], x2[t], y[t]}, {t, tmin, tmax}, MaxStepFraction -> 1/150, ...



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