Outer
The first two examples demonstrate the basic use of Outer.
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.
The next cell does the same thing, and is based on an example in the Help
Browser.
The next cell shows what we get for the outer product of two matrices.
The next cell flattens what we get from the computing Outer with 4 lists.
In the next two examples we provide integers as additional arguments.
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.
In the next cell we see the part at position (2,0) of the list {1,1+π} is
Plus.
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.
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[[n,list2]] returns a list of elements at positions (list2) in row n. The
next cell gives an example.
Likewise m[[list1,n]] returns a list of elements at positions (list1) of
column n. The next cell gives an example.
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.
In the next cell All is used to access the first, second and fourth columns
of matrix (m).
In the next cell we change elements of (m) where rows (1,3) intersect with
columns (2,3,5).
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.
In the next example (f) is only defined when given an argument that is a
positive integer.
In the next example (f) is defined when given an argument that is a positive
integer or a positive real number.
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.
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) ?
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.
Created by Mathematica (May 16, 2004)