Verbeia.com Mathematica Tips
The following are some of the tips Luci has picked up in the years she has been using Mathematica. See Ted’s Tricks, also on this site, for more comprehensive Mathematica tips.

If you’re getting bracket matching errors in version 3, even when you are sure you had them right, try deleting all the linefeeds in the cell. Sometimes they confuse the kernel.

Functional Programming works faster, although the code is harder to understand. Instead of
Tables
, consider Mapping a function to aRange[]
statement. UseFoldList[]
andNestList[]
instead of loops. This Mathematica notebook shows concrete evidence of the difference. Functional programming can be powerful, but sometimes it gets difficult to handle when you have multiple nested pure functions. For example, to get list of lists of labels, with a different number of labels in each sublist.LagNums =ToString/@Range[16]; Var = {"Y ", "DGDP ", "CommclPropInf ", "CredittoGDP "}; LagList = {6, 7, 8, 6}; VaRLagLabelFn[VarString_, Lag_] := Map[StringJoin[VarString, #] &, Take[LagNums, {1, Lag}]] ModelLabels = MapThread[VaRLagLabelFn[#1, #2] &, {Var , LagList}];
You can’t make this into a single step because the pure function labels don’t nest properly. Instead of using
MapThread[Map
, useMapThread[Inner
:ModelLabels[Var : {__String}, LagList : {__Integer?Positive}] := MapThread[ Inner[StringJoin, Table[#1, {#2}], (ToString /@ Range[#2]), List] &, {Var, LagList}]

Don’t forget the & in functional programming!!

When creating matrices and arrays, don’t use formatting commands like
//TableForm
and//MatrixForm
in the initial assignment statements. This just won’t work if you then want to manipulate your matrix like a normal list. Instead, try defining the matrix, suppressing the output of the definition by putting a semicolon at the end of the line. Then have a command that just readsnameOfMatrix//MatrixForm
 you can even put it on the same line after the semicolon.The reason for this is that if you define the object with a
//MatrixForm
at the end, it has the formMatrixForm[List[...]]
, instead of justList[..]
, and so can’t be manipulated like a list. 
Which
statements are much more effective than nestedIf
statements. 
If you are writing a package that depends on other (builtin) packages (standard addon), you must ensure that your
BeginPackage
statement includes imports not only for the package that you think you need, but all the packages that it imports when it is read it. You can find out what those other packages are by reading in the package you want in a fresh Mathematica session, and then evaluating the$Packages
command. Put all of the package contexts that come up into yourBeginPackage
statement. 
Sometimes, if you are multiplying a matrix containing some zeros and some approximate real numbers, with another matrix of algebraic expressions, you will end up with approximate zeros multiplied by the algebraic elements in the result. But of course you want them to cancel out, right?
The fix is to mapChop[]
onto the matrix.Chop[]
gets rid of elements smaller than a specified level (the default is 10^10). Don’t forget that toMap[]
a function to the elements of a matrix, you have to useMap[
function,matrix,{2}]
, not function/@
matrix. 
Be careful with
Apply
ingPlus
functions to matrices.Map
andApply
have higher precedence than most other operators, including powers. So if, for example, you want to sum the squares of a vector, usePlus @@ (vector^2)
notPlus @@ vector^2
. 
If you need to use singleletter local or placeholder variables in a function or package, consider using the nonstandard character sets, such as the script, gothic or doublestruck letters. That way, they are less likely to conflict with other global variables.