Supplementary Mathematica Help Browser
by Ted Ersek   ted.ersek@navy.mil

What I try to provide through this resource is additional material to help people get over the Mathematica learning curve. More specifically I try to demonstrate features that are hard to master from the Wolfram Research documentation. I also try to explain nuances that lots of users would otherwise never know about.

This resource isn't intended for people who never used Mathematica and want to start learning how to use it. I suggest that users in that position get a copy of The Mathematica Book and read sections 1.0 through 1.10,  sections 2.1 through 2.4 (260 pages of light reading). After that one should read any sections in Part III (Advanced Mathematics in Mathematica) in the same book that are of interest. At that point one should have a working knowledge of Mathematica.  One can then use The Mathematica Book, and of course this resource as reference material.

Because I don't intend to teach Mathematica from the ground up, a lot of principles fundamental to Mathematica aren't explained here. I figured there is no need to explain features that are explained very well in The Mathematica Book.

You also won't find discussion of certain features that even I haven't used (ie. working with Files and Streams).

New or Revised in May 2000

FoldList
Insert
Part
Together
ToString
Simplify & FullSimplify
Split
Sort
Pure functions with no arguments
Controlling Memory Usage
Programming For Speed
Tech Support Secrets
Working with a List or Table of data
An Algebraic Transformation
Making a Tensor into a Matrix

New or Revised in Oct 2002

Apply
ButtonProgramming
ButtonStyles
CoefficientsToPolynomial
Condition
Complement
Count
DeleteCases
Distribute, an application
Dot
EvaluateAt
EvaluatePattern
ExactNumberQ, InexactNumberQ, NumberQ, NumericQ
FreeQ
Function
Inner
Launching an email message
Level
MemberQ
Message
Options, OptionQ
Outer
Position
Replace
Scan
(s_Symbol) may not be specific enough
TagBox
Tr
Trig option
Union without sorting
Update
Where definitions are stored
$OutputForms
$PreRead, $Pre, $Post, $PrePrint

New or Revised in May 2004

Alternatives
Clear, ClearAll, Remove
Condition
Blank(_), BlankSequence(_), BlankNullSequence(___)
Button Programming
Evaluate
The Evaluation Process
Flatten
HoldAll, HoldFirst, HoldRest
HoldAllComplete
ListConvolve
ListCorrelate
NHoldAll, NHoldRest, NHoldFirst
Optional
Ordering
Pattern
Pattern Matching
PatternTest
Random
Repeated (..), RepeatedNull (...)
Replace
ReplaceAll, ReplaceRepeated
Tech Support Secrets
Unevaluated
Verbatim

Alternatives

Alternatives as in (p1 | p2 | p3) are used when you want to refer to one of multiple patterns.  So in the next cell ( _Sin | _Cos | _Tan ) represents an expression with the with the head Sin, Cos, or Tan.

{(E^2), 2, Sin[1] + x, Cos[1] - 1/2, 3.4, 
        Tan[x + 1]} /. (_Sin | _Cos | _Tan) -> 
        Trigonometric

in the next cell (f) is defined when given one argument with the head Real or Integer.

Clear[f, s]
f[x_Real | x_Integer] := x + 1
{f[2], f[2.4], f[2, 4], f[s]}

In the next cell (f) is defined when given one or more real arguments or one or more integer arguments.  However, this definition isn't used when (f) is given a sequence of arguments where some are real and others are integer.  Allowing a mix of different patterns requires is demonstrated in the section on Repeated, RepeatedNull.

Clear[f, s]
f[x__Real | x__Integer] := x + 1
{f[2], f[2.4], f[2, 4], f[2.4, 2.1], f[2, 2.4], f[s]}

Instead of using  (x_Real  x_Integer) we can use  x : (_Real | _Integer).  
And instead of using (x__Real | x_Integer) we can use  x(__Real | __Integer).  

In addition any other combination of pattern matching constructs can be used with Alternatives.  Nuances of pattern matching are discussed in another section.

A common place to use Alternatives is in the second argument of MakeBoxes when you want to use a MakeBoxes rule for StandardForm, and TraditionalForm, but not for  InputForm, TableForm, etc.  In that case use  form:(StandardForm|TraditionalForm)  as the second argument in the MakeBoxes definition as in the next cell.  You can see below that this MakeBoxes rule is used for StandardForm, TraditionalForm, but not InputForm.

MakeBoxes[foo[expr_], form : (StandardForm | TraditionalForm)] := 
      RowBox[{"{", MakeBoxes[expr, form], "}"}]

Apply

First I define (lst) which will be used to demonstrate Apply.

lst = 
      Table[Random[Integer, {0, 9}],    {3}, {2}, {2}, {2}]

(h@@lst) simply changes the head of list to h.

ClearAll[h, g1, g2, g3, g4];
h @@ lst

Starting with Mathematica 4.0 we could use  (h@@@lst)  to apply a head to all sub-expressions at a specific level.

h @@@ lst

(h@@lst)  is normally equivalent to Apply[h, expr] and  (h@@@lst)  is normally equivalent to Apply[h, lst, 1] as demonstrated in the next cell.

{h @@ lst === Apply[h, lst],   h @@@ lst === 
        Apply[h, lst, 1]}

If you use Apply in packages you write and you need to make the package highly reliable you should be completely explicit by using things like
Apply[h, expr, Heads->False]  or  Apply[h, expr, 1, Heads->False]  since the user of your package may have changed the default option using  SetOptions[Apply, Heads->True].

The next example changes the heads at levels 1 and 2 to h.

Apply[h, lst, 2]

The next example changes the heads at all levels from 0 down to level 2.

Apply[h, lst, {0, 2}]

Fortunately we can Apply a function to an atomic expression without getting an error message.  In that case we simply get the atom back.

{h @@ 2, h @@@ 3}

The heads we are changing don't have to be lists and they don't have to be the same.  Here I make a more general expression to demonstrate this.

expr = 
        g1[{2, 3}, {3, 4}] g2[{4, 5}, {5, 6}] + 
          g3[{6, 7}, {7, 8}] g4[{8, 9}, {9, 10}];

This changes the head Plus to h.

h @@ expr

This changes the head Times to h.

h @@@ expr

This changes the heads g1, g2, g3, and g4 to (h).

Apply[h, expr, {2}]

Heads Option

Apply has a Heads option with the default setting (Heads->False).  I needed help from David Park to come up with some examples here.  In the next input Apply is used with the default setting (Heads->False), and the only sub-expression whose head is changed are Part[expr,1]⟶g1[2] and Part[expr,2]⟶g2[3].  Not including heads these are the only sub-expressions at level 1.

ClearAll[f, g1, g2, h, y];
expr = f[y][g1[2], g2[3]];
Apply[h, expr, 1]

Part[expr, 0]

Now the previous example is repeated using (Heads->True) and (h) is applied to the sub-expression Part[expr,0]⟶f[y]. The expression f[y] is the head of (expr) and Apply only affects the heads when the setting (Heads->True) is used.

Apply[h, expr, 1, Heads -> True]

Also read  "Further Examples" at the end of the  Apply  documentation in the Help Browser.

Array

In the first line below we get a list of Cos[x/4] sampled at 
x={1, 2, 3, ... 12}.  You can get the same result using Cos[Range[1/4,3,1/4]], and timing tests show it's faster to do this using Range.  If you are concerned about speed, you should use Range whenever possible.  The #& notation is explained under Function.

Array[Cos[#/4] &,   12]

The next line gives 12 samples of Cos[x/4] starting at (x=4).

Array[Cos[#/4] &,   12, 4]

Array can take a fourth argument which should be applied to the result instead of list.  The next line finds the minimum of the samples given in the previous line.  Notice the function to be applied must be the fourth argument, so you must provide an starting value as a third argument.

Array[Cos[#/4] &, 12, 4, Min]

The starting place for Array doesn't have to be an integer.  In the next line Array starts at the symbol (t) and samples Cos[_] at 11 points.

ClearAll[f, t];
Array[Cos, 11, t]

In the next line Array is used to make a matrix.  Range can do many things Array can do very efficiently.  However, Array is the method of choice for making matrices such as the following.

Array[f, {5, 3}] // MatrixForm

In the line above  both arguments of (f) start at one (the default).  
Using the next line both arguments of (f) start at 10.

Array[f, {5, 3}, 10] // MatrixForm

In the next line the array starts with 4 in the first argument and 10 in the second argument and counts from there

Array[f, {5, 3}, {4, 10}] // MatrixForm

Blank (_), BlankSequence (__), BlankNullSequence (___)

Blank (_), BlankSequence (__) and BlankNullSequence (___) are the most primative of Mathematica's pattern matching features.

Basic Blank (_) usage

In the first example Blank (_) is used for individual arguments.  In this case (f) is only defined when it's given two arguments.

Clear[f];
f[x_, y_] := {x + 1, y + 1}
{f[t, 3], f[t, 3, 5], f[]}

Basic BlankSequence (__) usage

In the next example BlankSequence (__) is used and (x___) represents a sequence of one or more arguments.  So in this case (f) is defined when it's given one or more argument, but undefined when given no arguments.

Clear[f];
f[x__] := {0, x, 0}
{f[t], f[t, 3, 5], f[]}

Basic BlankNullSequence (___) usage

In the next example BlankNullSequence(___) is used and (x___) refers to a sequence of any number of arguments, or an empty set of arguments.  In this case f[] returns {0,0} while (f) was undefined in the previous example.

Clear[f];
f[x___] := {0, x, 0}
{f[t], f[t, 3, 5], f[]}

Use of Blanks without naming a variable

If a function doesn't use some of the arguments, it's more efficient to not name them. 
In the following example I define a function that returns True when given any single argument.  Often times we will give a function other definitions to use in other cases, and make it use a catch all definition such as this in all other cases.

Clear[f];
f[_] := True
{f[t], f[t, 3, 5], f[]}

In the next cell (f) returns True when given one or more argument.

Clear[f];
f[__] := True
{f[t], f[t, 3, 5], f[]}

In the next cell (f) returns True when given any number of arguments or no arguments at all.

Clear[f];
f[___] := True
{f[t], f[t, 3, 5], f[]}

A more common use of an unamed BlankNullSequence is in the next cell.  In this case (f) is defined when it gets two or more arguments.  The first argument goes with (x_), the second argument goes with (y_), and all other arguments go with (___).

Clear[f];
f[x_, y_, ___] := {x + 1, y + 1}
{f[t], f[t, 3], f[u, 4, v, b, n, m]}

Use of named Blanks with a specified head

In the next example (f) is only defined when given two arguments that are symbols.

Clear[f, s, t];
f[x_Symbol, y_Symbol] := {x + 1, y + 1}
{f[s, t], f[s, 3], f[s, s + t]}

In the next example (x__Real) represents a sequence of one or more arguments that are symbols.  If any argument of (f) is not a symbol, (f) is not defined.

Clear[f, s, t];
f[x__Symbol] := Plus[x, 1]
{f[s], f[s, t], f[], f[s, t, x + y]}

In the next example BlankNullSequence(___) is used and (x___Symbol) refers to a sequence of any number of arguments with the head symbol, or an empty set of arguments.

Clear[f, s, t];
f[x___Symbol] := Plus[x, 1]
{f[s], f[s, t], f[], f[s, t, x + y]}

Use of unamed Blanks with a specified head

In the next few examples there is no advantage in naming the arguments, and the definition evaluated faster than one where the argument is named.  In the next cell (f) returns True when given a single argument with the Head Real.

Clear[f];
f[_Real] := True
{f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

In the next cell (f) returns True when given one or more argument and all the arguments have the Head Real.

Clear[f];
f[__Real] := True
{f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

In the next cell (f) returns True when given any number of arguments or no arguments at all, but only when all arguments given have the Head Real.

Clear[f];
f[___Real] := True
{f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

Block

Block[{x, y}, ....] is normally used to make the kernel to temporarily forget about any values that symbols (x,y) may have.  Contrary to what Module does the variables (x,y) inside Block are the same variables (x,y) outside the Block.  The only thing Block does is temporarily change or clear the values of these variables. Some applications are given in the grouped cells below. 

The next cell clears any previous definitions that might interfere with the examples below.

Clear["Global`*"]

Temporarily clear the meaning of built in functions

If you need to write a program that you know may post messages you can suppress the display of messages during intermediate steps of your program.  As a specific example the expression in the next cell causes the kernel to evaluate Message[Prime::intpp, 2.25] inside the Block.  The head Message has no meaning while inside the Block, so the message is never printed.  Try evaluating Prime[2.25] without using  Block and a message will be printed (unless you turned off the message earlier).

Block[{Message}, Prime[2.25]]

For another example of making the kernel forget what a built-in function does consider the expression below which is wrapped in HoldForm.

expr = 
        HoldForm[{{Cos[π], (Sqrt[8]), 
              Cos[π], (((x y z))^2)}, 
{(Sqrt[12]), 
              Sin[ArcCos[x]], 2, 0}}];

First we use ReleaseHold to see what the expression evaluates to without HoldForm.

ReleaseHold[expr]

Suppose you want to evaluate this expression, but you don't want to evaluate (Sqrt[8])⟶2 (Sqrt[ 2]) or (Sqrt[12])⟶2 (Sqrt[ 3]). This can be done by using Block to make the kernel temporarily forget what Power does!  Recall (Sqrt[n])has the FullForm Power[n, 1/2].  Once inside Block we change the head of (expr) from HoldForm to List, and let it evaluate.  After evaluation is finished we change the head back to HoldForm so that (Sqrt[8])and (Sqrt[12]) don't evaluate after we get out of the Block.  The curious thing about this trick is that we didn't have to Unprotect Power.  Another method of allowing only certain parts of an expression to evaluate is given in the discussion of Fold.

Block[{Power}, HoldForm @@ List @@ expr]

Temporarily change the value of $DisplayFunction

The following line indicates the Default value Plot uses for the DisplayFunction option.

Options[Plot, DisplayFunction]

Besides making the kernel forget that a symbol has values you can use Block to temporarily use a new value for symbols. We can use Block to temporarily change the value of $DisplayFunction as in the cell below.  This creates the satisfying result of presenting only a single graphic, without typing  (DisplayFunction->Identity) four times!

\(\(Block[{$DisplayFunction = Identity}, 
\ \ \ \ \ p1 = 
          Plot[Sin[t], {t, 0, π/2}]; 
\ \ \ \ \ p2 = 
          Plot[Sin[2\ t], {t, 0, π/2}]; 
\ \ \ \ \ p3 = 
          Plot[Sin[3\ t], {t, 0, π/2}]; 
\ \ \ \ \ p4 = 
          Plot[Sin[4\ t], {t, 0, π/2}];
];\)\)
\(\(Show[
        GraphicsArray[{{p1, p2}, {p3, p4}}], 
\ \ \ ImageSize -> {400, 
            260}
];\)\)

Temporarily decrease $RecursionLimit  and/or  $IterationLimit

When you are writing preliminary drafts of a program you might want to use a lower value of $RecursionLimit and/or Iteration limit.  Using the code below you can make this change only take effect inside the Block.

ClearAll[f];
\(\(f[n_] := 2\ f[n - 1];\)
\ \)
\(Block[{$RecursionLimit = 25, \ $IterationLimit = 100}, 
\ \ f[
        3]
]\)

{$RecursionLimit, $IterationLimit}

Temporarily clear the numeric value of a symbol

In the first cell below (demo) is an expression in (x).  In the second cell (x) has a numeric value is assigned and demo will turn out to be a constant if evaluated.  Since demo is a constant (provided x has a numeric value) the derivative of demo is zero.   Worse than that we see below that D[demo,x] doesn't even evaluate as we would like.

Clear[x];
demo = x Exp[x];

x = 3;

D[demo, x]

In the next cell we can evaluate the derivative of (demo) as if (x) had no numeric value.  This was possible because Block[{x}, ....] ensured that (x) had no values.  Apply[HoldForm,....] had to be used to prevent evaluating the result with numeric values for (x).

Dx = Block[{x}, Apply[HoldForm, {D[demo, x]}]]

In the next cell ReleaseHold is used to allow complete evaluation of the above derivative.

ReleaseHold[Dx]

Finally in the next cell Block[{x=a}, ....] causes (x) to temporarily evaluate to (a), and then evaluate the derivative of demo.

Clear[a];
Block[{x = a}, D[demo, x]]

Modify a built-in function

Block can be used to modify the way built-in kernel functions evaluate in ways that are not otherwise possible.  For example suppose you wanted to change 'D' so that D[expr,x] would Expand expr before using the built-in definition of 'D'.

The definition for 'D' in the next cell is only used when the symbol $DoThis is True.  When this definition is used Block temporarily clears all values from the symbol $DoThis, and then evaluates D[Expand[expr],x].  If it were not for the use of Block{$DoThis},...] we would have infinite recursion since the new definition would call itself. However, once inside the Block $DoThis is not True, so the built-in definition of 'D' is used instead.

Unprotect[D];
$DoThis = True;

    
D[expr_, x_] /; $DoThis := 
Block[{$DoThis},  
        D[Expand[expr], x]  ]

D[(((x + 5))^3), x]

Button Programming

A hyperlink is a special type of a button.  We can also make buttons with predefined ButtonStyles, and most other buttons perform some sort of manipulation of a notebook. Such buttons that manipulate a notebook can usually be made by following the form in the next cell.  All you need to do is change "Button Name" and "A Pure Function" to suit you needs.

\(Cell[
BoxData[
          ButtonBox[
            RowBox[{"Button Name"}], 
ButtonFunction -> \(("A Pure Function")\), 
ButtonEvaluator -> Automatic, 
            Active -> 
              True
]], 
"Input"] // 
      CellPrint\)

The next cell shows the form needed to make a palette of three buttons.

\(Cell[
        BoxData[GridBox[{
{ButtonBox[
                  RowBox[{"Name 1"}], 
                  ButtonFunction -> \(("Pure Function 1")\), 
ButtonEvaluator -> Automatic, 
                  Active -> True]}, 
{ButtonBox[
                  RowBox[{"Name 2"}], 
                  ButtonFunction -> \(("Pure Function 2")\), 
ButtonEvaluator -> Automatic, 
                  Active -> True]}, 
{ButtonBox[
                  RowBox[{"Name 3"}], 
                  ButtonFunction -> \(("Pure Function 3")\), 
ButtonEvaluator -> Automatic, 
                  Active -> True]}
}, 
            RowSpacings -> 0]]
, "Input"] // 
      CellPrint\)

Working example of a notebook manipulating button (made from scratch)

Suppose you want to make a button that will delete all the messages in the InputNotebook[].  The non-trivial part is writing and debuging the lines of code that are used for the ButtonFunction. This can be solved by having two notebooks open at once.  As an example open an empty notebook and evaluate in the new notebook ( nb=SelectedNotebook[] ).  Next in the new notebook evaluate an erronerous input expression such as  (3=t).  Then come back to this notebook and evaluate the next cell.  The next cell will deletes all messages in the notebook assigned to (nb).  This is most of the code for our ButtonFunction.  The only other thing we need is for our ButtonFunction to make the assignment ( nb = InputNotebook[] ) before evaluating the code in the next cell.

NotebookFind[nb, "Message", All, 
        CellStyle];
NotebookDelete[nb];

The next cell makes the desired button.  However, the button that is created should be converted to a palette by selecting the CellBracket for the button and making the menu selection (File ⊳ Generate Palette From Selection).  Notice  the ButtonFunction setting must be a function.  If you aren't familar with pure functions read my section on Function.  Notice if you use '&' in the ButtonFunction then you have to wrap the button function in parentheses.  As I said earlier we need to use (ButtonEvaluator->Automatic) and (Active->True).

\(Cell[
BoxData[
          ButtonBox[
            RowBox[{"Delete Messages"}], 
ButtonFunction -> \((
Module[{nb = 
                        InputNotebook[]}, 
NotebookFind[
                      nb, "Message", All, 
                      CellStyle]; 
NotebookDelete[
                      nb]]
 &\ )\), 
ButtonEvaluator -> Automatic, 
            Active -> 
              True
]], 
"Input"] // 
      CellPrint\)

Other Button Tutorials

Links to all the tutorials about button programming that I know off are provided below.

See a tutorial by Paul Hinton at 
http://library.wolfram.com/infocenter/TechNotes/185

See several technical support pages on button programming at 
http://support.wolfram.com/mathematica/interface/buttons/

You might also find some helpful examples in this notebook under Button Styles or under Hyperlinks.

See How To Create Buttons, by Paul Hinton at
http://library.wolfram.com/conferences/devconf99/#frontend

See Programming the Front End, by John Novak at
http://library.wolfram.com/conferences/devconf99/#frontend

See Turning an Application into an Interface, Dale Horton at  
http://library.wolfram.com/conferences/devconf2001/#development

ButtonStyle

From the menu selection (Input ⊳ CreateButton) you can make buttons with ButtonStyles Paste, Evaluate, EvaluateCell, CopyEvaluate and CopyEvaluateCell. Each of these ButtonStyles are intended to be used when something is selected in an Input or Output cell and they all do something with the selected expression and the input cell.  Making one of these buttons is simple. Suppose for example you want a button that will Simplify the selected expression. To do that you would do the following:

(1)  Type Simplify[■] as I did in the next cell. 
       Note the ■ symbol can be entered as \(\(\ \)\(\[SelectionPlaceHolder]\)\(\ \)\) or using the alias  spl  or using the 'Complete Characters' palette.
(2)  Select "Simplify[■]" in the input cell.
(3)  Make the menu selection (Input ⊳ Create Button ⊳ Evaluate) 
(4)  Select the cell bracket for the input cell containing "Simplify[■]".
(5)  Make the menu selection (File ⊳ Generate Palette from Selection).

You will then have a palette containing the button just describe and it will work like those in the Algebraic Manipulation palette.

Simplify[\[SelectionPlaceholder]]

As far as I am aware you need to deal with lower level boxes if you want to make a palette of several buttons like the one I discuss above.  You can make such a palette by evaluating the next cell and doing the following:
(1)  Select the cell bracket for the cell created when the next cell is evaluated.
(2)  Make the menu selection (File ⊳ Generate Palette from Selection) and the palette will be displayed in it's own notebook.

\(Cell[
        BoxData[GridBox[{
{ButtonBox[
                  RowBox[{"Factor", "[", "\[SelectionPlaceholder]", "]"}], 
                  ButtonStyle -> "Evaluate"]}, 
{ButtonBox[
                  RowBox[{"Expand", "[", "\[SelectionPlaceholder]", "]"}], 
                  ButtonStyle -> "Evaluate"]}, 
{ButtonBox[
                  RowBox[{"Together", "[", "\[SelectionPlaceholder]", "]"}], ButtonStyle -> "Evaluate"]}
}, 
            RowSpacings -> 0]]
, "Input"] // 
      CellPrint\)

All buttons above above had ButtonStyle "Evaluate".  This and all other built-in ButtonStyles are explained below.

Built-in  ButtonStyles

Evaluating the next cell makes a sample palette containing a button with each of the built-in Button-Styles. Once the resulting palette is displayed, you can move your mouse pointer over one of the buttons and the ButtonStyle for that button will be shown in the bottom of the window. You can verify that each of the buttons works as I describe below.

\(\(nb = NotebookCreate[Visible -> False];\)
\ \)
\(NotebookWrite[
      nb, {Cell[
          BoxData[GridBox[{{ButtonBox[\(\[SelectionPlaceholder] + 1\), 
                    ButtonStyle -> "Paste", 
                    ButtonNote -> "Paste"]}, {ButtonBox[\(\[SelectionPlaceholder] + 1\), ButtonStyle -> "Evaluate", 
                    ButtonNote -> "Evaluate"]}, {ButtonBox[\(\[SelectionPlaceholder] + 1\), ButtonStyle -> "EvaluateCell", 
                    ButtonNote -> "EvaluateCell"]}, {ButtonBox[\(\[SelectionPlaceholder] + 1\), ButtonStyle -> "CopyEvaluate", 
                    ButtonNote -> "CopyEvaluate"]}, {ButtonBox[\(\[SelectionPlaceholder] + 1\), 
                    ButtonStyle -> "CopyEvaluateCell", 
                    ButtonNote -> "CopyEvaluateCell"]}}, 
              RowSpacings -> 0, ColumnWidths -> 10]], 
          NotebookDefault, Evaluatable -> True]}]; 
\ \)
SetOptions[nb, WindowSize -> {Fit, Fit}, 
        WindowFrame -> "Palette", 
        WindowElements -> "StatusArea", 
        WindowFrameElements -> "CloseBox", 
        WindowClickSelect -> False, ShowCellBracket -> False, 
        Active -> True, Visible -> True];

Clear[x];

Paste  ButtonStyle

The top button in the sample palette has the "Paste" ButtonStyle.  If you select 15 in the next cell and depress the top button, the 15 is changed to (15+1) and nothing is selected after making the change.  The results are similar if a part of an output cell is selected.

(Sqrt[15]) + x

Evaluate  ButtonStyle

The second button in the sample palette has the "Evaluate" ButtonStyle.  If you select 15 in the next cell and press the second button, the 15 is changed to 16 (which is 15+1) and the number 16 is selected after making the change. The results are similar if a part of an output cell is selected.

(Sqrt[15]) + x

EvaluateCell  ButtonStyle

The third button in the sample palette has the "EvaluateCell" ButtonStyle.  If you select the 15 in the next cell and press the third button, the 15 is changed to (15+1), the input cell evaluates, and the contents of the resulting output cell are selected once evaluation is finished. The results are similar if a part of an output cell is selected.

(Sqrt[15]) + x

CopyEvaluate  ButtonStyle

The fourth button in the sample palette has the "CopyEvaluate" ButtonStyle.  If you select 15 in the next cell and press the fourth button, then a new input cell containing 16 (the result of 15+1) is inserted after the original input cell.  The expression in the new input cell is selected once evaluation is finished. The results are similar if a part of an output cell is selected.

(Sqrt[15]) + x

CopyEvaluateCell  ButtonStyle

The bottom button in the sample palette has the "CopyEvaluateCell" ButtonStyle.  If you select 15 in the next cell and press the bottom button, a new input cell containing (15+1) will be inserted below the original input, and the new input cell will evaluate. After evaluation is finished the resulting output expression will be selected. The results are similar if a part of an output cell is selected.

(Sqrt[15]) + x

Make your own ButtonStyle

If you tried the sample button with setting  ButtonStyle->EvaluateCell  you would have noticed that after the button is used the contents of the new output cell are selected.  I would prefer that after the button finishes nothing is selected, and the insertion point is after the new output cell, and I made a new ButtonStyle to do this.  To try this new ButtonStyle the "Prototype for style: EvaluateCell2" cell below should be pasted into your style sheet under "Palatte Styles".  You can open the Style Sheet with menu selection (Format ⊳ Edit Style Sheet ).

⚠  Warning ⚠
I am using Mathematica 4.1 under Windows 98, and the ButtonStyle wouldn't work when I added it to a private StyleSheet. However, the ButtonStyle would work when I pasted the prototype below in the style sheet (Default.nb).  You might have the same problem.

To see the details of the "EvaluateCell2" prototype in the next cell select the cell bracket and make the menu selection (Format ⊳ ShowExpression).

The "EvaluateCell2" prototype above is almost identical to the one for the "EvaluateCell" prototype  The only difference is in the ButtonFunction setting and the difference is colored red below.  The next cell shows the "EvaluateCell"  ButtonFunction setting.

ButtonFunction:>(FrontEndExecute[{FrontEnd`NotebookApply[FrontEnd`InputNotebook[], #, 
                  All],FrontEnd`SelectionMove[FrontEnd`InputNotebook[], All, 
                  Cell, 1],FrontEnd`SelectionEvaluateCreateCell[FrontEnd`InputNotebook[],All]}]&)

The next cell shows the ButtonFunction setting I made for the "EvaluateCell2"  prototype.

ButtonFunction:>(FrontEndExecute[{FrontEnd`NotebookApply[FrontEnd`InputNotebook[], #, 
                  All],FrontEnd`SelectionMove[FrontEnd`InputNotebook[], All, 
                  Cell, 1],FrontEnd`SelectionEvaluateCreateCell[FrontEnd`InputNotebook[],After]}]&)

If you want to learn more about FrontEndExecute, and the  FrontEnd`*  functions see chapter 56 of The Beginner's Guide To Mathematica, by Jerry Glynn and Theodore Gray.  

You will also notice that all of the built-in ButtonStyles do strange things to the selected notebook if nothing is selected.  If you are ambitious you could go into the shared style sheets and change the ButtonFunction for the built-in ButtonStyles to ensure they do nothing if a part of an input expression is not selected. 

That covers most of this subject but you might want to also read   
http://support.wolfram.com/mathematica/interface/buttons/buttonstyle.html

Cases

Consider the list of (x,y,z) coordinates below.

Clear[data, x];
\(\(data = {
		{12, 2, 4}, {3, 4, 6}, {2, 8, 5}, {7, 13, 12}, {3, 22, 
            5}, {11, 2, 7}, {10, 0, 14}, {6, 2, 23}, {8, 9, 4}, {6, 3, 
            7}};\)\)

The line below picks out all elements of data where the second of three in a list is greater than 7.

Cases[data, {x_, y_, z_} /; y > 7]

Now the next line will pick the points where y>7 and return the list {x, y, (Sqrt[(x^2) + (y^2)])}.

Cases[
      data, {x_, y_, z_} /; y > 7 -> {x, 
          y, (Sqrt[(x^2) + (y^2)])}]

The method above works as long as (x) doesn't have a global value.  In the cell below (x) has a global value and one doesn't get the expected result.

x = π/4;
Cases[
      data, {x_, y_, z_} /; y > 7 -> {x, 
          y, (Sqrt[(x^2) + (y^2)])}]

The problem above can be avoided by using the line below. The difference is the use of (expr :> x) instead of (expr -> x).

Cases[
      data, {x_, y_, z_} /; y > 7 :> {x, 
          y, (Sqrt[(x^2) + (y^2)])}]

Notice (x) still has a global variable.

x

In the previous examples involving Cases[data,{x_,y_,z_}....] all occurrences that met the pattern were returned.  Cases can take a fourth argument which indicates the maximum number of elements it should return.  In the next line the fourth argument is 1 so Cases only returns {x, y, (Sqrt[(x^2) + (y^2)])} for the first element where (y>7).  Notice that a third argument was necessary since we wanted to use the fourth argument.  In this example the third argument provided is the default level specification (1).

Cases[
      data, {x_, y_, z_} /; y > 7 :> {x, y, (Sqrt[(x^2) + (y^2)])}, 
      1, 1]

In the previous examples Cases had only two arguments.  Cases can take a level specification as a third argument.  The default level specification is 1 or {1} (both are equivalent).  In the next line the default level specification is used, and Cases doesn't look inside each term of the sum.

Clear[a, w, y, u, z];
\(Cases[
      a + E\^\(-x\^2\) + Exp[y\^2] + w\^\(5\^x\) + u\^2 + 1\/z\^2 + 1\/4, 
      x_\^y_]\)

The next example is from the online Help Browser that comes with Mathematica.  Here the level specification is {1,∞} and Cases looks at every level (except level 0).  As a result the list returned by Cases includes \(\(\(\ \)\(x\^2, \(\(y\^2\)\(\ \)\(or\)\(\ \ \)\(5\^x\)\(\ \)\)\)\)\).

\(Cases[
      a + E\^\(-x\^2\) + Exp[y\^2] + w\^\(5\^x\) + u\^2 + 1\/z\^2 + 1\/4, 
      x_\^y_, {1, ∞}]\)

I don't provide further examples, but any pattern matching constructs can be used in Cases. Nuances of pattern matching are discussed in another section.

Heads Option

Cases has a Heads option with the default setting (Heads->False). This default setting almost always gives the desired effect.  In the next cell Derivative[_] is only used as a head and gives an example where the setting (Heads->True) is needed to find the use of Derivative.

ClearAll[f, x];
Cases[f[x] + 2  f'[x] - 3  f''[x] + 4  g'[x], 
      Derivative[_], {0, -1}, Heads -> True]

Clear, ClearAll, Remove

Mathematica has several places where it stores defintions, and I discuss this subject in another section.  Clear, ClearAll, and Remove are used to erase previous definitions.

The following clears symbols (f, h, g).

Clear[f, h, g]

The following claers all symbols matching character strings "MakeGraph*" and "MakeCurve*"

Clear["MakeGraph*", "MakeCurve*"]

The following clears all symbols in the Global context.

Clear["Global`*"]

In each example above we could have used ClearAll or Remove and got similar results.

Clear[f]

Clear[f] clears all definitions associated with (f) except for:
   (1)  Attributes of (f)
   (2)  DefaultValues for (f)
   (3)  Options[f]
   (4)  Messages associated with (f)
  
Also:
   (1)  The Symbol (f) remains on the list of symbols in the appropriate context after evaluating Clear[f].
   (2)  Assingments to In, and Out which are normally accessed using %,  %8, etc are not affected by evaluating Clear[f].
   (3)  Assignments such as  (g = f + 1),  and  (h[f] = value) are not affected by evaluating  Clear[f].
   
I have done experiments to check everything I can think of and all other definitions associated with (f) are erased by Clear[f].  In particular Clear[f] erases DownValues[f], UpValues[f], OwnValues[f], SubValues[f], NValues[f], and FormatValues[f].

ClearAll[f]

ClearAll[f] clears all definitions associated with (f):
However, after evaluating  ClearAll[f]  
   (1)  The Symbol (f) remains on the list of symbols in the appropriate context after evaluating ClearAll[f].
   (2)  Assingments to In, and Out which are normally accessed using %,  %8, etc are not affected by evaluating ClearAll[f].
   (3)  Assignments such as  (g = f + 1),  and  (h[f] = value) are not affected by evaluating  ClearAll[f].
   
Specifically  ClearAll[f]  erases  Attributes[f], DownValues[f], UpValues[f], OwnValues[f], SubValues[f], NValues[f], FormatValues[f], DefaultValues[f], Options[f] and 
Messages associated with (f).

Remove[f]

For all practical purposes Remove[f]  deletes any previous use of the symbol (f) from the Mathematica session.
Specifically Remove[f] does the following: 
    (1)  Clears all definitions associated with (f).
    (2)  The symbol (f) is removed from the list of symbols in the appropriate context.
    (2)  For all practical purposes assingments such as  (h[f] = value),   (g = f + 1)  are erased.  
    
    This includes assingments to the In, and Out history which are normally accessed using %,  %8, etc.
    Such assignments aren't actually erased.  Instead the symbol (f) in such assingments is changed to  Removed["f"].

Coefficient, CoefficientList

In the next cell Coefficient returns the coefficient of a particular term of a polynomial.  In this case (-210 (c^2) (x^2) y (z^2)) is a term of (((c x - 2  y + z))^7)after it's expanded.

Clear[a, b, c, d, x, y, z];
poly = (((c x - 2 y + z))^7);
Coefficient[poly, (x^2) y (z^4)]

We can get the same information from CoefficientList which returns a list of coefficients.  In the cell below Part[coeff, 3,2,5] returns the coefficient of x^(3-1)y^(2-1)z^(5-1).  In general if we say   lst=CoefficientList[poly,{x1,x2,x3,...}]  then  Part[lst,  n1, n2, ,n3, ...] will be the coefficient of x1^(n1-1) x2^(n2-1) x3^(n3-1).

coeff = CoefficientList[poly, {x, y, z}];
Part[coeff, 3, 2, 5]

The next line gives the coefficient of x^5.  As expected there is more than one term of poly with x^5 as a factor.

Coefficient[poly, (x^5)]

We can get the same result as the previous example if we use the next line.

Coefficient[poly, x, 5]

One can't get the result above directly from CoefficientList.  Instead pieces of the above result are included in the result of CoefficientList[poly,{x,y,z}].  The line below can be used to get pieces of the result above.  Specifically  coeff[[6]] contains all coefficients of x^5 (including those that are zero).

coeff[[6]]

Part[coeff, 6, 1, 3] is (-84 (c^5)) the coefficient of ((x^(6 - 1))*(y^(2 - 1))*
            (z^(2 - 1))) = ((x^6)*y*z) in poly. Part[coeff,6,3,1] is (84  (c^5)) the coeficient of ((x^(6 - 1))*(y^(3 - 1))*
            (z^(1 - 1))) = ((x^5)*(y^2)) in poly.  All other coefficients under Coeff[[6]] are zero which agrees with the result of Coefficient[poly, (x^5)].

The function defined in the next cell is a slight modification of a solution Roberto Colistete sent to the MathGroup. This function takes the coefficients from CoefficientList and returns the polynomial with those coefficients.  See the discussions of MapIndexed, and Function for further explanation of some features used here.

CoefficientsToPolynomial[coeffs_,  vars_List] /;(Length[Dimensions[coeffs]] === Length[vars]):= Plus @@ 
          Flatten[ 
            MapIndexed[#1 Times @@ (vars^(#2  -  1)) &,  
              coeffs,  {Length[vars]}] ]

The next cell shows another implementation of the same function by Michael Trott of Wolfram Research.

CoefficientsToPolynomial[coeffs_List,  
            vars_List] /; (Length[Dimensions[coeffs]] === 
              Length[vars])  :=  
        Plus  @@  Flatten[
            MapIndexed[(#1 Inner[Power,  vars,  #2  -  1,  
                      Times]) &,  coeffs,  {Length[vars]}]]

A most elegant implementation of the same function is given in the next cell. This is a slight modification of a solution Alan Hayes sent to the MathGroup.

CoefficientsToPolynomial[coeffs_, 
          vars_List] /; (Length[Dimensions[coeffs]] === 
            Length[vars]) := 
Dot @@ 
        Reverse[Append[vars^(Range[Dimensions[coeffs]] - 1), 
            coeffs]]

Either of the above definitions can be used on the examples below.

lst = 
        CoefficientList[(3  x - 2  y + z + Exp[a])^3, {x, y, 
            z}];
poly = Factor@CoefficientsToPolynomial[lst, {a, b, c}]

lst2 = 
        CoefficientList[
          a (x^3) + c x y + b (x^4) y + d (y^2) + Exp[t], {x, 
            y}];
CoefficientsToPolynomial[lst2, {a, b}]

Also this hyperlink will take you to a Wolfram Research web site with more information.

Complement

In the cell below we get all elements that are in (lst1) but not in (lst2) or (lst3).  Of course this would work with lists of arbitrary expressions, not only numbers.

lst1 = {3, 2, 7, 5, 2, 2, 3, 4, 5, 6, 1};
lst2 = {2, 3};
lst3 = {4, 6, 27, 23};
Complement[lst1, lst2, lst3]

In the cell above Complement gives a sorted list of all elements of (lst1), that are not in (lst2).  If you want to do this without having the result sorted, DeleteCases should be used as in the next line.

DeleteCases[lst1, Alternatives @@ Union[lst2, lst3]]

In the previous example (Alternatives@@Union[lst2, lst3]) evaluates to (2 | 3 | 4 | 27 | 23).

Complement has the option SameTest with the default setting (SameTest->Automatic).  It seems when the setting (SameTest->Automatic) is used numeric values are compared using criteria equivalent to the built-in SameQ function. Notice in the next example 45 is not in the output because it is in (lst1) and (lst2). However, 164 is in the output and this is apparently because SameQ[164,164.0] returns False.

lst1 = {12, 45, 67, 144, 164};
lst2 = {13, 65, 88, 45, 90, 164.0};
Complement[lst1, lst2, SameTest -> SameQ]

In the next example the SameTest option is used to change the criteria for when to items should be considered "the same".  Here we get a list of numbers in (lst1) such that an element of (lst1) is discarded if an element of (lst2) differs from it by less than 0.2.

Complement[lst1, lst2, 
      SameTest -> (Abs[#1 - #2] < 0.2 &)]

Complement can work with expressions that have a head other than List. I demonstrate this point with Complement in the next cell.

ClearAll[h];
lst1 = h[3, 2, 7, 5, 2, 2, 3, 4, 5, 6, 1];
lst2 = h[2, 3];
lst3 = h[4, 6, 27, 23];
Complement[lst1, lst2, lst3]

Compile

You might want to download a related package posted on MathSource.  The package is posted at 
http://library.wolfram.com/database/MathSource/555/

Examples of compiled functions that take an arbitrary length vector and an arbitrary dimension matrix

Using Compile[{{x1,t1,n1},...},expr] indicates (x1) is a rank (n1) tensor where each element has the type (t1).  A vector is a rank 1 tensor,and a matrix is a rank 2 tensor.  A function (magnitude) is defined below which finds the magnitude of an arbitrary length vector.

\(\(magnitude = 

        Compile[{{vector, _Real, 
              1}}, \@\(\(Plus @@ \(vector\^2\)\)\(\ \)\)];\)\)

magnitude[{2.3, 4.5, 1.6, 6.7, 8.9}]

Using Compile[{{x, _Real, 2}}, ...] indicates (x) is a matrix of real numbers.  The matrix can have any dimensions.  As an example a CompiledFunction (func) is defined below.  The function (func) takes two matrices of real numbers named (a) and (p) and computes (p a (p^(-1))).

\(\(func = 
	
        Compile[{{a, _Real, 2}, {p, _Real, 2}}, 
		\ \((p . a . 
              Inverse[p])\)];\)\)

A demonstration of (func) is given below.

x=(({{1.2, 4.3, 0.1}, {0.2, 1.4, 0.2}, {0.1, 0.3, 0.4}}));
y=(({{1.2, 0.2, 0.0}, {0.3, 1.5, 2.3}, {0.2, 2.2, 0.7}}));
result = func[x, y];
MatrixForm[result]

How to make a compiled functions that run fast.

In version 3.0 Compile returns an expression with the form CompiledFunction[args, nregs, instr, func].  In version 4.0 Compile returns CompiledFunction[args, argregs, nregs, instr, func].  Either way the second from the last argument is (instr), a list of compiled function instructions.  Hence if (func) is a function defined using Compile func[[-2]] will return the list of instructions.  The compiled function instructions include low level operations such as 'store a real number in a particular register'.  You don't need to know what each instruction does, but you want make sure the instructions don't include something with the form Function[args, expr].  Any instructions with this form will probably cause the function to run much slower than it should because standard Mathematica evaluation instead of compiled evaluation will be used.

There are several reasons why the compiled function instructions would include the form Function[args,expr], and all reasons I know of are demonstrated below.

An important undocumented feature in Version 4

Version 4.0 has an undocumented feature that will post useful messages when a function is defined using Compile and a portion of the function can't be computed with compiled evaluation.  Evaluating the next cell will enable the undocumented feature.

Developer`SetSystemOptions["CompileReportExternal" -> 
          True];

Once the feature above is enabled (using Mathematica Version 4) a message is displayed after the next cell is evaluated.

f1 = 
        Compile[{{x, _Real}, {n, _Integer}}, 
          x^n/ Factorial[n]];

(1)  Only a subset of kernel functions can be used in compiled evaluation.

One possible reason for having Function[args,expr] in the instructions is that (expr) may use one or more kernel functions that can't be represented as a low level op-code instruction (a list of integers).  As a rule of thumb functions that have a simple translation into C can be converted to op-code.  This is only a rule of thumb because Factorial has a simple translation into C, but can't be represented in op-code.  In fact that is why the compiled function instructions for (f1) below includes Function[{x,n},n!].

f1 = 
        Compile[{x, {n, _Integer}}, x^n/n!];
f1[[-2]]

The function (f2) in the next cell does the same thing as (f1) in the previous cell, and will evaluate using compiled evaluation because the compiled function instructions don't include the form Function[args,instr].

f2 = 
        Compile[{x, {n, _Integer}}, 
          x^n/Times @@ Range[n]];
f2[[-2]]

An alphabetical list of kernel features that can be converted to op-code can be found by searching for Compile at: http://www.wolfram.com/support/Kernel/Symbols/.   The same symbols are sorted below by category below.

Math functions and constants

Plus, Times, Power, Sqrt, 
Divide, Minus, Subtract, Quotient, 
Sign, Abs, Arg, Re, Im, Conjugate, 
Floor, Ceiling, Round, Mod, Min, Max, Log, Exp, 
Sin, Cos, Tan, ArcSin, ArcCos, ArcTan, 
Csc, Sec, Cot, ArcCsc, ArcSec, ArcCot, 
Sinh, Cosh, Tanh, ArcSinh, ArcCosh, ArcTanh, 
Csch, Sech, Coth, ArcCsch, ArcSech, ArcCoth, 
N, Chop, Random, IntegerDigits, Product, Sum, EulerE
E, Pi, Degree, GoldenRatio, Catalan

     Compile in Version 4 and later supports the following:
     UnitStep, Khinchin, Glaisher, BitAnd, BitOr, BitXor, BitNot

Vector, matrix and tensor mathematics

List, Det, Inverse, Dot, DiagonalMatrix, IdentityMatrix, 
Transpose, Dimensions, TensorRank

Boolean related features

True, False, Not, And, Or, Xor, Equal, Unequal, 
Greater, GreaterEqual, Less, LessEqual, Equal, Unequal, SameQ, UnsameQ, 
EvenQ, OddQ, Positive, Negative, NonNegative, FreeQ, MemberQ, OrderedQ, 
VectorQ, MatrixQ

Building and manipulating expressions and lists

Set, List, CompoundExpression, Length, 
Array, Table, Range, Count, Select, Cases, DeleteCases, 
Position, ReplacePart, Join, Intersection, Complement, Union, 
Append, AppendTo, Prepend, PrependTo, 
Part, Take, Insert, Delete, Drop, Rest, First, Last, 
Flatten, Sort, Reverse, RotateLeft, RotateRight, Partition, Permutations

Functional programming

Apply, Map, MapAll, MapAt, MapIndexed, MapThread, 
Scan, FixedPoint, FixedPointList, Nest, NestList, 
Fold, FoldList, Array, Outer, ComposeList, Function, Slot, SlotSequence

Scoping and procedural programming

Module, With, Block, Do, While, For, 
Set, If, Switch, Which, Implies, 
Break, Continue, Catch, Throw, Goto, Label, Return, 
Increment, Decrement, PreIncrement, PreDecrement, 
AddTo, SubtractFrom, TimesBy, DivideBy, Null, $Failed

(2)  Compiled evaluation can't use global variables.

In the next cell a compiled function uses a global variable (width).  Later we see that the function works, but timing tests will show that it's no faster than if the function was defined without using Compile.

width = 2.5;
g2 = Compile[{x}, x + width];

g2[0.3]

The next cell defines the same function as the previous example and takes the global variable as an argument. This is the way global variables should be used in a compiled function.

g2 = Compile[{x, y}, x + y];
g2[0.3, width]

The next cell shows what happens if we try to set the value of a global variable inside a compiled function.  Here again the function works, but timing tests show that the function is no faster than the same function defined without using Compile.

h2 = Compile[{x}, (temp = 2.5; x + temp)];

h2[0.3]

In the last example the value of (temp) was set inside Compile.  When something like this is needed Block, Module, or With should be used to make (temp) a local variable.  Compiled functions are defined below that do the same thing as the last example using Block, Module, With and they all use compiled evaluation which runs much faster.  Besides the advantage in speed the approach below ensures that evaluating say (ha[1.5], hb[1.5], hc[1.5]) will not change the value (temp) might have outside the compiled function.

ha = 
        Compile[{x}, 
          Block[{temp = 2.5}, x + temp]];
hb = 
        Compile[{x}, 
          Module[{temp = 2.5}, x + temp]];
hc = 
        Compile[{x}, 
          With[{temp = 2.5}, x + temp]];
{ha[0.3], hb[0.3], hb[0.3]}

(3)  Compiled evaluation can't work with patterns.

The function in the next cell takes a list of real numbers, and determines if (0.5) is in the list.  This function will use compiled evaluation.

f4 = 
        Compile[{{lst, _Real, 1}}, 
          FreeQ[lst, 0.5]];
f4[{0.2, 0.3, 0.3}]

The function in the next cell also takes a list of real numbers, and determines if the list is free of negative numbers.  This function works, but timing tests will show that it's no faster than the same function defined without using Compile.  The problem in this case is that the compiled function uses the pattern (_?Negative).  Compiled evaluation isn't used if the second argument in Compile includes patterns of any kind.  When faced with a problem like this we should use an algorithm that doesn't require patterns, or define a function without using Compile.

f4 = Compile[{{lst, _Real, 1}}, FreeQ[lst, _?Negative]];
f4[{0.2, 0.3, 0.3}]

The next cell shows one way the function from the last example can be written to avoid the use of patterns.  This version will use compiled evaluation.

f4 = 
        Compile[{{lst, _Real, 1}},  
          FreeQ[Sign[lst], -1]  ];
f4[{0.2, 0.3, -0.4, 0.5}]

(4)  Local variables in compiled evaluation must always have the same type.

The next cell defines a function which uses a local variable (temp) where temp starts with an integer value.  Later in the function (temp) is changed to a real number.  In this case the function works, but timing tests will show it's no faster than the same function defined without using Compile.  Local variables used in a compiled function should always have the same type (Real, Integer, Complex, True|False) to ensure compiled evaluation is used.

f6 = 
        Compile[{x}, 
          Module[{temp = 5}, (temp = temp + x; 
              Round[temp])]];

f6[2.2]

(5)  A compiled function can't change the value of it's argument.

The next cell shows an attempt to define a function.  If you try to use this function you will see it doesn't work.  The problem here is that the function tries to change the value of it's argument, but a compiled function can't change the value of it's argument for the same reason we can't evaluate (3=5).

f7 = 
        Compile[{x}, 
Do[
            x = Cos[x], {4}]; 
x];

Some times we want to use an algorithm that starts with some value and replaces it with another value.  If we want to do this in a compiled function we should initialize a local variable to the value of the functions argument, and then change the value of the local variable.  The function in the next cell does this and uses compiled evaluation.

f7 = 
        Compile[{x}, 
Module[{temp = 
                x}, 
Do[
              temp = Cos[
                  temp], {4}]; 
temp
]];
f7[0.2]

(6)  Compiled evaluation can't work with all possible list structures.

The only list structures compiled evaluation can work with are vectors, matrices, and other tensors with (rank<101).  The function defined in the next cell returns a list structure that doesn't meet these restrictions so compiled evaluation can't be used.  In a case like this there is no advantage to using Compile to define the function.

f5 = Compile[{x}, {x, {2  x, 3  x}}];

f5[2.1]

Condition  (Pattn/;Test)

Condition is closely related to PatternTest.

Typical use of Condition

Condition is used to put a restriction on when a pattern is used and the syntax is forgiving.  Below I give three ways to use Condition to define a function that takes a positive number and return the square of the number.

Clear[foo];
foo[x_ /; Positive[x]] := x^2
foo[3]

Clear[foo];
foo[x_] /; Positive[x] := x^2
foo[4]

Clear[foo];
foo[x_] := x^2 /; Positive[x]
foo[5]

In the next cell I show ho the same function can be defined using PatternTest.  It doesn't make much difference which style is used.  If you want to optimize running time, some experimenting is recommended.

foo[x_?Positive] := x^2

The next cell defines a function that takes a list of numbers and returns a list where each number is squared.

ClearAll[g1, g2];
g1[lst : {x_, y_} /; (x < y)] := lst^2
g1[{2, 4}]

The next cell shows an unsuccessful attempt to define the function that was defined in the previous input.

ClearAll[g2, x];
g2[lst : {x_, y_ /; (x < y)}] := lst^2
g2[{2, 4}]

The definition above failed because the global value of (x) was used in (y_/;(x<y)) instead of the value associated with (x_).  The definition that worked used the condition ({x_,y_}/;(x<y)) in which case all pattern variables used were in the pattern we had a condition on.  Notice the "broken" definition from the previous input is used when (x) has a suitable value.

x = 0;
g2[{2, 4}]

A Subtle Point

David Park noted that using Condition in the definition of a function can give surprising results as demonstrated below.

ClearAll[g, a, UseDefinition];
g[a] /; UseDefinition := 2

In the next cell the definition of g[a] isn't used and this is what we expect.

UseDefinition = False;
expr = g[a]

Then in the next cell the definition of g[a] is used as we expect.

UseDefinition = True;
g[a]

Earlier the expression g[a] was assigned to expr, and one might expect that the following would evaluate to 2 since g[a]=2, but that isn't the case.

expr

Allan Hayes indicated this seems to stem from internal optimization procedures and he reminds us that section 2.5.12  of The Mathematica Book says the following. Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see Section 2.3.5). One particularly awkward case is when the condition involves a global variable. Mathematica may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell Mathematica to update all expressions involving s.

Below Allan Hayes shows how we can define  expr  to ensure we get the expected result. 
Here  Update  and  (lhs := rhs)  are used instead of  (lhs = rhs).

Clear[expr];
UseDefinition = False;
expr := (Update[g]; g[a])

expr

In the last cell g[a] is returned when we had (UseDefinition = False).  In the next cell  we get  (expr⟶g[a]⟶2)  when (UseDefinition = True).

UseDefinition = True;
expr

Allan Hayes also gave the next solution which is slightly different from the one above, but gives the same result.

Clear[expr];
UseDefinition = False;
expr /; (Update[g]; True) = g[a];

expr

UseDefinition = True;
expr

Count

First I define (lst) which will be used to demonstrate Count.

lst = 
      Table[Random[Integer, {0, 9}],    {3}, {2}, {2}, {2}]

The following returns 3 because lst has three elements and each has the form (_List).

Count[lst, _List]

The three lists found with the example from the previous cell are returned by evaluating the next input.

Part[lst, 1]
Part[lst, 2]
Part[lst, 3]

The next input counts six lists at level 2.

Count[lst, _List, {2}]

The six lists found with the example from the previous cell are returned by evaluating the next input.

Part[lst, 1, 1]
Part[lst, 1, 2]
Part[lst, 2, 1]
Part[lst, 2, 2]
Part[lst, 3, 1]
Part[lst, 3, 2]

The next input counts ten lists at levels zero through 2.

Count[lst, _List, {0, 2}]

The ten lists found with the example from the previous cell are returned by evaluating the next input.

lst
Part[lst, 1]
Part[lst, 2]
Part[lst, 3]
Part[lst, 1, 1]
Part[lst, 1, 2]
Part[lst, 2, 1]
Part[lst, 2, 2]
Part[lst, 3, 1]
Part[lst, 3, 2]

I don't provide further examples, but any combination of pattern matching constructs can be used with Count.  Nuances of pattern matching are discussed in another section.

Heads Option

Count has a Heads option with the default setting (Heads->False).  First I define (expr) which will be used to demonstrate this option.  In the first example Count ignores the (h) in h[x,y] because of the default setting for Heads.

ClearAll[h];
expr = {h[x, y], {h, 0}, {h, 3}, {3, 
            4}};
Count[expr, h, {2}]

When the last example is repeated using (Heads->True) the (h) in h[x,y] is accounted for.

expr = {h[x, y], {h, 0}, {h, 3}, {3, 
            4}};
Count[expr, h, {2}, Heads -> True]

DeleteCases

First I define (lst) which will be used to demonstrate DeleteCases.

expr = {1, 1.2, {2, 1.3, {3, 1.4}, {4, {1.5}}}};

The first example deletes all real numbers at level 1 and the only one at that level was 1.2.  Recall 1, 2, 3, ... have the head Integer not Real.

DeleteCases[expr, _Real]

The next example deletes all Real numbers at levels 1 through 3.  The integers deleted were 1.2, 1.3, 1.4.  More discussion on level specification is given in another section.

DeleteCases[expr, _Real, {1, 3}]

The next example deletes Real numbers at level 3.  The only Real number at level 3 was 1.4.

DeleteCases[expr, _Real, {3}]

The next example deletes all Real numbers at any level.

DeleteCases[expr, _Real, {0, -1}]

In the next example I give an alternate way to delete Real numbers at any level.

expr /. _Real -> Sequence[]

In the next example I give an alternate way to delete Real numbers at levels 1 through 3.

Replace[expr, _Real -> Sequence[], {1, 3}]

I don't provide further examples, but any combination of pattern matching constructs can be used with Count.  Nuances of pattern matching are discussed in another section.

Heads Option

DeleteCases has a Heads option with the default setting (Heads->False).  First I define (expr) which will be used to demonstrate this option.

ClearAll[h, x, y];
expr = {h[x, y], {6, h, 7}};

In the next cell DeleteCases ignores the (h) in h[x,y] because of the default setting for Heads.

DeleteCases[expr, h, {0, -1}, Heads -> False]

When the previous example is repeated with the option (Heads->True) the (h) in h[x,y] is deleted.

DeleteCases[expr, h, {0, -1}, Heads -> True]

Below I give alternate methods of getting the same results as the last two examples.

Replace[expr, h :> Sequence[], {0, -1}]

expr /. h[args__] :> args /. 
      h -> Sequence[]

Default

Default can be used with one two or three arguments, and each form is discussed below.

Default[f]

In the next cell (f) has the default value 0.  Depending on the pattern used f[e] can be treated as f[e,0] or  f[0,e].  If the second argument in the pattern is optional f[e] is treated as f[e,0].  If the first argument in the pattern is optional f[e] is treated as f[0,e].  All throughout this discussion on Default details the functions that are given default values will have no attributes. However, the values assigned to Default[f] have a different effect when (f) has the OneIdentity attribute.  This is covered in the discussion of OneIdentity.

ClearAll[f, g, e];
Default[f] = 0;
f[e] /. f[a_, b_. ] :> g[a, b]

f[e] /. f[a_. , b_] :> g[a, b]

Default[f,i]

In the cell below values are given for Default[f,2],  Default[f,3] and definitions for evaluating (f) are given.

ClearAll[f];
Default[f, 2] = 4;
Default[f, 3] = 1/8;
f[a_Integer, b_. , c_] := {a, b, c}
f[a_Rational, b_Rational, c_. ] := {a, b, c}

The definitions for (f) above are used in the input cells below.  For the cells below the default values are not needed because the definitions for (f) could be used directly.

f[10, 11, 12]

f[1/2, 1/2, 1/2]

Now for the next input (f) has only two arguments, and the definition for integer arguments is used.  However, the definition can only be used if a default value is inserted for the optional argument.  The applicable definition indicates that the second argument is optional.  The default value for the second argument of (f) is 4 so the value 4 is used for the second argument.

f[10, 11]

Then another input is evaluated where (f) has three arguments,and the definition for rational arguments is used.Again the definition can only be used if a default value is inserted for the optional argument.The applicable definition indicates the third argument is optional.The default value for the third argument of (f) is 1/8 so the value 1/8 is used for the third argument.

f[1/2, 1/2]

In the next cell an attempt to make a rule for (f) with the first argument optional is not permitted.  That's because (f) only has default values for the second and third arguments.

f[a_. , b_Real, c_Real] := {a, b, c}

Default[f,i,n]

We can also use Default[f,i,n] to specify the default value to use when argument (i) out of (n) arguments is optional.  New default values and definitions for (f) are given below to demonstrate this.  The input below uses definitions for (f) with integer arguments and rational arguments.

ClearAll[f];
Default[f, 2, 3] = 8;
Default[f, 2, 4] = 1/16;
f[a_Integer, b_. , c_Integer] := {a, b, c}
f[a_Rational, b_. , c_Rational, d_Rational] := {a, b, c, d}

In each case below the use of a default values were not needed because the definitions for (f) could be used directly.

f[11, 12, 13]

f[1/2, 1/2, 1/2, 1/2]

Now in the next input (f) has two integer arguments.  The definition can only be used when a default value is inserted for the optional (second) argument.  Once a default value is inserted for the optional argument we will have three arguments.  The default value for the second of three arguments in (f) is 8 so a value of 8 is inserted as the second argument.

f[11, 12]

Then for another input (f) has three rational arguments.  Again the definition for rational arguments can only be used if a default value is inserted for the optional (second) argument.  Once a value is inserted for the second argument we will have four arguments.  The default value for the second of four arguments for (f) is 1/16 so a value of 1/16 is inserted as the second argument.

f[1/2, 1/2, 1/2]

In the next cell we can't give a rule where the second of two arguments is optional.  That is not permitted because we don't have a value assigned to Default[f, 2]  or  Default[f, 2, 2].

f[a_, b_. ] := {a, b}

Distribute

Use of Distribute is well explained in the documentation.  However, I provide a hyperlink to an elegant program that uses Distribute.

Dot

Mathematica computes the dot product of two vectors exactly as we do in Linear Algebra. In the next cell we get the Dot product of two 3-D vectors.

ClearAll["Global`*"];
{a, b, c} . {x, y, z}

In the next cell we try to compute the Dot product of things that aren't litsts, and we see it doesn't work.

Dot[h[a, b, c], h[x, y, z]]

One of the advantages of Inner is that it can do what we tried to do in the previous cell as we see with the next cell.

Inner[Times, h[a, b, c], h[x, y, z], Plus]

Below we see that matrix multiplication is also implemented using Dot.

The Dot product of a vector and a matrix

We can compute the Dot product of a vector and a matrix.  In the next cell we get the Dot product of (vector A) with each column of (matrix B).

A = {a1, b1, c1};
B = {{x1, x2}, {y1, y2}, {z1, z2}};
A . B
A . B === Map[A . # &, Transpose[B]]

However, in the next example we get the Dot product of each row of (matrix A) with (vector B).

A = {{x1, x2, x3}, {y1, y2, y3}};
B = {a1, b1, c1};
A . B
A . B === Map[# . B &, A]

The Dot product of two matrices

When Mathematica computes the Dot product of matrices you essentially get the product of the matrices as defined in Linear Algebra. Hence if A is a (m × n) matrix and B is a (n × j) matrix then Dot[A, B] is a (m ×  j) matrix. A generic example is given in the next cell.

A = {{a1, a2}, {b1, b2}};
B = {{x1, x2, x3}, {y1, y2, y3}};
A . B

The Dot product of two tensors

Mathematica can compute the Dot product of tensors with compatible dimensions. In the next cell the Dot product of tensors A and B is computed.

A = {{{a1, a2}, {b1, b2}}, {{c1, c2}, {d1, d2}}};
B = {  {{w1, w2, w3}, {x1, x2, x3}}, {{y1, y2, y3}, {z1, z2, 
              z3}}  };
A . B

The next cell helps explain how parts of the dot product above are computed.

A . B === {A[[1]] . B, A[[2]] . B}

In the cell above A[[1]] is a matrix, but B is a tensor. The next cell shows how parts of (A[[1]]  .  B) are computed. The parts of (A[[2]]  .  B) are computed in a similar manner. All the Dot products below involve vectors and matrices and are computed as explained above.

A[[1]] . 
        B === {
{A[[1, 1]] . First[Transpose[B]], 
          A[[1, 1]] . 
            Last[Transpose[B]]}, 
{A[[1, 2]] . 
            First[Transpose[B]], 
          A[[1, 2]] . Last[Transpose[B]]}
}

The Dot product of three or more arguments

Mathematica can also compute Dot[A1, A2, A3,  ... ]  provided  (A1, A2, A3, ...) have suitable dimensions.
Dot[A1, A2, A3 ]  can be considered a concise way or writing Dot[A1, Dot[A2, A3] ] or  
Dot[Dot[A1, A2], A3]].  An example of the Dot product of three arguments is given in the next cell.

A1 = {a, b, c};
B1 = {{x1, x1}, {y1, y2}, {z3, z3}};
C1 = {2, 3};
Dot[A1, B1, C1]

Evaluate

Evaluate is used to ensure that one or more argument of a function evaluates even if the argument(s) would have not evaluated due to one of the attributes HoldAll, HoldRest, HoldFirst.  As an example consider the definitions in the next cell.

ClearAll["Global`*"];
Attributes[f] = {HoldRest};
f[x__] := Hold[x]
{s, t, u, v, w} = {12, 15, 23, vv, 37};
vv = 28;

Because  f  has the HoldRest attribute only the first argument of  f  evaluates before the definition of  f  is used.

f[s, t, u, v, w]

In the next cell Evaluate is used to ensure the (4^th) argument evaluates when it would have been held.  Notice (v) evaluated to (vv), and then (vv)  evaluated to 28.  It would be very difficult to make (v) evaluate to (vv), but prevent evaluation of (vv) to 28.

f[s, t, u, Evaluate[v], w]

We can give Evaluate multiple arguments and they evaluate to a sequence as in the next cell.

f[s, t, Evaluate[u, v], w]

You might expect the following to evaluate the (v) in {u,v}, but it doesn't work. That is because in order for Evaluate to have an effect, the expression of which it is an argument must be evaluated.  In this example Evaluate[v]  is an argument of {u,Evaluate[v]}, and this list doesn't evaluate because (f) has the HoldRest attribute.

f[s, t, {u, Evaluate[v]}, w]

In order to evaluate something deep inside a held expression you have to do something like the following.

expr = f[s, t, {u, v}, w];
ReplacePart[expr, Part[expr, 3, 2], {3, 2}]

Common applications for Evaluate

The first argument of  Plot[f, {x,xmin,xmax}]  must evaluate to a machine size real number when (x) is substituted fro a value between (xmin) and (xmax).  Plot also allows the form  Plot[{f1,f2,f3}, {x,xmin,xmax}] but this only works when the first argument of Plot has the head List before the Plot algorithm is called.  Sometimes we need to Evaluate the first argument of Plot to give it what it needs.  Several examples of this are given below.

In the first three examples Evaluate is needed to ensure the first argument has the head List.

ClearAll["Global`*"];
f2 = {(x^2), (x^3)};
Plot[Evaluate[f2], {x, 0, 1.3}];

Plot[
        Evaluate[{x^2 - c1, x^2 + c1} /. c1 -> 2], {x, -2, 
          2}];

Plot[
        Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}];

If Evaluate isn't used in the next example the Plot algorithm tries to take the derivative with respect to numeric values such as 
D[Sin[((π 2.53)/20)] (400/π),  2.53] but this is nonsense.  The solution is to use Evaluate to ensure the derivative is computed before the Plot algorithm is called.

Plot[
        Evaluate[D[Sin[((π x)/20)] 400/π, x]], {x, 0, 
          50}];

ExactNumberQ, InexactNumberQ, NumberQ, NumericQ

ExactNumberQ is undocumented, but as far as I can tell it returns True if it's argument has the head Integer, Rational, or if the argument is complex with infinite precision.  It seems ExactNumberQ returns False in every other case as demonstrated with the next input.  You may not be familiar with the feature Root that I use below, but it's well documented in the Help Browser and The Mathematica Book.  I suggest you refer to this documentation if you are confused with the use of Root below.

Clear[x];
ExactNumberQ /@ {3, 5/3, 
        2 + 3  I}
ExactNumberQ /@ {π, 3.2, 2.3 + 1.25  I, 
        2 + 1.25  I, 2.3 + I, N[π, 50], 
        N[π (1 + I), 50], (Sqrt[3]), π/4, 
        Root[-7 - #1 + #(1^3) &, 1], ∞, x}

InexactNumberQ /@ {3.2, 2.3 + 1.25  I, 
        2.3 + 5  I, 2 + 1.25  I, N[π, 50], 
        N[π (1 + I), 50]}
InexactNumberQ /@ {3, 5/3, 
        2 + 3  I, ∞, (Sqrt[3]), π/4, 
        Root[-7 - #1 + #(1^3) &, 1], ∞, x}

?NumberQ

It seems NumberQ  returns True whenever it's argument has the head Integer, Rational, Real, or Complex and False in every other case.

NumberQ /@ {3.2, 2.3 + 1.25  I, 3, 5/3, 
        2/3 + 3  I, (3^300), (((3/5))^300), 
        (3^300) + (5^200) I, N[π, 50], 
        N[π (1 + I), 50]}
NumberQ /@ {∞, (Sqrt[3]), π/4, Root[-7 - #1 + #(1^3) &, 1], 
        x}

?NumericQ

NumericQ /@ {3.2, 2.3 + 1.25  I, 3, 5/3, 
        2 + 3  I, (Sqrt[3]), π/4, Root[-7 - #1 + #(1^3) &, 1], 
        N[π, 50], 
        N[π (1 + I), 50]}
NumericQ /@ {∞, 1/0, 0. /0, ∞, x}

Flat

If a function has the Flat attribute nested layers are automatically flattened during evaluation.  This aspect of how Flat works is understood by many Mathematica users, and is demonstrated in the cell below.

ClearAll[f]
Attributes[f] = {Flat};
f[f[f[f[1, 2, 3]]]]

I was only able to understanding many of the subtle ways that Flat works after getting Technical Support from Dave Withoff.  One subtle point many users don't understand is the effect the Flat attribute has on pattern matching.  In the cell below (f) still has the Flat attribute and no other attributes.  In the example below the pattern matcher tries to find a way to make the expression match the pattern.  The pattern matcher finds that the pattern matches when the f[1,2,3,4] is treated as f[f[1],f[2,3,4]] and the two are considered equivalent to when (f) has the Flat attribute.  When the definition is applied f[f[1],f[2,3,4]] evaluates to {f[1],f[2,3,4]}.  Then f[2,3,4] is treated as f[f[2],f[3,4]] which evaluates to {f[2],f[3,4]}.  Then f[3,4] evaluates to {f[3],f[4]}.  When this is all put together we get   (  {f[1]{f[2]{f[3],f[4]}}}  ).

f[x_, y_] := {x, y}
f[1, 2, 3, 4]

The result above would be different if (f) had the attributes Flat and OneIdentity.  For further discussion of this see the details of OneIdentity, Flat.

A warning about attributes and pattern matching

The attributes Flat, Orderless, and OneIdentity effect pattern matching.  If a user defined function needs either of these attributes, they should be set before any definitions are entered, and not changed after the definitions are made.  This way the given attributes effects both pattern matching and the evaluation process (usually the desired effect).  This point is demonstrated for the Flat attribute in the cells below.

ClearAll[f]
Attributes[f] = {Flat};
f[x_, y_] := {x, y}
f[1, 2, 
      3]

f[
      f[f[1]]]

If definition(s) are given before the attributes are set then the attribute effects evaluation, but not pattern matching.  This is demonstrated in the next few cells.

ClearAll[f]
f[x_, y_] := {x,  y}
Attributes[f] = {Flat};
f[1, 2, 
      3]

f[
      f[f[1]]]

When the next input cell is evaluated (f) is Flat for pattern matching, but not for evaluation.

ClearAll[f]
Attributes[f] = {Flat};
f[x_, y_] := {x, y}
Attributes[f] = {};
f[x_, y_] := {x, y};
f[1, 2, 
      3]

f[
      f[f[1]]]

It now seems like (f) has the same definition twice.  We can evaluate ( f[x_,y_]=. ) once to get rid of one definition, but we need to use ( f[x_,y_]=. ) again to get rid of the second definition.  Alternatively we could use Clear[f] and all definitions for (f) would be removed in one step.

?f

f[x_, y_] =.

?f

f[x_, y_] =.

Now (f) has no definitions.

?? f

{f[1, 2], f[f[1]]}

A problem with the Flat attribute and how to solve it

A Wolfram Research Technical Support web page points out that one should avoid definitions such as (f[p_]:=p) for functions that have the Flat attribute. In the cell below this was attempted,and the kernel would have gone into infinite iteration if it didn't bail out when the iteration limit was exceeded. The reason this leads to infinite iteration is that the pattern matcher tries to rewrite f[1,2] so (f) has only one argument so the expression will match the pattern (f[p_]). The pattern matcher is able to fit the expression to the pattern by treating f[1,2] as f[f[1,2]].The outer (f) is evaluated and results in f[1,2] which has to be evaluated.However,this is exactly where we started. Then the kernel does this over and over until the iteration limit is exceeded.

ClearAll[f]
Attributes[f] = {Flat};
f[p_] := p
f[1, 2]

Plus and Times have the Flat attribute. At first glance it seems Plus and Times have the definitions 
( Plus[p_]:=p; Times[p_]:=p ) but it isn't that simple. Something special had to be done because Plus[a,b] and Times[a,b] don't cause an $IterationLimit message. Allan Hayes indicated the following definition can be used to allow a user defined function to work like Plus and Times in this regard. I suspect this works because the definition for f[a__]  is only used after pattern matching is finished.

ClearAll[f]
SetAttributes[
      f, {Flat, OneIdentity}]
f[a__]  /; ( Length[{a}] === 1 ) :=  a
f[x_Real, y_] := {x, y}

In the next output cell we see nested layers of (f) are flattened.  In addition the result is f[1,2] which does not lead to infinite iteration.

f[f[f[f[1, 2]]]]

Next we see (f) is flat for pattern matching. In this case f[1.5, 2.5, 3.5] was treated as the equivalent expression f[1.5,f[2.5,3.5]] which evaluated to {1.5, {2.5, 3.5}}.

f[1.5, 2.5, 3.5]

Finally we verify that when (f) is evaluated with only one argument it returns the argument.

Clear[p];  f[p]

Flatten

The basic use of Flatten is demonstrated in the next cell.  Lots of users know about the basic use of Flatten, but there is much more to the story.

Flatten[{{1, {2, {3, 4}}, 5, {6}}}]

First of all Flatten will flatten nested layers of any head as demonsrated in the next cell.

Flatten[h[h[1, h[2, h[3, 4]], 5, h[6]]]]

When Flatten is given only one argument, it checks the head of the expression and flattens nested layers of that head.  Notice the nested lists in the next example aren't flattened.  Also in  this example we are flattening layers of (h), starting with the arguments of (expr) and going down to deeper levels.  When Flatten gets to a subexpression that doesn't have the head (h) it decides not to look inside this subexpression for nested layers of (h).  Hence the output has nested layers of (h) in {h[h[7],h[8]]}.

expr = 
        h[h[1, h[2, h[3, 4]], 5, 
            h[6]], {h[h[h[7]], h[8]]}, {9, {10}, {11, {12}, 
              13}}];
Flatten[expr]

In the next input we give Flatten (2) as a second argument and only two layers of (h) are flattened.  Here too the subexpression g[h[8,h[9,10]]] doesn't have the head (h), so Flatten doesn't flatten layers of (h) in this subexpression.

expr = 
        h[1, 2, h[3, 
            h[4, h[h[h[5]], 6], 
              g[h[8, h[9, 10]]]]]];
Flatten[expr, 2]

We can give Flatten a third argument to indicate that layers with a certain head should be flattened. This is needed in the next cell to ensure layers of (h) are flattened.  If Flatten was given one or two arguments it would try to flatten layers of (g).

expr = 
        g[1, h[2, 
            h[3, 4, h[5, h[6, h[7, h[8]]]]]]];
Flatten[expr, ∞, h]

In the next example Flatten is trying to flatten layers of (h).  Flatten finds that the subexpression g[h[9,h[10,11]]] doesn't have the head (h) so it doesn't flatten layers of (h) in that subexpression.

expr = 
        g[1, h[2, h[3, 4, h[5, h[6, h[7, h[8]]]]]], 
          g[h[9, h[10, 11]]]];
Flatten[expr, ∞, h]

If we want to flatten all nested layers of (h), the replacement rule in the next cell should be used.

expr = 
        g[1, h[2, h[3, 4, h[5, h[6, h[7, h[8]]]]]], 
          g[h[9, h[10, 11]]]];
expr //. h[e1___, h[e2___], e3___] :> h[e1, e2, e3]

Fold

The basic example of Fold is given below, but first I clear values from all variables.

ClearAll["Global`*"];
Fold[f,x,{a,b,c}]

The basic example above doesn't do much to help one understand how to make a useful program using Fold.  Some more illuminating examples are given below.  If you don't understand the #& notation see the discussion of Function. The three examples below aren't very useful, but they demonstrate the form that is normally needed when using Fold.

From these examples we get:
((((a+b)+c)+d)+e)

((((a b)c)d)e)

StringJoin[StringJoin[StringJoin[StringJoin[StringJoin[
  "","a"],"b"],"c"],"d"],"e"]

Fold[(#1 + #2) &, 0, {a, b, c, d, e}]

Fold[(#1* #2) &, 1, {a,  b,  c, d, e}]

Fold[
      StringJoin[#1, #2] &, "", {"a", "b", "c", "d", "e\
"}]

A more useful application of Fold is given in the next cell.  Here we have a function that expresses a polynomial using Horner's rule.

HornerForm[poly_?PolynomialQ, 
        x_Symbol] := 
Fold[(x #1 + #2) &, 0, 
        Reverse[CoefficientList[poly, x]]]

    
HornerForm[2 + 3  x + (x^2) - 4  (x^3) + 2  (x^4), x]

In the Cell below Fold is used to make an Alternating Series.  If ( lst ) was used instead of (Reverse[lst] ),  the first term would be multiplied by (-1) when the Length of ( lst ) is even.  Jerry Keiper posted this solution to the MathGroup.

alternate[lst_List] := 
      Fold[(#2 - #1) &, 0, Reverse[lst]]

    
Clear[a1, a2, a3, a4, a5, a6, a7, a8];
alternate[{a1, a2, a3, a4, a5, a6, a7, a8}]

Another application of Fold is shown below (copied from the Help Browser).  This function (EvaluateAt) takes an expression and a list of positions, and evaluates in place only the parts at the specified positions.

EvaluateAt[expr_, positions_] := 
      Fold[ReplacePart[#1, Part[#1, Sequence @@ #2], #2] &, expr, 
        positions]

To demonstrate this function consider the expression below which is wrapped in HoldForm to prevent evaluation.

expr = 
        HoldForm[{{Cos[π], (Sqrt[8]), 
              Cos[π], (((x y z))^2)}, 
{(Sqrt[12]), 
              Sin[ArcCos[x]], 2, 0}}];

First we use ReleaseHold to see what the expression evaluates to without HoldForm.

ReleaseHold[expr]

In the next cell EvaluateAt (defined above) is used to evaluate only the parts of (expr) that contain trigonometric functions.

EvaluateAt[expr, {{1, 1, 1}, {1, 1, 3}, {1, 2, 2}}]

If you have trouble figuring out the position of a portion of a sub-expression you are interested in you might want to down load a package Called (ExpressionManipulation.m) from http://home.earthlink.net/~djmp/Mathematica.html .

An alternate method of getting the same result in this example is given in the discussion of Block.

For another elegant application of Fold see the program Alan Hayes wrote which quickly finds the integers Relatively Prime to an integer n using Fold.

FoldList

The usage message of FoldList is shown below, and of course it's closely related to Fold. For a long time I wondered where something like this would have useful applications.

?FoldList

Jürgen Tischer used FoldList to solve a common problem in the MathGroup. The solution is given below which takes a list of angles and adds to, or subtracts from, each element a multiple of 360°  to minimize the jump from the previous element. This trick forms the heart of the SmoothPhase package that I have posted on MathSource.

lst = {5, 35, 65, 95, 125, 
          155, -175, -145, -115, -85, -55, -25};
FoldList[(Round[(#1 - #2)/360] 360 + #2) &, 
        First[lst], Rest[lst]]

Another elegant application of FoldList is given in the section on Clever little programs.

Format

The lines below ensure a polynomial is written in the opposite order from the usual Format.

\(\(\(Unprotect[Plus];\)\(
\)
    \)\)
\(\(\(Format[HoldPattern[Plus[p__]]] := 
        Module[{s1, s2}, 
s1 = Hold[p]; 
s2 = Reverse[s1]; 

          ReplacePart[HoldForm[Evaluate[s2]], Plus, {1, 0}] /; 

            OrderedQ[s1] && \((s1 =!= s2)\)];\)\(
\)
    \)\)
Clear[x];  
Expand[(((2 + x))^4)]

Before moving on the normal convention is restored.

Format[HoldPattern[Plus[p__]]] =.    ;
Protect[Plus];

Another application of Format is given below. A user once wrote to the MathGroup asking how one could ensure rational numbers greater than one are displayed as improper fractions.  P.J. Hinton of Wolfram Research gave the solution below to change the way rational numbers are formatted.

Unprotect[Rational];
Format[
        Rational[num_Integer, 
          den_Integer]] := 
DisplayForm[
RowBox[{ToString[
              Quotient[num, den]], 
FractionBox[
              ToString[Mod[num, den]], 
              ToString[
                den]]
}
]
]
\(\(\(Protect[Rational];\)\(
\)
    \)\)
14/3  - 1

Before continuing we the next cell should be evaluated to remove the above formatting rule.

Unprotect[Rational];
FormatValues[Rational] = {};
Protect[Rational];

FreeQ

FreeQ is like MemberQ in that is also takes a level specification.  By default FreeQ looks at all levels, so in the next example FreeQ returns False because it see the 2 in (x^2).

FreeQ[y + x + (x^2), _Integer]

Next I tell FreeQ to only look at level 1 and it says there are no Integers.

FreeQ[y + x + (x^2), _Integer, 1]

Next FreeQ returns False because the whole expression matches the pattern (_List).

FreeQ[{1, 0}, _List]

I don't provide further examples, but any combination of pattern matching constructs can be used with FreeQ.  Nuances of pattern matching are discussed in another section.

Heads Option

FreeQ has a Heads option like several other functions, and it has the default setting (Heads->True).  So in the next example FreeQ checks the heads of subexpressions and determines that there is one with the head Plus.

FreeQ[{{x, y}, {1 + x + (x^2), y}}, Plus]

In the next example the setting (Heads->False) is used and FreeQ doesn't check the heads of subexpressions.

FreeQ[{{x, y}, {1 + x + (x^2), y}}, Plus, 
      Heads -> False]

Function

Why use pure functions?

• If a function is used many times over it can run 15 times faster if it's implemented as a pure function.
• Programming with pure functions is often more concise.  
   For example the following line uses a pure function in Select to return all elements of data with a magnitude less than 50.

data = {-4, 34, -98, -23, 68, 
          31};
Select[data,  Abs[#] < 50 &]

The following line also returns all elements of data with a magnitude less than 50.  This version doesn't use pure functions. Notice the version using a pure function is more concise but seems cryptic until you get used to pure functions.

Small[x_] := Abs[x] < 50;
Select[data, Small]

Before looking at examples below it might help to clear definitions from all global symbols with the next cell.

ClearAll["Global`*"]

Pure functions that use only one argument

The next cell defines a function (f) which returns 3 plus it's argument.

f[x_] := 3 + x

In the next cell I give four different ways to define a pure function (g) which returns 3 plus it's first argument. The expression used to define the function varies depending which version is used, but as far as I can tell there is no meaningful difference between the different versions.
The Symbol (#) is short hand for Slot[1] and it refers to the first argument given to a pure function.

g = 3 + # &;
g = Function[3 + #];
g = Function[x, 3 + x];
g = Function[{x}, 3 + x];

In the next cell we see f[t] returns the same thing as g[t].

{f[t], g[t]}

Functions (f) and (g) don't always do the same thing.  Function (f) is only defined when given one argument, but the pure function (g) is defined when given one or more arguments.  The pure function (g) uses the first argument and ignores all other arguments.

{f[t1, t2], g[t1, t2]}

Pure functions that use an  (n^th) argument

ClearAll[f, g]

In the next cell I define a function (f) that takes three arguments.

f[x_, y_, z_] := {x - y, x - z};
f[a, b, c]

In the next cell I give three ways to define a pure function that does essentially the same thing as (f) in the previous cell.  The FullForm of (#n)  is Slot[n], and we use (#2, and  #3) refer to the second and third arguments of a pure function. The third way of defining the pure function results in differnt form of storing the functions definition that the first two solutions, but as far as I can tell there is no meaningful difference between the different versions.

g = {#1 - #2, #1 - #3} &;
g = Function[{#1 - #2, #1 - #3}];
g = Function[{x, y, z}, {x - y, x - z}];

g[a, b, c]

Notice the previous definition for (g) will complain if it's given an insufficient number of arguments.  Because of this you shouldn't use pure functions to define a function an end user will use.  However, pure functions are a good choice for private functions in a package since the end user will not be using the private functions.

g[x, y]

Notice it's perfectly legal for a pure function to ignore some arguments before the (n^th) argument.  To demonstrate this point I define (g) below which returns the sum of it's (2^nd) and (4^th)arguments. In this case all other arguments are ignored.

ClearAll[g, x2, x4];
g = #2 + #4 &;
g[x1, x2, x3, x4, x5, x6]

Pure functions that use an arbitrary number of arguments

ClearAll[f, g]

The following cell defines a function (f) that puts all it's arguments in a list and squares each element.  The squaring operation is mapped to each element because Power has the Listable attribute, but that's another subject.

f[x__] := {x}^2
f[x, y, 5, (3/(Sqrt[2]))]

In the next cell I give two ways to define a pure function that puts all its elements in a list and square each element.  The FullForm of ## is SlotSequence[1] and is used to refer to all the arguments passed to a pure function.  The two styles used here result in exactly the same definition for the function.

g = {##}^2 &;
g = Function[{##}^2];

g[x, y, 5, (3/(Sqrt[2]))]

Use of ##n

ClearAll[f, g]

The next cell defines a pure function (g) that puts all it's arguments starting with the third argument in a list and squares each element. The FullForm of  (##3)  is SlotSequence[3] and it refers to all arguments of a pure function from the third argument on.  The two styles used here result in exactly the same definition for the function.

g = {##3}^2 &;
g = Function[{##3}^2];

g[v, x, y, 5, (3/(Sqrt[2]))]

Earlier I demonstrated that Mathematica will complain if a pure function uses #3 and is given less than three arguments.  So it's curious that Mathematica doesn't complain below when (g) tries to use ##3, but is given only two arguments.

g[x, y]

Pure functions with one attribute

ClearAll[f, g]

In the next cell I define a function (f) that returns True if it's argument is a Symbol before evaluating and otherwise returns False.  Programming this function is slightly tricky because evaluation is prevented in two places.  First we give (f) the HoldAll attribute to prevent it's argument from evaluating before using the definition of (f).  In addition we use Unevaluated[x] to prevent the argument (x) from evaluating before getting it's head.

Attributes[f] = {HoldAll};
f[x_] := (Head[Unevaluated[x]] === Symbol)

In the next cell we see f[t] returns True even when (t) evaluates to a number rather than a symbol.

t = 1.4;
{f[t], f[1.4]}

In the next cell I give four ways to define a pure function (f) that does essentially the same thing as (f) defined above.

g = 
        Function[  x,   Head[Unevaluated[x]] === Symbol, 
          HoldAll];
g = 
        Function[{x}, Head[Unevaluated[x]] === Symbol, 
          HoldAll];
g = 
        Function[  x,   Head[Unevaluated[x]] === 
            Symbol, {HoldAll}];
g = 
        Function[{x}, 
          Head[Unevaluated[x]] === Symbol, {HoldAll}];

In the next cell we see (g) does the same thing as (f).  I have found useful applications for this pure function.

t = 1.4;
{g[t], g[1.4]}

Pure functions with multiple attributes

ClearAll[f, g]

The next cell defines a function (f) which prevents it's arguments from evaluating, puts the arguments in canonical order, converts the arguments to strings and concatenates the sorted strings with another string.

Attributes[f] = {HoldAll, Orderless};
f[x_, 
          y_] := (ToString@Unevaluated@x <> "..." <> 
            ToString@Unevaluated@y);

In the next cell we see (f) in action.  You will find f[t,s] returns the same thing as f[s,t].

s = 6; t = 9;
f[t, s]

The next cell defines a pure function (g) that does essentially the same thing as (f) above.

g = 
        Function[{x, y}, 
          ToString@Unevaluated@x <> "..." <> 
            ToString@Unevaluated@y, {HoldAll, Orderless}];

In the next cell we see (g) in action.

s = 6; t = 9;
g[t, s]

Pure functions that use no arguments

ClearAll[f1, f2, x];

(h&) is a pure function with no arguments which always returns (h). An occasion where this is needed is in the PolygonTessellation package available at http://library.wolfram.com/packages/. This package includes a function called TessellatePolygon3D with a WindingRule option. The WindingRule setting should be a function that returns True or False. A typical setting of this option is WindingRule->OddQ, but in some cases you want a test that always comes out True. In that case the setting WindingRule->(True &) should be used.

Often times the ButtonFunction for buttons is a pure function with no arguments.  I give such an example in my section on ButtonProgramming.

Consider f1, f2 in the next cell. Here the function f1 returns True no matter what argument(s) it has, and f2 returns a random number between 0 and 1 no matter what argument(s) it has.

f1 = True &;
f2 = Random[] &;
{f1[x], f1[24.5], f1[4], f1[1, 2, 3], f2[x], f2[10^7], 
      f2[1, 2, 3]}

Keep in mind (f1) isn't True it's a function that always returns True and that explains why the first element of the next list is False.

{f1 === True, f1[] === True, f1[x] === True}

Use of  #0  (rarely useful)

ClearAll[f];

When used in a pure function (#0) represents the pure function itself.  I can't imagine when this would be useful, but I demonstrate it in with the pure function (f) in the next cell.  This pure function prints (The second argument is ...) and returns the entire pure function as an expression.

f = ((Print["The second argument = " <> 
                  ToString[#2]]; #0) &);

In the next cell we see (f) in action.

f[wx1, wx2, wx3, wx4]

Also read  "Further Examples" at the end of the  Function  documentation in the Help Browser.

HoldAll, HoldFirst, HoldRest

If a function (f) has the HoldAll attribute (f) does it's work before it's arguments evaluate.
It's easy to see why Clear must have the HoldAll attribute.  Consider the cell where values are assigned to (x,y,z).

x = 2;
y = 3;
z = 6;

If Clear didn't have the HoldAll attribute the following would evaluate to Clear[2,3,6] which is nonsense.

Clear[x, y, z]

The example in the next cell is from Power Programming With Mathematica by David Wagner.

ClearAll[f, g]
SetAttributes[f, HoldAll];
f[x_] := g[x]
Trace[f[2 + 2]]

In the example above the unevaluated sum (2+2) is substituted for (x) on the right side of the definition  f[x_]:=g[x].  The sum is only evaluated when it's time for (g) to evaluate it's arguments.  If (g) also held it's arguments the sum wouldn't evaluate at all.

The FullForm of (x=2) is Set[x,2].  Set has the HoldFirst attribute and it's important that is does.  If Set didn't hold it's first argument the second assignment in the next cell would evaluate to (2=5) which is nonsense.

x = 2;
x = 5

Also it's easy to see why the symbol If has the attribute HoldRest.  Whe given an expression such as  
If[ Test, T, F]  we want to only evaluate 'T' when the Test is True, and we only want to evaluate 'F' when the Test is False.  If the Test is neither True or False we want neither 'T' or 'F' to evaluate.

Built-in Symbols with attributes HoldAll, HoldFirst, HoldRest

The next cell makes a list of all built-in symbols.

symbs = 
        Cases[ToExpression /@ Names["System`*"], _Symbol];

The next cell gives a list of all built-in symbols with the HoldAll attribute.

Select[symbs, MemberQ[Attributes[#], HoldAll] &]

The next cell gives a list of all built-in symbols with the HoldFirst attribute.

Select[symbs, MemberQ[Attributes[#], HoldFirst] &]

The next cell gives a list of all built-in symbols with the HoldRest attribute.

Select[symbs, MemberQ[Attributes[#], HoldRest] &]

HoldAllComplete

Consider the following expression wrapped in Hold.  Hold has the HoldAll attribute, but we can still force evaluation of arguments and sequences are automatically spliced together.  However, nested layers of Plus aren't flattened, and the arguments of Plus aren't sorted.

f[x_] := x^2;
Hold[Evaluate[2 + 3^2], Sequence[v, w], Sequence[x, y], 
      Plus[d, s, Plus[z, a, Plus[u, u]]], f[5]]

In the next cell we use HoldAllComplete.  In this case Evaluate is ignored and sequences are not spliced together.

HoldComplete[
Evaluate[2 + 3^2], Sequence[v, w], 
      Sequence[x, y], Plus[d, s, Plus[z, a, Plus[u, u]]], 
      f[5]
]

Built-in Symbols with the HoldAllComplete attribute

The next cell makes a list of all built-in symbols.

symbs = 
        Cases[ToExpression /@ Names["System`*"], _Symbol];

Very few buit-in symbols have the HoldAllComplete attribute and they are all listed below.

Select[symbs, MemberQ[Attributes[#], HoldAllComplete] &]

HoldForm

The next line gives a sum of integers squared.

Sum[HoldForm[#^2] &[i], {i, 1, 9}]

We can use ReleaseHold to complete the calculation.

ReleaseHold[%]

HoldPattern

You might be very surprised that the following evaluates to True!

MatchQ[And[x, y, z], Times[p__]]

When the cell above is evaluated  Times[p__] evaluates to (p__) before the kernel checks to see if the pattern matches.  MatchQ then determines if And[x,y,z] matches the pattern (p__) and it does because And[x,y,z] is itself a sequence of one.

Now the next cell also evaluates to True because both ( And[p__] ) and ( Times[p__] ) evaluate to ( p__ ).

Times[p__] === And[p__]

We can prevent patterns in the examples above from evaluating by wrapping them with HoldPattern as in the following lines.

MatchQ[And[x, y, z], HoldPattern[Times[p__]]]

HoldPattern[Times[p__]] === HoldPattern[And[p__]]

With the next line one might expect to have the head (And) changed to (List), but it doesn't work.

Clear[x, y, z, prod, a];
And[x, y, z] /. And[x__] -> List[x]

In the next cell HoldPattern is used to ensure the head (And) is changed to (List).  The two examples that follow have the same effect, but the use of HoldPattern isn't needed.

And[x, y, z] /. HoldPattern[And[a__]] -> List[a]

And[x, y, z] /. And -> List

And[x, y, z] /. And[a_, b___] -> List[a, b]

HoldPattern wasn't needed in the last example because ( And[a_,b__] ) evaluates to itself.

Sometimes we need to use HoldPattern with care.  Suppose we wanted to replace a product of terms to the (1/3) power with (prod).  We can attempt to do this on (expr) below.

Clear[x, y, z, expr, prod];
expr = (((x*y*z))^(1/3)) + (((x + y + z))^(1/3));

In the next line HoldPattern isn't used, and Times[__]^(1/3) evaluates to \(TraditionalForm\`\((__\^\(1/3\))\)\).  Then both terms of expr match the evaluated pattern.  We then get (prod+prod)⟶(2 prod).

expr /. Times[__]^(1/3) -> prod

Now if the entire pattern is wrapped in HoldPattern, the exponent in the pattern is held in the form 
(Times[1,Power[3,-1]] )  instead of  ( Rational[1,3] ), and neither term matches the pattern.

expr /. HoldPattern[ ((( Times[__]))^(1/3)) ] -> prod

The desired result is given when the expression HoldPattern[Times[__]] is raised to the (1/3) power.

expr /. (((HoldPattern[Times[__]]))^(1/3)) -> prod

An easier way to get the desired result is to use  (_Times)^(1/3)  as a pattern.

expr /. _(Times^(1/3)) -> prod

Alan Hayes explained that HoldPattern[_] is also needed to ensure that the rule below is used.  Similar to the other examples HoldPattern is needed because Dt[x_] evaluates to something other than itself.

Clear[a, b];
(a^2) + 2  a + Dt[a] /. Dt[x_] :> 1

(a^2) + 2  a + Dt[a] /. HoldPattern[Dt[x_]] :> 1

Hyperlinks

How to make a hyperlink to a part of the Help Browser

Here is how you make hyperlinks to some portion of the Help Browser with Mathematica under MS-Windows.   The procedure should be similar on other platforms.

Step 1:
Find the part of the Help Browser that you want the hyperlink to jump to.  Don't use the MasterIndex radio button, because other radio buttons can access the same stuff, and I don't know how to make a link to the MasterIndex.  Once you have the right help page displayed make a note of the radio button selected, and the title of the Help Browser topic (to the right of the GoTo button).

Step 2:
Type the title of the Help Browser topic in a cell, and select the topic.  It doesn't matter what type of cell the hyperlink is in (Text, Section, Title, etc.), but one doesn't normally include a hyperlink in an Input cell.

Step 3:
Make the menu selection (Input ⊳ Create Button), then from the pull down menu select the radio button you want to link to.  The choices listed should include:
    MainBookLink
    AddOnsLink
    RefGuideLink
    Getting StartedLink
    OtherInformationLink
The selection (RefGuideLink) reefers to the (Built-in Functions) button in the Help Browser.  The other selections refer to a Help Browser radio button by the same name.  After making the appropriate menu selection you should have your hyperlink to the Help Browser.

As an example we make a hyperlink to section 3.2.4 of the Main Book portion of the Help Browser.

A hyperlink to 3.2.4 in the Help Browser

You simply type in the appropriate text, select the cell bracket, and make the menu selection Format  ⊳ Style ⊳ 
        Text.  Next select "3.2.4" and make the menu selection  Input  ⊳ 
        CreateButton  ⊳ MainBookLink.  The text "3.2.4" is now a hyperlink to section 3.2.4 of the main book.

How to make a hyperlink to a part of the Help Browser, and control the way it looks.

Suppose you want to make the hyperlink in the next cell.  In this case the hyperlink will jump to section 3.2.4 of the Main Book, but the text for the hyperlink isn't  "3.2.4" as when the procedure from above is used.

A hyperlink to Number theory functions in the Help Browser

Steps 1-3:
Make an hyperlink to section 3.2.4 of the Main Book using the procedure above.  That will give you the hyperlink and text in the next cell.

A hyperlink to 3.2.4 in the HelpBrowser

Step 4:
Put the insertion point (place where keyboard input would go) in the cell containing the hyperlink you are making and make the menu selection (Format ⊳ Show Expression) and you get the full expression of the cell as shown below.

Cell[TextData[{
  "A hyperlink to ",
  ButtonBox["3.2.4",
    ButtonStyle->"MainBookLink"],
  " in the Help Browser"
}], "Commentary"]

Step 5:
Edit the cell expression to look like the cell below.  Notice the cell is changed to include ButtonData->"3.2.4".

Cell[TextData[{
  "A hyperlink to ",
  ButtonBox["3.2.4",ButtonData->"3.2.4",
    ButtonStyle->"MainBookLink"],
  " in the HelpBrowser"
}], "Commentary"]

Step 6:
Make the menu selection (Format ⊳ Show Expression) and the cell looks the same as it did before.  Now that you use the ButtonData option of ButtonBox you can change what the hyperlink looks like without changing what it does!

Step 7:
Use the keyboard arrows (not the mouse) to move the insertion point into the text for the hyperlink, and change the text of the hyperlink from "3.2.4" to "Number theory functions".  You should then have the cell below.

A hyperlink to Number theory functions in the Help Browser

How to make a hyperlink to a specific cell in the Help Browser.

The entire contents of The Mathematica Book is included in the Help Browser, and some hyperlinks in the Help Browser take you to specific paragraphs (cells) in the book. How can you make your own hyperlinks that do this?  I will explain how you can make 
this button ⟶ Options in Show ⟵.

If you click on the above button you will see that it takes you to a specific paragraph in The Mathematica Book, and the top of the Help Browser will show that you are in section 1.9.4.  Now select the paragraph in the Help Browser that the button jumped to, and make the menu selection (Format ⊳ ShowExpression).  Scroll to the bottom of the expression for this cell, and you should see that the cell expression has option 
CellTags :>{"S1.9.4","4.15"}.  You then need to make a button with the options 
       ButtonData:>{"1.9.4","4.15"}, ButtonStyle->"MainBookLink".  
The setting of the ButtonData option is the same as the CellTags setting except the 'S' in the first string was removed. The same button is shown below, and you can Show the expression of this cell to see what it should look like.

Now to make the desired button type 'A button to Options in Show in the Help Browser' in a text cell.  Then select 'Options in Show', and make the menu selection (Input ⊳ Create Button ⊳ Custom). Select 'Main Book Link' for ButtonStyle, check the box for ButtonData and enter {"1.9.4","4.15"} in the box below the ButtonData check. Next select OK and you should have the cell below.

A button to Options in Show in the Help Browser.

How to make a hyperlink to a web page

This is how you make a hyperlink to a web page using Mathematica under Windows 95/98.  The procedure should be similar under other operating systems.

Step 1:
In a text cell type the text that will be a link to a web page.

Step 2:
Select the text that will be the hyperlink.

Step 3:
Make the menu selection (Input ⊳ Create Hyperlink).

Step 4:
A dialog box will come up.  Type the URL of the web page in the blank space next to "Other notebook or URL".  Click on the OK button and the selected text is a hyperlink to the URL you entered.

As an example I give a hyperlink to the Wolfram Research web page.

How to make a hyperlink that launches an email message

My computer uses Windows 98 and has Microsoft Outlook installed.  With my configuration I can follow the steps below to make a button which launches an email to message to (ersektr@navair.navy.mil).  I suspect the steps are similar with other confugurations.

Step 1:
In a text cell I type the text that I want to use for the hyperlink.

Step 2:
Select that portion of the above text cell which will be the hyperlink.

Step 3:
Make the menu selection (Input | Create Hyperlink).

Step 4:
In the field for "Other Notebook or URL" type
   mailto:ersektr@navair.navy.mil

Step 5:
Click on OK and the text that was selected is now a button that launches an email to 
(ersektr@navair.navy.mil)

You might also like to know that the evaluating the next input cell will also launch an email message to 
   ersektr@navair.navy.mil.

FrontEndExecute[{FrontEnd`NotebookLocate[{URL["mailto:\
ersektr@navair.navy.mil"], None}]}]

Still More

If you still don't see what you're looking try these links to Button Styles and Button Programming.

Inner

Inner is a generalization of Dot in that other functions can be used in place of Times and Plus. In the next two cells we see Inner[Times, list1, list2, Plus] is equivalent to  Dot[list1, list2] .  Before demonstrating Dot I clear the values from any symbols.

Clear["Global`*"];
Dot[{a, b, c}, {x, y, z}]

Inner[Times, {a, b, c}, {x, y, z}, Plus]

The next cell shows a generic application of Inner.

Inner[f, {a, b, c}, {x, y, z}, g]

In the next cell we see that when Inner is given only three arguments Plus is used in place of a fourth argument.

Inner[f, {a, b, c}, {x, y, z}]

In the next cell we see Inner can handle things other than lists. Inner only requires that the objects have the same Head and compatible dimensions.

Inner[f, h[a, b, c], h[x, y, z], g]

Inner product with Matrices and Tensors

In the section on Dot we saw that Dot can be used on lists, matrices and tensors. Inner does the same thing with Lists, matrices and tensors except other functions can be used in place of Times and Plus.

In the next cell (A) and (B) are matrices and Inner[Times, A, B, Plus] is the same as Dot[A, B].

A = {{a1, a2}, {b1, b2}};
B = {{x1, x2, x3}, {y1, y2, y3}};

    
Inner[Times, A, B, Plus] === Dot[A, B]

In the next cell we evaluate  Inner[f, A, B, g]  and we get somthing like matrix multiplication except (f) is used in place of multiplication, and (g) is used in place of addition.

Inner[f, A, B, g]

I don't give examples of using Inner on tensors, but I think you will find it's very much like the Dot product of tensors.

Giving Inner a (5^th) argument

In the next example Inner is given (1) as a fifth argument and, (at least in this example) Inner[Times,A,B,Plus,1] is equivalent to Dot[Transpose[A], B].

A = {{a1, a2}, {b1, b2}};
B = {{x1, x2, x3}, {y1, y2, y3}};

    
Inner[Times, A, B, Plus, 1] === Dot[Transpose[A], B]

The next example shows that (at least in this example) the default value of the fifth argument is 2.

Inner[f, A, B, g] === Inner[f, A, B, g, 2]

Insert

Consider the list of ordered pairs (lst) below.

Clear["Global`*"];
lst = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, 
            y5}};

Now suppose you want to insert {a1,b1},{a2,b2},{a3,b3}
between the third and fourth ordered pairs. The line below does the trick nicely.

Insert[lst, Unevaluated[Sequence @@ {{a1, b1}, {a2, b2}, {a3, b3}}], 
      4]

Here Unevaluated is needed since Insert doesn't have a Hold attribute, and we don't want Sequence@@{...} to evaluate until after Insert does it's job.  The use of (Sequence@@) simply strips the head List from the list of ordered pairs.

Notice you can use Insert[expr, elem, {{i1,j1, ...},{i2,j2, ...}, ...}]  to inset an expression at more than one position, but this is very slow if you are inserting at lots (>100) of positions.

Level

Lots of examples using Level can be found in the discussion of Level Specification. Below I explain some of the subtle details. 

In the next cell we get a list of all subexpressions at level 2 in expr.  Notice x1 and  x7  each appear twice in expr and twice in the result returned by Level.

ClearAll["Global`*"];
expr = {g[x1], {x1, x2}, {x3, x4}, g[x6], h[1][x7], 
          g[x7]};
Level[expr, {2}]

It seems Level gives a result such that the elements are sorted according to their position in the original expression.  This is demonstrated in the next cell where a sorted list of positions is used to get the same result we got above and the positions are in canonical order.  Notice each list of positions has length 2 and this is what we expect since we asked for all expressions at level 2.

posn = 
        Sort[{{1, 1}, {2, 1}, {2, 2}, {3, 1}, {3, 2}, {4, 1}, {5, 1}, {6, 
              1}}];
Extract[expr, posn]

Heads Option

By default Level uses the setting (Heads->False).  In the next cell we use (Heads->True) and get the heads at level 2.

Level[expr, {2}, Heads -> True]

Using the third argument in Level

Finally Level can be given a third argument that can be applied to the list returned.  To demonstrate this feature consider the next cell which gives a list of all subexpressions at level 3 and deeper.

demo = 
        x + Cos[((π (Sqrt[ 2 + x/3]))/3)] + 
          Sin[((π (Sqrt[ x]))/6)];
Level[demo, {3, ∞}]

In the next cell Level returns the sum of the elements we got from the previous cell.

Level[demo, {3, ∞}, Plus]

Level Specification

The concept of level specification is explained in The Mathematica Book (Sections 2.1.7, and A.3.6), and is explained further here.  The "level" of an expression is fundamental to an understanding of level specification.  Level 1 of an expression is all subexpressions that can be accessed by giving Part a single integer position (e.g.   expr[[2]]).  Level 2 of an expression is all subexpressions that can be accessed by giving Part two integers to specify the position (e.g.    expr[[2,3]]).  The parts of an expression at level (n) can be accessed by giving Part a sequence of (n) integers.  Level 0 of an expression is the expression itself.  As indicated by the usage message for Level (below) the second argument of Level is a level specification.  In the cells that follow Level[demo,_] will be used to explore the different variations of level specification.

?Level

Clear[x];
demo = 
        x + Cos[((π (Sqrt[ 2 + 1/x]))/3)] + 
          Sin[((π (Sqrt[ x]))/6)];

The functions (Apply, Cases, Count, DeleteCases, FreeQ, Level, Map, MapIndexed, MemberQ, Position, Replace, Scan) can  use level specification with the same conventions described below.  Several examples of using level specification are given in the discussion of Map in addition to those in this discussion of Level.  Each of these commands also have the option Heads.  The Heads option can be True or False and controls whether the command should look at the heads of subexpressions.

A positive integer for a level specification

When (levelspec) in Level[expr,levelspec] is an integer (n) a list of all subexpressions of (expr) at all levels from 1 through (n) is returned.  In the cell below Level is used to get a list of all subexpressions of demo at level 1.

Level[demo, 1]

In the next line Level[demo, 2] returns a list of all subexpressions at levels 1 through 2.

Level[demo, 2]

A list of one positive integer for a level specification

If the level specification given to Level is a list containing one positive integer, then only subexpressions at the level in the list are returned.  In the next cell Level gives a list of all subexpressions at level 2.

Level[demo, {2}]

A level specification deeper than the expression can be given.  When Level gets such a level specification it returns an empty list as in the following line.

Level[demo, {25}]

A list of two positive integers as a level specification

If Level is given a list of two integers {m,n} as a level specification, then we get a list of all subexpressions at level (m) at level (n) and all subexpressions at levels between (m) and (n).  The next line returns a list of all subexpressions at levels 2, 3, and 4.

Level[demo, {2, 4}]

The next line gives a list of all subexpressions at levels 0, 1, and 2.  The subexpression at level 0 is the expression itself.

Level[demo, {0, 2}]

Negative integers in level specifications

To understand negative levels you have to first understand the Mathematica meaning of depth.  The depth of an expression is said to be one greater than the maximum number of indices needed to access any part of an expression.

A list of one negative integer for a level specification

A level specification {-n} (where -n is a negative integer) is all subexpressions that have depth (n).  Atoms have depth 1, so the level specification {-1} in the next line causes Level to return a list of all the atoms in (demo2).

Note:  Rational numbers have a Numerator and Denominator.  However the numerator and denominator of rational numbers can't  be accessed using Part so they are regarded as atoms and have a depth of 1.  Complex numbers are also regarded as atoms although none were included in (demo).

{AtomQ[1/3], Depth[1/3]}

Part[1/3, 1]

Level[demo, {-1}]

In the next line the level specification is {-4}, and Level returns a list of all subexpressions of demo with depth equal to 4.

Level[demo, {-4}]

As with positive integer level specifications Level may return an empty list if the given expression has no subexpressions sufficiently deep.  The next line returns an empty list because neither demo or any of it's subexpressions have a depth of 25.

Level[demo, {-25}]

A negative integer as a level specification

If Level is given a negative integer as a level specification it returns all subexpressions from level 1 down to (and including) the negative level specified.  The next line returns a list of all subexpressions from level 1 down to  those at level -4.  Since the level specification goes down to -4 all sub expressions returned have a depth  of 4 or greater.

Level[demo, -4]

If Level is given (-1) as a level specification (as in the next line) we get a list of all subexpressions from level 1 down to the atoms.  This amounts to every subexpression, but not the whole expression itself.

Level[demo, -1]

In the next line we see level specifications -1 and ∞ are equivalent.

Level[demo, -1] === Level[demo, ∞]

A list of integers including one or two negative integers for a level specification

The next line gives a list of all subexpressions at any level (except level 0) that have a depth of 3 or greater.

Level[demo, {1, -3}]

Then the next line gives a list of all subexpressions at levels 2 or above with a depth of 4 or less.

tt = Level[demo, {-4, 2}]

In the next two cells level specification {0,-1}  is effectively the same as level specification {-∞,∞}.

Level[demo, {0, -1}] === 
      Level[demo, {-∞, ∞}]

In the next line Level is asked to return all subexpressions at levels 3 or deeper that have a depth of 6 or greater.  Expression demo2 has no subexpressions meeting this criteria, so an empty list is returned.

Level[demo, {3, -6}]

The next line gives a list of all subexpression with a depth of 1, 2, or 3.

Level[demo, {-3, -1}]

ListConvolve

The next cell demonstrates the basic use of ListConvolve.

Off[General::spell1];
Clear["Global`*"];

    
ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}]

Next we see that ListConvolve is equivalent to flattening the result of a certain matrix product.

(({{a4, a3, a2, a1, 0, 0}, {0, a4, a3, a2, a1, 0}, {0, 0, a4, a3, a2, a1}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}]

The next cell shows that ListConvolve is closely related to ListCorrelate.

listA = {a1, a2, a3, a4};
listB = {b1, b2, b3, b4, b5, b6};
ListConvolve[listA, listB] ===  
      ListCorrelate[Reverse[listA], listB]

Specifying the "overhang" using  {(Subscript[K, L]), (Subscript[K, R])}

In the next cell we provide ListConvolve {-1,-1} as a third argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {-1, -1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (-1) indicates last element of {a1, a2, a3, a4}, and (-2) would indicate the second from the last element of {a1, a2, a3, a4}.

(({{a4, a3, a2, a1, 0, 0}, {0, a4, a3, a2, a1, 0}, {0, 0, a4, a3, a2, a1}, {a1, 0, 0, a4, a3, a2}, {a2, a1, 0, 0, a4, a3}, {a3, a2, a1, 0, 0, a4}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {-1, -1}]

In the next cell we provide ListConvolve {1,1} as a third argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, 
        1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the first element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (2) would indicate the second element of {a1, a2, a3, a4}.

(({{a1, 0, 0, a4, a3, a2}, {a2, a1, 0, 0, a4, a3}, {a3, a2, a1, 0, 0, a4}, {a4, a3, a2, a1, 0, 0}, {0, a4, a3, a2, a1, 0}, {0, 0, a4, a3, a2, a1}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, 
          1}]

In the next cell we provide ListConvolve {1,-1} as a third argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {1, -1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.

(({{a1, 0, 0, a4, a3, a2}, {a2, a1, 0, 0, a4, a3}, {a3, a2, a1, 0, 0, a4}, {a4, a3, a2, a1, 0, 0}, {0, a4, a3, a2, a1, 0}, {0, 0, a4, a3, a2, a1}, {a1, 0, 0, a4, a3, a2}, {a2, a1, 0, 0, a4, a3}, {a3, a2, a1, 0, 0, a4}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}]

In the next cell we provide ListConvolve {-1,1} as a third argument. Notice this gives the same result as 
ListConvolve[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6}]  (ie. {-1,1} is the default for the third argument).

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
        1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the First element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.

(({{a4, a3, a2, a1, 0, 0}, {0, a4, a3, a2, a1, 0}, {0, 0, a4, a3, a2, a1}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
          1}]

Specifying the padding

In the next cell we provide (s) as a (4^th) argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, 
      s]

The next cell performs a series of Dot products that give the same result as in the previous cell.  Notice we gave ListConvolve {1,-1} as a third argument, and the left part of the first row starts with the first part of {a1,a2,a3,a4}.  Also the last row ends with the last element of {a1,a2,a3,a4}.

{
{a1, a2, a3, a4, 0, 0} . {b1, s , s , s , s , 
            s }, 
{a2, a1, a3, a4, 0, 0} . {b1, b2, s , 
            s , s , s }, 
{a3, a2, a1, a4, 0, 0} . {b1, 
            b2, b3, s , s , s }, 
{a4, a3, a2, a1, 0, 
            0} . {b1, b2, b3, b4, s , s }, 
{0, a4, a3, 
            a2, a1, 0} . {b1, b2, b3, b4, b5, s }, 
{0, 0, 
            a4, a3, a2, a1} . {b1, b2, b3, b4, b5, 
            b6}, 
{a1, 0, 0, a4, a3, a2} . {s , b2, b3, 
            b4, b5, b6}, 
{a2, a1, 0, 0, a4, a3} . {s , 
            s , b3, b4, b5, b6}, 
{a3, a2, a1, 0, 0, 
            a4} . {s , s , s , b4, b5, 
            b6}
};
% === ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}, s]

In the next cell we provide {u1 ,u2, u3, u4, u5, u6} as a (4^th) argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {1, -1}, {s1, s2, s3, s4, s5, s6}]

The next cell performs a series of Dot products that give the same result as in the previous cell. Notice the difference between giving (s) and giving {s,t,u} as a (4^th) argument.  I actually have a hard time seeing when this would be useful.

{
{a1, 0, 0, a4, a3, a2} . {b1, b2, b3, s4, s5, 
            s6}, 
{a2, a1, 0, 0, a4, a3} . {b1, b2, b3, b4, 
            s5, s6}, 
{a3, a2, a1, 0, 0, a4} . {b1, b2, b3, 
            b4, b5 , s6}, 
{a4, a3, a2, a1, 0, 0} . {b1, 
            b2, b3, b4, b5, b6}, 
{0, a4, a3, a2, a1, 
            0} . {b1, b2, b3, b4, b5, u6 }, 
{0, 0, a4, 
            a3, a2, a1} . {b1, b2, b3, b4, b5, b6}, 
{a1, 
            0, 0, a4, a3, a2} . {s1 , b2, b3, b4, b5, 
            b6}, 
{a2, a1, 0, 0, a4, a3} . {s1, s2 , b3, 
            b4, b5, b6}, 
{a3, a2, a1, 0, 0, a4} . {s1, 
            s2 , s3 , b4, b5, 
            b6}
};
% === ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}, {s1, s2, s3, s4, s5, s6}]

To make this more clear I give another example where we give ListConvolve a (4^th) argument in the next cell.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {1, -1}, {s1, s2}]

The next cell performs a series of Dot products that give the same result as in the previous cell.

{
{a1, 0, 0, a4, a3, a2} . {b1, b2, b3, s2, s1, 
            s2}, 
{a2, a1, 0, 0, a4, a3} . {b1, b2, b3, b4, 
            s1, s2}, 
{a3, a2, a1, 0, 0, a4} . {b1, b2, b3, 
            b4, b5 , s2}, 
{a4, a3, a2, a1, 0, 0} . {b1, 
            b2, b3, b4, b5, b6}, 
{0, a4, a3, a2, a1, 
            0} . {b1, b2, b3, b4, b5, u6 }, 
{0, 0, a4, 
            a3, a2, a1} . {b1, b2, b3, b4, b5, b6}, 
{a1, 
            0, 0, a4, a3, a2} . {s1 , b2, b3, b4, b5, 
            b6}, 
{a2, a1, 0, 0, a4, a3} . {s1, s2 , b3, 
            b4, b5, b6}, 
{a3, a2, a1, 0, 0, a4} . {s1, 
            s2 , s1 , b4, b5, 
            b6}
};
% === ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}, {s1, s2}]

It's curious that providing an empty list as a (4^th) argument is equivalent to providing the integer 1 as a (4^th) argument.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}, {}] === 
      ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, 
        1]

The next cell shows that by default ListConvolve uses the second argument in place of a (4^th) argument when none is provided.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}] === 
ListConvolve[{a1, a2, a3, 
          a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, {b1, b2, b3, b4, b5, 
          b6}]

Generalizing beyond Times, Plus

In the next cell we provide ListConvolve (g) as a (5^th) argument and it uses the head (g) in place of Times.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 1}, 
      lst, g]

In the next cell we provide ListConvolve (g, h) as a (5^th) and (6^th) arguments respectively and it uses the heads (g) and (h) in place of Times and Plus respectively.

ListConvolve[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 1}, 
      lst, g, h]

ListConvolve with matrices

In the next cell we perform a ListConvolution on matrices.

m1=(({{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}));
m2=(({{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}));
ListConvolve[m1, m2]

The next cell shows how the same ListConvolution can be done as Dot products on parts of the matrices.

ListConvolve[m1, 
        m2] === 
{{Part[m1, 1] . Reverse[Part[m2, 3]] + 
            Part[m1, 2] . Reverse[Part[m2, 2]] + 
            Part[m1, 3] . Reverse[Part[m2, 1]]}}

We can give ListConvolve a level specification as a (7^th) argument.  The next cell shows that level (2) is the default specification when working on matrices.

ListConvolve[m1, m2] === 
ListConvolve[m1, 
        m2, {-1, 1}, m2, Times, Plus, 2]

In the next cell we give ListConvolve the integer (1) as a level specification.

ListConvolve[m1, m2, {-1, 1}, m2, Times, Plus, 1]

The next cell shows how the same ListConvolution can be done as Dot products on parts of the matrices.

mat1 = Transpose[m1];
mat2 = Transpose[m2];

    
\(ListConvolve[m1, m2, {\(-1\), 1}, m2, Times, Plus, 
        1] === 
{{Reverse[Part[mat1, 1]] . Part[mat2, 1], 
          Reverse[Part[mat1, 2]] . Part[mat2, 2], 
          Reverse[Part[mat1, 3]] . Part[mat2, 3]}}\)

The next cell shows two ways of expressing the default for the third argument of ListConvolve when working with matrices.

ListConvolve[m1, m2] === ListConvolve[m1, m2, {-1, 1}] === 
      ListConvolve[m1, m2, {{-1, -1}, {1, 1}}]

I am having a hard time seeing a pattern to how a ListConvolution on matrices is done in different cases.  I would like to have a complete understanding for what the third argument of ListConvolve does when working with matrices, but it's too hard to decipher.  For example I would like to understand the result of the examples below.

ListConvolve[m1, m2, {1, 1}]

ListConvolve[m1, m2, {{1, 1}, {-1, -1}}]

ListConvolve[m1, m2, {1, 1}, m2, Times, Plus, 1]

I also understand we can also perform a ListConvolution on tensors, but that is also too confusing for me to think about.

ListCorrelate

The next cell demonstrates the basic use of ListCorrelate.

Off[General::spell1];
Clear["Global`*"];

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}]

Next we see that ListCorrelate is equivalent to flattening the result of a certain matrix product.

(({{a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}]

The next cell shows that ListConvolve is closely related to ListCorrelate.

listA = {a1, a2, a3, a4};
listB = {b1, b2, b3, b4, b5, b6};
ListConvolve[listA, listB] ===  
      ListCorrelate[Reverse[listA], listB]

Specifying the "overhang" using  {(Subscript[K, L]), (Subscript[K, R])}

In the next cell we provide ListCorrelate {-1,-1} as a third argument.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {-1, -1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (-1) indicates last element of {a1, a2, a3, a4}, and (-2) would indicate the second from the last element of {a1, a2, a3, a4}.

(({{a4, 0, 0, a1, a2, a3}, {a3, a4, 0, 0, a1, a2}, {a2, a3, a4, 0, 0, a1}, {a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {-1, -1}]

In the next cell we provide ListCorrelate {1,1} as a third argument.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, 
        1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the first element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (2) would indicate the second element of {a1, a2, a3, a4}.

(({{a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}, {a4, 0, 0, a1, a2, a3}, {a3, a4, 0, 0, a1, a2}, {a2, a3, a4, 0, 0, a1}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, 
          1}]

In the next cell we provide ListCorrelate {1,-1} as a third argument.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
        b6}, {1, -1}]

The previous example is equivalent to the matrix product in the next cell.  Here we have the first element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the last element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.

(({{a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}, {a4, 0, 0, a1, a2, a3}, {a3, a4, 0, 0, a1, a2}, {a2, a3, a4, 0, 0, a1}, {a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}]

In the next cell we provide ListCorrelate {-1,1} as a third argument. Notice this gives the same result as 
ListCorrelate[{a1,a2,a3,a4},{b1,b2,b3,b4,b5,b6}]  (ie. {-1,1} is the default for the third argument).

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
        1}]

Length[%]

The previous example is equivalent to the matrix product in the next cell.  Here we have the last element of {a1, a2, a3, a4} in the upper left position of the left matrix..  We also have the First element of {a1, a2, a3, a4} in the lower right position of the left matrix.  The (1) indicates first element of {a1, a2, a3, a4}, and (-1) indicates the last element of {a1, a2, a3, a4}.

(({{a4, 0, 0, a1, a2, a3}, {a3, a4, 0, 0, a1, a2}, {a2, a3, a4, 0, 0, a1}, {a1, a2, a3, a4, 0, 0}, {0, a1, a2, a3, a4, 0}, {0, 0, a1, a2, a3, a4}, {a4, 0, 0, a1, a2, a3}, {a3, a4, 0, 0, a1, a2}, {a2, a3, a4, 0, 0, a1}})).(({{b1}, {b2}, {b3}, {b4}, {b5}, {b6}}));
Flatten[%] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
          1}]

Specifying the padding

In the next cell we provide (s) as a (4^th) argument.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 1}, 
      s]

The next cell performs a series of Dot products that give the same result as in the previous cell.  Notice we gave ListCorrelate {1,-1} as a third argument, and the left part of the first row starts with the first part of {a1,a2,a3,a4}.  Also the last row ends with the last element of {a1,a2,a3,a4}.

{
{a4,  0,  0, a1, a2, a3} . {b1, s , s , s , 
            s , s }, 
{a3, a4,  0,  0, a1, a2} . {b1, 
            b2, s , s , s , s }, 
{a2, a3, a4,  0,  
            0, a1} . {b1, b2, b3, s , s , s }, 
{a1, a2, 
            a3, a4,  0,  0} . {b1, b2, b3, b4, s , 
            s }, 
{ 0, a1, a2, a3, a4,  0} . {b1, b2, 
            b3, b4, b5, s }, 
{ 0,  0, a1, a2, a3, 
            a4} . {b1, b2, b3, b4, b5, b6}, 
{a4,  0,  0, 
            a1, a2, a3} . {s , b2, b3, b4, b5, b6}, 
{a3, 
            a4,  0,  0, a1, a2} . {s , s , b3, b4, b5, 
            b6}, 
{a2, a3, a4,  0,  0, a1} . {s , s , 
            s , b4, b5, 
            b6}
};
% === ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {-1, 1}, s]

In the next cell we provide {s1 s2, s3, s4, s5, s6} as a (4^th) argument.

w1 = 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
          1}, {s1, s2, s3, s4, s5, s6}]

The next cell performs a series of Dot products that give the same result as in the previous cell. Notice the difference between giving (s) and giving {s,t,u} as a (4^th) argument.  I actually have a hard time seeing when this would be useful.

{
{a4,  0,  0, a1, a2, a3} . {b1, s2 , s3 , 
            s4 , s5 , s6 }, 
{a3, a4,  0,  0, a1, 
            a2} . {b1, b2, s3 , s4 , s5 , s6 }, 
{a2, 
            a3, a4,  0,  0, a1} . {b1, b2, b3, s4 , s5 , 
            s6 }, 
{a1, a2, a3, a4,  0,  0} . {b1, b2, 
            b3, b4, s5 , s6 }, 
{ 0, a1, a2, a3, a4,  
            0} . {b1, b2, b3, b4, b5, s6 }, 
{ 0,  0, 
            a1, a2, a3, a4} . {b1, b2, b3, b4, b5, 
            b6}, 
{a4,  0,  0, a1, a2, a3} . {s1 , b2, 
            b3, b4, b5, b6}, 
{a3, a4,  0,  0, a1, 
            a2} . {s1 , s2 , b3, b4, b5, b6}, 
{a2, a3, 
            a4,  0,  0, a1} . {s1 , s2 , s3 , b4, b5, 
            b6}
};
% === ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {-1, 1}, {s1, s2, s3, s4, s5, s6}]

To make this more clear I give another example where we give ListCorrelate a (4^th) argument in the next cell.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {-1, 
        1}, {s1, s2}]

The next cell performs a series of Dot products that give the same result as in the previous cell.

{
{a4,  0,  0, a1, a2, a3} . {b1, s2 , s1 , 
            s2 , s1 , s2 }, 
{a3, a4,  0,  0, a1, 
            a2} . {b1, b2, s1 , s2 , s1 , s2 }, 
{a2, 
            a3, a4,  0,  0, a1} . {b1, b2, b3, s2 , s1 , 
            s2 }, 
{a1, a2, a3, a4,  0,  0} . {b1, b2, 
            b3, b4, s1 , s2 }, 
{ 0, a1, a2, a3, a4,  
            0} . {b1, b2, b3, b4, b5, s2 }, 
{ 0,  0, 
            a1, a2, a3, a4} . {b1, b2, b3, b4, b5, 
            b6}, 
{a4,  0,  0, a1, a2, a3} . {s1 , b2, 
            b3, b4, b5, b6}, 
{a3, a4,  0,  0, a1, 
            a2} . {s1 , s2 , b3, b4, b5, b6}, 
{a2, a3, 
            a4,  0,  0, a1} . {s1 , s2 , s1 , b4, b5, 
            b6}
};
% === ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {-1, 1}, {s1, s2}]

It's curious that providing an empty list as a (4^th) argument is equivalent to providing the integer 1 as a (4^th) argument.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}, {}] === 
      ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, 
        1]

The next cell shows that by default ListCorrelate uses the second argument in place of a (4^th) argument when none is provided.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, 
          b6}, {1, -1}] === 
ListCorrelate[{a1, a2, a3, 
          a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, {b1, b2, b3, b4, b5, 
          b6}]

Generalizing beyond Times, Plus

In the next cell we provide ListConvolve (g) as a (5^th) argument and it uses the head (g) in place of Times.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, 
      lst, g]

In the next cell we provide ListCorrelate (g, h) as a (5^th) and (6^th) arguments respectively and it uses the heads (g) and (h) in place of Times and Plus respectively.

ListCorrelate[{a1, a2, a3, a4}, {b1, b2, b3, b4, b5, b6}, {1, -1}, 
      lst, g, h]

ListCorrelate with matrices

In the next cell we perform a ListCorrelate on matrices.

m1=(({{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}));
m2=(({{x1, x2, x3}, {y1, y2, y3}, {z1, z2, z3}}));
ListCorrelate[m1, m2]

The next cell shows how the same ListCorrelate can be done as Dot products on parts of the matrices.

ListCorrelate[m1, 
        m2] === {{Part[m1, 1] . Part[m2, 1] + Part[m1, 2] . Part[m2, 2] + 
            Part[m1, 3] . Part[m2, 3]}}

We can give ListCorrelate a level specification as a (7^th) argument.  The next cell shows that level (2) is the default specification when working on matrices.

ListCorrelate[m1, m2] === 
ListCorrelate[m1, 
        m2, {1, -1}, m2, Times, Plus, 2]

In the next cell we give ListCorrelate the integer (1) as a level specification.

ListCorrelate[m1, m2, {1, -1}, m2, Times, Plus, 1]

The next cell shows how the same ListCorrelate can be done as Dot products on parts of the matrices.

mat1 = Transpose[m1];
\(mat2 = Transpose[m2]; 
\)
ListCorrelate[m1, m2, {1, -1}, m2, Times, Plus, 
        1] === {{Part[mat1, 1] . Part[mat2, 1], 
          Part[mat1, 2] . Part[mat2, 2], 
          Part[mat1, 3] . Part[mat2, 3]}}

The next cell shows two ways of expressing the default for the third argument of ListCorrelate when working with matrices.

ListCorrelate[m1, m2] === ListCorrelate[m1, m2, {1, -1}] === 
      ListCorrelate[m1, m2, {{1, 1}, {-1, -1}}]

I am having a hard time seeing a pattern to how a ListConvolution on matrices is done in different cases.  I would like to have a complete understanding for what the third argument of ListCorrelate does when working with matrices, but it's too hard to decipher.  For example I would like to understand the result of the examples below.

ListCorrelate[m1, m2, {1, 1}]

ListCorrelate[m1, m2, {{-1, -1}, {1, 1}}]

ListCorrelate[m1, m2, {-1, 1}, m2, Times, Plus, 1]

I also understand we can also perform a ListCorrelate on tensors, but that is also too confusing for me to think about.

Map

Map and (f[#]&) can be used to map any function (f) to each argument under any Head.  In the next Cell (Exp[_]) mapped to each term in a sum.  Many users understand what Map does with the default level specification.  In the first example the default level specification is used, and Exp[_] is mapped to each expression at the first level.  The #& notation is explained in the discussion of Function.

Clear[x];
demo = 
        x + Cos[(((Sqrt[x]) π)/3)] + Sin[(((Sqrt[x]) π)/6)];
Map[ Exp[#] &,  demo]

Note:  The shorthand notation for Map is   /@ .  This shorthand notation is used to do the same as the previous line.  At first this notation seems very cryptic.

Exp[#] & /@ demo

If you need to specify a level specification the short hand notation is not at all convenient.  Different variations of level specification are demonstrated in the lines below.  Many other commands allow a user to specify the level specification, and the conventions are always the same.  Level specification {3} means to only map the function to Level 3.  In the next Cell we Map 
(# + 1)& to all subexpressions in (demo) at Level {3}.

Map[(# + z) &, demo, {3}]

Level specification {-1} refers to the smallest subexpressions (the atoms).  In the next cell we add (1) to each atom in (demo).  It should be pointed out that the list of atoms includes 1/6, 1/3,  and 1/2, instead of 1, 2, 3, 6.  This is because since Mathematica rational numbers as atoms.  Complex numbers are also considered atoms.

Map[ (# + 1) &, demo,  {-1}]

Level specification (2) refers to all levels from (Level 1) to (Level 2).  Now we Map ( #/z&) to all subexpressions of (demo) from (Level 1) to (Level 2).

\(Map[#\/z &, demo, 2]\)

The next line Maps (#+z&) to all subexpressions of (demo) at (Level 2) and (Level 3).

Map[# + z &, demo,  {2, 3}]

Heads Option

Map has a Heads option like several other functions, but it's hard to think of a practical use for this feature.  By default Map uses the setting (Heads->False).  If your writing a program and you need to make it full proof you should use the form  Map[f, expr, Heads->False]  instead of the shorter (and normally equivalent) form  f/@expr.  The reason is that the user may have changed the default setting via SetOptions[Map, Heads->True] which would change the behavior of  f/@expr.  Now consider the next cell to see how Map uses the Heads option.

ClearAll[f, h];
expr = {h[1], {2, 3}};
Map[f, expr, {2},  Heads -> False]

In the cell above (f) was mapped to expressions at positions {1,1}, {2,1}, and {2,2} since they are all the expressions at level 2.  In the next cell the same example is evaluated with the setting (Heads->True) and (f) is also mapped to every head at level 2. In this case the heads at level 2 are (h) at position {1,0} and List at position {2,0}.

Map[f, expr, {2}, Heads -> True]

MapAt

MapAt allows us to Map a function to specified positions.  The next cell maps foo to position 3.

ClearAll["Global`*"];
MapAt[foo, {a, b, c, d, e, f}, 3]

The next cell maps foo to positions {2} and {4}.

MapAt[foo, {a, b, c, d, e, f}, {{2}, {4}}]

MapIndexed

It's a little difficult to learn how to use the MapIndexed function.  In the next cell I clear values form all variables and give the simplest use of MapIndexed.  Notice the results are in the form f[2,{1}],  f[3,{2}],  etc.

ClearAll["Global`*"];
lst = Prime[Range[6]];
MapIndexed[foo, lst] // TableForm

Instead of the form above, one might rather have the result of MapIndexed in the form 
f[2,1], f[3,2], etc.  In the line above (f) is applied to two arguments, and the second argument has the head List.  A result in the form  f[2,1], f[3,2]  is obtained in the next line by removing the head list from the second argument.  Technically the head was changed to Sequence, then Sequence was removed during evaluation.  The #& notation is explained in the discussion of Function.

MapIndexed[foo[#1, Sequence @@ #2] &, lst] // TableForm

Another use of MapIndexed involves mapping two functions as in the line below.

MapIndexed[{foo[#1], goo[#2]} &, lst] // TableForm

In the next line (g@@#2) is used to ensure we end up with g[1], g[2], .. instead of g[{1}], g[{2}], ....

MapIndexed[{foo[#1], goo @@ #2} &, lst] // TableForm

By giving MapIndexed a level specification for only level 2 you can MapIndex to each element of a matrix.

MapIndexed[foo, {{a, b}, {c, d}}, {2}]

MapIndexed[{foo[#1], goo[Sequence @@ #2]} &, {{a, b}, {c, 
          d}}, {2}]

MapIndexed can take a level specification using the same conventions as the examples where level specification is explained.  A practical example that uses MapIndexed with a level specification is given in the section on Coefficient and CoefficientList.

Heads Option

MapIndexed has a Heads option with the default setting (Heads->False).  In the next cell we use (Heads->True) and the function with an index is mapped to the head of {a,b,c}.

MapIndexed[foo, {a, b, c}, Heads -> True]

MapThread

MapThread can be used to Apply a function to each column of a matrix as I do in the next cell. However, before using MapThread I clear the values from all variables.

ClearAll["Global`*"];
mat = {{a, b, c, d, e, f}, {1, 2, 3, 4, 5, 6}};
MapThread[foo, mat]

MapThread is equivalent to using Map followed by Apply at level 2 (see below).  I have to wonder why it isn't called ApplyThread.

MapThread[foo, mat] === Apply[foo, Thread[mat], 2]

In the next few lined we use MapThread to work with the tensor below.

Off[General::spell1]
\(\(tensor = {
		{{a111, a112, a113, a114}, {a121, a122, a123, 
              a124}}, 
		{{a211, a212, a213, a214}, 
			{a221, a222, 
              a223, a224}}, 
		{{a311, a321, a323, a324}, 
			{a321, 
              a322, a323, a324}}
	};\)\)
On[General::spell1]
Dimensions[tensor]

In the next line MapThread is used on the above tensor.

t1 = MapThread[foo, tensor];
t1[[1]]

t1[[2]]

MapThread can work on the tensor above with a third argument as in the next example, and gives a very different result than the previous example.

t2 = MapThread[foo, tensor, 2];
t2[[1]]

t2[[2]]

An interesting application of MapThread is given below where MapThread is used to apply a list of functions to a list of arguments.  The #& notation is explained in the discussion of Function.

funcs = {f1,  f2,  f3};
values = {val1, val2, val3};
MapThread[(#1[#2]) &,  {funcs, values}]

Alan Hayes provided the code below to make a list of replacement rules.

pos = Array[p, {4, 3}]

posval = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {11, 12, 13}};
Flatten[MapThread[Rule, {pos, posval}, 2]]

MakeBoxes

The code below will ensure any difference of two terms is displayed as a difference when you use any form that outputs 2D expressions (StandardForm, MatrixForm, TableForm, etc.).   TraditionalForm displays (b-a) even if you don't make a special definition, it also has many other conventions that come with it.

The kernel automatically calls MakeBoxes when it needs to format an expression using 2D notation.  The rules for MakeBoxes specify that how expressions with a certain form are should be displayed one way, and other forms should be displayed different ways.  Typically a definition  says a certain box structure should be built using parts of the given expression.  The various parts that are used in the box structure are often smaller expressions that also need to be made into 2D expressions, so MakeBoxes must be called on these smaller expressions.  This continues recursively until MakeBoxes formats atoms directly into box structures.

When the kernel calls MakeBoxes it passes the form used in the second argument. The form can be StandardForm, TraditionalForm, MatrixForm, or any other form that formats the output as 2D expressions.  A rule for MakeBoxes can have a specific form for the second argument as in MakeBoxes[pattn, StandardForm] in which case the definition is only used for StandardForm.  You can even define your own for using MakeBoxes[pattn, MyForm], and the provided formatting rules will be used when you evaluate expr//MyForm.  In the definition below the second argument of MakeBoxes is the pattern (form_).  In this case the formatting rule is used for any form that uses 2D expressions to format the output.

MakeBoxes has the HoldAllComplete attribute.  As a result you can't use MakeBoxes[Evaluate[expr], form] to force the evaluation of (expr).  If (expr) must complete evaluation that can be accomplished using 
( MakeBoxes@@{expr,form} ).

\(\(MakeBoxes[\((n_Real | n_Integer | n_Rational)\) a_.  + b_ /; 

            Head[b] =!= Plus && n < 0, form_] := 

        RowBox[{MakeBoxes[b, form], "-", 
            MakeBoxes @@ {\(-n\)\ \ a, form}}];\)\)

The above rules for MakeBoxes will produce a more readable display of expressions such as the following:

Clear[a, y];
Log[y - 2/3]/Sqrt[y - 4 a] - Exp[y - 2.4]

The rules for MakeBoxes are stored as FormatValues.

FormatValues[MakeBoxes]

It may be tempting to write a slightly simpler implementation, but this simpler implementation has flaws.

The simpler implementation is:
    MakeBoxes[(n_Real|n_Integer|n_Rational)a_.+b_/;
    Head[b]=!=Plus&&n<0,form_]:=
    RowBox[{MakeBoxes[b,form],MakeBoxes@@{n*a,form}}];

In private email Neil Soiffer of Wolfram Research noted that the version above has a few problems.
(1) The '-' sign is grouped with the subsequent term as a unary "minus", instead of binary "difference".
This will effect spacing after the minus (not symmetric with respect to '-' as is easily seen with 'a-b').
(2) Grouping doesn't work well.  If one evaluates (a-b) and triple clicks on the '-' in the output, automatic grouping doesn't work right.
(3)  Perhaps the worst problem is that because '-' is interpreted as a unary minus, there is implied multiplication between the 'a' and the '-b' and so it will evaluate incorrectly.  Evaluate (a-b) and select the output cell, go to
cell | cell properties | cell evaluatable to make the output cell evaluatable, and then evaluate the cell.  The result looks like '-a b'.

Another example

David park noted that the minus sign gets pulled into the sum in the next input.

(((-1)/2)) (a + 2.3*b + c)

By looking at the box structure of the output cell above you can see what we need to use in a MakeBoxes definition.  The input in the next cell does the job.  The first definition ensures the negative sign is factored out, and the second definition puts the negative sign in the numerator.

\(MakeBoxes[a_*\(b : Plus[_?Negative*_, \((_?Negative*_)\) .. ]\), 
        form : \((StandardForm | TraditionalForm)\)] := 
      RowBox[{MakeBoxes @@ {\(-a\), form}, 
          RowBox[{"(", 
              MakeBoxes @@ {\(-b\), 
                  form}, ")"}]}]
\)
MakeBoxes[p_Rational?Negative, 
        form : (StandardForm | TraditionalForm)] := 
      FractionBox[MakeBoxes @@ {Numerator[p], form}, 
        MakeBoxes @@ {Denominator[p], form}]

Now the negative sign is always factored out.

{-((1/2)) (a + 2.3*b + c), (1/2) (-a - 2.3*b - 
            c)}

MatchQ

In order for a pattern to match an expression, the pattern and the expression it is compared with must have the same FullForm after evaluation.  For example the expression a/b has the FullForm Times[a,Power[b,-1]], so it doesn't match the pattern (_Rational).  MatchQ is a very good tool to use when testing a pattern to see if it matches the intended expressions.  Some interesting examples of MatchQ are given below.

{MatchQ[3/2, p_/q_], MatchQ[3/2, _Rational]}

The head HoldPattern used in next cell is explained in another section.

Clear[a, b];
\({MatchQ[a/b, _Rational], 
MatchQ[a/b, _/_], 

      MatchQ[a/b, HoldPattern[_/_]], 
MatchQ[a/b, p_/q_]}\)

\({MatchQ[a/2, p_/q_], 
	MatchQ[a/2, _Rational], 
	
      MatchQ[a/2, _*_Rational]}\)

\({MatchQ[1/Sqrt[b], _/Sqrt[_]], 
      MatchQ[1/Sqrt[b], HoldPattern[_/Sqrt[_]]], 
	
      MatchQ[1/Sqrt[b], 1/Sqrt[_]]}\)

\({MatchQ[a/Sqrt[b], _/Sqrt[_]], 

      MatchQ[a/Sqrt[b], HoldPattern[_/Sqrt[_]]], 

      MatchQ[a/Sqrt[b], HoldPattern[x_\ *y_^\((\(-1\)/2)\)]], 

      MatchQ[a/Sqrt[b], HoldPattern[x_/Sqrt[y_]]], 

      MatchQ[a/Sqrt[b], x_\ *y_^\((\(-1\)/2)\)], 

      MatchQ[a/Sqrt[b], x_/Sqrt[y_]]}\)

{MatchQ[2 + 3  I, a_ + b_*I], MatchQ[2 + 3  I, _Complex]}

\({MatchQ[a + b\ I, _Complex], 
MatchQ[a + b\ I, _ + _Complex], 

      MatchQ[a + b\ I, _ + _*I], 
MatchQ[a + b\ I, \ _ + _*Complex[0, 1]], 

      MatchQ[a + b\ I, HoldPattern[_ + _*I]], 

      MatchQ[a + b\ I, HoldPattern[_ + _*Complex[0, 1]]], 

      MatchQ[a + b\ I, _ + _\ *_Complex\ ], 

      MatchQ[a + b\ I, \ x_ + y_*I]}\)

data = {{2, 3}, {1, 4}, {6, 7}, {8, 6}, {2, 1}};
MatchQ[data, {{_, _} .. }]

I don't provide further examples, but any combination of pattern matching constructs can be used with MatchQ.  Nuances of pattern matching are discussed in another section.

MemberQ

The first example is simple. MemberQ finds that the sum includes (x) to an Integer power.

Clear[x, y];
MemberQ[1 + x + (x^2), Power[x, _Integer]]

You might wonder why MemberQ doesn't return True in this case.  The reason is that the default level specification doesn't have MemberQ check the whole expression.

MemberQ[(x^2), Power[x, _Integer]]

Next MemberQ is given a level specification that has it check all levels including the whole expression, and True is returned.

MemberQ[(x^2), Power[x, _Integer], {0, -1}]

Next the default level specification stops MemberQ from looking deep enough to find the (x^2).

MemberQ[{1 + x + (x^2), y}, Power[x, _Integer]]

In the next example MemberQ is given a level specification deep enough to find the (x^2).

MemberQ[{1 + x + (x^2), y}, Power[x, _Integer], {0, -1}]

I don't provide further examples, but any combination of pattern matching constructs can be used with MemberQ.  Nuances of pattern matching are discussed in another section.

Heads Option

MemberQ has a Heads option with the default setting (Heads->False).  So in the next example MemberQ doesn't check the heads of subexpressions.

MemberQ[1 + x + (x^2), Plus]

In the next example the setting (Heads->True) is used and MemberQ does check the heads of subexpressions.

MemberQ[1 + x + (x^2), Plus, Heads -> True]

Message

In addition to what I present here you might also want to study (PackageDesignTutorial.nb) posted at 
http://library.wolfram.com/infocenter/Conferences/184
In that notebook you will find a section on error handling where good use of messages is covered.  A non-trivial application of messages is given below, but first I define a function in the next cell that computes the magnitude of a vector is defined and the function is used to find the magnitude of the vector {2, 3, 4}.

Mag[v_?VectorQ] := (Sqrt[Plus]) @@ (v . v)
Mag[{2, 3, 4}]

The next cell demonstrates that when Mathematica is given invalid input it normally posts a message and returns the expression that is undefined.

Sin[π/2,  0]

We would like our Mag function to behave like a built in function when given an invalid argument.  The next cell defines a message for this purpose.

Mag::vect = "Mag is only defined for vectors.  The expression `1` \
is not a vector.";

We could use the definition  Mag[expr_]:=Message[mag::vect,expr].  That would cause Mag[expr] with an invalid argument to evaluate to a message and the input expression would not be returned.  But we want Mag to work like a built-in function so we have it post a message, and return expression passed to Mag.  This can be done by putting the message in a condition as in the next line.  When this is done the condition will never evaluate to True, so the right side of the definition can be almost anything.

Mag[expr_] /; 
        Message[Mag::vect, expr] := "Never get here."

Now when Mag is given an argument that isn't a vector it prints a message and returns the input expression since no definition matched.  Here the argument given to Mag was  {{1, (96/4) - 5}, {2, 3}} which evaluated to {{1,19},{2,3}}.  None of the Mag definitions apply, so  Mag[{{1, 19}, {2, 3}}]  is returned.

Mag[{{1, (96/4) - 5}, {2, 3}}]

Mathematica has an undocumented function called ArgumentCountQ which can be useful when making definitions for functions that post a message when the function is given an incorrect number of arguments. An explanation of this feature can be found by looking up ArgumentCountQ at  http://support.wolfram.com/Kernel/Symbols/

Checking for non-options

We can also account for the possibility that one or more of several arguments are not options when only options are allowed in these positions.  This is what I do with Func defined below.  At the begining of the next cell I clear values assigned to all global variables.

ClearAll["Global`*"];

      
Func[expr_, 
          opts___?OptionQ] := {expr};

 (* The next definition is only used if the previous definition didn' t match. *) 
\(\(\(Func[\ _, opts__] /; 
          With[{nonOptions = 
                Cases[Flatten[{opts}], _?\((\(! OptionQ[#]\) &)\)]}, 
nonOptions =!= {} && 
              If[Length[nonOptions] === 1, 
Message[
                  Func::opt, 
                  StringTake[
                    ToString@
                      nonOptions, {2, \(-2\)}]], 
Message[
                  Func::opts, 
                  StringTake[
                    ToString@
                      nonOptions, {2, \(-2\)}]]
]
] := "Never get here.";\)\(
\)
    \)\)
\(\(\(Func::opt = "`1` was was used in Func as an argument beyond the first argument. Func only allows options beyond the first argument.";\)\(
\)
    \)\)
Func::opts = "Expressions, `1`, were given beyond the first \
argument in Func. Only options are allowed beyond the first \
argument.";

Next we see Func[__] evaluates whether there are options or not.

Func[x + y]
Func[a + b, opt1 -> val1, opt2 :> val2]

When one or more argument beyond the first in Func[__] are not options we get messages as below, and the original expression is returned.

Func[a + b, opt1 -> val1, opt, opt2 -> val2]

Func[a + b, opt1 -> val1, opt, opt2 -> val2, opt3]

General Messages

Lets take another look at Mag defined above.  We could also define a message for the case where Mag gets more that one argument.  However, we would be wise to note define a special message for when too many arguments are given, because Mathematica  already has a set of generic messages that can be associated with many functions.  All the built-in messages are defined in the file (messages.m).  In the next cell the message used is  Message[Mag::argx, _, _], but no message named Mag::argx has been defined.  When the kernel sees there is no message named (Mag::argx), it looks for one named (General::argx) in  (messages.m) and it finds one.  Mathematica then usees the text for (General::argx) but when the message is displayed its labled (Mag::argx).  This allows commonly used messages to be shared by many functions.  The messages (General::tag) are briefly explained in section 2.8.21 of the Mathematica Book

\(Mag[arg1_, others__] /; 
	\ \ \ Message[Mag::argx, Mag, 
          Length[{arg1, others}]] := "Never get here."\)

Once the rule above is defined, Mag[__] will post a message and return the input when it is given more than one argument.

Mag[3, 4, 5, 6]

I tried to pick out the general messages that might have applications in packages users write and they are each used in a generic example below.  You will find that related messages are arranged in grouped cells.

Number of arguments

Below we have general messages that report an incorrect number of arguments.

Message[General::argctu, ARG1]

Message[General::argct, ARG1, ARG2]

Message[General::argmu, ARG1, ARG2]

Message[General::argr, ARG1, ARG2]

Message[General::argx, ARG1, ARG2]

Message[General::argbu, ARG1, ARG2, ARG3]

Message[General::argf, ARG1, ARG2, ARG3]

Message[General::argm, ARG1, ARG2, ARG3]

Message[General::argrx, ARG1, ARG2, ARG3]

Message[General::argtu, ARG1, ARG2, ARG3]

Message[General::argb, ARG1, ARG2, ARG3, ARG4]

Message[General::argt, ARG1, ARG2, ARG3, ARG4]

Invalid arguments

Below we have general messages that report incorrect type of arguments.

Message[General::fnsym, ARG1]

Message[General::notnorm, ARG1]

Message[General::bool, ARG1, ARG2]

Message[General::ilsm, ARG1, ARG2]

Message[General::ilsmi, ARG1, ARG2]

Message[General::ilsmn, ARG1, ARG2]

Message[General::ilsmp, ARG1, ARG2]

Message[General::innf, ARG1, ARG2]

Message[General::int, ARG1, ARG2]

Message[General::intm, ARG1, ARG2]

Message[General::intnm, ARG1, ARG2]

Message[General::intnz, ARG1, ARG2]

Message[General::intpm, ARG1, ARG2]

Message[General::ipnf, ARG1, ARG2]

Message[General::list, ARG1, ARG2]

Message[General::normal, ARG1, ARG2]

Message[General::seqs, ARG1, ARG2]

Message[General::seqso, ARG1, ARG2]

Message[General::string, ARG1, ARG2]

Message[General::strs, ARG1, ARG2]

Message[General::strse, ARG1, ARG2]

Message[General::sym, ARG1, ARG2]

Message[General::zval, ARG1, ARG2]

Options

Below we have general messages that report problems with options

Message[General::mult, ARG1]

Message[General::estep, ARG1, ARG2]

Message[General::iopf, ARG1, ARG2]

Message[General::iopnf, ARG1, ARG2]

Message[General::iopnm, ARG1, ARG2]

Message[General::ioppf, ARG1, ARG2]

Message[General::modgp, ARG1, ARG2]

Message[General::modn, ARG1, ARG2]

Message[General::modp, ARG1, ARG2]

Message[General::npt, ARG1, ARG2]

Message[General::opset, ARG1, ARG2]

Message[General::opstl, ARG1, ARG2]

Message[General::optrs, ARG1, ARG2]

Message[General::opttf, ARG1, ARG2]

Message[General::opttfa, ARG1, ARG2]

Message[General::optx, ARG1, ARG2]

Message[General::par, ARG1, ARG2]

Message[General::nonopt, ARG1, ARG2, ARG3]

Matrices

Below we have general messages that report problems with matrices.

Message[General::inf]

Message[General::mindet]

Message[General::sing, ARG1]

Message[General::luc, ARG1, ARG2]

Message[General::matrix, ARG1, ARG2]

Message[General::matsq, ARG1, ARG2]

Iterators

Below we have general messages that report problems with iterators.

Message[General::iterb]

Message[General::itraw, ARG1]

Message[General::ittag, ARG1]

Message[General::itform, ARG1, ARG2]

Directories, Files, I/O

Below we have general messages that report problems with directories, files, or I/O.

Message[General::isdir]

Message[General::filro, ARG1]

Message[General::fstr, ARG1]

Message[General::ioarg, ARG1]

Message[General::ioerr, ARG1]

Message[General::longp, ARG1]

Message[General::nfdir, ARG1]

Message[General::nffil, ARG1]

Message[General::noopen, ARG1]

Message[General::openr, ARG1]

Message[General::openw, ARG1]

Message[General::openx, ARG1]

Message[General::aofil, ARG1, ARG2]

Polynomials

Below we have general messages that report problems related to polynomials.

Message[General::eival]

Message[General::bmod, ARG1]

Message[General::modint, ARG1]

Message[General::modm, ARG1]

Message[General::npoly, ARG1]

Message[General::npolys, ARG1]

Message[General::poly, ARG1]

Message[General::polyx, ARG1]

Message[General::nmod, ARG1, ARG2]

Message[General::npv, ARG1, ARG2]

Message[General::nrpi, ARG1, ARG2]

Message[General::nrtpi, ARG1, ARG2]

Graphics

Below we have general messages that report problems related to graphics.

Message[General::pcolor]

Message[General::color, ARG1]

Message[General::lspec, ARG1]

Message[General::colfun, ARG1, ARG2]

Message[General::colfun1, ARG1, ARG2]

Message[General::gprim, ARG1, ARG2]

Miscellaneous

Below we have general messages that report a variaty of other problems.

Message[General::bktfn, ARG1, ARG2, ARG3, 
      ARG4]

Message[General::cfail]

Message[General::dbyz]

Message[General::fas]

Message[General::ovfl]

Message[General::unpack1]

Message[General::fmtval, ARG1]

Message[General::globn, ARG1]

Message[General::ifexp, ARG1]

Message[General::ifpa, ARG1]

Message[General::indet, ARG1]

Message[General::ivar, ARG1]

Message[General::level, ARG1]

Message[General::nalg, ARG1]

Message[General::nlist3, ARG1]

Message[General::notfound, ARG1]

Message[General::notnorm, ARG1]

Message[General::notstr, ARG1]

Message[General::primm, ARG1]

Message[General::prims, ARG1]

Message[General::punpack1, ARG1]

Message[General::rep, ARG1]

Message[General::stop, ARG1]

Message[General::unpack, ARG1]

Message[General::unuser, ARG1]

Message[General::dupv, ARG1, ARG2]

Message[General::globm, ARG1, ARG2]

Message[General::par, ARG1, ARG2]

Message[General::plln, ARG1, ARG2]

Message[General::precw, ARG1, ARG2]

Message[General::punpack, ARG1, ARG2]

Message[General::trace, ARG1, ARG2]

Message[General::mbrpos, ARG1, ARG2, ARG3]

Message[General::tag, ARG1, ARG2, ARG3]

Message[General::bktmch, ARG1, ARG2, ARG3, ARG4]

Message[General::heads, ARG1, ARG2, ARG3, ARG4]

Message[General::itflrw, ARG1, ARG2, ARG3]

(* 
      The next message is normally turned off. *) 
On[General::newsym]
Message[General::newsym, ARG1]
Off[General::newsym]

How to tell if a message is On or Off

The direct answer to this problem is the following:
    If  Head[symbol::tag] === Off
    then  (the message is Off)
    else   (the message is On)

If a message is on and the message is not one of the general messages (which are explained in the previous sub-section), then the message has the head String.  That is why the next cell returns String.

On[Show::gtype];
Head[Show::gtype]

On the other hand if a message is on and the message is one of the general messages, then the message has the head MessageName. That is why the next cell returns MessageName.  As explained above Mathematica defines a message (General::argx) that can be used as a message for any function, but the message (Show::gtype) is only associated with Show unless you make assignments that associate it with other symbols.

On[Sin::argx];
Head[Sin::argx]

The next cell is typical for what (symbol::tag) evaluates to when the message is Off, and the message is not one of the general messages.

Off[Show::gtype];
Show::gtype

The next cell shows what (symbol::tag) evaluates to when the message is Off, and the message is one of the general messages.

Off[Sin::argx];
Sin::argx

The next input turns both messages back on.

On[Show::gtype, Sin::argx]

NestList

The documentation for NestList is clear, but it isn't immediatly clear when one might want to use NestList. I give a usefule example in the next cell which efficiently makes a list of the ten smallest prime numbers that are larger than 10^15.

Needs["NumberTheory`NumberTheoryFunctions`"];
NestList[NextPrime, 10^15, 10] // Rest

NHoldAll, NHoldFirst, NHoldRest

The next cell gives (f) the NHoldRest attribute.

ClearAll[f];
Attributes[f] = {NHoldRest};

Now only the first argument of (f) can be evaluated numerically.

N[f[(π/2), (π/3), (π/4)]]

NHoldAll, NHoldRest and NHoldFirst prevent evaluation even deep inside an expression as demonstrated in the next cell.

N[{π + {f[(π/2), (π/3), (π/4)]}}]

Built-in symbols with attributes NHoldAll, NHoldFirst, NHoldRest

The next cell makes a list of all built-in symbols.

symbs = 
        Cases[ToExpression /@ Names["System`*"], _Symbol];

The next cell returns all built-in symbols with the NHoldAll attribute.  If you understand what C and Root do it's easy to see why they have the attribute NHoldAll.

Select[symbs, MemberQ[Attributes[#], NHoldAll] &]

The next cell returns all built-in symbols with the NHoldRest attribute.  If these functions didn't have the NHoldRest attribute they wouldn't work correctly in certain cases.

Select[symbs, MemberQ[Attributes[#], NHoldRest] &]

The next cell returns all built-in symbols with the NHoldFirst attribute.  I have no experience with the functions that have the NHoldFirst attribute.

Select[symbs, MemberQ[Attributes[#], NHoldFirst] &]

OneIdentity

Many users don't understand what the OneIdentiy attribute does.  The cells below show how this attribute can effect pattern matching.  OneIdentity can have an effect related to the use of optional arguments and another effect on functions that also have the Flat attribute.  I was only able to understanding many of the subtle ways that OneIdentity works after getting Technical Support from Dave Withoff.

OneIdentity and optional arguments

In the line below we see Default[Power,2] evaluates to (1) and Power has the attribute OneIdentity.

?? Power

Since Default[Power,2] evaluates to (1), the pattern matcher treats (x) as Power[x,1] when matched with the pattern ( Power[x_,n_.] ).  Using  (n_.)  as the exponent in power means the exponent is optional, and the Default should be used when an exponent is not explicitly present.  This causes the definition in the next cell to work on ( integral[x,x] ).  In addition to having a value defined for Default[Power,2], the symbol Power has to have the OneIdentity attribute or the example in the next cell would not work.  Further details of default values are included in the discussion on Default.

ClearAll[integral,  x];
integral[x_^(n_. ), 
          x_] /; (FreeQ[n, x] && 
            n =!= -1) := x^(n + 1)/(n + 
            1) 
{integral[x, x], integral[x^3, x]}

In the next output cell we see the rule for integral[x_^(n_.), x_]  isn't used for integral[x,x]  when Power doesn't have the OneIdentity attribute.

Unprotect[Power];
ClearAttributes[Power, OneIdentity]
ClearAll[integral]
integral[x_^(n_. ), 
          x_] /; (FreeQ[n, x] && 
            n =!= -1) := x^(n + 1)/(n + 
            1)  
{integral[x, x], integral[x^3, x]}

To illustrate how Default[expr]  is used, the attribute OneIdentity is returned to Power, and  the value of Default[Power,2] is changed.  Then the pattern matcher treats integral[x, x] as integral[Power[x, junk], x].

SetAttributes[Power,  OneIdentity]
Default[Power, 2] = junk;
ClearAll[integral, junk];
integral[x_^(n_. ), 
          x_] /; (FreeQ[n, x] && 
            n =!= -1) := x^(n + 1)/(n + 
            1) 
{integral[x, x], integral[x^3, x]}

Next the value of  Default[Power,2]  is returned to normal, and Power is protected again.

Default[Power, 2] = 1;
Protect[Power];
?? Power

There are only three functions in the Mathematica kernel (Version 4.2 or earlier) that have built-in values for Default[symb, ___].  The functions are Times, Plus, and Power with the default values ( Default[Times]=1,  Default[Plus]=0,  Default[Power,2]=1 ).

The cells above demonstrated the way OneIdentity and the value assigned to Default[Power,2] affects pattern matching.  A similar demonstration is given below on a generic function (f).  In the next cell (f) has no attributes.  Here 5 doesn't match the pattern f[a_,b_.] and the rule isn't used.

ClearAll[f, g]
Default[f] = 0;
5 /. f[a_, b_. ] :> g[a, b]

Next the same thing is done when (f) has the attribute OneIdentity.  The OneIdentity attribute permits the pattern matcher to treat 5 as f[5,0].  A zero was used as the second argument of (f) because the second argument in the pattern is optional and (f) has a default value of zero.  Once 5 is treated as f[5,0] the rule can be used.

Attributes[f] = {OneIdentity};
Default[f] = 0;
5 /. f[a_, b_. ] :> g[a, b]

In the next cell Default[f] has no assigned value, but 5 still matches the pattern f[a_,b_:t].   Here again the second argument of (f) is optional.  Since (f) has the OneIdentity attribute the pattern matcher treats 5 as f[5,t] and the rule is applied.  The rule wouldn't be applied if (f) didn't have the OneIdentity attribute.

ClearAll[f, g, t];
SetAttributes[f, OneIdentity];
5 /. f[a_, b_:  t] :> g[a, b]

OneIdentity and the Flat attribute

OneIdentity also has an effect (independent of optional arguments) for symbols that have the attributes Flat and OneIdentity.  Recall the effect the Flat attribute has on pattern matching.  In the line below (f) has the Flat attribute and no other attributes.  For the given expression (f) has more than two arguments so the rule can't be applied as entered.  However the pattern matcher knows (f) is Flat, and it treats f[a1,a2,a3,a4] as the equivalent expression f[f[a1],f[a2,a3,a4]] in which case the outer (f) has two arguments.  The rule can then be applied and we are left with {f[a1],f[a2,a3,a4]}.

ClearAll["Global`*"];
Attributes[f] = {Flat};
f[a1, a2, a3, a4] /. f[p_, q_] :> {p, q}

In the next example (f) has the attributes Flat and OneIdentity.  The OneIdentity attribute prevents the pattern matcher from wrapping (f) around a single argument.  As a result the pattern matcher treats f[a1,a2,a3,a4] as f[a1,f[a2,a3,a4]] and we are left with {a1,f[a2,a3,a4]} after the rule is applied.  Notice how this differs from the previous result.

ClearAll[f]
Attributes[f] = {Flat, OneIdentity};
f[a1, a2, a3, a4] /. f[p_, q_] :> {p, q}

In the last example the rule f[p_,q_]:>{p,q} was used with ReplaceAll (expr/.rule).  If ReplaceRepeated (expr//.rule) is used the rule is applied as many times as possible. You can try it if you like.

In the next output we see Join has the attributes Flat and OneIdentity, and the previous demonstration is repeated using Join instead of (f).

?? Join

Since Join has the attributes Flat and OneIdentity the pattern matcher treats Join[a1,a2,a3,a4] as Join[a1,Join[a2,a3,a4]], and the rule is used.  Here use of HoldPattern is required or Join[a_,b_] would evaluate to Pattern[a,_,b,_] before pattern matching is finished.

Join[a1, a2, a3, a4] /. HoldPattern[Join[a_, b_]] :> {a, b}

In the next cell Join doesn't have the OneIdentity attribute, and the pattern matcher treats Join[a1,a2,a3,a4] as Join[Join[a1],Join[a2,a3,a4]], and the rule is used.

ClearAttributes[Join, OneIdentity];
Join[a1, a2, a3, a4] /. HoldPattern[Join[a_, b_]] :> {a, b}

Before continuing we should restore the OneIdentity attribute to Join.

SetAttributes[Join, OneIdentity]

In the cells below we look at other examples where a function (f) has the attributes Flat, OneIdentity. Consider the next cell where (f) still has the attributes Flat and OneIdentity.  In this case f[a1, a2, a3, a4] evaluates to {a1, f[a2, a3, a4]}, and f[a2, a3, a4] evaluates to {a2, f[a3, a4]}.  Then f[a3, a4] evaluates to {a3, a4}.  When evaluation is complete we have {a1, {a2, {a3, a4}}}.

ClearAll[f]
Attributes[f] = {Flat, OneIdentity};
f[p_, q_] := {p, q}
f[a1, a2, a3, a4]

The next input is the same as the last input except (f) does not have the OneIdentity attribute.  In this case the pattern matcher treats f[a1, a2, a3, a4] as f[f[a1], f[a2, a3, a4]] which evaluates to {f[a1], f[a2, a3, a4]}.  Then the pattern matcher treats f[a2, a3, a4] as f[f[a2], f[a3, a4]] which evaluates to {f[a1], f[a3, a4]}.  Then the pattern matcher treats f[a3, a4] as f[f[a3], f[a4]] which evaluates to {f[a3], f[a4]}.  When evaluation is complete we have {f[a1],  {f[a2],  {f[a3], f[a4]}}}. Notice how this differs from the previous result.

ClearAll[f]
Attributes[f] = {Flat};
f[p_, q_] := {p, q}
f[a1, a2, a3, a4]

In the next cell f[a,b,2,c,d] is treated as f[f[a],f[b,2,c,d]] and other variations such as f[f[a,b],f[2,c,d]], but none have two arguments under (f) where the first argument has the head Integer.  The pattern matcher is unable to find a pattern that matches so the rule is not used.

ClearAll[f]
Attributes[f] = {Flat};
f[x_Integer, y_] := {x, y}
f[a, b, 2, c, d]

Below the very same thing is done except (f) now has the attributes Flat and OneIdentity.  In this case the pattern matcher treats f[a,b,2,c,d]  as  f[a,b,f[2,f[c,d]]]  in which case f[2,f[c,d]] matches the required pattern.  Then f[2,f[c,d]] evaluates to {2,f[c,d]} and f[c,d] doesn't evaluate further because the first argument isn't an Integer. We then have f[a,b,{2,f[c,d]}].  The outer (f) doesn't evaluate further because the pattern matcher can't make it match the required pattern.

ClearAll[f]
Attributes[f] = {Flat, OneIdentity};
f[x_Integer, y_] := {x, y}
f[a, b, 2, c, d]

For the input below the condition was evaluated for different ways of nesting layers of (f).  Every possible subexpression matching the pattern was tried in the search for a subexpression that matches the pattern f[x_,y_] and makes the condition True.  The condition (a print statement) never evaluated to True so the rule was not applied.  While searching for a way to make the condition True, the expression f[a1, a2, a3, a4] was treated as (in this order):
   f[f[a1],f[a2,a3,a4]],
   f[f[a1,a2],f[a3,a4]],
   f[f[a1,a2,a3],f[a4]],
   f[f[f[a1],f[a2,a3]],f[a4]],
   f[f[f[a1,a2],f[a3]],f[a4]],
   f[f[a1],f[f[a2],f[a3,a4]]],
   f[f[f[a1],f[a2]],f[a3,a4]],
   f[f[a1],f[f[a2],f[a3]],f[a4]],
   f[f[a1,a2],f[f[a3],f[a4]]]

ClearAll[f]
Attributes[f] = {Flat};
f[a1, a2, a3, a4] /. 
      f[x_, y_] :> 
        1 /; Print[HoldForm[x], ",  ", HoldForm[y]]

Optional (x_^n_.), (x_+y_.), (x_*y_.)

Optional (_.)  is used to indicate that a default value should be used of a value isn't provided.  The feature can only be used with heads that have assigned values for either Default[h], Default[h, n], or Default[h, n, i].  The only heads that have built-in default values are Plus, Times, Power, and they are each demonstrated below.

Example using (x_^n_.)

In the first example (f) tells us the base and exponent of (s^n).

Clear[f, s]
f[x_^n_. ] := 
      StringJoin["base =", ToString[x], ",  exponent =", 
        ToString[n]]
f[s^2]

The next cell (s) matches the pattern (x_^n_.) and the pattern matcher uses the default exponent of (1).  The use of (n_.) instead of (n_) indicates that the default value should be used if one is not provided explicitly.

f[s]

Example using (x_+y_.)

In the next example (s) matches the pattern (x_+y_.) and the pattern matcher uses the default value of (0) for (y_.) when only a single term is provided.  Because Plus has the Flat attribute the pattern matcher recognizez that (s1+s2+s3+s4)  is a case of  Plus[s1, Plus[s2,s3,s4] ].

Clear["Global`*"];
f[x_ + y_. ] := Add[x, y]
{f[s], f[s1 + s2], f[s1 + s2 + s3 + s4]}

Next we see that the rule is stored as a definition for  f[ y_. + x_ ]  instead of a definition for  f[ x_ + y_. ]  as entered.  I suppose that explains why (y) is matched with (s1) in (s1+s2+s3+s4).

?? f

Example using (x_*y_.)

In the next example (s) matches the pattern (x_*y_.) and the pattern matcher uses the default value of (1) for (y_.) when only a single factor is provided.  Because Times has the Flat attribute the pattern matcher recognizez that (s1  s2  s3  s4)  is a case of  Times[s1, Times[s2, s3, s4] ].

Clear["Global`*"];
f[x_*y_. ] := Multiply[x, y]
{f[s], f[s1  s2], f[s1  s2  s3  s4]}

Next we see that the rule is stored as a definition for  f[ (y_.)  x_ ]  instead of a definition for  f[ x_ (y_.) ]  as entered.  I suppose that explains why (y) is matched with (s1) in 
(s1  s2  s3  s4).

?? f

Other applications (rarely used)

You can also use arguments like (y_.) as arguments for functions other than Power, Plus, Times, but only if you first assign values via Default[f], Default[f,i] or Default[f,i,n].  To see examples of this goto the section on Default.

Clear[f];
Default[f] = z;
f[x_, y_. ] := {x, y}

In the next cell the default value (z) is used when (f) is give a single argument.

f[a, b]
f[5]

Options, OptionQ

OptionQ details

OptionQ isn't documented in The Mathematica Book or the Help Browser, but it's helpful when defining a function with options.  OptionQ does however have the usage message shown below.

?OptionQ

In the cells below we see OptionQ is True when it's given a Rule, a RuleDelayed or a list of these things. You get an error message if you give OptionQ more than one argument.

ClearAll["Global`*"];
OptionQ[opt1 -> True]
OptionQ[opt2 :> var2]
OptionQ[{opt1 -> 15, opt2 -> Automatic, 
        opt3 :> False, opt4 -> All}]

OptionQ also returns True when it's given nested lists of options.

OptionQ[{{opt1 -> 15, {opt2 -> 10}, 
          opt3 :> False, opt4 -> All}}]

As indicated in the usage message OptionQ returns False when it's given a single argument with any other form.  This is seen in the next cell.

OptionQ[2]
OptionQ[h[2]]
OptionQ[{Opt1 -> All, 2}]
OptionQ[{Opt1 -> All, h[2]}]
OptionQ[h[Opt1 -> All]]

Typical use of OptionQ

The next cell defines a function that takes options.  This function takes one required argument and optional arguments are allowed as long as they all pass OptionQ.  The form (opts___?OptionQ) is perhaps the most frequent use of BlankNullSequence ( ___  ie. "Tripple Blank").  Notice this allows for a single option, a sequence of options, or no options.

foo[expr_, opts___?OptionQ] := {expr, h @@ Flatten[{opts}]}

Each usage of foo in the next cell matches the definition of foo above.

foo[x + y]
foo[x + y, opt1 -> 15, opt2 -> Automatic, 
      opt3 -> False]
foo[
      x + y, {opt1 -> 15, opt2 -> Automatic, 
        opt3 -> False}]
foo[x + y, {opt1 -> 15, {opt2 -> Automatic}}, 
      opt3 -> False]

The definition of foo doesn't apply to the next example because 6  isn't an option.

foo[x + y, opt1 -> True, 6, opt2 -> False]

Getting the setting of Options

Section  2.3.10 of The Mathematica Book suggests using the form 
    name/.{opts}/.Options[f]
to get the setting of an option.  This technique is used below to get the PlotStyle and Axes settings for a function.

ClearAll[PlotFunction, x];
Options[PlotFunction] = {PlotStyle -> GrayLevel[0], 
          ImageSize -> Automatic};

    
PlotFunction[expr_, opts___?OptionQ] := 
      Module[{style, 
          size}, 
{style, 
            size} = {PlotStyle, ImageSize} /. {opts} /. 
            Options[PlotFunction]; 
MakeGraphic[
          expr, "PlotStyle=" <> ToString[style], "ImageSize=" <> 
            ToString[size]]
]

In the next cell PlotFunction performs as expected when given no options, one option, or two options.

PlotFunction[x^2]
PlotFunction[x^2, PlotStyle -> Thickness[0.03]]
PlotFunction[x^2, PlotStyle -> Thickness[0.03], 
      ImageSize :> {xx, yy}]

In the next cell Plot alows us to provide a list of options rather than a sequence of options.  For consistency we would like our PlotFunction to allow us to give a list of options as well.

Plot[
        Sin[x], {x, 0, 12}, {PlotStyle -> Thickness[0.03], 
          ImageSize -> {400, 250}}];

Unfortunately our PlotFunction defined above has a problem when a list of option is given in the next cell.

PlotFunction[
      x^2, {PlotStyle -> Thickness[0.03], 
        ImageSize -> {400, 250}}]

The way to avoid the problem in the last exaple is to use a different method for getting the option settings.  Instead the following form should normally be used to get option settings.
	{var1,var2,var3}={opt1,opt2,opt3}/.Flatten[{opts,Options[f]}]
This more robust method is used in the next cell.

ClearAll[PlotFunction];
Options[PlotFunction] = {PlotStyle -> GrayLevel[0], 
          ImageSize -> Automatic, Ticks -> Automatic, 
          Axes -> True};

PlotFunction[expr_, opts___?OptionQ] := 
      Module[{style, 
          size}, 
{style, size} = {PlotStyle, ImageSize} /. 
            Flatten[{opts, 
                Options[PlotFunction]}]; 
MakeGraphic[
          expr, "PlotStyle=" <> ToString[style], "ImageSize=" <> 
            ToString[size]]
]

The next cell demonstrates that the new version of PlotFunction can take any way of expressing options that passes the OptionQ test.

PlotFunction[x^2]
PlotFunction[x^2, PlotStyle -> Thickness[0.03]]
PlotFunction[x^2, PlotStyle -> Thickness[0.03], 
      ImageSize :> {xx, yy}]
PlotFunction[
      x^2, {PlotStyle -> Thickness[0.03], 
        ImageSize -> {400, 250}}]
PlotFunction[
      x^2,  {PlotStyle -> GrayLevel[0], {{ImageSize -> Automatic}, 
          Ticks -> Automatic}, Axes -> True}]

Ordering

The Ordering usage message is shown below.

?Ordering

The next cell gives us the ordering of a list.

lst = {-2, 4, -5, Sin[2], 4, π, -∞, 
          4, ∞};
ord = Ordering[lst]

The next cell shows how we can take the results of Ordering and get a sorted list.  When Ordering is given one or two arguments we get the ordering of a cononical sort which may not be the ordering of a numerically sorted list.

Part[lst, ord]
Sort[lst]

Ordering[lst,4]  gives the first four elements of Ordering[lst].  The second argument of Ordering can be (All) or an Integer meeting the condition (-Length[list] ≤ n ≤ Length[list]).  A few cells after this example we use All as a a second argument for Ordering.

Ordering[lst, 4]

The next cell gives the last four elements of Ordering[lst].

Ordering[lst, -4]

The next cell gives the (2^nd) through the (4^th) elements of  Ordering[lst].

Ordering[lst, {2, 4}]

The output of the next cell returns with the (3^rd) element of Ordering[lst], and ends with the (2^nd) from the last element of Ordering[lst].

Ordering[lst, {3, -2}]

The next cell returns every other element of Ordering[lst] starting with the first element of (lst) and ending with the last element of (lst).

Ordering[lst, {1, -1, 2}]

As mentioned above Ordering normally gives the ordering of a cononical sort.  We can give Ordering a third argument to get the Ordering of another type of sort.  In the next cell we get the ordering of a numeric sort.

ord2 = Ordering[lst, All, Less]

The next cell shows how we can take the results of the last Ordering and get a numerically sorted list.

Part[lst, ord2]
Sort[lst, Less]

In all the examples above Ordering was given a list, but Ordering can work with expressions having any head as shown in the next cell.

Clear[h];
Ordering[
      h[-2, 4, -5, Sin[2], 4, π, -∞, 
        4, ∞]]

Orderless

For the input below (f) has the Orderless attribute which causes (f) to sort  it's arguments into canonical order.

ClearAll["Global`*"];
Attributes[f] = {Orderless};
f[z, x, c, v, b]

In the next cell we see that numeric arguments aren't necessarily sorted in numeric order.  Instead they are sorted into canonical order.

f[π, 4, 3, 2]

Functions with the Orderless attribute sort the arguments before definitions are applied.  That explains why the result of the input below is not {x, z} or {z, x}.

f[a_, b_, c__] := {a, b}
f[z, x, c, v, b]

The Orderless attribute can effect pattern matching in ways many users don't understand.  The situation demonstrated in the cells below is one such example.  The next input clears the definitions and Orderless attribute from (f).  The rule applied in the next cell can only be used when the first argument of (f) has the head Real.  For this example the first argument is an integer so the rule isn't used.

ClearAll[f]
f[5, 4, 3.5, 2, 1.7,  1] /. f[x_Real, y__] -> {x, {y}}

For the next input (f) has the Orderless attribute and same rule is applied again.  This time the arguments are sorted in canonical order before the rule is used.  Then the pattern matcher finds that the rule matches when (x->1.7) and (y-> Sequence[1, 2, 3.5, 4, 5]).  The pattern matcher could have used (x -> 3.5) and the appropriate sequence for (y), but the other match was found first.  Once the pattern matcher found a match it stopped searching.  Notice the arguments used for (y) in the output are sorted in canonical order.

Attributes[f] = {Orderless};
f[5, 4, 3.5, 2, 1.7,  1] /. f[x_Real, y__] -> {x, {y}}

Outer

The first two examples demonstrate the basic use of Outer.

Outer[f, {x1, x2}, {y1, y2, y3}]

Outer[f, {x1, x2}, {y1, y2, y3}, {z1, z2}]

A user asked the MathGroup how to define tables like the one below, but having larger dimensions without writing the whole table explicitly?

Bob Hanlon provided the following solution.

ClearAll["Global`*"];
Outer[#1[#2]  &,  {f1,  f2,  f3,  f4} ,  {x1,  x2,  
          x3}]

The next cell does the same thing, and is based on an example in the Help Browser.

Outer[
        MapAll,  {f1,  f2,  f3,  f4} ,  {x1,  x2,  x3}]

The next cell shows what we get for the outer product of two matrices.

Outer[f, {{a11, a12}, {a21, a22}}, {{b11, b12}, {b21, b22}}] // 
      MatrixForm

The next cell flattens what we get from the computing Outer with 4 lists.

Flatten[
      Outer[h, {a1, a2, a3}, {b1, b2, b3}, {c1, c2}, {d1, d2}]]

In the next two examples we provide integers as additional arguments.

Outer[f, {{a11, a12}, {a21, a22}}, {{b11, b12}, {b21, b22}}, 2, 1] // 
      MatrixForm

Outer[f, {{a11, a12}, {a21, a22}}, {{b11, b12}, {b21, b22}}, 1, 2] // 
      MatrixForm

Part

Part[expr,0] is always the same as Head[expr], but Head is a bit faster since it doesn't need to determine what part.  For example Part[1+π, 0] returns Plus.

Part[1 + π, 0]

In the next cell we see the part at position (2,0) of the list {1,1+π} is Plus.

Part[{1, 1 + π}, 2, 0]

In the next cell a matrix (m) is made and used in some examples below. Of course the method demonstrated in the cells that follow can be used on any expression not only matrices.

Clear["Global`*"];
m = {{x11, x12, x13, x14, x15}, {x21, x22, x23, x24, x25}, {x31, 
            x32, x33, x34, x35}, {x41, x42, x43, x44, x45}, {x51, x52, x53, 
            x54, x55}};

    
MatrixForm[m]

Part has a powerful feature that many users aren't aware of.  The expression m[[list1,list2]] returns the sub-matrix of (m) formed by the intersection of the rows given by list1 and the columns given by list2. The next cell gives an example. This is probably the quickest way to get such a submatrix of (m).

m[[{1, 3, 4}, {2, 5}]]  // MatrixForm

m[[n,list2]] returns a list of elements at positions (list2) in row n. The next cell gives an example.

m[[2, {1, 2, 5}]]

Likewise m[[list1,n]] returns a list of elements at positions (list1) of column n. The next cell gives an example.

m[[{1, 2, 5}, 2]]

Use of the forms demonstrated above gives the fastest way to change multiple parts of an expression provided the parts can be reached with this method. Rob Knapp makes this point in a tutorial on Packed Arrays at   http://library.wolfram.com/database/TechNotes/391/.  In that tutorial he shows how this method gives a significant speed advantage in an implementation of LUDecomposition. This method of changing multiple values is demonstrated on simple examples in the cells below where elements of the matrix (m) from above are changed.

Notice the use of All inside Part is not available in Version 3 or earlier.

m[[All, 3]] = {a1, a2, a3, a4, a5};
MatrixForm[m]

m[[2, All]] = {b1, b2, b3, b4, b5};
MatrixForm[m]

In the next cell All is used to access the first, second and fourth columns of matrix (m).

Part[m, All, {1, 2, 4}] // MatrixForm

In the next cell we change elements of (m) where rows (1,3) intersect with columns (2,3,5).

m[[{1, 3}, {2, 3, 5}]] = {{jjj1, jjj2, jjj3}, {kkk1, kkk2, 
            kkk3}};
MatrixForm[m]

Partition

Lots of users are not familiar with the Partition function.  Partition is well documented under Built-in Functions in the Help Browser.  The usage message for this powerful feature is shown below.

?Partition

A simple demonstration of Partition is given in the next cell.  If the third argument is not provided the result will be the same as if the a third argument equal to the second was provided.

lst = Range[50];
Partition[lst, 4]

The effect of the third argument is demonstrated in the next two examples..

Partition[lst, 4, 3]

Partition[lst, 4, 10]

Next we give Partition a fourth argument of 3.  Partition then treats the list as if it were periodic and ensures the first element of (lst) is the third element of the first sublist.

Partition[lst, 5, 5, 3]

Next the fourth argument of Partition is 7 and we get a partition where the first element of (lst) would be the seventh element of the result if it were flattened.

Partition[lst, 5, 5, 7]

Partition can work in higher dimensions. In the next example Partition seperates a matrix into blocks.

Clear["Global`*"];
mtrx = {
{w1, w2, w3, w4, w5, 
            w6}, 
{x1, x2, x3, x4, x5, 
            x6}, 
{y1, y2, y3, y4, y5, 
            y6}, 
{z1, z2, z3, z4, z5, 
            z6}
};

Partition[mtrx, {2, 2}, {2, 2}] // MatrixForm

The next example demonstrates that Partition can work on expressions that don't have the head list.

ClearAll[f];
flst = f @@ Range[1, 20]

Partition[flst, 3]

I haven't been able to find ways to clearly demonstrate use of the Partition features listed below.  Each of these features are in the Partition usage message.

•  Partition[list, n, d, {kL, kR}] specifies that the first element of list should appear at position kL in the first sublist, and the last element of list should appear at or after position kR in the last sublist.

•  Partition[list, n, d, {kL, kR}, x] pads if necessary by repeating the element x. 

•  Partition[list, n, d, {kL, kR}, {x1, x2, ... }] pads if necessary by cyclically repeating the elements xi.

•  Partition[list, n, d, {kL, kR}, {}] uses no padding, and so can yield sublists of different lengths. 

•  Partition[list, nlist, dlist, {klistL, klistR}, padlist] specifies alignments and padding in a nested list.

Position

Position is used to determine the where a certain pattern can be found in an expression.  In the next cell Position returns a list of all the position of all parts of demo that match the pattern (_Plus).  The empty list at the end of the output list indicates that (demo) itself matches the pattern Position is looking for.

Clear[x];
demo = 
        x + (1/(x - 1)) + Cos[((π (Sqrt[ 2 + x/3]))/3)] + 
          Sin[((π (Sqrt[ f])[x + y + 1])/6)];
posn = Position[demo, _Plus]

In the next cell we get the subexpression at three of the positions listed in the previous result.  Notice each of the sub-expressions is a sum.  The last element of the list returned by Position is {}, which indicates that demo itself matches the pattern (_Plus).

Part[demo, 1, 1]
Part[demo, 3, 1, 3, 1]
Part[demo, 4, 1, 3, 1, 1]

In the next cell we pass the list Position returned and get a list of all the parts of (demo) that match the pattern.  However, if you have no other need for the positions, it would be more direct to get the same result using Cases[demo,_Plus,{0,∞}].  The results returned by Position can also be used as arguments of MapAt, and ReplacePart.

Extract[demo, posn]

In the next cell only the the whole expression matches the pattern and Position indicates this by returning {{}}.

Position[h[1, 2, 3], _h]

When Position finds no parts that match the pattern an empty list is returned as in the next cell.

Position[x + y, _h]

I don't provide further examples, but any combination of pattern matching constructs can be used with Position.  Nuances of pattern matching are discussed in another section.

Position[ expr, pattn, levspec ]

Position will take a level specification and all the variations I discuss under Level Specification are supported.  The next cell repeats an earlier example and returns all positions of demo with the head Plus.

Position[demo, _Plus]

The next cell uses level specification 4 and we get all sub-expressions at levels 1 through 4 with the head Plus.

Position[demo, _Plus, 4]

The next cell uses level specification {4} which means to only look at level 4.

Position[demo, _Plus, {4}]

The next cell uses level specification {0,4} which means to look at levels 0 through 4. Level 0 is the entire (demo) expression.

Position[demo, _Plus, {0, 4}]

The default level specification for Position is {0,∞} as in the next cell.

Position[demo, _Plus, {0, ∞}]

Position[ expr, pattn, levspec, n]

In the next cell we get the position of all integers in (data).

data = {1.3, {1.3, 10}, 1.3, 11, {{1.3, 1.3, 12, 13}}, 14, 15, 1.3, 
          16, 1.3, 17, 18, 19, 1.3};
posn = Position[data, _Integer]

Next we can use Extract to get the integers at the above positions.

Extract[data, posn]

We can give position a positive integer as a  (4^th) argument to get only the first (n) positions.  In the next cell we get the position of the first 4 integers.  Here I am specifying {0,∞} as the level specification.

posn = Position[data, _Integer, {0, ∞}, 4]

Next we ask for the position of the first 50 integers.  There are only 10 integers to be found, so we are given all 10 positions.

posn = Position[data, _Integer, {0, ∞}, 50]

Heads option

In the next cell Position tells us where the head Plus can be found.  Plus is at position 0 (the head of demo).  The head Plus is also at positions {1, 1, 0}, {3, 1, 3, 1, 0} and {4, 1, 3, 1, 1, 0} which are the heads of sub-expressions at positions {1, 1}, {3, 1, 3, 1}, {4, 1, 3, 1, 1}.

Position[demo, Plus]

Position has an option Heads with the default setting (Heads->True). If Position is used with the option setting (Heads->False) it doesn't look at the heads of an expression or its sub-expressions.  Hence when the last example is repeated with the setting (Heads->False) an empty list is returned.

Position[demo, Plus, Heads -> False]

In the next cell we get the only position of (2*f[x]+3*f'[x]) where the integer 1 can be found.  The only place in this example where the integer 1 can be found is in the order of the derivative.

ClearAll[f];
Position[2*f[x] + 3*f'[x], 1, Heads -> True]

The next cell repeats the previous example with the setting (Heads->False) and an empty list is returned.  An empty list is returned because the pattern matcher would have to look in the head of   f'[x]  to find the integer 1.

Position[2*f[x] + 3*f'[x], 1, Heads -> False]

Also read  "Further Examples" at the end of the  Position  documentation in the Help Browser.

Pattern  (x:pattn)

Mathematica lets us use (x_Foo) to say that something with the head Foo will be called (x).  It also lets us use (x_?Test)  to say something for which Test returns True will be called (x).  Now if we want to say that something matching a certain pattern is called (x) we use (x : pattn).

The next cell gives an example where use of Pattern is helpful.  Here (ff) is only defined when it's argument is an Integer raised to a power.  In addition the exponent must have the head Plus.

ff[x : (_Integer^_Plus)] := Thread[x, Plus]

In the example below we get the result of evaluating  Thread[(2^(3 x - 5  y)),  Plus].

ff[(2^(3  x - 5  y))]

Next we see (ff) is undefined when it's argument isn't an Integer raised to a power where the exponent has the head Plus.

ff[(2^(3  x))]

ff[(x^(3  x - 5  y))]

Pattern Test (x_?Positive), (y_Real?Positive)

PatternTest is closely related to Condition.  PatternTest is used to specify that a certain pattern must meet a certain condition.  So for example in the next cell (f) is defined when given an argument that is a positive number.

Clear[f, s]
f[x_?Positive] := x + 1
{f[2], f[8.3], f[π/2], f[(Sqrt[-2])]}

In the next example (f) is only defined when given an argument that is a positive integer.

Clear[f, s]
f[x_Integer?Positive] := x + 1
{f[2], f[8.3], f[π/2], f[(Sqrt[-2])]}

In the next example (f) is defined when given an argument that is a positive integer or a positive real number.

Clear[f, s]
f[x : ((_Integer | _Real)?Positive)] := 
      x + 1
{f[2], f[8.3], f[π/2], f[(Sqrt[-2])]}

In some cases we want to use a test that isn't a built-in unary operator (Positive, NumericQ, AtomQ, ...).  In that case we can use a pure function as in the next example.  In the next example (f) is only defined when given an argument that is an integer between 0 and 10.

Clear[f, s]
f[x_Integer?(0 < # < 10 &)] := x + 1
{f[2], f[25], f[8.3], f[π/2], f[(Sqrt[-2])]}

In the next cell (f) is only defined when given an argument of any type between 0 and 10.  Can you see why we need the part about (Im[#]===0) ?

Clear[f, s]
f[x_?(Im[#] === 0 && 0 < # < 10 &)] := 
      x + 1
{f[2], f[25], f[8.3], f[π/2], f[(Sqrt[-2])]}

We can't use pattern variables in the test portion of (Pattn/;Test), and that is why the definition in the next cell isn't used.

ClearAll[foo];
foo[x_?(0 < x < 1)] := x^2
foo[0.4]

Random

On 5 May 2000 Daniel Lichtblau of Wolfram Research reported at    
http://forums.wolfram.com/mathgroup/archive/2000/May/msg00088.html
that the numbers produced using to produce rando approximate machine numbers are not completely random.  Further more at 
http://forums.wolfram.com/mathgroup/archive/2004/May/msg00002.html 
Andrzej Kozlowski reported that this is still a problem in Mathematica 5.0. 
Daniel Lichtblau suggests that instead of using  Random[]  to generate random real machine numbers the following should be used.  I believe this will give a random number between 1 and 2.

Random[Integer, 2^30 - 1]/2.0^30 + 
      Random[Integer, 2^30 - 1]/2.0^30

However, based on Daniel's explanation of the problem, I expect that the following also works around the bug in Random, and runs twice as fast as the solution above.

N[Random[Real, {0, 1}, 17]]

In the above message Daniel Lichtblau also explained how one can make necessary changes to some of the standard packages.

Rationalize

?Rationalize

The line below finds a rational number that is very close to the machine number approximation of Pi.

est1 = Rationalize[N[π],  0]

Rationalize[N[π],0.0]  gives the same number as the line above.

N[π - est1,  17]

The next line gives a rational approximation of π that is remarkably close to the actual value of π.

est = Rationalize[π,  0.0]

\(Block[{$MaxExtraPrecision = 800}, 
N[π - est, \ 17]
]\)

If you want to quickly convert a Real to a Rational and you don't care about large integers in the numerator and denominator the next line should be used. It's about 26 times faster than Rationalize!

SetPrecision[14.2, ∞]

Range

In the cells that follow we see Range can be used in a lot of cases where Table is often used.  One difference  is that Range will normally make a list in about half the time Table would need to make the same list.

Range doesn't  have to count over integers.  In the next line Range starts at 0 and counts in steps of 1/20  up to the last value less than (π/3).  Notice (1<π/3), but (21/20>π/3), sot the list stops at 1.

Range[0, π/3, 1/20]

The next example is similar to the previous example.

Range[0, 1, π/20]

In the same line where Range makes a list, a listable function such as Times or Power can be applied to the list as in the next line. The same list can be made using Table, but Range is almost always faster.  If the function being applied to each element of the list isn't listable, you can map it over the list, and it will work about as fast as if it was listable.  In the two following examples Power is applied to the list returned by Range.

Clear[x, n, a];
x^Range[n, n + 10, 2]

The next line give an interesting example.

Range[a, a + 12  n,  2  n]

In the next line Range is used to make a series expansion for Cos[x].  Timing experiments show that this is much faster than using Series, but the method below is fairly cryptic.  Also creating the series using Range requires knowledge of the general form of each term, while Series doesn't need this information.  The fact that Series must determine the coefficients for each term is probably the reason why it takes much longer.  The #& notation is explained in the discussion of Function.

Plus @@ (-(-1)^(#) 
              x^(2  # - 2)/(2  # - 2)! &[
          Range[7]])

Repeated (pattn ..), RepeatedNull (pattn ...)

Repeated (pattn ..) is used to represent a sequence of arguments that match a certain pattern.  For example in the next cell (f) is defined when given a sequence of one or more arguments that have the head Real or Integer.  When Repeated (..) is used the definition applies when some of the arguments are Real and others are Integer.

Clear[f, s]
f[x : (_Real | _Integer) .. ] := 
      Join[{x}, {∞}]
{f[2], f[2.4], f[2, 4.3], f[], f[2, s]}

In the previous example (f) was only defined when given one or more arguments that are Real or Integer.  NullRepeated (...) represents a sequence multiple, one, or an empty set of arguments matching a pattern.  So in the next example (f) is defined when given no arguments.

Clear[f, s]
f[x : (_Real | _Integer) ... ] := 
      Join[{x}, {∞}]
{f[2], f[2.4], f[2, 4.3], f[], f[2, s]}

Replace

The Replace function is greatly under appriciated because the Wolfram Research documentation gives very little discussion of what it can do. After reading the documentation on Replace you get the impression that it tries to use replacement rules on an entire expression and nothing more. 

The cell below demonstrates this basic use of Replace.  In this case the expression (demo) matches the form for the replacement rule. The rule is used once and that is the end of it.

Clear[a];
demo = {0, {1, {2, {3, {4, {5, {6, {7, 7}}}}}}}};
Replace[demo, {x_, y__} :> {x, a, y}]

The basic use of Replace isn't very useful. The power of Replace lies in use of it's third argument where a level specification is given. In the next cell the replacement is used at level 1 down to the atoms and all levels between.

Replace[demo, {x_, y__} :> {x, a, y}, -1]

In the next case the replacement is only performed at level 3.

Replace[demo, {x_, y__} :> {x, a, y}, {3}]

The next cell uses the replacement on levels 2, 3, and 4.  As explained below the replacements start at the deepest level (level 4 in this case) and works up to the higher levels.

Replace[demo, {x_, y__} :> {x, a, y}, {2, 4}]

When the third argument of Replace is  -6  the replacement start with expressions with a depth of 6 and works up to level 1.

Replace[demo, {x_, y__} :> {x, a, y}, -6]

When the third argument of Replace is {-7,4} the replacement start at level 4 and work up to expressions with a depth of 7.

Replace[
      demo, {x_, y__} :> {x, a, y}, {-7, 4}]

We can give Replace a list of replacement rules as in the next cell.  We could use (lhs:>rhs) instead of (lhs->rhs) and the difference between the two is discussed in the section on Rule instead of RuleDelayed.

Replace[
      w + x + y + z, {x -> 2  x, 
        y -> 3  y}, {0, ∞}]

Once Replace changes a part of an expression no further replacements are made on that portion of the expression or any of it's subparts.  For example the rule in the next cell is only used once on each logrithm.  Normally ReplaceRepeated would be used to fully expand the logrithms, but you could use FixedPoint and Replace to fully expand these logrithms (I won't do that here).

I don't provide further examples, but any combination of pattern matching constructs can be used on the left side of rules given to Replace.  Nuances of pattern matching are discussed in another section.

Replace[Log[d p q r] Log[a b c d],  
      Log[a_  b_] :> 
        Log[a] + Log[b], {0, ∞}]

The order of matching patterns and making replacements

I learned from Allan Hayes that Replace attempts replacements starting at the deepest level allowed by the level specification and works its way up to higher levels. Allan also explained that Replace doesn't evaluate the expression between replacements. Replace doesn't have a holding attribute, so it's arguments evaluate before doing replacements. The demonstration and explanation below is based on an example Allan Hayes once provided.

Clear["Global`*"];
rls = {a1 + b2 -> baab, b1 + b2 -> b12, 
          a1 -> b1};
a2 = b2;
h[b1 + b2] = bb;

Replace[h[a1 + a2], rls, {1, 2}]

The important parts of evaluation above are as follows:
The first argument of Replace evaluates as  h[a1+a2]⟶h[a1+b2]  since (a2=b2).
Next Replace starts replacements at the deepest level, so the first replacement used on  h[a1+b2] is (a1->b1) resulting in  h[b1+b2].  No evaluation is conducted until Replacements are finished.  Moving up a level the replacement (b1+b2⟶b12) is used and resuts in  h[b12] which is the final expression.  Notice we had h[b1+b2] after the first replacement, but the definition for h[b1+b2] wasn't used.  Once replacements started nothing evaluated until all replacements were finished.

ReplaceAll and ReplaceRepeated make replacements starting at the top level and work down, but Replace is different since it starts replacements at the deeper levels and works it's way up.

The next input shows how you can easily see the order that Replace tries replacements on different parts of an expression.

Replace[
      h[f1[a1], f2[e][a2]], (a_ /; Print[a] :> 
          0), {0, ∞}]

Replace makes replacements starting at the deepest level, so in the next cell Log[x] is changed to Log[a x].

Replace[(Sqrt[x]) + 2  (x^3) - Log[x]/y, {x -> a x, 
        Log[x] -> Log[x + 1]}, {0, ∞}]

Since ReplaceAll starts making replacements at the highest level, Log[x] in the next cell is changed to Log[x+1].

(Sqrt[x]) + 2  (x^3) - Log[x]/y /. {x -> a x, 
        Log[x] -> Log[x + 1]}

Heads Option

Replace has a 'Heads' option which controls whether it consideres making replacements on the heads of expressions.  The default setting is (Heads->False).  The next cell shows the order that different parts of the expression are considered for replacement when replacing heads is allowed.

Replace[
      h[f1[a1], f2[e][a2]], (a_ /; Print[a] :> 
          0), {0, ∞}, Heads -> True]

ReplaceAll, ReplaceRepeated

The next cell demonstrates the basic use of ReplaceAll.

Clear[x, y];
expr = y + Sin[x];
expr /. y -> 4

Evaluation of the previous cell doesn't change the value of (y) or (expr).

{y, expr}

If we provide a list of rules or delayed rules on the right side of ReplaceAll several replacements are made.  The difference between (lhs->rhs) and (lhs:>rhs) is covered in the section on Rule instead of RuleDelayed.

expr /. {x -> (π/4), y -> 5}

ReplaceAll considers a list of rules starting from the left.  In the next cell the rule (y->5) is used and the symbol (y) is no longer in the expression.  As a result the rules (y->3) and (y->4) are not used.

expr /. {y -> 5, y -> 3, y -> 4}

If we provide a list of lists of replacement rules on the right side of ReplaceAll we get a list of results from making several sets of replacements.

expr /. {{x -> 1, y -> 2}, {x -> 5, 
          y -> 6}, {x -> 12, y -> 13}}

Any combination of pattern matching constructs can be used on the left side of rules.  The next cell gives a non-trivial example.  Nuances of pattern matching are discussed in another section.

(Sqrt[x]) + 4 (z^2) + (z^3) - Log[x]/(y^2) /. 
      c_. *s_Symbol^n_Integer :> c*n*s^(n - 1)

Once ReplaceAll changes a part of an expression no further replacements are made on that portion of the expression or any of it's subparts.  For example the rule in the next cell is used once on each logrithm.

Log[d p q r] Log[a b c d] /. 
      Log[x_  y_] :> Log[x] + Log[y]

ReplaceRepeated works just like ReplaceAll except it continues trying the rules until the result no longer changes.  In the next cell ReplaceRepeated fully expands each logrithm.

Log[d p q r] Log[a b c d] //. 
      Log[x_ y_] :> Log[x] + Log[y]

The order of matching patterns and making replacements

If we put a Print statment at the right place we can see the order that the pattern matcher considers different parts of an expression for replacement.  Notice the Print statement never returns True, so nothing can match the pattern, but each pattern considered is printed.

Clear["Global`*"];
h[f1[a1], f2[e][a2]] /. (a_ /; Print[a] :> 
          0)

Since ReplaceAll starts making replacements at the highest level, Log[x] in the next cell is changed to Log[x+1].

(Sqrt[x]) + 2  (x^3) - Log[x]/y /. {x -> a x, 
        Log[x] -> Log[x + 1]}

Replace makes replacements starting at the deepest level, so in the next cell Log[x] is changed to Log[a x].

Replace[(Sqrt[x]) + 2  (x^3) - Log[x]/y, {x -> a x, 
        Log[x] -> Log[x + 1]}, {0, ∞}]

In the next cell we can see (expr/.rules) evaluates (expr), then it evaluates (rules), it makes the replacements without evaluating the new results.  A new expression is returned ((2^2) + 7) in this case, and this new expression is allowed to evaluate to 11.

expr = x + y;  
    rules = {x :> (2^2), 
        y :> 7};
Trace[expr /. rules]

In the next cell we can see (expr//.rules) evaluates (expr), then it evaluates (rules).  The first replacement made is  \(TraditionalForm\`Log[e\^5\ a\ b]\ ⟶\((Log[e\^5] + Log[a\ b]\ )\)\)  and Log[(e^5)] evaluates to 5.  Then the replacement rule is used again to change Log[a b] to  (Log[a]+Log[b] ).

expr = Log[(E^5) a b] Log[c d]; 
    rules = {Log[x_  y_] :> 
          Log[x] + Log[y]};
Trace[expr //. rules]

ReplacePart

The ReplacePart function is well documented.  Below I give useful function that uses ReplacePart.

SwapParts[expr_, pos1_, 
        pos2_] := 
ReplacePart[#, #, {pos1, pos2}, {pos2, 
              pos1}] &[expr]

In the next cell I make a matrix and I swap the parts at positions {1,2} and {3,2}.

\(\(\(mtrx = {{11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34}, {41, 
            42, 43, 44}};\)\(
\)
    \)\ \)
SwapParts[mtrx, {1, 2}, {3, 2}]

In the next cell I swap the first row with the third row.

SwapParts[mtrx, {1}, {3}]

Next I demonstrate that SwapParts can work with arbitrary expressions as well as matrices.

Clear[x, y, z];
demo = (2  x - Sqrt[3 + y^2])/(-4 + z^2)

SwapParts[demo, {1, 2}, {2, 1, 2}]

RotateRight, RotateLeft

RotateRight and RotateLeft can be used on an expression that doesn't have the head List.  In the two cells below the arguments of an expression are rotated two places to the right and two places to the left.

ClearAll["Global`*"];
RotateRight[foo[a, b, c, d, e, f, g, h], 2]

RotateRight[foo[a, b, c, d, e, f, g, h], -2]

Normally RotateRight and RotateLeft are used on a simple list and the second argument (if present) is normally an integer.  However, the second argument can be a list of integers.  First consider the tensor (m2) below.

m2 = {
{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, 
              c3}}, 
{{d1, d2, d3}, {e1, e2, e3}, {f1, f2, 
              f3}}, 
{{g1, g2, g3}, {h1, h2, h3}, {i1, i2, 
              i3}}
};
MatrixForm[m2]

First the default case for RotateRight is demonstrated on the above tensor.

RotateRight[m2]//MatrixForm

In the cell below we see the default case is equivalent to a few values for the second argument.

\(RotateRight[m2] === RotateRight[m2, {1}] === 
      RotateRight[m2, {1, 0}] === 
	RotateRight[m2, {1, 0, 0}]\)

When the second argument is {0,1} or {0,1,0} the second level is rotated one place.

RotateRight[m2, {0, 1}] // MatrixForm

When the second argument is {0,0,1} the third level is rotated one place.

RotateRight[m2, {0, 0, 1}] // MatrixForm

Rule (->)  versus  RuleDelayed (:>)

A good application of RuleDelayed (:>) is in the following line. Here RuleDelayed (:>) must be used instead of Rule (->) since Factor must not evaluate until it is given the argument of Log.

Clear["Global`*"];
(1 + 2 x + (x^2)) Log[24 + 26  x + 9  (x^2) + (x^3)] /. 
      Log[a_] :> Log[Factor[a]]

The function below will modify any expression so all approximate numbers that are equal to an integer are converted to the equivalent integer.  In this case RuleDelayed (:>) must be used instead of Rule (->) since the rule must not evaluate until it has an actual value to operate on.

\(RealToInteger[expr_] := 
      expr /. 
		x_Real :> If[Round[x] == x, Round[x], x]\)

(E^(5.0 x)) + (1.34/(4.0 + x)) // 
      RealToInteger

Another for using (lhs:>rhs) instead of (lhs->rhs) is when (lhs) includes named patterns. Named patterns can include (x_), (x__), (opts___), (m_:4) (j_Integer), (t_?Positive), (n_.) and many other forms. To demonstrate how use of (lhs->rhs) can cause problems consider the input in the next cell.

x = 34;
Log[(a1 + a2) (b1 + b2 ) (c1 + c2)] /. 
      Log[x_   y_] -> Log[x] + Log[y]

In the cell above the global value of (x) was used where we wanted (a1+a2) to go.  Most often the problem is much more subtle than in the example above. Typically the variable used to name the pattern had a value assigned long before you use it in (lhs->rhs). One method sure to avoid this problem is to clear the values from any variables used to name patterns as in the next cell. This of course requires that you don't have values assigned to (x) or (y) that you care about.

Clear[x];
Log[(a1 + a2) (b1 + b2 ) (c1 + c2)] /. 
      Log[x_   y_] -> Log[x] + Log[y]

Another, often more desirable, method of avoiding the error above is to use (lhs:>rhs) instead of (lhs->rhs).  This is done in the next cell, and the value assigned to (x) doesn't prevent us from getting the right result.

x = 34;
Log[(a1 + a2) (b1 + b2 ) (c1 + c2)] /. 
      Log[x_   y_] :> Log[x] + Log[y]

The only time when using (lhs:>rhs) might not work very well is when you can save a lot of time by evaluating (rhs) only once.  An example of this is given in the next cell where a complicated integral must be evaluated each time (g2) is used. It's much more efficient to evaluate the integral only once.

g2 = g[x_] :> Integrate[Log[(Sqrt[x]) + 1], x]

In that case you might want to use the next cell where any values assigned to (x) are cleared, and the integral is evaluated immediately using (lhs->rhs). It's also possible to get the best of both worlds where (rhs) evaluates immediately as in the next cell, and gives the right result even if (x) has an assigned value!  To do that you need to use a package I make available at  http://library.wolfram.com/database/MathSource/425.

Clear[x];
g2 = g[x_] -> Integrate[Log[(Sqrt[x]) + 1], x]

Scan

Suppose we have a list of expressions which in the example below is a simple list of integers.  Now suppose we need to define a function (f) for each element of this list. The best way to make the assignments is using Scan.  A first attempt at this is given in the next cell.

ClearAll["Global`*"];
lst = {12, 24, 35, 46};
Scan[f[#] = Prime[10^7 + #] &, lst]

Next we see that our attempt to make assingments for (f) didn't work, and the reason is that assignment (f[#]=Prime[10^7+#]&) evaluated before it was give integers.

?? f

The solution is to make the assignment a function with the HoldAll or HoldFirst attribute as I do in the next cell.

ClearAll[f];
Scan[Function[n, f[n] = Prime[10^7 + n], {HoldAll}], lst]

?? f

We could have made the above assingments using Map as I do in the next cell.  In this case Map makes a list of prime numbers that would be returned if it were not for the semi-colon at the end.  Using Scan for this task is more efficient than using Map because Scan never builds up an expression to return.

ClearAll[f];
Map[Function[n, f[n] = Prime[10^7 + n], {HoldAll}], 
        lst];

?? f

The next example makes assingments for  f[g1[5,3]],  f[g2[8,9]],  and  f[g3[12,13]].

expr = h[g1[5, 3], g2[8, 9], g3[12, 13]];
ClearAll[f];
Scan[Function[a, f[a] = (a^4), {HoldAll}], expr]

?? f

However, what we got above might not be the desired result. What if you wanted to make assignments for f[5], f[3], f[8], etc. In that case we can get the desired result by giving Scan the level specification {2}.  Scan then makes assingments for all subexpressions of (expr) at level 2.  Scan can work with any of the level specifications that I exaplain earlier.

ClearAll[f];
Scan[Function[a, f[a] = (a^4), {HoldAll}], expr, {2}]

?? f

Heads Option

Scan has a Heads option with the default setting (Heads->True).  In the next example I have Scan work on level {2} of expr with the setting (Heads->True).  Because of the (Heads->True) setting assignments are made for f[g1], f[g2], f[g3] where g1, g2, g3 are heads of sub-expressions of (expr).

ClearAll[f];
Scan[Function[a, f[a] = "$" <> ToString[a], {HoldAll}], expr, {2}, 
      Heads -> True]

?? f

Sequence

The head Sequence is rarely seen in directly.  Consider the function (f) below, where the first two arguments are p1,and p2, and all the remaining arguments are part of p3.  All the remaining arguments go with p3 because the definition of (f) uses BlankSequence (___) for p3.  The only thing the function (f) does is return p3.  One might wonder what head is given to p3.  It turns out it is given the head Sequence. After (f) is defined, we use it to make the Sequence (seq).

ClearAll[f, foo, h];
f[p1_, p2_, p3__] := p3;
seq = f[9, 10, 11, 12, 13, 14, 15]

If you have a number of sequences inside almost any head the sequences are automatically flattened, and the fact that the arguments are a sequence is not indicated (see the example in the next line).  The only functions that don't splice the sequences together are those with the Attribute SequenceHold.

{Sequence[2, 3, 4, Sequence[7, 8]]}

Most useful applications of Sequence are variations of the next cell where the head (h) is changed to Sequence and the effect is that h[7,11,13] is changed to (7,11,13).

foo[2, 3, 5, h[7, 11, 13], 17] /. h -> Sequence

SequenceHold

Question:  What does the Attribute SequenceHold do for you?

Answer:  Rule (->) has the SequenceHold Attribute.  As a result a rule such as the one in the next line can be used.  If Rule didn't have the SequenceHold Attribute  Rule[y,Sequence[y1,y2,y3]]
would evaluate to  Rule[y, y1, y2, y3]  which is an invalid expression.

Clear["Global`*"];
{x, y, z} /. y -> Sequence[y1,  y2,  y3]

Nine built in commands have the SequenceHold Attribute.  They are:
Rule (->), RuleDelayed (:>), Set (=), SetDelayed (:=), TagSet, TagSetDelayed, Timing, UpSet (:=), UpSetDelayed (^:=)

Series

The typical method of finding a series expansion is to enter the complete expression as in  Series[(x+2)Sin[x],{x,0,5}]. or  Series[Log[(x^2) + x], {x, 1, 3}].  A seldom documented trick is to get the same series expansions using the cells below.

Clear[x];
(x + 2) Sin[ x] + O[x]^5

Log[(x^2) + x] + O[x, 1]^4

You can often use Series to find an asymptotic expansion provided one exists for the function in question.  An asymptotic expansion is one where the error gets smaller as the argument approaches infinity.  Series will try to find an asthmatic expansion if you ask for a series about infinity.  The next cell gives an asthmatic expansion of Erfc[x].

Series[Erfc[x], {x, ∞, 6}]

The next cell gives an asthmatic expansion of ArcTan[x].

Series[ArcTan[x], {x, ∞, 6}]

It turns out you must first use TrigToExp  to get an asymptotic expansion of ArcSinnh[x].

Series[TrigToExp[ArcSinh[x]], {x, ∞, 6}]

You may need to make a power series in two or three variables and truncate it such that the sum of all exponents in each term is less than or equal to a certain integer.  If that is the case download a package I have posted at  http://library.wolfram.com/infocenter/MathSource/4950/

Set (=)  versus  SetDelayed (:=)

Alan Hayes provided an example where (  func[x_]=  ) instead of  
( func[x]:=  ) should be used.  If ( func[x_]:=  ) was used in this example
the least squares fit would be computed  again for every value of (x).

ClearAll["Global`*"];
data = 
        Table[{x, Cos[x] + Random[]/10}, {x, 0, π/3, 0.025}];
curve[x_] = Fit[data, {1, x, (x^2)}, x]

In the following line Alan uses "Epilog->Point/@data"  to ensure the data points
are drawn along with a Plot of the curve.  For this application it makes 
little difference whether Epilog or Prolog is used.

Plot[curve[t], {t, 0, π/3}, 
        Epilog -> Point /@ data];

In the following example (expr) is not a polynomial in (x) until (expr) is evaluated.  So we have to use Set (=) to have the function perform as intended.

expr = 1 + x + (x^2);
fun1[x_] := expr
fun1[E]

Clear[x];
fun2[x_] = expr;
fun2[E]

In the next example the right hand side can't evaluate until x is given a numeric value, so we have to use Set (=).

area[x_?NumericQ] /; (x > 0) := 
      NIntegrate[Log[t]/(Exp[t] + 1), {t, 1, x}]
{area[1], area[2], area[3], area[4]}

In the next example evaluation of (deriv1[expr]) starts with substituting the value of (expr) for (x) in the definition of deriv1[x_].  Next the Derivative is evaluated, and all is well as (expr) does not have a numeric value.  When the argument of ( deriv1 )  is a numeric value the value is substituted for (x) in the definition of (deriv1), and a Derivative with respect to a constant is not defined.  As a result (deriv1) doesn't work for numeric arguments.

deriv1[x_] := D[(x^3), x]
{deriv1[a], deriv1[π]}

In the next cell the definition of (deriv2) differs from the definition of (deriv1) in that Set (=) is used rather than SetDelayed (:=).  Here the Derivative is only evaluated  when (deriv2) is defined.  The new function  (deriv2) gives the expected result for symbolic or numeric arguments.

Clear[x];
deriv2[x_] = D[(x^3), x];
{deriv2[b], deriv2[π]}

Notice the definition of (deriv1) involves the Derivative operation, but the definition of (deriv2) does not.

?deriv1

?deriv2

In the next two lines a Cumulative Distribution Function is defined.  For  CDF1 SetDelayed (:=) must be used because NIntegrate can not evaluate until we have a numerical value for both limits (upper and lower).  On the other hand CDF2 is defined using Set (=).  In this case SetDelayed (:=) could have been used but this would be far less efficient.  When CDF2 is defined using Set (=) the symbolic integration is only performed when CDF2 is defined.  If CDF2 was defined using SetDelayed (:=) the symbolic integration would be performed every time CDF2[_] is evaluated.

\(CDF1[a_?NumericQ] := 

      NIntegrate[\((\(\(-2\)\ E\^\(\(-2\)\ x\)\)\/\(EulerGamma + Log[2]\))\) 
          Log[x], {x, 0, a}]\)

Clear[a];
CDF2[a_] = 
        Integrate[(((-2 (E^(-2 x)))/(EulerGamma + Log[2]))) 
            Log[x], {x, 0, a}];

The next example is from the FAQ section of the Wolfram Research web page.  When ( f1[n_] ) is defined using Set (=)  Expand has no effect because Expand makes no change if the exponent is not a positive integer.

Clear[n];
f1[n_Integer] = Expand[(((x + 1))^n)];
f1[5]

On the other hand ( f2[n_] ) is defined using SetDelayed (:=), and
Expand does not evaluate until the exponent has an integer value.

f2[n_Integer] := Expand[(((x + 1))^n)]
f2[5]

A different perspective on explaining Set versus SetDelayed is give in the notebook posted at  http://library.wolfram.com/infocenter/MathSource/425/

A warning about named patterns in lhs when using (lhs=rhs)

One must be careful about using (lhs=rhs) when (lhs) includes named patterns and the pattern name is needed in (rhs).  Named patterns can include (x_), (x__), (opts___), (m_:4) (j_Integer), (t_?Positive), (n_.) and many other forms. When the variable used to name the pattern is used in (rhs) the global value of the variable is used in (rhs).  The next two cells demonstrate the problem.

x = 47;
ClearAll[f];
f[x_] = x + Log[x];

f[1/6]

Above we see the global value of (x) is used instead of 0.125 to compute f[x].  An easy way to avoid the problem is to clear any symbols used to name patterns when the patterns are needed on the left side.  Below we can be sure that the definition will work as intended because any Global values were cleared from x.  This obviously requires that the variables used to name patterns have no variables that you care about.  Another solution that doesn't have this limitation is available from my MathSource item at  http://library.wolfram.com/database/MathSource/425.

ClearAll[f, x];
f[x_] = x + Log[x];

f[1/6]

However, you can use the symbol used to name a pattern variable on the left side of (=) with out problems.  Below the global value of (x) doesn't prevent us from getting the intended result.

ClearAll[f];
x = 47;
f[x_] /; (x < 0) = -1;
f[0] = 0;
f[x_] /; (x > 0) = 1;

{f[-4], f[0], f[5], f[50]}

Evaluation of (lhs=rhs)

The next cell causes (g) to evaluate to (h) and causes (h) to evaluate to 9. As a result both (g) and (h) evaluate to 9.

g = h;
h = 9;
{g, h}

In the next cell (g=0) is evaluated and the value of zero is assigned to (g) without letting (g) evaluate.  As a result (g) evaluates 0 but (h) still evaluates to 9.

g = 0;
{g, h}

From the examples above it appears (lhs) in (lhs=rhs) doesn't evaluate and that is the case if MatchQ[Unevaluated[lhs],_Symbol] would return True. However, it isn't that simple if Unevaluated[lhs] isn't a Symbol. To demonstrate this consider the next cell where both (h[2]) and (g[2]) return 9.

ClearAll[g, h];
g = h;
h[2] := 9;
{g[2], h[2]}

Now when the next cell is evaluated (g[1+Tan[π/2]]) partially evaluates prior to assigning the value 0 to something. First (g) evaluates to (h). Then (1+Tan[π/4]) evaluates to (2). The last thing to happen is that 0 is assigned to h[2].  The first argument is held in the sense that h[2] doesn't evaluate to 9. If that happened this example would end up with 9=0 which is nonsense. A user defined function with the (HoldFirst) attribute will not work this way unless complicated definitions are used to ensure that it does.

g[1 + Tan[π/4]] = 0;

Now when g[2] is evaluated (g) evaluates to (h) and h[2] evaluates to 0.

g[2]

Below we see that the new assignment is associated with (h) not with (g).

?g

?h

Simplify & FullSimplify

Consider a special ComplexityFunction

One might like the following to simplify to 1-Exp[-x], and we would like FullSimplify[Log[10^20]] to return (20 Log[10]) which FullSimplify does by default.

Clear[x, a];
FullSimplify[1 - Cosh[x] + Sinh[x]]

The definitions in the next cell define a new setting for the ComplexityFunction that does the trick.

ClearAll[DigitsLength, Digits1, Digits2, VariableCount];
DigitsLength[0 | 1] = 1/10;
DigitsLength[-1] = 1/8;
DigitsLength[n_] := Length[IntegerDigits[n]]*2;
Digits1[expr_] := 
        Plus @@ (DigitsLength[#] & /@ 
              Cases[expr, _Integer, -1]);
Digits2[expr_] := 
        Plus @@ Flatten[
Map[
              DigitsLength[#] &, ({Numerator[#], Denominator[#]} &  /@ 
                  Cases[expr, _Rational, {-1}]), {-1}
]
];
VariableCount[expr_] := 
      Count[expr, _Symbol?(Not[
                NumericQ[#]] &), {-1}]

    
SetOptions[FullSimplify, 
        ComplexityFunction -> (LeafCount[#] + Digits1[#] + 
                Digits2[#] + (33/10)*
                  VariableCount[#] &)];

In the next cell we see that using the new setting for ComplexityFunction FullSimplify does what we want.

FullSimplify[1 - Cosh[x] + Sinh[x]]

The next example is taken from the Help Browser. In this case the new setting for ComplexityFunction gives the same result we get using ComplexityFunction->Automatic.

n = 
        70017833065954769132658110933808716053755043900310001;
FullSimplify[Log[n]]

In the next cell we see that when using the new setting for ComplexityFunction FullSimplify still does a nice job of simplifying a complicated algebraic expression.

FullSimplify[
      (x^3) Cot[(a^2)] - 
        (1/4) Csc[
            (a^2)] (4 (E^(I (a^2))) (x^3) + 
              Sin[(a^2)] (2 I (2 (a^2) - π) x + ((4 I (E^(-2 I (x^2))) π x)/(1 + (E^(-2 I (x^2))))) + ((8 I (E^(2 I ((a^2) + (x^2)))) x ((a^2) + (x^2)))/(1 - (E^(2 I ((a^2) + (x^2)))))) + 
                    4 (a^2) x Cot[(a^2) + (x^2)] - 
                    2 π x Tan[
                        (x^2)]))
]

A simple problem Simplify & FullSimplify can't handle

Jürgen Tischer sent a problem to the MathGroup that Simplify couldn't simplify very well.  Simplify has trouble with the example in the next cell for the same reason as the more complicated example Jürgen Tischer provided.

Clear[n];
Simplify[5 ((20^n)) + 7 ((20^n))]

The code in the next cell forces Simplify to make the appropriate simplification.  This is based on a very clever solution Allan Hayes sent to the MathGroup.

Unprotect[Simplify];
HiddenSymbols`ModifySimplify = True;

    
Simplify[expr_] /; HiddenSymbols`ModifySimplify := 
      Block[{HiddenSymbols`ModifySimplify}, 
Module[{temp}, 
Simplify[
Factor[
                expr /. (n_Integer)^(k_) :> 
(Times @@ (temp[First[#]]^(k*Last[ #]) &) /@ 
                          FactorInteger[n])
] /. 
              temp[a_] :> 
                a
]
]
]

    
Protect[Simplify];

After evaluating the previous cell Simplify does better with the example above.

Simplify[5 ((20^n)) + 7 ((20^n))]

One might wonder why (12 ((20^n))) isn't returned in the output above.  The reason is that the kernel insists on changing (12 ((20^n))) into (3 (4^(1 + n)) (5^n)).  You can use a function I defined called HoldTemporary which prevents further evaluation of an expression, but does not have to have a hold released when used in the future. Through use of HoldTemporary one can have the output above displayed as (12  (20^n) ).  I have a package which defines HoldTemporary posted at  http://library.wolfram.com/infocenter/MathSource/705

Slot, SlotSequence  (#, #n, ##, ##n)

The discussion of (#, #n, ##, ##n) was expanded and moved to the section on Function.

Sort

By default Sort arranges items in cannonical order rather than numerical order. This is reasonable because some or all of the elements may not have a numeric value. If all elements of a list have the heads Integer, Rational, or Real the cannanocal ordering is a numerical ordering. In the next cell some elements of the list don't have the head Integer, Rational or Real and the elements aren't sorted numerically even though they could be sorted numerically.

Sort[{-2, 4, -5, 
        Sin[2], π, -∞, ∞}]

You can provide Sort a second argument which specifies how elements should be compared when they are sorted. In the next cell Less is used as an ordering function and the list is sorted numerically. Of course you would get the list in opposite order if the ordering function Greater was used. However, Allan Hayes has pointed out in the MathGroup that Sort is much slower when an ordering function is specified.

Sort[{-2, 4, -5, Sin[2], π, -∞, ∞}, 
      Less]

The ordering function (#1<#2&) has the same effect as the ordering function Less, but is more cryptic. In this case there isn't a good reason to use the more cryptic ordering function, but in more complicated cases the cryptic notation is harder to avoid. If you aren't familiar with the #& notation see the section on Function.

Sort[{-2, 4, -5, 
        Sin[2], π, -∞, ∞}, #1 < #2 &]

Suppose you have the matrix created by the next cell and you want to sort the rows according to the value in a certain row of the matrix. The next cell makes such a matrix we can use as an example.

\(\(mtrx = 
        Table[{Random[Integer, {0, 10}], 
	Random[Integer, {50, 60}], 
	
            Random[Integer, {\(-10\), 0}]}, {i, 8}];\)\)
TableForm[mtrx]

In the next cell the matrix is sorted according to the value in the first column.

m2 = Sort[mtrx];
TableForm[m2]

If we want to sort the matrix according to the value in the second column we can use the ordering function  (#1[[2]]<#2[[2]]&) as in the next cell.

m2 = 
        Sort[mtrx, #1[[2]] < #2[[2]] &];
TableForm[m2]

The code in the next cell effectively sorts the matrix on the second column. In this case each row is rotated before using Sort. Then Sort can be used without an ordering function. After the matrix is sorted the columns are put back in the original order. Sort evaluates much faster this time because an ordering function wasn't specified. When given a matrix with thousands of rows this method is about 18 times faster even after accounting for the time to rotate each column before and after sorting.

m2 = 
        Sort[RotateLeft[#, 1] & /@ mtrx];
m2 = RotateRight[#, 1] & /@ m2;
TableForm[m2]

Sort can work with any expression. In the next cell Sort takes an expression with the head (h).

ClearAll[h];
Sort[h[2, 6, 3, 8, 5, 6, 8, 1, 0, 3]]

Split

The usage message for Split and demonstrations of it are shown in the next few cells.

?Split

Clear["Global`*"];
Split[{a, a, b, b, b, b, a, b, a, a, b, b, b, a}]

Many of the most useful applications of Split require providing Split a test function as a second argument. In the next cell Slit Split considers two adjacent elements of (lst) "identical" if they are both less than 100 or if they are both greater or equal to 100. For an explanation of the # & notation see the discussion of Function.

lst = {10, 11, 100, 234, 648, 467, 12, 13, 14, 356, 15, 16, 
          457};

    
Split[lst, #1 < 100 === #2 < 100 &]

The next cell sorts (lst) and returns a list of lists where the first sublist is the elements between 0, 10. The second sublist is the elements between 10 and 20. The third and fourth sublists are the elements from 20 to 30 and from 30 to 40.

lst={8.06833, 32.1809, 20.357, 22.7313, 38.2098, 48.3307, 
 34.9967, 13.2429, 24.3229, 1.89359, 41.7259, 10.4685, 
 26.4448, 41.3348, 46.9688};
 
Split[Sort[lst],Floor[#1/10]===Floor[#2/10]&]

Finally Split can be used on expressions that don't have the head List.  This is demonstrated in the next cell.

Split[h[a, a, b, b, b, b, a, b, a, a, b, b, b, a]]

Subscript (using subscripted symbols)

I wrote a package that makes it very easy to do switch back an forth between treating subscripts as symbols and not.  The package is largely based on code samples discussed here and the package can be downloaded from  http://library.wolfram.com/database/MathSource/4268. 

The next line is a slight variation of a tip David Bailey sent to the MathGroup.  Using this code an 'a' with a non-negative integer subscript evaluates to a symbol where the character 'a' is concatenated with the symbol.

Clear["Global`*"];
(Subscript[a, i_?(IntegerQ[#] && NonNegative[#] &)]) := 
      Symbol[ToString[a] <> ToString[i]]

Now in the next line the first three in the list are symbols, and all the rest are subscript expressions.

Head /@  {(Subscript[a, 0]),  (Subscript[a, 1]),  (Subscript[a, 2]),  (Subscript[a, b]),  (Subscript[a, 1/2])}

If this approach is used you must avoid using both {(Subscript[a, 1]),  (Subscript[a, 2]),  (Subscript[a, 3])} and {a1, a2, a3} as symbols, since the former will evaluate to the latter.  This is demonstrated in the next cell.

{(Subscript[a, 1]),  (Subscript[a, 1]) === a1}

The next line shows that the rule above is stored in DownValues[Subscript].

DownValues[Subscript]

Before continuing the above DownValue is cleared.

DownValues[Subscript] = {};

Colin Rose gave the following examples where Subscripts can be used in Mathematica Version 4 without using the notation palette.

z = Thread[Subscript[y, Range[12]]]

z /. Subscript[y, x_] :> y^x

% /. y^x_.   :> Subscript[y, x]

z /. Subscript[y, t_] :> Subscript[y, t - 1]

In v4, you can generally use subscripted "variables" as if they were symbols.  The examples below work fine.

expr = (Subscript[y, 1]) + (Subscript[y, 2]);
Solve[expr == 2, (Subscript[y, 1 ])]

Integrate[expr, (Subscript[y, 1])]

Plot3D[Sin[(Subscript[y, 1]) + (Subscript[y, 2])], {(Subscript[y, 1]), 0, 3}, {(Subscript[y, 2]), 0, 3}];

The most common problem occurs when people simultaneously try to use 
(x) and (Subscript[x, 1]),  (Subscript[x, 2]),  etc.  In that case if one uses (x=7) then  (Subscript[x, 1])  ->  (Subscript[7, 1]) ,  
      (Subscript[x, 2])  ->  (Subscript[7, 2]) .  This sort of problem is easily avoided by NOT simultaneously using x with (Subscript[x, 1]), (Subscript[x, 2]),   ...

Carl Woll indicated we can give Subscript the attribute HoldFirst. As in the example below.

ClearAll[Subscript];
SetAttributes[Subscript, HoldFirst];

Now we can have symbols  t,  (Subscript[t, 1]) without conflict.

t = 34;
(Subscript[t, 1])

Table[(Subscript[t, 1]), {(Subscript[t, 1]), 5}]

ClearAll[Subscript]

Collin Rose also indicated use of Symbolize button in the notation palette can result in problems as in the following example.

Needs["Utilities`Notation`"]

Symbolize[(Subscript[m, 1])];
Symbolize[(Subscript[m, 2])];
Symbolize[(Subscript[m, 3])];

z1 = {(Subscript[m, 1]), (Subscript[m, 2]), (Subscript[m, 3])};
Head /@ z1

So far we don't have a problem.

z2 = Table[(Subscript[m, i]), {i, 3}]

Head /@ z2

We now have two sets of "identical" notation in use.  We have (Subscript[m, 2]) as a Symbol in z1, and  (Subscript[m, 2]) as subscript expression in z2.  However, the elements in z1 and z2 look the same on screen.  One must be careful to make sure this doesn't happen.

The steps used above to make {(Subscript[m, 1]),  (Subscript[m, 2]),  (Subscript[m, 3])} symbols can be undone by using the RemoveSymbolize button in the FullNotation palette.  It's under Symbolizations, StandardForm Symbolize.

RemoveSymbolize[(Subscript[m, 1]) ];
RemoveSymbolize[(Subscript[m, 2]) ];
RemoveSymbolize[(Subscript[m, 3]) ];

Allan Hayes suggested the following to symbolize ALL subscript objects.  Below we see that this will cause even (Subscript[(Log[3]),  a]) to have the head Symbol.  It's hard to imagine why one would use such a form, so this may be of little concern.

Needs["Utilities`Notation`"]

Symbolize[\(\(\(_\)\(\ \)\)\__\)]

Head /@  {(Subscript[a, b]),  (Subscript[t, 2]),  (Subscript[(Log[3]), a])}

After using the code above to treat anything with any type of a subscript as a Symbol the use of Table below doesn't return the list {(Subscript[x, 1 ]),  (Subscript[x, 2 ]),  
        (Subscript[x, 3 ]),  (Subscript[x, 4])}.

Table[(Subscript[x, i]), {i, 4}]

Before we continue we might want to remove the rule that makes anything with a subscript a symbol.  The line after RemoveSymbolize demonstrates that the rule was in fact removed.

RemoveSymbolize[\(\(\(_\)\(\ \)\)\__\)]

Head /@ Table[(Subscript[a, i]), {i, 4}]

A modification of code Jason Harris sent to the MathGroup is given below.

One variation of code Jason Harris sent to the MathGroup is shown below.  The version below will treat (i) with a non-negative integer subscript as a symbol, but (i) with any other subscript is treated as a Subscript expression.  This requires the use of some advanced features of the Notation palette.  Documentation can be found at Notation:Definition:NotationPatternTag under Add-ons in the Help Browser.

Needs["Utilities`Notation`"]

(Subscript[i, n_?(IntegerQ[#] && NonNegative[#] &)]) := 
        ToExpression[MakeBoxes[(Subscript[i, n])]]
 
IntegerTest[n_] := 
        ToExpression[n, StandardForm, IntegerQ[#] && NonNegative[#] &]
 
Symbolize[(Subscript[i, _?IntegerTest])]
 
(Subscript[i, n_?(IntegerQ[#] && NonNegative[#] &)]) := 
      ToExpression[MakeBoxes[(Subscript[i, n])]]

Now (i) with a positive integer subscript is an symbol, while (i) with any other subscript is a subscript expression.

Head /@ {(Subscript[i, 2]), (Subscript[i, -3]), (Subscript[i, a]), (Subscript[i, 1.2]), (Subscript[i, π]), 
        (Subscript[i, 2/3])}

Below (Subscript[i, n]) is an expression rather than a Symbol, so the list { (Subscript[i, 0 ]),  (Subscript[i, 1 ]),  
        (Subscript[i, 2  ]),  (Subscript[i, 3 ])}  is returned.

lst = Table[(Subscript[i, n]), {n, 0, 3}]

Head /@ lst

■  How to make the expression:
Symbolize[(Subscript[i, _?IntegerTest])]

Short of manually entering the necessary cell expression there are two ways to make the Symbolize expression above.  Both are explained below.

◼  Use keyboard shortcuts:

Type EscsymbEsc⇥i

Symbolize[i]

Depress Ctrl-

Symbolize[(Subscript[i, \[SelectionPlaceholder]])]

Type  EscpattwraperEsc⇥_?IntegerTest

Symbolize[(Subscript[i, _?IntegerTest])]

◼  Use the Notation palette:

⚠ You may need to patch a bug before this will work.

In some versions of the Notation package one of the needed buttons needs to be fixed before the code below will work.To fix the problem use an ASC text editor (MS-Word will work on a PC) to edit the file (FullNotationPalette.nb).  The file is normally in the directory 
     WolframResearch\Mathematica\AddOns\ExtraPackages\Utilities.
Once you have the file open with a test editor delete the line shortly after the introductory comments containing the word CacheID.  Then use the Find feature to locate "InsertPatternWrapper" which will bring you to the code segment below.  Shortly after 
     StyleBox["InsertPatternWrapper", ...
     
Change the code  
   ButtonData:>TagBox["\[PlaceHolder]", NotationPatternTag,  ...
   
to
  ButtonData:>TagBox["\[SelectionPlaceHolder]", NotationPatternTag, ...
  
After making these minor edits save the file.

Enter the input cell below.

(Subscript[i, _?IntegerTest])

Select the entered expression, and depress the Symbolize[■] button in the Notation palette.

Symbolize[(Subscript[i, _?IntegerTest])]

Select the subscript 
    _?IntegerTest
Depress the button for "Full Notation Palette".
When the Full Notation Palette comes up select "InsertPatternWrapper" under "Wrapper Boxes".

Symbolize[(Subscript[i, _?IntegerTest])]

Unfortunately it's rather difficult to use the RemoveSymbolize button to remove the definition above.

Another modification of code Jason Harris provided is given below.

Once the Notation package is loaded evaluating the next cell will define a function (SubscriptSymbols) that takes a symbol as an argument, set up things so that if one evaluates SubscriptSymbols[vari], then (vari) with a non-negative integer subscript will be treated as a symbol.

\(\(\(IntegerTest[n_] := 
      ToExpression[n, StandardForm, IntegerQ[#] && NonNegative[#] &]\)\(
\)
    \)\)
Needs["Utilities`Notation`"];
\(SubscriptSymbols[
        x_Symbol] := \((Symbolize[
          NotationBoxTag[
            SubscriptBox[ToString\ @\ x, 
              TagBox[RowBox[{"i_", "?", "IntegerTest"}], 
                NotationPatternTag]]]]; 

x\_\(i_?IntegerQ\) := 
          ToExpression\ @\ MakeBoxes[\ x\_i])\)\)

The next cell causes (v) with non-negative integer subscript to be treated as a Symbol.

SubscriptSymbols[v]

In the next cell only (Subscript[v, 2]) has the Head Symbol.

(Subscript[v, 2]) = 5;

Head /@ {(Subscript[v, 2]), (Subscript[v, a]), (Subscript[v, 1.5]), (Subscript[v, 2/3])}

Still another modification of code Jason Harris provided is given below.

Needs["Utilities`Notation`"];
\(\(\(IntegerTest[n_] := 
      ToExpression[n, StandardForm, IntegerQ[#] && NonNegative[#] &]\)\(
\)
    \)\ \)
SymbolTest[n_] := 
      ToExpression[n, 
        StandardForm, (Head[#] === 
                Symbol && ! NumericQ[#]) &]

    
VariableQ[_Symbol?(! NumericQ[#] &)] := True
VariableQ[__] := False

    
\(\(\(Symbolize[
        NotationBoxTag[
          SubscriptBox[
            TagBox[RowBox\ @\ {"_", "?", "SymbolTest"}, 
              NotationPatternTag], 
            TagBox[RowBox\ @\ {"_", "?", "IntegerTest"}, 
              NotationPatternTag]]]];\)\(
\)
    \)\)
(Subscript[((symb_?VariableQ)), i_?IntegerQ]) := 
      ToExpression @ MakeBoxes[ (Subscript[symb, i])]

After evaluating the cells above the first three symbols below have the head Symbol while the others have the head Subscript.

Head /@ {(Subscript[a, 0]), (Subscript[b, 1]), (Subscript[γ, 0]), (Subscript[a, d]), (Subscript[b, 1.5]), 
        (Subscript[c, 2/3]), (Subscript[π, 2])}

Remove

Remove is discussed in the section on Clear, ClearAll, Remove.

TagBox

The feature named TagBox is essential to an improved Units package I wrote and have posted at  
http://library.wolfram.com/database/MathSource/4283  
The usage message for TagBox is given below.

?TagBox

The second argument in TagBox is the head the should have rather than the head normally used for the boxes displayed.  Only one line of code is needed to demonstrateTagBox.

MakeBoxes[UnitsWrapper[expr_, u_], 
        form : (StandardForm | TraditionalForm)] := 
      TagBox[RowBox[{MakeBoxes[expr, form], " ", MakeBoxes[u, form]}], 
        UnitsWrapper]

After evaluating the cell above the output of the next cell looks like Times[0,Second] which would evaluate to 0, but the fullform of the expression is actually UnitsWrapper[0, Second].

expr = UnitsWrapper[0, Second]

FullForm[expr]

Table

When we use Table[expr, {i,imin,imax,di}] (imax-imin) doesn't have to be a multiple of di.  See the following example.

Clear[a, x];
Table[a + dx, {dx, 0, 3, π/8}]

You can also give Table (imax, imin,di) that are not numeric as in the next line.

Table[i, {i, -x, x, x/5}]

However, if you want to make a long list Range, can usually make in about half the time it takes Table to do the same.  In the lines below Range is used to make the lists in two examples above.

a + Range[0, 3, π/8]

x Range[-1, 1, 1/5]

The iterator in Table can be omitted if it isn't used.  The next line makes a 4x4 matrix of real numbers.  This can save considerable time.  For example Table[Random[],{1000},{100}] evaluates in less than half the time it takes to evaluate Table[Random[],{i,1000},{j,100}].

Table[Random[], {4}, {4}]

The following construct can be used to make a list of lists that grow in length.

Table[1/(x + y), {x, 6}, {y, x}] // TableForm

Thread

First an example of Thread is given in it's simplest form.  When used in this simple form Thread takes an expression with arguments that are all lists of equal length.

ClearAll["Global`*"];
Thread[f[{x1, y1}, {x2, y2}, {x3, y3}]]

In the next cell we see Thread automatically adapts if one or more elements aren't lists.

Thread[f[a, {y1, y2, y3}, {z1, z2, z3}, d]]

The next three examples have frequent applications.  They have the same form an example above but it may not look that way because infix operators (->, ==) are used.

Thread[{opt1, opt2, opt3, opt4} -> False]

Thread[{w, x, y, z} -> {1.2, 3.2, 4.2, 5.2}]

Thread[{w, x, y, z} == {1.2, 3.2, 4.2, 5.2}]

As indicated above Thread works over lists by default.  In the next example some of the expressions at level 1 of (lst1) aren't lists.  In a case such as this the expressions that aren't lists are copied.

\(\(lst1 = 
        f[{x1, y1}, {x2, y2}, {x3, y3}, 
		g[x4, y4], g[x5, y5], 
          g[x6, y6]];\)\)
Thread[lst1]

Thread will work over a different head if one is provided as a second argument.  The next line threads the same expression as the last one, but this time it threads over (g) instead of threading over list.  Similar to the last example expressions that didn't have the head (g) were copied.

Thread[lst1, g]

In the next line Thread is provided a second and third argument.  The third argument tells Thread to only thread across the first three arguments.

\(\(lst2 = 
        f[g[x1, y1], \ g[x2, y2], \ 
		g[x3, y3], g[x4, y4], 
          g[x5, y5]];\)\)
Thread[lst2, g, 3]

In the next line Thread is given a third argument which says to only thread over arguments 2 through 4.

Thread[lst2, g, {2, 4}]

Finally Thread is given (-3) as a third argument.  This means to only thread over the last three arguments.

Thread[lst2, g, -3]

Roman Meader give the elegant program below which automatically threads listable functions across the head Equal.  The program is posted in MathSource under Enhancements, Algebraic, 0202-204.  The code for this enhancement is shown in the next cell.

Unprotect[Equal]; 
listableQ[f_]  :=  MemberQ[Attributes[f],  Listable];
\(\(Equal /: \ 
        lhs : \((f_Symbol)\)?
              listableQ[___, \ _Equal, \ ___]\  := \ 
\ \ \ Thread[
          Unevaluated[lhs], \ Equal];\)\)
Protect[Equal];

The program above allows one to 'add' two equations in the next cell.

Clear[x];
eqn1 = (4 - 2  x + (x^2) == 0);
eqn2 = (5 + 6  x == 0);
eqn1 + eqn2

Trig option

The usage message for the built-in symbol Trig is shown below.

?Trig

The built-in functions that have the Trig option are 
Apart, ApartSquareFree, Cancel, Coefficient, CoefficientList, Collect, Denominator, Expand, ExpandAll, ExpandDenominator, ExpandNumerator, Exponent, Factor, FactorList, FactorSquareFree, FactorSquareFreeList, FactorTerms, FactorTermsList, FullSimplify, Numerator, PolynomialGCD, PolynomialLCM, PolynomialMod, Resultant, Simplify, Together.

Simplify and FullSimplify have the default setting (Trig->True) and all other functions above have the default setting (Trig->False).  I searched through all available documentation and couldn't find a single example to demonstrate this option. The examples using the Trig option below were provided by Allan Hayes and Andrzej Kozlowski.  In each case the original expression would be returned if it were not for the use of (Trig->True).

Clear[x];
Expand[Sin[2  x], Trig -> True]

Apart[Csc[2  x], Trig -> True]

Cancel[Csc[x] Sin[2  x], Trig -> True]

ToString

ToString has several options that I won't address here because I never became familiar with them. However, there is an interesting problem with converting a symbol to a string.  How can we convert a symbol to a string without letting the symbol evaluate?  ToString2 below does the job. Notice evaluation has to be prevented in two places here.

mass = 56;
ClearAll[ToString2]
Attributes[ToString2] = {HoldAll};
ToString2[a_] := ToString[Unevaluated[a]]

In the next cell we get the characters used to make the Symbol mass without letting the symbol evaluate.

InputForm[ToString2[mass]]

Sometimes you might want a function to convert one of it's arguments to a string before the argument evaluates. Then you might want your function to do something with the resulting string. In this case a pure function can be used to do the string conversion. The next cell defines a function Letters that returns the characters used to make the argument before the argument has a chance to evaluate.

ClearAll[Letters];
Attributes[Letters] = {HoldAll};
Letters[s_] := 
      Characters[Function[
            a,   ToString[Unevaluated[a]],   {HoldAll}] [
          s]    ]

Letters[mass]

Notice mass still has the value that was assigned above.

mass

Tr

Tr computes the trace of a rectangular matrix.  Some examples are given in the next two cells.

Clear["Global`*"];
Tr[{{a, b, c, d, e}, {v, w, x, y, z}}]

mtrx = {
{a1, a2, a3}, 
{b1, b2, 
            b3}, 
{c1, c2, 
            c3}
};
Tr[mtrx, Max]

Computing the trace of a matrix is a common operation in linear algebra.  Mathematica generalizes the notion of trace to make it so we can compute Tr[vector].  When Tr is given a vector we get the sum of the elements.  The use of (Tr) in the next cell does the same thing as Plus @@ {a, b, c, d}.  Rob Knapp showed us that when a vector is a packed array  (Tr[vector] ) is much faster than (Plus @@ vector).  My timing tests vary, but I found Tr to be at least 25 times faster when given a packed array.

Tr[{a, b, c, d, e}]

Next I give some examples that go down to different levels of a tensor.

t1 = {{{{a1, b1}, {a2, b2}, {a3, b3}}, {{c1, d1}, {c2, d2}, {c3, 
                d3}}}, 
{{{e1, f1}, {e2, f2}, {e3, 
                f3}}, {{g1, h1}, {g2, h2}, {g3, h3}}}};
Dimensions[t1]

By default Tr works at the deepest level, which is level 4 in this example.

Tr[t1]

Tr[t1, Plus, 4]

t1[[1, 1, 1, 1]]  + t1[[2, 2, 2, 2]]

Next Tr is used at level 3.

Tr[t1, Plus, 3]

t1[[1, 1, 1]] + t1[[2, 2, 2]]

Next Tr is used at level 2.

Tr[t1, Plus, 2]

t1[[1, 1]] + t1[[2, 2]]

Finally Tr is used at level 1.

Tr[t1, Plus, 1]

t1[[1]] + t1[[2]]

Transpose

Transpose can be used on tensors as well as on matrices and the expressions don't have to be nested lists either.
By using the second argument of Transpose you can control the levels that are transposed.

Clear[a, b, c, d, e, f, g, h];
tt = {{{{1, a}, {2, b}}, {{3, c}, {4, d}}}, {{{5, e}, {6, f}}, {{7, 
                g}, {8, h}}}};

    
MatrixForm[tt]

Transpose[tt]//MatrixForm

Transpose[
        tt, {1, 2, 4, 3}]//MatrixForm

Transpose[tt, {2, 1, 4, 3}] // MatrixForm

Tr will do the same as the following, but faster when done with a large expression.

Transpose[tt, {1, 1}] // MatrixForm

Unevaluated

When Unevaluated[expr] is an argument of a function (expr) is given to the function it is an argument of without evaluating.  Consider the example in the next cell.  If it were not for the use of Unevaluated the sum would evaluate to 30 which has the head Integer.  Instead the head Plus is returned.

Head[Unevaluated[2 + 3 + (5^2)]]

Often times when we make a function with a holding attribute (e.g.  HoldAll, HoldRest, HoldFirst) we have to use Unevaluated in the definition of the function.  Consider the function in the next cell that converts it's argument to a string before the string can evaluate.

Here the HoldAll attribute prevents MakeString from evaluating it's argument.  However, we also need to use Unevaluated to ensure ToString doesn't evaluate it's argument.

ClearAll[MakeString]
Attributes[MakeString] = {HoldAll};
MakeString[a_] := 
      ToString[Unevaluated[a]] <> " is a string"

The next cell demonstrates the MakeString function.  We see that (mass) is converted to a string even though the symbol has a numeric value assigned to it.

mass = 56;
{mass, MakeString[mass]}

For more understanding of Unevaluated see examples in my explanation of the Mathematica evaluation process.

Update

To see a situation where use of Update is needed see the section on Condition.

UpValues

Normally Cosh[x]+Sinh[x] doesn't evaluate to Exp[x] but there are different ways of making the kernel automatically perform this simplification.  A few ways to do this are discussed in the cells below.  Before we begin the next cell should be evaluated to unprotect the functions that will be modified.

Unprotect[Plus, Cosh, Sinh];
Clear[Plus, Cosh, Sinh, t, x]

Once Plus is unprotected evaluating the next cell will cause an expression such as Cosh[x] + Sinh[x] to evaluate to Exp[z].

Cosh[z_] + Sinh[z_] := Exp[z]

The rule above has the desired effect in the cell below.

(1/(1 + Cosh[t + 1.5] + Sinh[t + 1.5]))

However, Plus is a function that's used very often, and the code above will slow down evaluation of Plus.  Actually it doesn't slow down Plus as much as one might think.  In  Mathematica versions 3 and 4 the functions Plus and Times apply built in rules before user defined rules.  So when the next cell evaluates the kernel uses a built-in rule to compute (2+3) before it checks for the above user defined identity.  Likewise built-in rules are used for (3+2/3), and (x+2 x), so here too the kernel never gets a chance to see if the user defined rule above should be used.  As a result the user defined rule had no effect on the speed of evaluating the cell below.

(Sqrt[2 + 3]) Exp[3 + 2/3] (x + 2 x)

On the other hand consider evaluation of the next cell after the above identity for Cosh[z_]+Sinh[z_] is in the kernel.

Exp[3 + 2/3 + x] (Sqrt[2 + 3 + x]) Sin[x + 2 x + y]

When evaluating the cell above the kernel uses a built-in rule to evaluate  (3 + 2/3 + x⟶11/3 + x ), then it checks to see if the user defined rule for Sinh[z_] + Cosh[z_] applies to (11/3 + x).  Later in the evaluation the kernel uses a built-in rule to evaluate (2 + 3 + x ⟶5 + x) and it checks to see if the user defined rule for Cosh[z_] + Sinh[z_] applies to (5 + x).  Still later in the evaluation the kernel uses a built-in rule to evaluate (x + 2 x + y ⟶3 x + y) and it checks to see if the user defined rule for Cosh[z_] + Sinh[z_] applies to (3 x + y).  In this case the user defined rule for Cosh[z_] + Sinh[z_] causes degradation in the time it takes to evaluate the last example even though the functions Cosh, Sinh appear nowhere in the expression.

The user defined rule above was a "DownValue" for Plus as can be seen from the next cell.  When the rule was entered above the kernel determined that Plus is the head of Cosh[z_] + Sinh[z_] and that is why it was associated with Plus.

DownValues[Plus]

Instead of associating the above identity with Plus (an often used function) we can associate the identity with a head one level deeper than Plus.  In this case we can associate the identity with Cosh and/or Sinh as an UpValue.  Before proceeding we should do away with the above definition.

Clear[Plus, Cosh, Sinh]

Evaluating the next cell will associate the identity with Sinh as an UpValue.  For more on UpValues see The Mathematica Book section 2.4.10.

Sinh /: Cosh[z_] + Sinh[z_] := Exp[z]

By starting the line above with Sinh/:  the kernel knows to associate the identity with Sinh.  Once the previous cell is evaluated the next cell can be used to see that the identity is associated with Sinh.

UpValues[Sinh]

At first one might guess that the above Identity would cause the next cell to change into Exp[3.5] which then evaluates to 33.1155.  However Cosh[3.5] evaluates to 16.5728, and Sinh[3.5] evaluates to 16.5426 before the kernel checks for the above identity.

Cosh[3.5] + Sinh[3.5]

Now when the next line evaluates the above identity is used, but only after the kernel finds it can't do anything with Cosh[3] or Sinh[3].

Cosh[3] + Sinh[3]

Now consider evaluation of the expression below.  Here the above identity is still stored in UpValues[Sinh] and not  in DownValues[Plus] or anywhere else.  Since the identity isn't stored with Plus there is no impact on the evaluation of (x + 1), Cosh[x + 1] , (3 + Cosh[x + 1] ) or (((3 + Cosh[x + 1] ))^(-1)).  In fact the above identity has no impact on the evaluation of  Sinh[3].  However, when the kernel evaluates (2 x Sinh[3]) it checks for user defined UpValues of Integer (the head of 2), but there are none.  Then the kernel checks for user defined UpValues of (x), but there are none.  Then the kernel checks the for user defined UpValues of Sinh, and there is an UpValue.  At that point the kernel must see if the UpValue for Sinh can be used, but finds that it can't.  Then the kernel checks for built in UpValues for Integer, x, Sinh and either finds none or none that apply to  (2 x Sinh[3] ).  Then the kernel checks for user defined DownValues for Times and finds none.  Finally the kernel checks for built in DownValues for Times, and either finds none or none that apply to (2 x Sinh[3]).  At that point evaluation is complete.

((2 x Sinh[3])/(3 + Cosh[x + 1]))

Storing the identity for Cosh[z_] + Sinh[z_] with UpValues[Sinh] should have much less impact on the evaluation of typical expressions than storing it with DownValues[Plus].  However, this identity will have an impact on the evaluation of many expressions that include Sinh, and this is likely the reason why Wolfram Research didn't include it as part of the default evaluation process.

Evaluating the next cell will remove any the above definitions.

Clear[Plus, Cosh, Sinh]

Instead of storing the above identity with UpValues[Sinh] one could store it with UpValues[Cosh] using the next cell.  The only difference this will make is that it will slow down evaluation some Cosh[z_] expressions, but never slow down evaluation of Sinh[_] expressions.  Starting the next line with Cosh/:  tells the kernel to store the identity with UpValues[Cosh].

Cosh /: Cosh[z_] + Sinh[z_] := Exp[z]

Still another way to store this identity is to use the next line.  That will store the identity with UpValues for each symbol at level 1 of (Cosh[z_] + Sinh[z_] ).  In this case Plus is at level 0 and Sinh, Cosh are the only symbols at level 1.  This approach will slow down evaluation of Sinh[_] and Cosh[_] expressions with no advantage over storing the identity only with UpValues[Sinh], or only with UpValues[Cosh].

Cosh[z_] + Sinh[z_] ^:= Exp[z]

All example above used UpValues with delayed assignment.  Mathematica provides ways to make UpValues with immediate assignment, but one should be very cautious of them.  This is done in the cells below.  In each case then variable (z) is used for the named pattern on the left side, but when (z) has a global value this global value will be used on the right side.

z = 7 + 5 I;
Clear[Plus,  Sinh,  Cosh]
Sinh /: Cosh[z_] + Sinh[z_] = Exp[z];

(Sqrt[x]) + Cosh[3] + Sinh[z]

Instead a definition such as the one above should be stored as an UpValue with delayed assignment as I do in the next cell.  When delayed assignment is used a global value assigned to (z) will not affect the results.

Clear[Plus,  Sinh, Cosh];
Sinh /: Cosh[z_] + Sinh[z_] := Exp[z]

(Sqrt[x]) + Cosh[3] + Sinh[3]

We can't assign a definition with a symbol that's too deep inside an expression.

We can use UpValues to store the identity (Sin[z_]^( 2) + 
            Cos[z_]^( 2) = 1).  However, this identity can't be stored with the UpValues[Sin] or UpValues[Cos] because Sin, Cos are too deep in the expression ( 
        Sin[z_]^( 2) + Cos[z_]^( 2) ).  UpValues must be stored with a symbol that appears at level 1.  In this case Plus is at level 0, Power is at level 1, Sin, Cos, Integer are at level 2.  The identity can be stored in  UpValues[Power] as in the next cell.

Unprotect[Plus, Power];
Clear[Plus, Power, b];
Power /: Sin[z_]^2 + Cos[z_]^2 = 1;

Sin[b + π/6]^2 + Cos[b + π/6]^2

UpValues[Power]

We can also store the identity with DownValues[Plus] using the next cell.

Unprotect[Power, Plus];
Clear[Plus, Power, a]
Sin[z_]^2 + Cos[z_]^2 = 1;

Sin[a + π/3]^2 + Cos[a + π/3]^2

DownValues[Plus]

Before continuing you might want to evaluate the following to remove all definitions made above.

Unprotect[Plus, Cosh, Sinh, Power];
Clear[Plus, Cosh, Sinh, Power];
Protect[Plus, Cosh, Sinh, Power];

Verbatim

The Verbatim usage message is shown below.

?Verbatim

Verbatim[expr] is used when you want the pattern matcher to ignore the normal meaning of a pattern matching feature.  To demonstrate the next cell shows the FullForm of some patterns.

FullForm[{x_, y_List}]

The attempt to delete all such patterns in the next cell fails because the pattern matcher treats the use of  "Pattern" here as a pattern matching construct instead of an actual form to search for.

DeleteCases[{x_, y_List, 3, 4, 5}, Pattern[_, _Blank]]

In the next cell the pattern matcher knows to ignore the meaning of "Pattern" so (x_) and (y_List) are deleted.  In this case only Pattern was wrapped in Verbatim so the pattern matcher did consider the meaning of (_) and (_Blank).

DeleteCases[{x_, y_List, 3, 4, 
        5}, Verbatim[Pattern][_, _Blank]]

In the next example (Pattern[_,_Blank]) is wrapped in Verbatim so only that literal expression is deleted.

data = {x_, y_List, 3, 4, 5, Pattern[_, _Blank], 6, 
          7};
DeleteCases[data, Verbatim[Pattern[_, _Blank]]]

Suppose you're given a list and want to get the element that matches the pattern (opt1->_).  The attempt in the next cell doesn't work because use of a rule as the second argument in Cases has special meaning.

Cases[{1 + x, opt1 -> val, opt2 -> val2, opt1}, 
      opt1 -> _]

In the next cell Verbatim makes Cases treat it's second argument as a form to search for instead of replacing (opt1) with (_).

Cases[{1 + x, opt1 -> val1, opt2 -> val2, 
        opt1}, Verbatim[Rule][opt1, _]  ]

$OutputForms

The $OutputForms usage message is shown below.

?$OutputForms

m = 
      MatrixForm[Inverse[{{1, 2, 0}, {0, 2, 3}, {1, 2, -1}}]]

Notice Head[m] returns MatrixForm, but Head[%] retunrs List.

{Head[%], Head[m]}

The next cell removes MatrixForm from the list assigned to $OutputForms.

Unprotect[$OutputForms];
$OutputForms = Complement[$OutputForms, {MatrixForm}]

m = 
      MatrixForm[Inverse[{{1, 2, 0}, {0, 2, 3}, {1, 2, -1}}]]

Now that MatrixForm isn't in the list returned by $OutputForms, we get MatrixForm for Head[%] and Head[m].

{Head[%], Head[m]}

The next cell restores the default setting of $OutputForms.

$OutputForms = Union[$OutputForms, {MatrixForm}];

$PreRead, $Pre, $Post, $PrePrint

The evaluation of expressions can be affected by definitions assigned to symbols 
$PreRead, MakeExpression, $Pre, $Post, $PrePrint, Format, MakeBoxes. The process of going from input cell to output cell is as follows.  Each of these steps requires evaluation of an expression, and that evaluation process is explained in a later section.

   (1)  Input Boxes are processed using $PreRead.
   (2)  The boxes $PreRead returns are converted to an expression using MakeExpression.
   (3)  The expression MakeExpression returns is processed using $Pre.
   (4)  The expression $Pre returns is evaluated.
   (5)  The expression returned by evaluation is processed by $Post.
   (6)  The expression $Post returns is assigned to (%n) and stored in DownValues[Out].
   (7)  The expression that was assigned to (%n) is processed by $PrePrint.
   (8)  The expression $PrePrint returns is processed by definitions made using Format.
   (9)  The expression Format returns is converted to boxes using MakeBoxes.

$PreRead

Consider the input in the next cell.  The 2-D representation of the cell is stored as 
SqrtBox[
      RowBox[{
        FractionBox["35", "100"], "+", "1"}]]
        
Before Mathematica can evaluate this input it converts it to the expression
     Power[Plus[Times[35,Power[100,-1]],1],Rational[1,2]]
     
However, before the boxes are converted to this expression Mathematica checks to see if the boxes should be changed using $PreRead.

(Sqrt[1 + (35/100)])

The next cell cause $Pre to change the box representation of (num/den)into the box representation of MyDivide[num,den].

Clear[$PreRead, $Pre, $Post, $PrePrint, 
        MyDivide];
$PreRead = (# /. 
              FractionBox[num_, den_] :> 
                RowBox[{"MyDivide", "[", 
                    RowBox[{num, ",", 
                        den}], "]"}]) &;

(Sqrt[1 + (35/100)])

The next cell removes the above $PreRead assignment.

$PreRead =.

$Pre

In the next cell $Pre is given the input before evaluation and changed any occurance of 100 into the symbol Hundred.  Use of Unevaluated and HoldAll is needed to ensure the expression doesn't evaluate before making the replacement.

Clear[$PreRead, $Pre, $Post, $PrePrint, 
        Hundred];
$Pre = 
        Function[expr, 
          Unevaluated[
              expr] /. (100 -> Hundred), {HoldAll}];

Below we see that $Pre makes the replacement before making the calulation  (35/100)⟶(7/20).

(Sqrt[(35/100) + 1])

The next cell removes the above $Pre assignment.

$Pre =.

$Post

In the next cell we make $Post display matrices in MatrixForm and all other results are not affected. The symbols returned by evaluating $OutputForms get special treatment in that they affect the printed output, but not the results assigned to (%n). By default $OutputForms includes MatrixForm, TableForm, ScientificForm and other built-in formatting functions.  Provided MatrixForm is in the list returned by $OutputForms we could have assigned the definition below to $PrePrint and got the same results.

Clear[$PreRead, $Pre, $Post, $PrePrint];
$Post = (# /. 
              mtrx_?MatrixQ :> 
                MatrixForm[mtrx]) &;

After making the assignment above the output of the following is automatically displayed in MatrixForm.

mtrx = {{1, 2, 0}, {0, 2, 3}, {1, 
          2, -1}}
{Head[%], Head[mtrx]}

The next cell makes it so MatrixForm isn't included in the $OutputForms list

Unprotect[$OutputForms];
$OutputForms = Complement[$OutputForms, {MatrixForm}];

When MatrixForm isn't in the $OutputForms list Head[%] below is MatrixForm.

mtrx = {{1, 2, 0}, {0, 2, 3}, {1, 
          2, -1}}
{Head[%], Head[mtrx]}

The next cell restores the default setting of $OutputForms and removes the above $Post setting.

$OutputForms = Union[$OutputForms, {MatrixForm}];
Clear[$PreRead, $Pre, $Post, $PrePrint];

$PrePrint

$Post and $PrePrint are very similar and are normally used to affect the way output is displayed.  For example in the next cell we make $PrePrint display matrices in MatrixForm and all other results are not affected. $PrePrint is used after results are assigned to %n, so use of $PrePrint will not affect assignments to (%n) regardless of what is assigned to $OutputForms.

Clear[$PreRead, $Pre, $Post, $PrePrint];
$PrePrint = (# /. 
              mtrx_?MatrixQ :> 
                MatrixForm[mtrx]) &;

After making the assignment above the output of the following is automatically displayed in MatrixForm. Because of the way $PrePrint works we get List for Head[%] below regardless of what is assigned to $OutputForms.

mtrx = {{1, 2, 0}, {0, 2, 3}, {1, 
          2, -1}}
{Head[%], Head[mtrx]}

Clear[$PreRead, $Pre, $Post, $PrePrint]

$Post versus  $PrePrint  (a closer look)

Consider the use of $PrePrint in the next cell.

Clear[$PreRead, $Pre, $Post, $PrePrint];
$PrePrint = (# /. 
              x_?NumericQ :> x + 1) &;

After making the previous assignment to $PrePrint, the next output shows (1+π) instead of simply π.  There is clearly no pratical use for this sort of thing.  I am only doing this to illustrate the difference between $Post and $PrePrint.

x = π

The result stored with (%n) from the evaluation above is π because $PrePrint is used after the result is assigned to %n. This is demonstrated in the next cell where the previous output is accessed using (%%).

Clear[$PreRead, $Pre, $Post, $PrePrint];
%%

In the next cell we use $Post to do the same thing we did with $PrePrint in the previous example.  When we use $Post the replacement is made before the result is assigned to (%n).

Clear[$PreRead, $Pre, $Post, $PrePrint];
$Post = (# /. x_?NumericQ :> x + 1) &;

x = π

In the next cell we see (1+π) is assigned to (%n) when the replacement is made using $Post.

Clear[$PreRead, $Pre, $Post, $PrePrint];
%%

EvaluateAt

The function in the next cell takes an expression and a list of positions, and evaluates in place the parts at the specified positions. This is taken verbatim from "further examples" for ReplacePart in the Help Browser.

EvaluateAt[expr_, positions_] := 
      Fold[ReplacePart[#1, Part[#1, Sequence @@ #2], #2] &, expr, 
        positions]

In this case, {1,2} specifies the second element of the held list, and {1,-1,1} the first part of the last element.

EvaluateAt[
      HoldForm[{2 + 3, 3 + 4, 2*4 + 5}], {{1, 2}, {1, -1, 1}}]

Evaluate Pattern

The function below evaluates all parts of a held expression that match a certain pattern. This is based on some code Robby Villegas of Wolfram Research discussed at the 1999 Developer Converence.  See "Working With Unevaluated Expressions" posted at 
http://library.wolfram.com/conferences/devconf99/#programming.
At that conference Micheal Trott and Adam Strzebonski of Wolfram Research are mentioned as the inventors of this trick : see "Trott-Strzebonski method for In-Place Evaluation".

EvaluatePattern[expr_, pattn_]:=expr/.Pattern[p, pattn] :> With[{eval  = p},  eval  /;  True]

The next cell creates a held expression and evaluates all sub expressions with the Head Plus but nothing else evaluates. In this example Erf[∞]+5, 1+3, 5+4, (Sqrt[36]) - (Sqrt[16]) evaluate to 6, 4, 9, 2 respectively since they each have the head Plus.

demo = 
        HoldForm[(Erf[∞] + 
                5) Sin[(π/(1 + 3))] ((Sqrt[5 + 4])/((Sqrt[36]) - (Sqrt[16])))];
EvaluatePattern[demo, _Plus]

ReplaceAll at subexpressions matching a pattern

\(\(\(PatternReplaceAll::usage = "PatternReplaceAll[expr, pattern, rule] uses ReplaceAll to make the changes specified by rule, but the replacements are only made to subexpressions of expr that match pattern. PatternReplaceAll[expr, pattern, {rules}] uses one or more rules.";\)\(
\)
    \)\)
PatternReplaceAll[expr_, 
        pattn_, {rules : (_Rule | _RuleDelayed) .. }] := 
      With[{posn = Reverse@Sort@Position[Hold @@ {expr}, pattn]}, 
        ReleaseHold[
          Fold[MapAt[Function[a, a /. {rules}], #1, #2] &, Hold @@ {expr}, 
            posn]]]

    
PatternReplaceAll[expr_, pattn_, 
        SingleRule : (_Rule | _RuleDelayed)] := 
      PatternReplaceAll[expr, pattn, {SingleRule}]

Comments about the code above:

(1)  (Reverse@Sort...) ensures the later positions in (posn) still apply even after folding several times. 

(2)  We can't let the replacements take effect until after Fold is done because the 
     position of things may change.  To solve that I use Hold@@{expr}.

(3)  I don't use (rules__?OptionQ) because OptionQ[n_Integer->n+2] returns False. 
     Instead I use  rules:(_Rule|_RuleDelayed).

Now some examples:

ex = 
        b*Gamma[1 + a]^2*Gamma[1 + b]/Gamma[2, 4 + 2  b]*
          PolyGamma[2*b]*(b + 2)^2;

Here we use the rule (b->bb) on all subexpressions with the head Gamma.

PatternReplaceAll[ex, _Gamma, b -> bb]

Here we use the rule (n_Integer->n+2) on all subexpressions with the head Gamma.

PatternReplaceAll[ex, _Gamma, n_Integer -> n + 2]

Here we use both rules (b->bb, n_Integer->n+2) on all subexpressions with the head Gamma.

PatternReplaceAll[
      ex, _Gamma, {b -> bb, n_Integer -> n + 2}]

Making a periodic function

In the next cell I define a periodic function that's piecewize linear.  
This example would still work if I changed 
f[x_?(Im[#]===0&)] := f[Mod[x,5]]   to simply 
f[x_] := f[Mod[x,5]]   but by using the more complicated the definition (f) is only used for real arguments.

ClearAll[f];
f[x_?(0 ≤ # < 5 &)] := 
        If[x < 2, 3,  x - 4];
f[x_?(Im[#] === 0 &)] := f[Mod[x, 5]];
Plot[f[x], {x, 0, 13}];

However, Mathematica can't do things like Integrals or Fourier series on the function defined above.  If you wanted to find several terms of the Fourier series of the above function I recommend defining the function over the period (-2.5 ≤ x ≤ 2.5) using UnitStep functions as in the next cell.  Of course the function isn't actually periodic this way, but the function FourierTrigSeries doesn't care about that.

ClearAll[f];
f[
          x_] := (1 + x) UnitStep[x + 5/2] + (2 - x) 
            UnitStep[x] + (-7 + x) UnitStep[x - 2];

Needs["Calculus`FourierTransform`"];
FourierTrigSeries[f[t], t, 4, FourierParameters->{0,5/2}]

In general you should find that Mathematica's full symbolic capabilities including Integrate, LaplaceTransform, etc. can be used on piecewise continuous functions defined in terms of UnitsStep functions. This isn't true for the programming style used earlier.

Take generalized

The #& notation is explained in the discussion of Function.

ClearAll["Global`*"];
\(\(\(TakeRepeated[ls_List, t_List] := 
      First /@ Rest[

          FoldList[Through[{Take, Drop}[#1[\([2]\)], #2]] &, {{}, ls}, 
            t]]\)\(
\)
    \)\)
a1 = {a, b, c, d, e, f, g, h, i, j, k, l, m};
TakeRepeated[a1, {2, 3, 4, 1}]

Portions of the function above are implemented below to illustrate how it works.

tst = 
        FoldList[
          Through[{Take, Drop}[#1[[2]], #2]] &, {{}, a1}, {2, 3, 4, 
            1}];
Do[Print[tst[[n]]   ], {n, 5}]

The effects of FoldList and Through are hard to grasp.  The lines below demonstrates what they do.

FoldList[f, x, {2, 3, 4, 1}]   // TableForm

ClearAll[f, g];
Through[(f + g)[x]]

Thread generalized

This was written by Carl Woll.

ClearAll["Global`*"];
\(\(\(WollThread[h_, args_List] := 
	
      Table[h @@ Part[args, Flatten[Position[Length /@ args, a_ /; a >= i]], 
            i], {i, Length[args]}]\)\(
\)
    \)\ \)
WollThread[Plus, {{a, b}, {c, d, e}, {f}}]

Flatten without evaluating subexpressions

This was written by Dave Wagner.

ClearAll["Global`*"];
Attributes[MyFlatten] = {HoldFirst};
\(\(\(MyFlatten[
        stuff_] := 
\ \ Flatten[Hold[stuff] /. List -> Hold, Infinity, 
          Hold] /. 
		Hold[x__] :> Hold[{x}]\)\(
\)
    \)\ \ \)
MyFlatten[{{Sqrt[4] + 3, x1}, {{x3}, x4}}]

Preventing TrigExpand from expanding as far as it can

You may not want TrigExpand to go as far as possible in expanding the trigonometric expression below.  Tom Burton prevents TrigExpand from expanding the products by changing the Head Times, and then changing it back to Times after using TrigExpand.

Clear["Global`*"];
expr = 
        Sin[2  π  ω  t + 2 π  δ];
TrigExpand[expr]

\(TrigExpand[expr /. HoldPattern[Times[p__]] :> TimesHold[p]] /. 
	
      TimesHold[p__] :> Times[p]\)

Finding the Domain of a defined function

Suppose we have a function (f[_]) that is defined for certain arguments.
The Cells below include a  program designed to indicate the values where (f) is defined.

ClearAll[f, Domain];
f[1] = Sin[1];
f[2] = Sin[2];
f[q_Rational] := Sin[Numerator[q]]
f[n_?Negative] := Sin[-n]

Definitions for (f) are stored in DownValues[f].

DownValues[f]

A certain part of each DownValue indicates the type of argument for which (f)
is defined.  This operation is an important part of the function below which gives the same
result without the Pattern matching notation.

Part[#, 1, 1, 1] & /@ DownValues[f]

ClearAll[Domain];
\(Domain[func_] := 
      Module[{temp, t0, t1, 
          t2}, 
\((temp = \(Part[#, 1, 1, 1] &\) /@ 
              DownValues[f]; 
			t0 = 
            Select[temp, NumericQ]; 
			t1 = 
            Map[Part[#, 2, 1] &, Cases[temp, _Pattern]]; 
			t2 = 
            Map[Part[#, 2] &, Cases[temp, _PatternTest]]; 
			Join[t0, 
            t1, t2])\)
	]\)

Now Domain[f] can be use to determine the values where (f) is defined.

Domain[f]

Finding the constant term(s) in a sum

The short program below will find the constant term in a sum.  The #& notation used here is explained in the discussion of Function.

ClearAll[ConstantTerm, x, a, b];
ConstantTerm[expr_?NumericQ] := expr;
ConstantTerm[expr_Plus] := 
        Select[expr, NumericQ[#] &];

    
ConstantTerm[expr_] /; 
          MemberQ[Attributes[Evaluate[Head[expr]]], NumericFunction] = 
        0;

Below we see several examples where the constant term is found.  When the expression has no constant term 0 is returned.

ConstantTerm[
      1/3  +  x  +  Sqrt[2]  +  x y^2  +  
        Pi  +  ((3 )/(1 + (Sqrt[y + 1])))]

ConstantTerm[x  +  x y^2  -  x^2]

ConstantTerm[(π/(4 + π/2)) x]

ConstantTerm[Sqrt[x] + 1/x + 5 + Sqrt[2]]

ConstantTerm isn't defined if it's given an argument that isn't a numeric function.

ConstantTerm[a && b]

An alternate definition for ConstantTerm with the same effect is given in the next cell.

ClearAll[ConstantTerm];
\(ConstantTerm[poly_?PolynomialQ] := 
	
      With[{posn = 
\ \ \ \ \ \ Position[
              poly, _Symbol?\((Not[
                      NumericQ[#]] &)\), 
	\ \ \ \ \ \ \ Heads -> 
                False
	\ \ \ ]
\ \ }, 
	
        ReplacePart[poly, 0, posn]
]\)

KroneckerProduct of matrices

ClearAll["Global`*"];
KroneckerProduct[A_?MatrixQ, 
        B_?MatrixQ] := 
With[{tensor = 
            Outer[Times, Transpose[A], B]}, 
  Flatten[
          MapThread[Join, tensor, 2], 1]]

A = {{a11, a12}, {a21, a22}};
B = {{b11, b12}, {b21, b22}};
KroneckerProduct[A, B] // MatrixForm

InterpolatingFunction form for the solution to a system of equations.

Carl Woll gave the solution below on how to find Interpolating functions that solve a set of equations.  
Suppose you are trying to find Interpolating functions for y[t], x[t] that solve the equations 
{ 
        x[t]^2 + y[t]^2 == (t^2)  ,   y[t] == x[t]^2 }   for (t) between 0 and 1.

ClearAll[x, y, f, g, t];
f[t_] := x[t]^2 + y[t]^2 - (t^2);
g[t_] := y[t] - x[t]^2;
eqs = {f'[t] == 0, g'[t] == 0, f[1] == 0, 
        g[1] == 0}

soln=NDSolve[eqs,{x,y},{t,0,1}]

x1=x/.soln[[1]];
y1=y/.soln[[1]];

Block[{$DisplayFunction=Identity},
    p1=Plot[Re[x1[t]],{t,0,1},PlotLabel->"Real Part of x"];
    p2=Plot[Im[x1[t]],{t,0,1},PlotLabel->"Imaginary Part of x"];
    p3=Plot[Re[y1[t]],{t,0,1},PlotLabel->"Real Part of y"];
    p4=Plot[Im[y1[t]],{t,0,1},PlotLabel->"Imaginary Part of y"];
    ];
Show[GraphicsArray[{{p1,p2},{p3,p4}},GraphicsSpacing -> 0.2,ImageSize->{550,380}]];

Making a list of integers relatively prime to n

Suppose we want to find a list of integers relatively prime to a Positive Integer (n). An immediate candidate is the function in the next cell, but it isn't very fast for large n because it has to examine each integer between 2 and (n-1).

ClearAll[RelativePrimes, pdLst, g];
RelativePrimes[n_Integer] /; n > 2 := 
Select[
        Range[n - 1], GCD[#, n] === 1 &]

RelativePrimes[400000]; // Timing

Ranko Bojanic found a faster solution that uses the fact  that  k< n is relatively prime to n if it is not divisible by any prime divisor of n. The list of  prime divisors of n is obtained by the following function.

pdLst[n_] := First[Transpose[FactorInteger[n]]]

The definition for (pdList) in the next cell is faster, but it doesn't work in Version 3 or earlier. I am sort of splitting hairs here because an expression has to be quite large for Part[expr, All, 1] to be significantly faster, and FactorInteger will seldom return a list with more than (10^6) factors.

pdLst[n_] := Part[FactorInteger[n], All, 1]

If (d) is a prime divisor of (n), then a list of integers between 2 and (n-1) which are not divisible by n is given by the following line.

g[n_Integer, d_Integer]:=Complement[Range[2, n - 1],  d Range[n / d]]

We have to take all these lists, for all prime divisors of n, and their intersection will be the list of all integers between 2 and (n-1) which are relatively prime to n.  It is now easy to see that the list of relative prime integers of n can be found from the following function.

ClearAll[RelativePrimes];
RelativePrimes[n_Integer] /; n > 2:=
Module[{g = 
              Complement[
                  Range[2, 
                    n - 1], #  Range[
                      n / #]] &, 
pdLst = 
              First[Transpose[FactorInteger[n]]]}, 
Apply[
            Intersection, Map[g, pdLst]]
]

RelativePrimes[42]

The solution in the next cell is a slight variation of a program Alan Hayes wrote and it's a little faster than the last program. Keep in mind this variation doesn't work in Mathematica Version 3 or earlier because of the use of Part[expt, All, 1].

ClearAll[RelativePrimes];
RelativePrimes[n_Integer] /; n > 2 := 
Fold[
        Complement[#, #2] &, 
        Range[2, n - 1], Range[#, n - 1, #] & /@ 
          Part[FactorInteger[n], All, 1]]

How the above implementation of RelativePrimes works

Lets consider what it does to compute RelativePrimes[42].

Part[FactorInteger[42],All,1] returns a list of prime factors of (42) which is {2.3.7}.
Then the pure function (Range[#,41,#]&) is mapped over the list {2, 3, 7}. This is computed as (lst1) in the next cell.. The #& notation is explained in the discussion of Slot and SlotSequence.

lst1 = Range[#, 41, #] & /@ {2, 3, 7}

Then (lst2) is simply a list of integers from 2 to 41 which is the output of the next cell.

lst2 = Range[2, 41]

Then the list of relative divisors is returned by effectively evaluating the next cell.

Fold[Complement[#, #2] &, lst2, lst1]

What Fold does in the previous cell is explicitly spelled out in the next cell.

Complement[
      Complement[Complement[lst2, Part[lst1, 1]], Part[lst1, 2], 
        Part[lst1, 3]]]

This implementation is elegant and runs efficiently in Mathematica, but I think the code is much more difficult to understand, so it's harder to maintain.

An Algebraic Transformation

A user in the MathGroup wanted Mathematica to change 
((-1+a) x-a y)\^2  into  ((a-1)x+a y)\^2
Allan Hayes gave the solution in the next cell.  This solution has to be entered on a case by case basis.

Clear[a, x, y];
((-1 + a) x - a y)^2 /. (p_^n_?EvenQ) :> 
        Collect[-p, x]^n

FactorRule in the next cell works and the rule doesn't need to know what variables are involved.  The only disadvantage of FactorRule is that it's more complicated than the first solution.  The use of (expr :) is discussed in my section on Pattern.

FactorRule = 
        expr : (__Plus)^(n_?
                  EvenQ) :> 
(Map[-# &, 
                  Part[expr, 1], 1]^n /. 
              Times[-1, a_, b___] :> -a*
                  b);

((-1 + a) x - a y)^2 /. FactorRule

A New Together

A user wrote to the MathGroup indicating that Together takes a very long time with expressions that have on the order of (10^7) leaves. They noticed together expands the numerator of the result as in the next example, and suspected time could be saved if the numerator wasn't expanded.

ClearAll["Global`*"];
Together[((a + b)/(c + d)) + ((e + f)/(g + f))]

expr = ((a + b)/(c + d)) + ((e + f)/(g + f));

Allan Hayes wrote SimpleTogether below which doesn't expand the numerator. Allan's code is given in the next cell and it's very fast and very slick.

SimpleTogether[
        expr_Plus] := ((Plus @@ (# List @@ expr))/#) &[
        Times @@ Union[Denominator /@ List @@ expr]]
SimpleTogether[expr_] := expr

In the next cell we see that SimpleTogether doesn't expand the numerator of the result.

SimpleTogether[((a + b)/(c + d)) + ((e + f)/(g + f))]

Notice Allan's program uses Union which has a SameTest option.  Some nuances of Union and it's option are discussed at:
http://support.wolfram.com/Kernel/Symbols/System/Union.html  where it says the default SameTest setting used by Union is stronger than using Equal or SameQ! This is demonstrated in the next line.

lst = N[Pi, 18] {1, 1 + (10^(-20))};
Union[lst]

Next Union returns only one of the numbers when SameQ is used for SameTest.

Union[lst, SameTest -> (SameQ[#1, #2] &)]

Considering the lines above I think Alan's SimpleTogether function should use Union with the option SameTest->(SameQ[{#1,#2}]&).  In some applications something else might be needed.  With that in mind I wrote a version of Allan's program that has a SameTest option. I also give SimpleTogether the options Modulus and Trig which the built-in version has and I give it a usage message. The only thing missing is the extension option which the built-in version of Together does have.

ClearAll[SimpleTogether];
SimpleTogether::usage = 
"SimpleTogether[expr] \
puts terms in a sum over a common denominator without 
factoring the numerator of the result.";

    
Options[SimpleTogether] = {SameTest -> (SameQ[#1, #2] &), 
          Modulus -> 0, Trig -> False};
Attributes[SimpleTogether] = {Listable};

    
\(\(\(SimpleTogether[expr_Plus, 
        opts___?OptionQ] := 
Module[{tst, mods, 
          trg}, 
{tst, mods, 
            trg} = {SameTest, Modulus, Trig} /. 
            Flatten[{opts, 
                Options[
                  SimpleTogether]}]; 
\(\((\((Plus @@ \((#\ List @@ expr)\))\)/#)\) &\)[
          Times @@ Union[\((\((Denominator[#, Modulus -> mods, 
                        Trig -> trg] &)\) /@ \(List @@ expr\))\), 
              SameTest -> tst]]
]\)\(
\)
    \)\)
SimpleTogether[expr_, opts___?OptionQ] := expr

Next we see that SimpleTogether still works.

SimpleTogether[((a + b)/(c + d)) + ((e + f)/(g + f))]

I don't demonstrate the options but the default settings are shown below.

Options[SimpleTogether]

Making a Tensor into a Matrix

Consider the tensor (t1) in the next cell.

Clear[t1];
t1 = {{{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, {{2, 2, 2}, {2, 2, 2}, {2, 
                2, 2}}}, {{{3, 3, 3}, {3, 3, 3}, {3, 3, 3}}, {{4, 4, 4}, {4, 
                4, 4}, {4, 4, 4}}}};
MatrixForm[t1]

A user in the MathGroup wanted to express this tensor as a matrix with MatrixForm in the next cell. Visually  this is simple. However, the complicated structure of a tensor makes it a challenge to write an elegant program that makes the conversion.

Allan Hayes provided the following two solutions in the MathGroup.

t2=Apply[Join, Join  @@  Transpose[t1,  {1,  3,  2}], 1];
MatrixForm[
      t2]

t2 = 
        Flatten[Map[Flatten, Transpose[t1,  {1,  3,  2}], {2}], 
          1];
MatrixForm[t2]

Now suppose you want to do the same thing on the higher rank tensor (t1) below

m1 = {   { {{11, 11}, {11, 11}}, {{12, 12}, {12, 
                12}}},  { {{13, 13}, {13, 13}}, {{14, 14}, {14, 
                14}} }};

    
m2 = {   { {{15, 15}, {15, 15}}, {{16, 16}, {16, 
                16}}},  { {{17, 17}, {17, 17}}, {{18, 18}, {18, 
                18}} }};

    
m3 = {   { {{19, 19}, {19, 19}}, {{20, 20}, {20, 
                20}}},  { {{21, 21}, {21, 21}}, {{22, 22}, {22, 
                22}} }};

    
m4 = {   { {{23, 23}, {23, 23}}, {{24, 24}, {24, 
                24}}},  { {{25, 25}, {25, 25}}, {{26, 26}, {26, 
                26}} }};

    
t1 = {{m1, m2}, {m3, m4}};
MatrixForm[t1]

Mathematica Version 4 has a NestWhile function that comes in handy here. The code in the next cell will merge together tensors of any rank.

t2=NestWhile[Apply[Join, Join  @@  Transpose[#,  {1,  3,  2}], 
                  1]&,t1,TensorRank[#] > 2 &];
MatrixForm[
      t2]

Distribute - A slick application

Dr. John Erb sent a problem to the MathGroup.
He had several pieces of plastic of different thickness and wanted elegant Mathematica code that would determine what thicknesses can be made by stacking together one or more of the pieces of plastic. 

Robby Villegas replied to the MathGroup in   [mg3203] Re: array ordered, of 18 Feb 1996:
This problem amounts to finding all subsets of the list of thicknesses T, and for each subset adding up its elements. Any subset of T can be described by giving a status to each of T's elements: absent or present (as noted in Jorma Virtamo's solution). In terms of a contribution to the total thickness, the ith element adds 0 if it is absent, or adds (ti) if it is present. Thus, if you take the Cartesian product of these ordered pairs: {0, t1} x {0, t2} x . . . x {0, tn} you get all possible combinations of plates, e.g. {t1, 0, 0, t4, t5, ...}, and you can add the elements of each combination. 'Distribute' is perfect for forming Cartesian products. .....
What follows is a slight variation of Robby's solution.
First we get a list of thicknesses paired with zero in s2.

s1 = {0.12, 0.34, 0.53, 0.53};
s2 = Thread[{0, s1}]

Next Distribute returns the cartesian product of all the ordered pairs.

lst = Distribute[s2, List, List]

As Robby Villegas pointed out Distribute can take third and fourth arguments which specify heads that should be used to replace the outer and inner heads that were distributed. In this case we want the outer head to remain as List and we want to add the sublists. Hence Distribute in the next line does the job.

Distribute[s2, List, List, List, Plus] // Union

Alternatively we can use the next cell to get lists of the form {{sum, list},...} where 'sum' is the total of the elements in 'list', and Union has removed elements with same sum, and sorted the list so the values of sum are increasing.

q1 = 
      Union[{Plus @@ #, #} & /@ lst, 
        SameTest -> (First[#1] === First[#2] &)]

Union without sorting

In June 2000 there was a long thread in the MathGroup and you can read about the discussion at 
http://library.wolfram.com/mathgroup/archive/2000/Jun/msg00115.html  on how to write an efficient function that does the same thing as Union without sorting the elements. Many agree that the best way to do this is with the following ingeniuos function written by Carl Woll.

ClearAll[DeleteRepititions];
DeleteRepititions[x_] := 
      Module[{t}, 
t[n_] := (t[n] = Sequence[]; n); 
        t /@ x
]

DeleteRepititions is demonstrated below.

DeleteRepititions[ {3, 1, 1, 6, 3, 2, 1, 1, 8, 2, 6, 8} ]

Actually the version Carl Woll posted used Block where I use Module above.  The function runs a bit faster when defined using Block, but then it gives the wrong result in the following example.

DeleteRepititions[ {3, 1, 1, t[3], 3, 2, t[3], 1, 1, 8, 2, 6, 
        8} ]

When evaluating an expression the mathematica kernel applies a collection of rules to an expression until the expression no longer changes.  In the discussion below I explain the evaluation process in greater depth.  This discussion of the evaluation process is based on a tutorial by David Withoff which is posted at   http://library.wolfram.com/infocenter/Conferences/4683/.   That tutorial applied to Mathematica 2.0, and further details to reflect changes in Mathematica 3.0 come from  Power Programming With Mathematica the kernel (by David Wagner).  The book by David Wagner is apparently out of print.  As far as I know there were no changes to the evaluation process in Mathematica 4 or Mathematica 5.

This discussion doesn't cover the use of MakeExpression, $PreRead, $Pre, $Post, $PrePrint, FormatValues, and MakeBoxes.  That is all discussed in an earlier section.  Each step discussed there requires completing the evaluation process discussed in this section.

Furthermore, this discussion doesn't address the pattern matching process.

Steps of Evaluating of  h[a1, a2, a3]

Below I talk about "external DownValues", "internal DownValues" and likewise for UpValues, SubValues, and NValues.  External values are those that are not part of the Mathematica kernel while internal values are those that are part of the kernel.  

The 17 steps below are repeated until an expression no longer changes, and the decision to end the process happens in step 3 below.  When the expression being evaluated does change the whole process starts again at the begininig on the new expression.  In pathalogical cases such as in the next cell evaluation completes due to exceeding $IterationLimit.  In other cases evaluation completes due to exceeding $RecursionLimit.  I don't discuss how counting iterarions and recursions fit into the evaluation process because I have never seen an explanation of when that happens.

Clear[f];
f[x_] := f[x + 1];
f[2]

This discussion overlooks the fact that NHoldAll, NHoldRest and NHoldFirst prevent N from approximating one or more argument of affected expressions.

Also the functions Plus and Times use internal UpValues and internal DownValues before external definitions, but that detail is overlooked below.

Step 1
If the expression being evaluated is a symbol with an OwnValue replace it with the OwnValue.  OwnValues are used when some value is assigned to a symbol (e.g.   x=5).

Step 2
Evaluate the head of the expression.

Step 3
If no part of the expression has changed during the last time around this procedure, return the expression.  This is an optimazation that prevents unecessary reevaluation of large expressions.

Step 4
If  (h) has the HoldAllComplete attribute skip to step 11 below. HoldAllComplete prevents any changes to the arguments of a function.  

If (h) doens't have attributes that prevent evaluation of some or all arguments, the argumenrts evaluate from left to right.  When (h) has either attribute (HoldFirst, HoldRest, HoldAll) evaluation continues without evaluating the affected arguments.  If any of the arguments have the head Unevaluated, then the head Unevaluated is removed and further evaluation of the argument is prevented.  In step 17 the head Unevaluated may be restored to the argument.

If (h) has either attribute (HoldFirst, HoldRest, HoldAll), and an argument of (h) has the head Evaluate, then the argument evaluates even if the attribute would have prevented evaluation.

Step 5
If (h) has the Flat attribute flatten layers of (h) as in the next cell.

ClearAll["Global`*"];
Attributes[h] = {Flat};
h[a1, h[h[a2], a3], h[a4]]

Step 6
If (h) does not have the SequenceHold attribute splice together sequences.  
In the next cell Sequences are spliced together.

ClearAll["Global`*"];
h[Sequence[a1, a2], Sequence[a3, a4], a5]

Next (h) has the SequenceHold attribute and sequences aren't spliced.

ClearAll["Global`*"];
Attributes[h] = {SequenceHold};
h[Sequence[a1, a2], Sequence[a3, a4], a5]

Step 7
If (h) has the Listable attribute thread (h) over Lists.  
This has the same effect as evaluating  Thread[ h[a1,a2,a3] ]  when (h) isn't Listable.

ClearAll["Global`*"];
Attributes[h] = {Listable};
h[{a1, a2, a3, a4}]

h[{a1, a2}, c, {b1, b2}]

Step 8
If (h) has the Orderless attribute sort the arguments of (h).  
Note the arguments are sorted in canonical order which may not be the same as numeric order when the arguments are numeric.  This is seen in the next cell.

ClearAll["Global`*"];
Attributes[h] = {Orderless};
h[π, 4, 2, 3]

Step 9
Use external Upvalues for the symbolic head of of each argument of the expression.  For example in the next cell we would use the external UpValues of (g1), then the external UpValues of (g2),  then the external UpValues of Derivative are used.

The "symbolic head" of an expression is the result of nesting Head until a symbol is returned.    Notice Derivative is the symbolic head of  ( f ' [a] ).  Here we have to nest head three times to get a symbol.  The head of of  ( f ' [a] ) is (f ' ), and the head of that is Derivative[1], which finally has the head Derivative.

ClearAll["Global`*"];
h[g1[a], g2[a], f'[a]];

Step 10
Use internal UpValues for the symbolic head of of each argument of the expression.  For example in the previous cell we would use the internal UpValues of (g1), then the internal UpValues of (g2),  then the internal UpValues of Derivative are used.

Step 11
If (h) in  h[g1[a],  g2[a],  g3[a] ]  is a symbol, the external DownValues for (h) are used.

Step 12
If (h)  in h[g1[a], g2[a], g3[a]]  is not a symbol the external SubValues of (h) are used.  The head of the expression in the next cell is h[1].  Since (h[1]) isn't a symbol the Subvalues of (h) would be used.

h[1][a, b, c];

Step 13
If (h) in  h[g1[a],  g2[a],  g3[a] ]  is a symbol, the internal DownValues for (h) are used.

Step 14
If (h)  in h[g1[a], g2[a], g3[a]]  is not a symbol the internal SubValues of (h) are used.

Step 15
Use external NValues if the expression being evaluated has the head N.  More precisely the NValues of the symbolic head of the first argument of N are used.

Step 16
Use internal NValues if the expression being evaluated has the head N.  As in the previous step the NValues of the symbolic head of the first argument of N are used.

Step 17
If no UpValues, DownValues, SubValues, or NValues were used, and any arguments of (h) did have the head Unevaluated. Then the head Unevaluated is restored to that argument.  This is demonstrated in the next cell.

ClearAll["Global`*"];
a = 26;
h[a + 4, Unevaluated[a]]

Unevaluated prevents sequences from splicing, and prevents attributes such as Flat and Orderless from taking effect.  This is demonstrated in the next cell.

ClearAll["Global`*"];
h[x + x, 
      Unevaluated[Sequence[x, 2  x], 
        Plus[z, Plus[a, d], Plus[d, a]]]]

However, in the next cell (h) has a downvalue that applies and we don't see Unevaluated in the result.

ClearAll["Global`*"];
h[x_, y_, z_] := h[x, y]
h[4 + a, Unevaluated[a], 0]

Some Examples

From the discussion above it may be hard to understand the order that parts of a complicated expression evaluate.  I provide two examples below to demonstrate.  First evaluate the next cell which makes some definitions we will use.

Clear["Global`*"]
a1 = b1;
a2 := {h[b2], b3};
b2 = c2;
b3 = b4;
h = g;
g[x_] := x + z;

Consider evaluation of the example in the next cell.

{{h[a1], a2}, a1}

You could see the order of evaluation using Trace or a similar function, but I find the output of these functions difficult to read. Instead I like to use Print statements as I do below.

Clear["Global`*"]
a1 := (Print["a1 -> b1"]; b1);
a2 := (Print["a2 -> {h[b2],b3}"]; {h[b2], 
            b3});
b2 := (Print["b2 -> c2"]; c2);
b3 := (Print["b3 -> b4"]; 
          b4);
h := (Print["h -> g"]; g);
g[x_] := (Print["g[_] -> " <> ToString[x] <> "+z"]; 
          x + z);

    
{{h[a1], a2}, a1}

Next consider how expressions evaluate when the head (h) in h[x] is a non-trivial expression.  The next cell gives one such example.

Clear["Global`*"];
h = g;
g[n_] := f[2 n];
f[n_][y_] := y^n
m = 3;
s = t;

    
h[m][s]

The next cell shows how evaluation in this example progresses.

Clear["Global`*"];
h = (Print["h -> g"]; g);
g[
          n_] := (Print["g[" <> ToString[n] <> "] -> f[" <>  
              ToString[2  n] <> "]"]; 
          f[2  n]);

    
f[n_][
        y_] := (Print["f[" <> ToString[n] <> "][" <> 
            ToString[y] <> "] -> " <> ToString[y] <> "^" <> 
            ToString[n]]; y^n)

    
m = (Print["m -> 3"]; 3);
s = (Print["s -> t"]; t);

    
h[m][s]

The order that definitions are stored and changing the order of the definitions is discussed in sections 2.4.7 and 2.4.13 of The Mathematica Book.

In some cases you might also need to write a function that will automatically examine the definitions of a certain symbol.  Your function might then return part of those definitions or automatically make changes to some of the definitions. In order to do this you need to know where the definitions are stored.  Mathematica stores definitions as DownValues, UpValues, OwnValues, NValues, SubValues, FormatValues, DefaultValues, and Messages.  Except for Attributes[symb] and Options[symb] anything that affects evaluation of expressions involving symb are stored in one of these places and they are each discussed below.

If you evaluate (??symb) all information asssociated with symb is displayed, except the only message displayed is (symb::usage) and only if symb has a usage message.

DownValues

Definitions made using (f[args] := rhs) are stored in DownValues[f] as demonstrated with the next cell.

ClearAll[f, x, y];
x = 4;
f[x_, y_] := x^2 - y^2
DownValues[f]

Definitions made using (f[args] = rhs) are also in DownValues[f] as demonstrated with the next cell.

ClearAll[f];
f[x_, y_] = x^2 - y^2
DownValues[f]

UpValues

Definitions made using (f/: lhs ^:= rhs) are stored in UpValues[f] as demonstrated with the next cell.

ClearAll[f];
x = 4;
f[x_] + f[-x_] ^:= 2 f[x]
UpValues[f]

Definitions made using (f/: lhs := rhs) are also stored in UpValues[f].

ClearAll[f, g];
f /: f[x_] + g[y_] := {x, y};
UpValues[f]

On rare occasions it's wise to store definitions using (f[args] ^= rhs) and these definitions are also stored in UpValues[f].

ClearAll[f, g];
f[x_] + g[y_] ^= {x, y};
UpValues[f]

Use of (f/: lhs = rhs) isn't even documented, but it seems to give the expected result and it also makes a definition stored in  UpValues[f].

ClearAll[f, g];
f /: f[x_] + g[y_] = {x, y};
UpValues[f]

OwnValues

Definitions for (symb=expr) and (symb:=expr) are stored in OwnValues[symb].  The next cell assigns a value to expr.

ClearAll[expr, x];
expr = 2 + x;

Next we see the above definition is stored in OwnValues[expr].

OwnValues[expr]

NValues

Definitions for N[f[args]] are stored in NValues[f].  The next cell makes a definition for  N[f[x_,y_]].

ClearAll[f];
N[f[x_, y_]] := N[{x, y, 0, 0, 0}]

The above definition isn't used to evaluate the next cell because N wasn't used.

f[π, (Sqrt[3])]

In the next cell our definition for N[f[x_,y_]] is used.

N[f[π, (Sqrt[3])]]

Our definition for N[f[x_,y_]] is stored in NValues[f].

NValues[f]

SubValues

Definitions for  f[arg1][args]  are stored in  SubValues[f].  This is demonstrated with the next cell.

ClearAll[f, a, x, y];
f[a_][x_, y_] := {a, x^2 - y^2}

SubValues[f]

ClearAll[f, Subscript]
(Subscript[f, 1])[x_, y_] := x^2 - y^2;
(Subscript[f, 1])[4, 1]

SubValues[Subscript]

FormatValues

A user once wrote to the MathGroup asking how one could ensure rational numbers greater than one are displayed as improper fractions.  P . J .  Hinton of Wolfram Research gave the solution below to change the way rational numbers are formated.

Unprotect[Rational];
Format[
        Rational[num_Integer, den_Integer] /; 
          Abs[num/den] > 
            1] := 
DisplayForm[
RowBox[{ToString[Quotient[num, den]], 
FractionBox[
              ToString[Mod[num, den]], 
              ToString[
                den]]
}
]
]

After evaluating the cell above the output of the next cell is formatted as an improper fraction.

14/3 - 1

Below we see that the above definition is stored in  FormatValues[Rational].

FormatValues[Rational]

Before you continue you might want to evaluate the next cell to restore the default formatting of rational numbers.

FormatValues[Rational] = {};
Protect[Rational];

MakeBoxes is similar to Format and FormatValues, and definitions using MakeBoxes are stored in DownValues[MakeBoxes] or possibly in UpValues for the type of expression being formatted.

DefaultValues

Anything stored using Default[f], Default[f,i], or Default[f,i,n] is stored in DefaultValues[f].  The built-in functions Plus, Times and Power have defaults stored as DefaultValues as the next cell shows. Use of Default was explained in an earlier section.

DefaultValues[Plus]
DefaultValues[Times]
DefaultValues[Power]

Options are also stored in DefaultValues as demonstrated with the next cell.

DefaultValues[Cancel]

You could use DefaultValues to see what options a function has or change the options, but I can't see why anyone would want to do this.  Since options are stored as DefaultValues you should be cautious about using DefaultValues[symb] = {} to delete all defaults.

In the next cell I assign a value to Default[f1,2] and I give f1 options.

ClearAll[f1];
Default[f1, 2] = {1};
Options[f1] = {opt1 -> val1, 
          opt3 :> val3};

The next cell shows that the previous definitions are stored in DefaultValues[f1].

DefaultValues[f1]

Messages

The next cell makes Messages related to the symbol foo.

foo::usage = "foo is a Symbol.";
foo::err1 = "foo had invalid input.";
foo::err2 = "foo got stuck.";

At times you might want to check on or manipulate the messages above.  The next cell gives us the list of messages as they are stored.

Messages[foo]

Below we see that evaluating (??foo) displays the usage message for foo, but not the error messages for foo.

?? foo

I agree with the result below.  If we multiply a number with 50 digits of precision by a machine precision number, all the extra digits are garbage.  I get back a number with 16 digits of precision and this is good.

pie = SetPrecision[Pi, 50];
area = pie (((1.25))^2);
{Precision[pie], Precision[area]}

mass = SetPrecision[4.2, 4];
density = mass/1.25;
{Precision[mass], Precision[density]}

To do the calculation above correctly we can do something like the line below.

mass = SetPrecision[4.2, 4];
density = mass/SetPrecision[1.25,  10];
{Precision[mass], Precision[density]}

x1 = Erf[26.0];
x2 = Erf[27.0];
{Precision[x1], Precision[x2]}

{ByteCount[x1], ByteCount[x2]}

a1 = Exp[1000.0]/Exp[1000];
b1 = a1 + π - 1;
{Precision[a1], Precision[b1]}

Block[{$MinPrecision = -∞}, 
b1 = 
          a1 + π - 1;]
{Precision[a1], Precision[b1]}

x = 15.4/0.125 + Log[1 - Erf[8]]

Below we see simply using N on the result above returns Indeterminate.  That's why it was returned unfinished.  We have to use arbitrary precision to get an ordinary number.  In this case I am impressed with how careful the kernel is.

{N[x], N[x, 17]}

x2 = N[x, 25];
{Precision[x2], MachineNumberQ[x2]}

x1 = 9.1 + 8*$MachineEpsilon;
x2 = 9.1 + 16*$MachineEpsilon;
{x1 === 9.1, x2 === 9.1, x1 - 9.1, x2 - 9.1}

ClearAll[f];
f[9.1] = 25;
{f[9.1], f[x1], f[x2]}

For more read the  Numerics Report  in the Help Browser.

Importing an columns of data from an ASCII file

Suppose you have matrix of data in an ASCII file, and the file is in a directory included in $Path.

!! data.txt

In version 4.0 and later data can be brought in with the Import command using the line in the next cell.  When the second argument is "Table" entries are brought in as numbers when possible and as strings when they can't be numbers.

MyData=Import["data.txt","Table"];
TableForm[MyData]

This can be done in any version of Mathematica using ReadList as below. You might also prefer this method because it may be faster than Import. In this example the second argument {Word, Number, Number} indicates each line is an ASC string, followed two numbers.

MyData=ReadList["data.txt", {Word,Number,Number}];
TableForm[MyData]

Working with a List or Table of data

For some efficient ways of making lists see examples of Range, Array, and Table.  Some good ways to work with a matrix of numbers are shown in the Cells below.  First we make a matrix of random integers called (data).

\(\(data = 
        Table[{Random[Integer, {0, 10}], 
	Random[Integer, {50, 60}], 
	
            Random[Integer, {\(-10\), 0}]}, {i, 8}];\)\)
TableForm[data]

Functions that have the Listable attribute are performed on each element of nested lists.  Hence the following line adds 100 to each element of data. Besides being very concise this is the most efficient way to do something like this in Mathematica.

d2 = data + 500;
TableForm[d2]

In the next line MapAt is used to add 500 to the fourth row of data.

d2 = MapAt[# + 500 &, data, 4];
TableForm[d2]

We can perform an operation f[] on the third column of data.

Clear[f];
data /. {x_, y_, z_} :> {x, y, f[z]} // TableForm

Sometimes the simple method above doesn't do what you want. For example in the next cell the pattern matcher assigns (x,y,z) to the 1st, 2nd, 3rd rows, but we wanted (x,y,z) to be the 1st, 2nd and 3rd elements of each row.

d2 = Take[data, 3];
d2 /. {x_, y_, z_} :> {x, y, f[z]} // TableForm

The problem in the last example can be avoided if we ensue either x, y or z is numeric as in the next cell.  We could just as well ensure x, y, or z is not a vector.

d2 = Take[data, 3];
d2 /. {x_?NumericQ, y_, z_} :> {x, y, f[z]} // TableForm

We can swap the second and third columns. To keep this example simple I don't ensure that neither x, y or z are numeric, but in some cases that would be necessary.

data /. {x_, y_, z_} :> {x, z, y} // TableForm

We can select the elements of data that have a second element less than 56.

Select[data, (#[[2]] < 56) & ]  // TableForm

The method below is less cryptic, but it requires the use of a global variable to name the testing function. It also turns out that the less cryptic method takes about twice as much time to evaluate.

small[sample_] := sample[[2]] < 56;
Select[data, small] // TableForm

One method that's very attractive is to use Cases as below.
Also see the separate discussion of Cases.

Cases[data,  {_, y_, _} /; y < 56]  // TableForm

To see how to sort a matrix according to the values in a certain column of the matrix see the discussion of Sort.  For more on this topic see 
http://documents.wolfram.com/v5/Built-inFunctions/AdvancedDocumentation/LinearAlgebra/

The symbols Blank, BlankSequence, BlankNullSequence, Alternatives, Pattern, Optional, Repeated, RepeatedNull, Condition, PatternTest, Verbatim, HoldPattern, Default, Orderless, Flat, and OneIdentity are all related to pattern matching in Mathematica.  Each of these symbols are discussed in this notebook.

Some examples

x_Integer

(* 
      A sequence of one or more Integers in a list *) 
{x__Integer}

(* A sequence of one or more Integers in a list,  
      or an empty list *) 
{x___Integer}

x_?
      Positive

(* 
      A sequence of one or more Positive numbers in a list *) 
{x__?Positive}

{x___?
        Positive}

x_Integer?
      Positive

x_?(-1 < # < 
            1 &)

(* 
      A list of {x, 
          y} coordinates *) 
data : {{_, _} .. }

(* 
      A pair of numbers where x is less than y *) 
{x_, y_} /; x < y

A pattern must match the FullForm

Finding the right pattern needed to make a transformation on an algebraic expression can be non-trivial. Consider the next cell where we would like to make the transformation
(a/(s - z1))⟶(a/(1 - 
                  Exp[z1] /z)) on each term.
This is a simplified example of a real world problem I encountered when I needed to change the transfer function of a continuous filter to the transfer function (in the z-domain) of the corresponding discrete filter. The most intuitive attempt to make this transformation is shown in the next cell, and you can see it doesn't work.

Clear[expr, x, s]

    
expr = ((8 )/(2 + 4  I + s)) + ((3 )/(2 - 4  I + s));

    
expr /. 
      a_/(s - z1_) :> a/(1 - Exp[z1]/z)

The attempt above doesn't work because the pattern doesn't match the FullForm of the expression we want to change.  The next cell indicates what the FullForm is.

FullForm[(8/(2 + 4  I + s))]

The transformation in the next cell mathces the FullForm and does what we want.

expr /. (a_*(s + z1_)^-1) :> 
        a/(1 - Exp[-z1]/z)

For more examples in pattern matching see the sections on  MatchQ and HoldPattern.

Note:  The attributes Orderless, Flat, and OneIdentity have an effect on pattern matching.

The pattern matcher doesn't look inside "atoms"

Integers, rational numbers, and complex numbers are "atoms", and the pattern matcher doesn't look at the individual parts of these things.  That is why we get only only one Position from the next input.

Position[{2, 222, (3/2), 2 + 3  I}, 2]

Keep in mind that the functions in the table below can be used to take apart atoms.  
Also be aware that the real and imaginary parts of a complex number can each be Real, Rational, or Integer values.

({{Head, Function to take apart the atom}, {Complex, Re,  Im}, {Rational, Numerator,  Denominator}, {Real, RealDigits}, {Integer, IntegerDigits}, {String, Characters}})

The pattern (x_Symbol) may not be specific enough

In the next input a function f[x, n] is defined where x must be a symbol and n must be an integer.

ClearAll[f, z, t, x, n];
f[x_Symbol, n_Integer] := Expand[(((x + 1))^n)]

Below we see that the above definition is used for f[π,3] and[s,3] when (s) evaluates to the symbol (t).

s = t;
{f[s, 3], f[Pi, 3]}

However you might want to require that the first argument of (f) is a variable and π is clearly not a variable.  The next definition does the job.

ClearAll[f];
f[x_Symbol?(! NumericQ[#] &), n_Integer] := 
      Expand[(((x + 1))^n)]

    
{f[s, 3], f[Pi, 3]}

However, it gets a more complicated if you want to account for symbols that were removed.  This was explained by Robby Villegas of Wolfram Research at the 1999 Developer Converence.  See "Working With Unevaluated Expressions" posted at 
http://library.wolfram.com/conferences/devconf99/#programming.

Below the symbol (t) is removed, so (s) evaluates to Removed["t"]. When f[s, 3] is evaluated the first argument Removed["t"] matches the pattern (x_Symbol?(!NumericQ[#]&)). One would think Removed["t"] would be an expression with Head Removed and argument "t", but it doesn't work that way.

Remove[t]

f[s, 3]

In the next cell I give a new definition for (f) that requires that the first argument is a non-numeric symbol that hasn't been Removed.

ClearAll[f];
f[x_Symbol?(! NumericQ[#] && NameQ[ToString[#]] &), n_Integer] := 
      Expand[(((x + 1))^n)]
{f[z, 3], f[s, 3], f[π, 3]}

You might find the function (UserSymbolQ) below useful.  It returns True when it's argument is a Non-numeric symbol that hasn't been removed and is not in the System Context.

UserSymbolQ=Function[{a},Head[a] === Symbol&&(Context[a]=!="System`")&&NameQ[ToString[a]]];

In the cell below the only "UserSymbol" is t4.

t2 = t1;
t3 = 3;
Remove[t1]
UserSymbolQ /@ {Sin, π, 3, t2, t3, t4}

Use a sensible  $HistoryLength

By default ($HistoryLength=∞), and this causes Mathematica to remember every 'In' cell and 'Out' cell during your entire session. This is ridiculous because you would never make reference to an output from a very long time ago as I do with  In[943]  in the next cell.

Expand[%27]

To avoid eating up more and more memory you should put something like the next line at the end of your (init.m) file.  That way $HistoryLenght will always be set to 15 when you start Mathemmatica.

$HistoryLength = 15;

How to find your (Init.m) file.
Evaluating the next cell will return the directory where your (init.m) file is located.

ToFileName[{$TopDirectory, "Configuration", "Kernel"}]

Save your notebook once in a while

Saving your notebook allows the Front End to return excess memory to the Operating System.

Clear the Clipboard

If you Copy or Cut something that requires a lot of memory clear the ClipBoard when you are done with it. An easy way to essentially clear the clipboard is to select a small expression, a few words in a text cell, or something else that is small, and select Copy under the Edit menu.

End with ; (a semi-colon) to avoid lengthy output

Ending with a semi-colon suppresses output. When an output is very long this saves time and memory.

From  http://support.wolfram.com/mathematica/kernel/Symbols/  you can access web pages Wolfram Research technical support provides for every built-in Mathematica symbol. In most cases you are linked to a web page that shows the usage message of the symbol and the full explanation from Appendix A.10 of the Mathematica Book for the selected symbol.  The real value of this resource is the extra information provided for 30 or so kernel symbols. The extra information they provide about these symbols isn't available anywhere else!  Unfortunately Wolfram Research does nothing to show which of the  1700+  symbols in the System context have this extra information.

I came across a lot of these pages with juicy bits of information and hyperlinks for the respective symbols are provided below.

AdjustmentBox
Apart
ArgumentCountQ
Assumptions
Coefficient
Compile
D
Derivative
Det
Dot
DSolve
Factor
FactorInteger
Flat
FullOptions
FullSimplify
HoldAllComplete
HoldComplete
Integrate
Interpolation
Interval
LinearProgramming
ListContourPlot
LUDecomposition
MakeRules
MatrixExp
MatrixQ
NDSolve
Notebook
OneIdentity
Permutations
Precision
Prime
PrimePi
Random
Residue
Return
Root
Roots
Series
Simplify
Solve
StoppingTest
Sum
Timing
Together
Union

In the cells below I give some rules of thumb that enhance the speed of Mathematica programs.

Use look up tables

When you have values for (f) defined for specific values such as 
f[1]=y1;
f[2]=y2;
f[3]=y3;
  ...
Mathematica can evaluate something like f[2] with great speed. The speed of evaluation is very fast for definitions that are free of patterns such as x_ or {x_,y_}. I refer to storage of function values for specific arguments as a look up table.

For example you could define a new version of Prime that evaluates over a certain range of large integers much faster than the built-in Prime function. The next cell defines MyPrime which uses Prime for n<5000000000, and has explicit values stored for 5000000000<n<5000000010.  I felt it would be appropriate to first try the rules for 5000000000<n<5000000010 so I rearrange the list of DownValues. The output below shows the resulting list of definitions.

ClearAll[MyPrime]
MyPrime[n_Integer?(# < 5000000000 &)] := 
      Prime[n]

    
\(\(\(Evaluate[MyPrime /@ Range[5000000000, 5000000010]] = 

        Table[Prime[n], {n, 5000000000, 5000000010}];\)\(
\)
    \)\)
DownValues[MyPrime] = RotateRight[DownValues[MyPrime], 1]

Now in the next cell I do the same thing with explicit values stored for 5000000000<n<5000010000, but display of the list of rules is suppressed because it's very long. You should be patient when the next cell is evaluated because it takes several minutes.

ClearAll[MyPrime]
MyPrime[n_Integer?(# < 5000000000 &)] := 
      Prime[n]

    
\(\(\(Evaluate[MyPrime /@ Range[5000000000, 5000010000]] = 

        Table[Prime[n], {n, 5000000000, 
            5000010000}];\)\(
\)
    \)\)
DownValues[MyPrime] = 
        RotateRight[DownValues[MyPrime], 1];

Once the definitions for MyPrime are stored a list of primes can be made with the next cell almost instantly.

Table[MyPrime[n], {n, 5000000000, 5000000400}]

An  implementation with more sensible use of memory would only store explicit definitions of MyPrime for say 5000000500, 50000001000, 5000001500, 5000002000, ... and would evaluate Nest[NextPrime,MyPrime[50000001000],43] to determine MyPrime[50000001043]. I didn't do that here because my purpose here is to demonstrate how fast the lookup table is.

Often times look-up tables are created on the fly using definitions of the form:
f[x_]:=f[x]=expr
This can result in efficient programs if (f) needs to be evaluated many times for the same argument.

Use functional programming

The tools of functional programming include:
   Map  MapAt  Thread  Apply  MapThread  MapAll  MapIndexed  Fold  FixedPoint  Nest  NestWhile
   FoldList  FixedPointList  NestList  NestWhileList  Scan  Inner  Outer  Distribute.
Each of these are often an important part of very efficient programs.

Other functional programming tools available are:
    ComposeList, Composition  Operate  Through.
 These other functional programming features are also efficient, but seldom needed in practice.
 
No examples of the functions listed above are provided here, but many of them have a devoted section in this notebook

Use pure functions

In the next cell three similar functions are defined . The function f1 is defined with a named pattern while pure functions are used to define f2 and f3. We see the implementation defined with pure functions run much faster than the definition defined with a named pattern.  Also the short hand form (6 #+13)&  runs a bit faster than Function[x, 6 x+13].

ClearAll[lst, f1, f2, f3];
SetAttributes[{f1, f2, f3}, Listable]; 
f1[x_] = 3  x + 5;
f2 = Function[x, 4 x + 6];
f3 = 6  # + 13 &;

    
lst1 = Range[10^6];

f1[lst1]; // Timing

f2[lst1]; // Timing

f3[lst1]; // Timing

Use "linked lists" instead of adding to a list

The most straight forward way of adding to a list uses PrependTo or AppendTo as in the next cell. There are a number of other ways you could effectively do the same thing but they are all very slow for making long lists. As an example consider the program in the next cell which makes a list by prepending values.

lst1 = {};
Do[(y = Sin[20.0 t]; 
          If[Positive[y], PrependTo[lst1, y]];), 
{t, 0, 
          10^4}] // Timing

The faster way to build the list in the previous cell is to make a linked list which looks like  
{... ,y4,{y3,{y2,{y1}}}} instead of {...  ,y4,y3,y2,y1} as returned by the program in the previous cell. Flatten can then be used to convert the linked list into a flattened list.

lst2 = {};
Do[(y = Sin[20.0 t]; 
          If[Positive[y], lst2 = {y, lst2}];), 
{t, 0, 
          10^4}] // Timing

lst1 == Flatten[lst2]

However a temporary head is needed if you want to quickly build up a list such as 
{... ,{x4,y4},{x3,y3},{x2,y2},{x1,y1}}. In that case an expression with the form 
h[{x4,y4},h[{x3,y3},h[{x2,y2},h[x1,y2]]]] can be built-up. Then you can get the desired result by flattening the nested list and changing the head (h) to List. This is done in the next two cells.

lst2 = h[];
Do[(y = Sin[20.0 t]; 
        If[Positive[y], lst2 = h[{t, y}, lst2]];), 
{t, 
        0, 12}]

    
lst2

lst2 = Flatten[lst2] /. h -> List

Avoid use of AppendTo and PrependTo

Use of PrependTo and AppendTo is not a problem if they only evaluate a few times.  But your program will be very slow if they evaluate many times.  This point was made in the previous rule of thumb.

Don't compute the same thing over and over

Often times one needs to have an understanding of the evaluation process to know when things are computed over and over.  An example of this point is given in the section on Set versus SetDelayed which is copied below.

Alan Hayes provided the code in the next cell as an example where (  func[x_]=  ) should be used instead of ( func[x]:=  ).
If we did use ( func[x_]:=  ) in this example the least squares fit would be computed  again for every value of (x) when we evaluate something like 
Plot[curve[t],{t,0,π/3}].

Clear[x];
data = 
        Table[{x, Cos[x] + Random[]/10}, {x, 0, π/3, 0.025}];
curve[x_] = Fit[data, {1, x, (x^2)}, x]

Use the simplest form possible

Some Mathematica functions are special cases of other functions. An example of this is Range which is a special case of Table. Hence Range will make a list in the next cell faster than Table would.

slow = Table[i, {i, 5000}];
fast = Range[5000];

Lots of Mathematica functions have multiple forms available. In many cases simpler forms are available which are special cases of more general forms. Hence Table runs faster in the next cell when an iterator isn't used.

slow = Table[Random[], {i, 10^5}];
fast = Table[Random[], {10^5}];

Minimize the number of expressions the kernel must evaluate

Ensuring that a program needs to evaluate the fewest number of expressions is an important part of minimizing the time a program needs to evaluate. For example the Dot product of two vectors evaluates much faster when the built in Dot function is used in the next cell.  Here the slow version evaluates Part 100 times, Times 50 times and one call to Sum.  The fast version makes only one call to Dot.  Clearly Dot must multiply respective parts of each list, but it does so much more effeciently than we can with the slow version.

Clear[x, y];
x = 
        Range[50];
y = 
        Table[(((-0.9))^n), {n, 50}];
slow=(∑_(i = 1)^50) Part[x, i] Part[y, i];
fast = x . y;

Another example (which only works in Version 4 or later) is given in the next cell where the slow method of making a list needs to evaluate Part 12 times, but the fast method does the same thing in one application of Take.

Clear[x, lst];
data = x Range[70];
slow = Table[Part[data, 3 n], {n, 1, 12}];
fast = Take[data, {3, 50, 3}]

Still another example (which only works in Version 4) is given in the next cell where the slow method maps First onto each element of data, but the fast method does the same thing with one application of Part. When the slow method is used First must be evaluated over and over.

Clear[data];
data = 
        Table[{i, 2.5 (i^3)}, {i, 1, 120, 3}];
slow = First /@ data;
fast = Part[data, All, 1]

In the next cell we can use Table and Take to split up a list into sublists.  The built-in function Partition is designed just for this purpose and does the job much faster.

data = Range[100];
slow = 
        Table[Take[data, {10*n, 10*(n + 1)}], {n, 1, 9}];
fast = Partition[data, 10]

Other special purpose functions are ListConvolve, ListCorrelate, Split, Ordering, and Tr to name a few.  Any direct use of these functions is probably faster than doing the same thing with some Mathematica code you might write. 

Another example of this principle is changing the value at multiple parts of an expression by using the Part feature only once. This is demonstrated in the section on Part.

Use approximate machine numbers whenever appropriate

If your application can tolerate the round off error inherent in machine precision arithmetic you can greatly improve the speed of your program by ensuring all numeric calculation is done on approximate machine numbers. Of course certain calculations over integers (e.g. multiplication and addition) are quickly computed over machine integers and computing with approximate numbers would not be any faster in that case.

Use Packed Arrays

Rob Knapp provides a tutorial on Packed Arrays at  http://library.wolfram.com/database/TechNotes/391/. and I give simplified examples of the trick he uses to speed up an implementation of LUDecomposition above in my discussion of Part.

As Rob explains you often need to use certain programming methods to get the improved speed possible with the with Packed Arrays. 
To start with you only get the benefit of packed arrays when your data structure can fit in a packed array.  For more on this subject see chapter 6 (Performance) at: 
http://documents.wolfram.com/v5/Built-inFunctions/AdvancedDocumentation/LinearAlgebra/.

Use Compile effectively

The speed of programs can often be enhanced if they are written using Compile, but you need to take certain steps to ensure the program evaluates with compiled evaluation. This is discussed in the section on Compile.  As I mention in that discussion only certain types of work can be done with compiled evaluation.

Don't use ReplacePart, MapAt or Insert when the last argument is a 
long List (>40).

ReplacePart, MapAt and Insert are very slow in this case. For mor explanation see the discussion of Slow Kernel Functions.

Be careful about modifying large data structures in place over and over

This discussion on modifying large data structures comes from  "Power Programming with Mathematica The Kernel"  by David B. Wagner.

This isn't so much a rule of thumb, but a trick that is useful in special cases. Consider computing a moving average of a list of numbers.  With the first approach below we use (s) as our input and output.  As explained where I discuss the Mathematica evaluation process each element of (s) is evaluated again each time s[[i]] is accessed because (s) was modified since it was last used.  All the exccess evaluation is very inefficient and the time needed with this approach is proportional to the square of the length of the list.

s = 
        Range[2*10^4];
Do[
          s[[i]] = (s[[i]] + s[[i + 1]])/2, {i, 
            Length[s] - 1}]; // Timing

By simply hanging the head of the list to HoldComplete the exccess evaluation is prevented, and the time needed is directly proportional to the length of the list.  At the end we change the head back to List.

s = 
        Range[2*10^4];
Timing[
s = HoldComplete @@ s; 
Do[
        s[[i]] = (s[[i]] + s[[i + 1]])/2, {i, 
          Length[s] - 1}]; 
s = 
        List @@ s;
]

It's even better to avoid a procedual approach all together as I do in the next cell.  The performance gain here is largely due to reducing the number of expressions that need to evaluate (one of my other rules of thumb).

However, in some cases the HoldComplete trick above provides the best solution.

s = 
        Range[2*10^4];
Timing[(Rest[s] + Drop[s, -1])/2;]

Beware of inefficient patterns

Most of this discussion on inefficient patterns is from "Power Programming with Mathematica The Kernel" by David B.Wagner.

An implementation of run length encoding is given below.  This implementation written by Frank Zizza is clearly elegant and it even won an award in a programming contest at a 1990 Mathematica conference.

runEncode[s_List] := 
      Map[{#, 1} &, 
          s] //. 
{h___, {x_, n_}, {x_, m_}, 
            t___} :> {h, {x, n + m}, t}

In the next cell we see an example using runEncode.

runEncode[{1, 0, 0, 1, 1, 0, 1, 1, 1, 1}]

Next we see that runEncode takes a long time to encode a list of 2000 integers.  In fact the time it takes this implementation to finish is proportional to the square of the length of the list, and this is not good at all.

tst = Table[Random[Integer, {0, 1}], {2*10^3}];

runEncode1[tst]; // Timing

The reason this implementation performs poorly is because of the way the kernel performs pattern matching. In the next cell we see pattern matching in action by causing the replacement to print each attempt at matching the pattern.

There are two important points about this result.  First, the kernel attempts to match sequence patterns (e.g.  __, ___, Repeated, RepeatedNull) from left to right.  Second, once the kernel finds a succeful match for the pattern, it starts the comparing patterns for the next iteration at the left end of the input, which for this particular algorithm is a guarenteed waste of time.

\({{1, 1}, {0, 1}, {0, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 
          1}} //. 
{h___, {x_, n_}, {y_, m_}, 
            t___} /; \((Print[{h, {{x, n}, {y, m}}, t}]; 
            x == y)\) :> {h, {x, n + m}, t}\)

Below I give a run length encoding implementation that performs much better.  Unfortunately the implementation below is more difficult to understand.  You will find the time this implementation takes is directly proportional to the length of the input list.

foo = 
        Module[{t1 = #1}, 
If[
              Part[#1, -1, 1] === 
                First[#2], 
Part[
                  t1, -1, -1]++, 
t1 = 
                Join[t1, {#2}]
]; 
t1] &;

runEncode2[s_List] := 
      With[{s1 = Map[{#, 1} &, s]}, 
Fold[foo, {First[s1]}, 
          Rest[s1]]
]

runEncode2[{1, 0, 0, 1, 1, 0, 1, 1, 1, 1}]

Below we see that the second implementation performs much better.

tst = Table[Random[Integer, {0, 1}], {2*10^3}];

runEncode2[tst]; // Timing

Check These Sources Too

http://library.wolfram.com/infocenter/Conferences/388/
http://library.wolfram.com/infocenter/Conferences/320/
http://library.wolfram.com/infocenter/Conferences/422/
http://library.wolfram.com/infocenter/Conferences/321/
http://library.wolfram.com/infocenter/Conferences/367/

Also see Alan Hayes in-depth articles on efficient Mathematica programming in The Mathematica Journal which are posted at 
http://www.mathematica-journal.com/issue/v2i2/  and  http://www.mathematica-journal.com/issue/v5i1/.

Chapter 10 of 
Power Programming with Mathematica The Kernel  
by David B. Wagner  (ISBN 0-07-912237-X)  
gives more advice on improving the speed of Mathematica programs.  
Unfortunately this great book is out of print, but you may be able to get a copy from an online auction such as e-bay or from the amazon.com out of print book service.

Functions ReplacePart, MapAt, Insert, AppendTo, and PrependTo are very slow (ie .   O[(n^2)] ) when they are used to make a large number of changes to an expression.  The other functions I list here are not extremly slow, but they are all much slower than the simpler form of the same function (eg. Sort[list,p] is much slower than Sort[list]).

ReplacePart, MapAt, Insert (when the last argument is a long list).

ReplacePart, MapAt, and Inset can be very slow.

Consider the expressions:
   ReplacePart[expr, 1, lst]
   MapAt[f,expr, lst]
   Inset[expr, 1, lst]
Where (lst) has the form {{__Integer},{__Integer}, ...] and Length[lst]=n. 
In that case each of these functions are O[n^2].  For those who aren't familiar with this notation, O[n^2] means the evaluation time is proportional to n^2.  Hence if (lst) is a long list of lists these functions are very slow.

Here is the evidence

ClearAll[TestReplacePart, TestMapAt, 
        TestInsert];
TestReplacePart[n_] := 
      Module[{size, p1, lst2, 
          t1}, 
(size = 
            250*2^(n + 1); 
p1 = 
            Union[Table[{Random[Integer, {1, size}]}, {size/
                    2}]]; 
lst2 = 
            Table[Random[], {size}]; 
t1 = (ReplacePart[lst2, 1, p1]; // Timing // 
                First); 
{Length[p1], 
            t1})
]

Table[TestReplacePart[n], {n, 5}] // TableForm

TestMapAt[n_] := 
      Module[{size, p1, p2, 
          t1}, 
(size = 
            2 Floor[250*2^n]; 
p1 = 
            Union[Table[{Random[Integer, {1, size}]}, {size/
                    2}]]; 
p2 = 
            Table[i, {i, size}]; 
t1 = 
            First[Timing[MapAt[# + 8 &, p2, 
                    p1];]]; 
{Length[p1], 
            t1}
)]

Table[TestMapAt[n], {n, 5}] // TableForm

TestInsert[n_] := 
      Module[{size, p1, p2, 
          t1}, 
(size = 
            2 Floor[150*2^n]; 
p1 = 
            Union[Table[{Random[Integer, {1, size}]}, {size/
                    2}]]; 
p2 = 
            Table[Random[], {i, size}]; 
t1 = 
            First[Timing[Insert[p2, 1, p1];]]; 
{Length[
              p1], t1}
)]

Table[TestInsert[n], {n, 5}] // TableForm

Would a faster algorithm give "wrong" results?

It seems like there must be algorithms for (ReplacePart, MapAt, Insert) that would evaluate much faster with a long list of parts.  It may be that the developers used slower methods which are needed to ensure that the result is correct.  A similar issue had to be addressed with Union.  However, in the case of Union the developers decided to use a method that is O[n Log[n]] and they accepted the risk that the result might include some duplicates.

At  http://support.wolfram.com/Kernel/Symbols/System/Union.html  we see Union treats elements as duplicates only if they appear in adjacent positions after sorting.  Apparently this method is O[n Log[n]], and Union would be O[n^2] if it compared all pairs of elements.

AppendTo, PrependTo

AppendTo and PrependTo are very slow when used on large expressions.  Instead use a "linked list" as I discuss under programming for speed.

Sort[list,p]

Sort[list]  is faster than  Sort[list, p] but sometimes we can't avoid the slower form.

Ordering[list,n,p]

Ordering[list, n, p] is slower than  Ordering[list]  or Ordering[list, n] but sometimes we can't avoid the slower form.

Split[list,test]

Split[list, test]  is slower than  Split[list] but sometimes we can't avoid the slower form.

MatrixQ[expr,test], VectorQ[expr,test]

MatrixQ[expr, test]  is slower than  MatrixQ[expr].

Likewise  VectorQ[expr, test]  is slower than  VectorQ[expr].

However, sometimes we can't avoid the slower form of VectorQ and MatrixQ.

(SameTest->func)  instead of (SameTest->Automatic)

The functions (Complement, FixedPoint, FixedPointList, Intersection, Union) all have a SameTest option. When they are given a non-default setting for the SameTest option they run much slower that with the default setting (SameTest->Automatic).  Sometimes we have no choice but to use these functions with a non default SameTest setting.  You should just be aware that this slows down performance.

All symbols built into the Mathematica kernel (Version 3.0, 4.0) with one or more of the attributes OneIdentity, Flat, or Orderless are listed in the table below.  This table shows a circle to indicate that a certain function has one of the attributes.

({{ , OneIdentity   , Flat   , Orderless}, {Max, •, •, •}, {Min, •, •, •}, {Plus, •, •, •}, {Times, •, •, •}, {Xor, •, •, •}, {And, •, •,  }, {Composition, •, •,  }, {Dot, •, •,  }, {Intersection, •, •,  }, {Join, •, •,  }, {NonCommutativeMultiply, •, •,  }, {Or, •, •,  }, {StringJoin, •, •,  }, {Union, •, •,  }, {Power, •,  ,  }, {ArithmeticGeometricMean,  ,  , •}, {Multinomial,  ,  , •}, {GCD  †,  , •, •}, {LCM  †,  , •, •}, {DiracDelta  ‡,  ,  , •}, {KroneckerDelta  ‡,  ,  , •}, {DiscreteDelta  ‡,  ,  , •}, {UnitStep  ‡,  ,  , •}})

†   GCD, LCM  have the attributes Flat, Orderless in Version 4.0, but not in Version 3.0.
‡  Functions new to the kernel as of version 4.0.

http://support.wolfram.com/

http://forums.wolfram.com/

http://library.wolfram.com/infocenter

http://www.ifm.ethz.ch/~kaufmann/news.html

http://www.webring.org/cgi-bin/webring?ring=mathematica;list

http://www.salford.co.uk/mathematica/

http://www.unca.edu/~mcmcclur/mathematicaGraphics/index.html

http://www.math.washington.edu/~lee/Ricci/

http://home.earthlink.net/~djmp/Mathematica.html

http://www.dimi.uniud.it/~gorni/

Experimental and Developer contexts
- From the Help Browser select "Built-in functions" and scroll the left column to the bottom.
- Select Additional Functions from the left column.
- Select Developer Context or Experimental context from the second column.
- Categories and functions are listed in the third, fourth columns.

Other Information   You will find lots of good stuff under 'Other Information'

Notation package documentation

More Notation documentation

Numerics Report