Tag Info

Hot answers tagged

24

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 ...


22

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 ...


19

Preamble It is hard to say what exactly is causing this without seeing the code, but, assuming that there are no memory leaks in the built-in functions you are using, I am only aware of a very few possible causes for memory leaks in Mathematica. Since almost anything is immutable, the leaks must be associated with some symbols for which definitions are ...


19

Like in other programming languages, such as C or Java, assertions are used to catch errors in the logic of your code. With discipline, you can also use exceptions for a similar purpose (see e.g. this discussion for an example). Using patterns and returning a function unevaluated is useful in different types of situations. The linked above answer also ...


15

I found a curious style of debugging that I call "Epicsauce Debugging Level 2." First you type this: x = 0; While[True, Pause[1]; (*Dynamic[x]*) x++ ]; Then you highlight the Dynamic[x] and press Ctrl+Shift+Enter. Then you execute the cell and BE AMAZED!!! It's quite hilarious, but I wonder how useful it would be for more serious code. I've already ...


15

(It's interesting I asked a similar question before. As your question is a superset of mine, I would like to post the answer here.) About the first question: How does the debugger know what to highlight? I don't really know the internal mechanism, but I hope this answer can provide some possible tiny clues. About the second question: Can we influence this ...


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}, ...


12

Here is a function findBadSets that will find any explicitly bad Set/SetDelayed attempts in a given expression. Simply wrap it around a syntactically complete block of code, or follow the block with // findBadSets and the errors are printed one per row, protected symbol followed by complete left-hand side for each bad Set: (* your example *) // findBadSets ...


12

You can enter arbitrary expressions in the Expressions debugging view: The function VariableValue must be used in such expressions in order to access any active variables. If the Expressions view is not visible, you can open it from the Workbench main menu using Window / Show View... / Expressions.


11

This is admittedly messy, but something along these lines might work: insertBelowEvaluationCell[expr_] := (SelectionMove[EvaluationNotebook[], After, EvaluationCell]; NotebookWrite[EvaluationNotebook[], Cell[BoxData@ToBoxes[expr], "Print"]]) This function moves the insertion point just below the evaluation cell before inserting the text or ...


11

There's absolutely no need to load a package if all you want to do is simple Gauss-Legendre quadrature: GaussLegendreQuadrature[f_, {x_, a_, b_}, n_Integer: 10, prec_: MachinePrecision] := Module[{nodes, weights}, {nodes, weights} = Most[NIntegrate`GaussRuleData[n, prec]]; (b - a) weights.Map[Function[x, f], Rescale[nodes, {0, 1}, {a, ...


11

There are indeed some open source alternatives, as other posters have suggested, but you will miss the unique facilities of WB to develop state of the art documentation. So if you want to develop some serious work in MMA, for yourself or others, you should seriously consider WB. Having said that, I use WB in a (probably) unconventional way. Within WB you can ...


11

In the original article there is a semi-colon after the final Show in tile. Remove that semi-colon and the program should run normally. While you're at it, replace GraphicsArray with GraphicsGrid. You probably also noted that the article uses unusual color names that Mathematica does not recognize. (The package, Graphics Colors is not found.) You will need ...


10

My first instinct in such cases is usually to use Trace to figure out which part of the evaluation is behaving differently from what I expect. For the example you already have this point isolated. Then I typically try to dissect it and determine why it misbehaves. An important part of this dissection is using FullForm to remove all the shorthands that are ...


10

Why complicate it? a[[3 ;; 4, 2 ;; 4]] = b; Here is a general function. It take a main matrix, and a sub matrix. It puts the sub matrix inside the main matrix. All what you have to do is just tell it the starting row number and starting column number for where to insert the sub matrix at. Updated: Added pattern checking on arguments. Added additional ...


10

The is also a TextMate bundle for Mathematica: https://github.com/dehowell/mathematica-tmbundle TextMate is for Mac only, but this bundle should work in other editors like Sublime Text (multi platform), e-Text Editor (windows)


10

Well, here is a suggestion: you can overload Needs using Villegas-Gayley trick. To do this safely, here is a generator for local environments, where Needs will be overloaded: createTraceEnvironment[context_String]:= Module[{inNeeds}, Function[ code, Internal`InheritedBlock[ ...


7

This is an arguably even messier solution than Szabolcs', and its performance isn't going to win any awards, but it has some (somewhat dubious) advantages: It allows you to choose whatever target you want for the printing, putting things in an arbitrary notebook, and the output will appear at the current selection in that notebook; After that, printing ...


7

Custom assignement operators Not quite what you asked for, but (as we already discussed recently), you can use custom assignment operators to define some variable that would have the value set to the name of your function inside its body. Here is a possibility: ClearAll[def]; SetAttributes[def, HoldAll]; def /: SetDelayed[def[f_[args___]], rhs_] := ...


7

I gave my answer to this question here: http://mathematica.stackexchange.com/a/3146/121 Let me show how those recommendations apply to this specific problem. 1. Expanding the selection with Ctrl+. Using Ctrl+. on a few parts would quickly show something like this: This would alert you that something strange is going on. 2. Displaying the expression ...


6

Perhaps I misunderstood, but look at how many elements there should have been in your list: ListLogLogPlot@Table[{i, Length[Permutations[Range[i], {2}]]}, {i,10^Range[1, 3., .2]}] It sounds like your kernel quits when you try to calculate the permutations.


5

I can see two problems : you need to include the function as an argument to GaussLegendreQuadrature and you need to call it with the correct name. GaussLegendreQuadrature[a_, b_, n_, f_] := Module[{weights, i}, weights = GaussianQuadratureWeights[n, -1, 1]; (b-a)/2 * Sum[weights[[i, 2]] f[(a + b)/2 + (b - a)/2 weights[[i, 1]]], {i, 1,n}]] ...


4

You have to set breakpoints in what is considered a source file, ie a .m package file. Breakpoints set within test or scrapbook files are not supported. So you would have a test that executes a function defined in a package, set the breakpoint in the package, and when you debug the test, it will suspend.


4

I´ll be happy to withdraw this answer if someone comes up with a more informed one, since essentially (as far as I know) the answer is no. You cannot step into Graphics or Graphics3D or Show expressions (in the sense of debugging the rendering sequence itself) with the debugger, since these expressions are sent by the kernel to the frontend wholesale for ...


4

A fairly crude way to do this is to use e.g. DialogInput or friends, but then you have to intersperse your code with a lot of unnecessary lines: f[x_Integer] := Module[{y, z, r}, y = 5; DialogInput["Proceed with z = 10;\n{y,z,r} = " <> ToString@{y, z, r}]; z = 10; DialogInput["Proceed with r = x + y + z;\n{y,z,r} = " <> ToString@{y, z, ...


4

The debugger in the notebook interface can give more information about errors of this kind. Granted, that information is sometimes cryptic -- but it is better than no information at all. Let's try it with the expressions from the question. We'll start by going to the Evaluation menu to enable the Debugger option: In the Debug palette that appears, ...


3

One way would be to just use Print. Whenever a subkernel prints something, it is immediately sent to the main kernel and ultimately displayed in the notebook, like any other print message. If this is too simple, you can create your own logging function and set it to be evaluated on the main kernel using SetSharedFunction. (This is not clearly documented, ...


3

Perhaps something along the lines of this helps visualize the FullForm, and would have made you realise that _?Function was being interpreted as a head of (_?Function)[x, True] SetAttributes[f, HoldFirst]; f[h_[args___]] := OpenerView[{HoldForm@Panel@h, Column[f /@ Unevaluated /@ {args}]}]; f[sth_] := HoldForm@sth; f@MatchQ[42, _?Function[x, True]] In ...


3

For a function defined using Piecewise you could try something like this SetAttributes[solve, HoldAll]; solve[(bc : boxCurve[a_][s_]) == b_, c__] := Solve[ Or @@ Append[(#1 == b && #2) & @@@ bc[[1]], bc[[2]] == b && Not[Or @@ bc[[1, All, 2]]]], c] solve[boxCurve[{{-1, 1}, {-1, 1}}][s] == {t, t/2}, {s, t}] (* {{t -> ...



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