## 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 a `Range[]` statement. Use `FoldList[]` and `NestList[]` 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`, use `MapThread[Inner`:

```ModelLabels[Var : {__String},
LagList : {__Integer?Positive}] :=
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 reads `nameOfMatrix//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 form `MatrixForm[List[...]]`, instead of just `List[..]`, and so can’t be manipulated like a list.

• `Which` statements are much more effective than nested `If` statements.

• If you are writing a package that depends on other (built-in) packages (standard add-on), 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 your `BeginPackage` 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 map `Chop[]` onto the matrix. `Chop[]` gets rid of elements smaller than a specified level (the default is 10^-10). Don’t forget that to `Map[]` a function to the elements of a matrix, you have to use `Map[`function,matrix`,{2}]`, not function`/@`matrix.

• Be careful with `Apply`ing `Plus` functions to matrices. `Map` and `Apply` have higher precedence than most other operators, including powers. So if, for example, you want to sum the squares of a vector, use `Plus @@ (vector^2)` not `Plus @@ vector^2`.

• If you need to use single-letter local or place-holder variables in a function or package, consider using the non-standard character sets, such as the script, gothic or double-struck letters. That way, they are less likely to conflict with other global variables.