# Tag Info

## Hot answers tagged guidelines

50

I'll just throw in a few random thoughts in no particular order, but this will be a rather high-level view on things. This is necessarily a subjective exposition, so treat it as such. Typical use cases In my opinion, Compile as an efficiency-boosting device is effective in two kinds of situations (and their mixes): The problem is solved most efficiently ...

42

The main change since that time seems to be that the modern way of using options is associated with OptionsPattern[] - OptionValue commands. A typical way of defining a function would be: Options[f] = { FirstOption -> 1, SecondOption ->2 } f[x_,y_,opts:OptionsPattern[]]:= Print[{x,y,OptionValue[FirstOption],OptionValue[SecondOption]}] ...

26

Here is a practical example from a StackOverflow question. I hope that it gives a good overview of the basic methods. Question What would be the best way to make a function out of the below code ? It would take a dataList as well as some graphical options (such as colors) as arguments and return a customized tabular representation as shown ...

20

Part of what you are asking is of course a matter of taste and habits, but here are my 2 cents: 1) if you want Mathematica to find your package files with a Needs or Get their context names must agree with the hierarchy of directories and filenames. I don't see any good reasons to diverge from that standard convention. For complex packages with many files ...

17

All palette state (i.e., variables which affect the palette and should be remembered between sessions) should be vectored through the palette's TaggingRules option, and its initialization should be done in the palette's NotebookDynamicExpression option. That, plus context isolation of any kernel functions you need to define should solve all of the points ...

13

While @Albert gave an excellent answer, here are my two cents: Naming One thing which is true almost always: make your context name for the package the same as the file name for that package (without .m, of course). While you technically can use a different name, this will make your life harder. A lot depends on the complexity of the project. If it fits ...

12

I have answered almost exactly this question (somewhat more general one, if we interpret this one as being concerned only with Throw and Catch) here. Since you asked a more narrow one, I feel it may be appropriate to borrow a part of my answer, to have it here. The method This method is IMO almost never appropriate for the top-level functions that are ...

12

Setting SetSystemOptions[ "CompileOptions" -> "CompileReportExternal"->True] will emit a message when parts of your function do not get compiled. After compilation, Needs["CompiledFunctionTools"] followed by CompilePrint[cF] (with cF the function you have compiled will display some bytecode; looking for CopyTensor or MainEvaluate in that helps locate ...

10

If you want to revert the entire system to some state, then CleanSlate may be the best option. If you want to unload a few specific packages though, you can use my package PackageManipulations, available here. It has a function PackageRemove, which does exactly that. It has an accompanying notebook with explanations. Some additional notes on it are in this ...

10

This question was indeed discussed on SO, here. I am usually using the version of CleanUp function by WReach, from that answer. It is however not fully bulletproof, as I noted in comments to that answer. Particular pieces of code which are problematic are nested exceptions or aborts like these: Throw[Unevaluated[Abort[]]] or ...

8

I would use the Execute Around Block pattern, which is superficially similar to RAII in c++. A simple example is what I use for ensuring that streams are closed after execution: OpenAndRead[file_String, fcn_]:= Module[{strm, res}, strm = OpenRead[file]; res = CheckAbort[ fcn[strm], $Aborted ]; Close[strm]; If[res ===$Aborted, Abort[], res] ...

8

For additional reference, found within the file \$InstallationDirectory/SystemFiles/Kernel/TextResources/English/Messages.m are all of the defined messages for the system, and there are 11 General messages for Options that do not conform. The list is General::optlist = "Value of option 1 -> 2 should be a list." General::optb = "Optional object 1 in ...

7

Providing optional arguments to functions is rather straight forward. I'll show you three possibilities. Positional arguments This defines a function with a second optional argument. If the argument is omitted it is taken to have the default value. fx[list_, panel_: True] := fx0[list, panel] Named arguments This sets up a default value for a named ...

5

The cleanest possible kernel you can get is starting a kernel only (no front end) with the -noinit option. math -noinit The front end will evaluate a lot of code when it connects to the kernel. -noinit prevents the kernel from loading initialization files (init.m). I don't believe you can get to a purer state than this unless you start surgically ...

5

This answer I did illustrates how to define the type of local variables used in a Module inside Compile which helps solve some type issues sometimes as compiled functions are more strongly typed than non compiled ones. InternalBag inside Compile

5

There are several things that I think you misunderstand. The most important recommendation would be to use this question as a trigger to go to the help and read up on the functions involved and on related tutorials First, a working solution to ease your mind fx1[x_] := 1 + x + RandomVariate[NormalDistribution[]]; fx2[x_] := x^2; fx3[x_] := x - 2; ...

4

You could generate the palette from code in a separate notebook, and have the generated palette use a unique context by setting CellContext -> Notebook when creating the palette notebook. I think this should help with items 2, 3, and 5. Example (there may be better ways..) CreateDocument[ {Cell[BoxData[MakeBoxes[x = 2]], "Input"]}, CellContext ...

2

Backtracking is never efficient so I would suggest always using C for this. However if you must do this in mathematica, here is an example of the generalized backtrack algorithm applied to graph coloring: backtrack[space_List, partialQ_, solutionQ_, flag_: 1] := Module[{n = Length[space], all = {}, done, index, v = 2, solution}, index = ...

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