Best Practices
George E. Hrabovsky
MAST
Introduction
In this lesson we will examine useful tips and ideas to make life, and code, better in MMA/WL. I will try to organize these by what I consider the most important ideas first. Unfortunately this is a personal choice on my part and nothing can be done about that.
The Most Important Things
To Do Right Away Before Starting Anything Else
Go to the Edit Menu. Then go to Preferences.
Uncheck the Show suggestions bar after last output. This is responsible for throwing lots of errors.
Activate the button Automatically insert matching delimiters while typing. In version 12,3 the default is unchecked, so you should activate it. In version 13 this is the default state.
No Autosave
Save your work often. There is no autosave.
Use the ?
If you forget how to use a command type a new cell with ?command to get a description of it. If you type ??command you will also get a list of function attributes.
Referring to Previous Output
Never, never, NEVER, use % to refer to previous output. Assign a symbol to any output so you can refer to it later.
Aborting a Command
You can either go to the Evaluation Menu and select Abort Evaluation, or hold down the [ALT] key and type . .
If you can’t abort the command, then you will have to go to the Evaluation Menu, then Quit Kernel, then choose Local. This will stop all evaluations and reset the system. It will not save your work. If MMA/WL becomes unstable and you can’t do anything with it, then use the system in your operating system to kill the process. This will destroy all unsaved work. Save your work often.
Important Things
Avoid Iterative Loops
Avoid iterative programming, use functional recursions instead. For example.
Look Up the Syntax
Get to know the syntax of the WL. In the documentation system go to the Guide Wolfram Language Syntax. From there there are two things to get to know: the first is the associated Guide Wolfram Language Syntax Characters. The second is a link to the tutorial Textual Input and Output. You can also type into the Search Bar Operator Input Forms, this links to an included Tech note of the same name that lists all of these things.
Using Transpose on Datasets
Say that you have two data sets that you need to merge for a function, say a ListPlot. Use Transpose to link them element by element. Here is an example.
Make sure that the two original data files are the same length.
Weak Typing
MMA/WL have what is called “weak typing” that is it does not enforce hard rules for establishing symbol typing. This is an advantage most of the time, as it lends flexibility. It can cause problems if you change code, as you might inadvertently change the type of a variable in your mind without remembering to treat the symbol that way in the code.
A Note About Associations
Associations are memory hungry.
Navigating Code
Use [CTRL]. to select groups of symbols.
Use [CTRL][SHIFT]→ or [CTRL][SHIFT]← to extend your selection by one token in either direction.
Use [CTRL]↑ of [CTRL]↓ to jump up or down three line while the cursor stays in its current location.
Autocompletion
Take advantage of autocompletion where possible, you can enter this manually by using [CTRL]k
Contexts
If you ever want a list of variables and functions you have defined in your session use the names under the notebook context. You can find the name for the context by using the Context command.
or we can use this.
For a more in-depth look, we can write this.
From this we see that the notebook has introduced a context and that the system has also introduced a context.
To find the names under the notebook context write this.
If you want to clear all of the names used in the context write this.
If you want to keep the names, but clear all definitions and attributes use this command.
The Remove command should resolve most variable conflicts.
Contexts are the names of work environments produced by the MMA/WL system. Each package you load will introduce a new context. You can create new contexts.
Turning Off System Complaints
If you are doing something you know is correct, but it will generate warnings, end the code with //Quiet.
Things
Indexed Variables
If you must use indexed variables it is best to use an association as this allows you to bind many values to a variable name by position.
To display indices you can use DownValues to assign the value as a rule to a function head.
Function Naming Convention
The names of all Mathematica commands begin with a capital letter. I recommend that your user-defined functions do not, that makes it easier to tell the difference.
Never use an underscore in a name, it is reserved for patterns.
Avoid using single capital letters as names.
If You Must Perform a Procedural Iteration
Using Which is much more efficient than nested If statements.
Shadowing
If you load a package and a name is duplicated it can cause a shadowing error. This can happen is you use [Get[context`], or its short form <<Context`. You can avoid this by using Needs[Context`].
Less Important Things
Wrapping Forms Instead of Including Them
Don’t use TableForm or MatrixForm in table or matrix definitions. Wrap the definition in these commands instead. That way the object is not defined in the formatted output form.
Note that we call the // symbol piping.
Finding Options
There are many functions that have hidden options or methods. This code is modified from the web page https://www.12000.org/my_notes/faq/mma_notes/MMA.htm.
# | Option | Options to this option | ||||||||||||||||
1 | NDSolve`Adams |
|
||||||||||||||||
2 | NDSolve`AnyEdge | {} | ||||||||||||||||
3 | NDSolve`Auxiliary |
|
||||||||||||||||
4 | NDSolve`BaderSequenceFunction | {} | ||||||||||||||||
5 | NDSolve`BDF |
|
||||||||||||||||
6 | NDSolve`BootstrapDenseOutput | {} | ||||||||||||||||
7 | NDSolve`BulirschSequenceFunction | {} | ||||||||||||||||
8 | NDSolve`Chasing |
|
||||||||||||||||
9 | NDSolve`ChebyshevCoefficients | {} | ||||||||||||||||
10 | NDSolve`CheckMethodProperty | {} | ||||||||||||||||
11 | NDSolve`Composition |
|
||||||||||||||||
12 | NDSolve`ComputeDerivatives |
|
||||||||||||||||
13 | NDSolve`ConsistentCoefficientsQ | {} | ||||||||||||||||
14 | NDSolve`Continuity | {} | ||||||||||||||||
15 | NDSolve`CountableCrossingEvent | {} | ||||||||||||||||
16 | NDSolve`CountableCrossingValue | {} | ||||||||||||||||
17 | NDSolve`CreateMethodData | {} | ||||||||||||||||
18 | NDSolve`CreateWorkspace | {} | ||||||||||||||||
19 | NDSolve`CreateWorkspaceList | {} | ||||||||||||||||
20 | NDSolve`CrossDiscontinuity | {} | ||||||||||||||||
21 | NDSolve`CubicHermite | {} | ||||||||||||||||
22 | NDSolve`DAEInitialize |
|
||||||||||||||||
23 | NDSolve`DAEInitializeForProjection |
|
||||||||||||||||
24 | NDSolve`DAEInitializeReduced |
|
||||||||||||||||
25 | NDSolve`DAESensitivityInitialize | {} | ||||||||||||||||
26 | NDSolve`DCayleyInverse | {} | ||||||||||||||||
27 | NDSolve`DDEFunction | {} | ||||||||||||||||
28 | NDSolve`DelayDESteps |
|
||||||||||||||||
29 | NDSolve`DeltaDiscontinuity | {} | ||||||||||||||||
30 | NDSolve`Dethread | {} | ||||||||||||||||
31 | NDSolve`DExpInverse | {} | ||||||||||||||||
32 | NDSolve`DExpInverseCoefficients | {} | ||||||||||||||||
33 | NDSolve`DiscontinuityEvaluate | {} | ||||||||||||||||
34 | NDSolve`DiscontinuityExclusion | {} | ||||||||||||||||
35 | NDSolve`DiscontinuityReplace | {} | ||||||||||||||||
36 | NDSolve`DiscontinuityScan |
|
||||||||||||||||
37 | NDSolve`DiscontinuitySurface | {} | ||||||||||||||||
38 | NDSolve`DiscontinuityValues | {} | ||||||||||||||||
39 | NDSolve`DoubleStep |
|
||||||||||||||||
40 | NDSolve`EmbeddedExplicitRungeKuttaCoefficients | {} | ||||||||||||||||
41 | NDSolve`EstimateStartingStep | {} | ||||||||||||||||
42 | NDSolve`EvaluateJacobianWithSolutionData | {} | ||||||||||||||||
43 | NDSolve`EvaluateWithSolutionData | {} | ||||||||||||||||
44 | NDSolve`EventData | {} | ||||||||||||||||
45 | NDSolve`EventLocator |
|
||||||||||||||||
46 | NDSolve`EventOrderBackward | {} | ||||||||||||||||
47 | NDSolve`EventOrderForward | {} | ||||||||||||||||
48 | NDSolve`ExplicitEuler | {} | ||||||||||||||||
49 | NDSolve`ExplicitMidpoint | |||||||||||||||||
50 | NDSolve`ExplicitModifiedMidpoint | |||||||||||||||||
51 | NDSolve`ExplicitRungeKutta |
|
||||||||||||||||
52 | NDSolve`Extrapolation |
|
||||||||||||||||
53 | NDSolve`FallingEdge | {} | ||||||||||||||||
54 | NDSolve`FinalizeMethod | {} | ||||||||||||||||
55 | NDSolve`FiniteDifferenceDerivative |
|
||||||||||||||||
56 | NDSolve`FiniteDifferenceDerivativeFunction | {} | ||||||||||||||||
57 | NDSolve`FiniteDifferenceWeights | {} | ||||||||||||||||
58 | NDSolve`FiniteElement |
|
||||||||||||||||
59 | NDSolve`FixedPoint | {} | ||||||||||||||||
60 | NDSolve`FixedStep |
|
||||||||||||||||
61 | NDSolve`GetDefaultDifferenceOrder | {} | ||||||||||||||||
62 | NDSolve`GetDenseOutput | {} | ||||||||||||||||
63 | NDSolve`GetMethodOptionValues | {} | ||||||||||||||||
64 | NDSolve`GMRES |
|
||||||||||||||||
65 | NDSolve`HairerOstermannSequenceFunction | {} | ||||||||||||||||
66 | NDSolve`HarmonicSequenceFunction | {} | ||||||||||||||||
67 | NDSolve`IDA |
|
||||||||||||||||
68 | NDSolve`ImplicitRungeKutta |
|
||||||||||||||||
69 | NDSolve`ImplicitRungeKuttaAdjointCoefficients | {} | ||||||||||||||||
70 | NDSolve`ImplicitRungeKuttaCoefficients |
|
||||||||||||||||
71 | NDSolve`ImplicitRungeKuttaGaussCoefficients | {} | ||||||||||||||||
72 | NDSolve`ImplicitRungeKuttaLobattoIIIACoefficients | {} | ||||||||||||||||
73 | NDSolve`ImplicitRungeKuttaLobattoIIIBCoefficients | {} | ||||||||||||||||
74 | NDSolve`ImplicitRungeKuttaLobattoIIICCoefficients | {} | ||||||||||||||||
75 | NDSolve`ImplicitRungeKuttaRadauIACoefficients | {} | ||||||||||||||||
76 | NDSolve`ImplicitRungeKuttaRadauIIACoefficients | {} | ||||||||||||||||
77 | NDSolve`IndexReductionVisualizer |
|
||||||||||||||||
78 | NDSolve`InitialHistory | {} | ||||||||||||||||
79 | NDSolve`InitializeMethod | {} | ||||||||||||||||
80 | NDSolve`InitializeSubmethod | {} | ||||||||||||||||
81 | NDSolve`InitializeSubmethods | {} | ||||||||||||||||
82 | NDSolve`IntraStepEvaluationData | {} | ||||||||||||||||
83 | NDSolve`InvokeMethod | {} | ||||||||||||||||
84 | NDSolve`iScaledVectorNorm | {} | ||||||||||||||||
85 | NDSolve`Iterate | {} | ||||||||||||||||
86 | NDSolve`iVectorNorm | {} | ||||||||||||||||
87 | NDSolve`iVectorNormWeights | {} | ||||||||||||||||
88 | NDSolve`iWeightedVectorNorm | {} | ||||||||||||||||
89 | NDSolve`KrylovIteration |
|
||||||||||||||||
90 | NDSolve`LinearlyImplicitEuler |
|
||||||||||||||||
91 | NDSolve`LinearlyImplicitMidpoint |
|
||||||||||||||||
92 | NDSolve`LinearlyImplicitModifiedMidpoint |
|
||||||||||||||||
93 | NDSolve`LinearStabilityBoundary | {} | ||||||||||||||||
94 | NDSolve`LocalInterpolation | {} | ||||||||||||||||
95 | NDSolve`LocallyExact |
|
||||||||||||||||
96 | NDSolve`LocalSeries | {} | ||||||||||||||||
97 | NDSolve`LSODA |
|
||||||||||||||||
98 | NDSolve`MakeStateSpaceNumericalFunction | {} | ||||||||||||||||
99 | NDSolve`MassMatrix |
|
||||||||||||||||
100 | NDSolve`MethodData | {} | ||||||||||||||||
101 | NDSolve`MethodOfLines |
|
||||||||||||||||
102 | NDSolve`MethodSymbol | {} | ||||||||||||||||
103 | NDSolve`MultistepControllerQ | {} | ||||||||||||||||
104 | NDSolve`NDSolveFunction | {} | ||||||||||||||||
105 | NDSolve`NDSolveMessage | {} | ||||||||||||||||
106 | NDSolve`NDSolveParametricFunction | {} | ||||||||||||||||
107 | NDSolve`NewMethodNorm | {} | ||||||||||||||||
108 | NDSolve`Newton |
|
||||||||||||||||
109 | NDSolve`NonstiffTest |
|
||||||||||||||||
110 | NDSolve`NonstiffTestData | {} | ||||||||||||||||
111 | NDSolve`NumericalFunctionToEquations | {} | ||||||||||||||||
112 | NDSolve`ODEInitialize |
|
||||||||||||||||
113 | NDSolve`ODESensitivityInitialize | {} | ||||||||||||||||
114 | NDSolve`OptimalRoundingSequenceFunction | {} | ||||||||||||||||
115 | NDSolve`OrthogonalChebyshevCoefficients | {} | ||||||||||||||||
116 | NDSolve`OrthogonalProjection |
|
||||||||||||||||
117 | NDSolve`ParametricPlugInFunction | {} | ||||||||||||||||
118 | NDSolve`Parareal |
|
||||||||||||||||
119 | NDSolve`PararealInitialize | {} | ||||||||||||||||
120 | NDSolve`PararealParallelStep | {} | ||||||||||||||||
121 | NDSolve`PararealParallelSteps | {} | ||||||||||||||||
122 | NDSolve`PararealStateData | {} | ||||||||||||||||
123 | NDSolve`PredicateBoundaryFunction | {} | ||||||||||||||||
124 | NDSolve`ProcessEquations |
|
||||||||||||||||
125 | NDSolve`ProcessSolutions | {} | ||||||||||||||||
126 | NDSolve`Projection |
|
||||||||||||||||
127 | NDSolve`RealLine | {} | ||||||||||||||||
128 | NDSolve`ReduceIndex |
|
||||||||||||||||
129 | NDSolve`Reinitialize |
|
||||||||||||||||
130 | NDSolve`ReinitializeVector |
|
||||||||||||||||
131 | NDSolve`RisingEdge | {} | ||||||||||||||||
132 | NDSolve`RombergSequenceFunction | {} | ||||||||||||||||
133 | NDSolve`SameDiscontinuities | {} | ||||||||||||||||
134 | NDSolve`SamePrecisionQ | {} | ||||||||||||||||
135 | NDSolve`ScaledVectorNorm | {} | ||||||||||||||||
136 | NDSolve`Self | {} | ||||||||||||||||
137 | NDSolve`SensitivityMethod |
|
||||||||||||||||
138 | NDSolve`SetSolutionDataComponent | {} | ||||||||||||||||
139 | NDSolve`SetState | {} | ||||||||||||||||
140 | NDSolve`Shooting |
|
||||||||||||||||
141 | NDSolve`SimplifySystem | {} | ||||||||||||||||
142 | NDSolve`SlidingModeFunction | {} | ||||||||||||||||
143 | NDSolve`SolutionData | {} | ||||||||||||||||
144 | NDSolve`SolutionDataComponent | {} | ||||||||||||||||
145 | NDSolve`SolutionDataIndex | {} | ||||||||||||||||
146 | NDSolve`SolutionDataListQ | {} | ||||||||||||||||
147 | NDSolve`SolutionDataTemplate | {} | ||||||||||||||||
148 | NDSolve`SpectralRadius |
|
||||||||||||||||
149 | NDSolve`SpectralRadiusOptionValues | {} | ||||||||||||||||
150 | NDSolve`Splitting |
|
||||||||||||||||
151 | NDSolve`StabilizedRungeKutta |
|
||||||||||||||||
152 | NDSolve`StateData | {} | ||||||||||||||||
153 | NDSolve`StateDataQ | {} | ||||||||||||||||
154 | NDSolve`StateSpace |
|
||||||||||||||||
155 | NDSolve`StateSpaceFunction | {} | ||||||||||||||||
156 | NDSolve`StateSpaceFunctionQ | {} | ||||||||||||||||
157 | NDSolve`StepSizeControlData | {} | ||||||||||||||||
158 | NDSolve`StiffnessSwitching |
|
||||||||||||||||
159 | NDSolve`StiffnessTest |
|
||||||||||||||||
160 | NDSolve`StiffnessTestData | {} | ||||||||||||||||
161 | NDSolve`Streamline |
|
||||||||||||||||
162 | NDSolve`StructuralIncidenceArray | {} | ||||||||||||||||
163 | NDSolve`StructuralIndex |
|
||||||||||||||||
164 | NDSolve`SubspaceIteration |
|
||||||||||||||||
165 | NDSolve`SwitchingVariable | {} | ||||||||||||||||
166 | NDSolve`SymmetricCompositionCoefficients | {} | ||||||||||||||||
167 | NDSolve`SymmetricCompositionSymmetricMethodCoefficients | {} | ||||||||||||||||
168 | NDSolve`SymplecticPartitionedRungeKutta |
|
||||||||||||||||
169 | NDSolve`SymplecticPartitionedRungeKuttaCoefficients | {} | ||||||||||||||||
170 | NDSolve`TestImplicitSolver | {} | ||||||||||||||||
171 | NDSolve`ToSymbol | {} | ||||||||||||||||
172 | NDSolve`ValidNumericalFunctionQ | {} | ||||||||||||||||
173 | NDSolve`ValidScaledVectorNormQ | {} | ||||||||||||||||
174 | NDSolve`ValidStateDataQ | {} | ||||||||||||||||
175 | NDSolve`VariableData | {} | ||||||||||||||||
176 | NDSolve`VODE | {} | ||||||||||||||||
177 | NDSolve`$EventSolution | {} | ||||||||||||||||
178 | {NDSolve`ImplicitRungeKuttaGaussCoefficients,NDSolve`ImplicitRungeKuttaLobattoIIIACoefficients,NDSolve`ImplicitRungeKuttaLobattoIIIBCoefficients,NDSolve`ImplicitRungeKuttaLobattoIIICCoefficients,NDSolve`ImplicitRungeKuttaRadauIACoefficients,NDSolve`ImplicitRungeKuttaRadauIIACoefficients} | {} | ||||||||||||||||
179 | 5 | {} |