(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";
MacintoshStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e8, 24, "Times";
fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeTitle, center, M7, bold, e6, 18, "Times";
fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times";
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect,
groupLikeSection, grayBox, M22, bold, a20, 18, "Times";
fontset = subsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times";
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll,
preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times";
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
12, "Times";
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 10, "Times";
fontset = input, noPageBreakInGroup, preserveAspect, groupLikeInput, M42,
N23, bold, B65535, L-5, 12, "Courier";
fontset = output, output, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = message, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier";
fontset = print, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, L-5, 12, "Courier";
fontset = info, inactive, noPageBreakInGroup, preserveAspect,
groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier";
fontset = postscript, PostScript, formatAsPostScript, output, inactive,
noPageBreakInGroup, preserveAspect, groupLikeGraphics, M7, l34, w282, h287,
12, "Courier";
fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
italic, 10, "Geneva";
fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times";
fontset = leftheader, inactive, L2, 12, "Times";
fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,
12, "Times";
fontset = leftfooter, inactive, L2, 12, "Times";
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,
10, "Times";
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = completions, inactive, nohscroll, noKeepOnOnePage,
preserveAspect, M7, 12, "Times";
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect,
M7, 12, "Times";
currentKernel;
]
:[font = smalltext; inactive; preserveAspect]
Copyright (C) 1997 Rich Neidinger, John Swallow, and Todd Will. Free for
distribution to college and university instructors for personal,
non-commercial use. If these notebooks are used in a course, the authors
request $20 per student.
:[font = title; inactive; preserveAspect]
6. Lists II: Iteration and List Handlers
:[font = smalltext; inactive; preserveAspect]
Last revision: September 10 1996
:[font = text; inactive; preserveAspect]
We introduce iterators in some generality, and then we continue our
exploration of list-based functions, exploring ways to construct a list
(typically of numbers) and ways to deconstruct a list into various pieces.
Some of this material you have seen before, and if you fully understand it,
feel free to move on. Finally, we outline an analogy between Mathematica
code and language grammar, studying how some functions in Mathematica
affect the operation of others.
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Iterators
:[font = text; inactive; preserveAspect]
Iterators are lists which instruct functions such as Sum[ ], Table[ ], Do[
], and Range[ ] on how many iterations, or steps, a certain operation
should be repeated.
:[font = text; inactive; preserveAspect]
If an iterator is of the form "{number}", then the function is instructed
to do the operation "number" times. For example, "{30}" as iterator means
to repeat the operation 30 times. We have already used this in our Table[
] command:
:[font = input; preserveAspect]
Table[r,{5}]
:[font = text; inactive; preserveAspect]
If an iterator is of the form "{variable, number}", then the function is
instructed to do the operation "number" times, where the first time
"variable" will be 1, the second time 2, and so on, up until "number".
:[font = input; preserveAspect]
Table[r, {r,5}]
:[font = text; inactive; preserveAspect]
If an iterator is of the form "{variable, number1, number2}", then the
function is instructed to do the operation "number" times, where the first
time "variable" will be the value of "number1", the second time "number1"
plus 1, and so on, until "variable" reaches "number2". "number1" is a
minimum value; "number 2" is a maximum value.
;[s]
5:0,0;291,1;298,0;322,1;330,0;337,-1;
2:3,13,9,Times,0,12,0,0,0;2,13,9,Times,2,12,0,0,0;
:[font = input; preserveAspect]
Table[r, {r,2,6}]
:[font = text; inactive; preserveAspect]
If an iterator is of the form "{variable, number1, number2, number3}", then
the function is instructed to do the operation "number" times, where the
first time "variable" will be the value of "number1", the second time
"number1" plus "number 3", the third time "number1" plus 2 times "number3"
and so on, until "variable" reaches "number2". "number1" is a minimum
value; "number2" a maximum value; "number3" a step value.
;[s]
7:0,0;357,1;365,0;384,1;391,0;411,1;415,0;423,-1;
2:4,13,9,Times,0,12,0,0,0;3,13,9,Times,2,12,0,0,0;
:[font = input; preserveAspect]
Table[r, {r,2,6,0.5}]
:[font = text; inactive; preserveAspect]
The "number" expressions need not be numbers but larger expressions if
necessary, and it is possible to have several iterators in a row. In this
case it is not true that the first iterator executes, finishes, and then
the next one executes, and so on. Instead the iterators are nested. For
instance, in the two-iterator case, the first iterator starts at its first
value, the second iterates through all of its valued, and then the first
iterator steps to its next value, the second iterates through all of its
values, and so on. Here is an example:
;[s]
3:0,0;426,1;430,0;554,-1;
2:2,13,9,Times,0,12,0,0,0;1,13,9,Times,2,12,0,0,0;
:[font = input; preserveAspect]
Table[Pi^r E^s,{r,2,6},{s,1,5}]
:[font = text; inactive; preserveAspect]
Note that "Table[expression,{a,amin,amax},{b,bmin,bmax}]" operates in the
same fashion that nested "Table[ ]" functions do, with the iterators
reversed; for example, the previous expression is the same as
:[font = input; preserveAspect; endGroup]
Table[Table[Pi^r E^s,{s,1,5}],{r,2,6}]
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
List Generators (List, Table, Range, Operations)
:[font = text; inactive; preserveAspect]
First, recall that the functional form of a list (given by "FullForm[ ]")
is simply "List[x, y, z, .... ]"; thus, the most naive way to form a list
is simply to write out each of its elements.
:[font = input; preserveAspect]
List[1,2,3,4]
:[font = input; preserveAspect]
{1,2,3,4}//FullForm
:[font = text; inactive; preserveAspect]
Another way to create a list is to form a table of values, using an iterator.
:[font = input; preserveAspect]
Table[x,{x,0,1,.2}]
:[font = input; preserveAspect]
Table[x^2,{x,0,1,.2}]
:[font = text; inactive; preserveAspect]
A third way is to use "Range[ ]", a primitive form of "Table[ ]" which
simply gives the value of the iterator (with no variable specified).
Notice that for "Range[ ]" braces are unnecessary.
:[font = input; preserveAspect]
xlist = Range[0,1,.2]
:[font = text; inactive; preserveAspect]
Finally, many operations which usually apply to numbers can be applied to
list, and typically the operation is applied to each element of the list.
Experiment with other operations!
:[font = input; preserveAspect]
ylist = xlist^2
:[font = input; preserveAspect; endGroup]
zlist = xlist + 5
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Extracting from List (Part, Take, Drop)
:[font = text; inactive; preserveAspect]
We've already seen one way to get a n element or portion of a list, using
double brackets [[]]. Recall that the functional form of [[ ]] is
"Part[list, ... ]". "Part[ ]", "Take[ ]", "Drop[ ]" are the "core" of list
extraction operators. In fact, "Take[ ]" and "Drop[ ]" can be defined
using "Part[ ]". Let's look at some examples.
:[font = text; inactive; preserveAspect]
Let's say we want the first "n" elements of a given list. In the first
argument of "Part[ ]" we must place the list from which we want elements
extracted, and in the second argument of "Part[ ]" we must place a list of
the positions, or indices, we want extracted from the list. To form a list
with the indices from 1 through "n", we will use the expression "Range[n]".
Here's a function definition of our operation:
:[font = input; preserveAspect]
myTake[list_, n_] := Part[list, Range[n]]
:[font = input; preserveAspect]
Clear[a,b,c,d,e,f];
myTake[{a,b,c,d,e,f},3]
:[font = text; inactive; preserveAspect]
Actually, though, there's a Mathematica function which does exactly what
we've defined. This function is Take[ ]: "Take[list,number]" gives the
first "number" elements of the list. Similarly,
"Take[list,{number1,number2}]" gives the elements from position "number1"
to position "number2". An interesting variation is that if "number" is
negative, Take[ ] gives that many elements from the end. (Note that
"myTake[ ]" does not have these additional features; some additional
programming is necessary to make "myTake[ ]" fully match "Take[ ]".)
:[font = input; preserveAspect]
Take[{a,b,c,d,e,f},3]
:[font = input; preserveAspect]
Take[{a,b,c,d,e,f},-3]
:[font = text; inactive; preserveAspect]
A related function is "Drop[ ]". "Drop[list,n]" returns "list" with its
first "n" elements dropped (omitted); if "n" is negative, the last "n"
elements are dropped; and if "n" is of the form "{number1, number2}", then
the elements from position "number1" to position "number2' are dropped.
:[font = input; preserveAspect]
Drop[{a,b,c,d,e,f},2]
:[font = input; preserveAspect]
Drop[{a,b,c,d,e,f},-2]
:[font = input; preserveAspect]
Drop[{a,b,c,d,e,f},{3,5}]
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Example of List-Based Programming: Finding Midpoints of Lists
:[font = text; inactive; preserveAspect]
Let's use these operations to consider a larger problem, that of finding
all of the midpoints of a list of numbers. A midpoint of two numbers "x"
and "y" is the number "(x+y)/2"; it's the average of the two numbers.
Finding all of the midpoints of a list means, for us, finding midpoints of
all consecutive numbers in the list, i.e., the midpoint of the first and
second numbers, the midpoint of the second and third, and so on. This
problem is a good one for list-based programming, because it affords us an
opportunity to use list operations.
:[font = text; inactive; preserveAspect]
We'll begin with a list "xlist".
:[font = input; preserveAspect]
xlist = {0., 0.2, 0.4, 0.6, 0.8, 1.}
:[font = text; inactive; preserveAspect]
What we want to do is add consecutive numbers and then divide by 2, but how
might we do this only with list operations? Well, we need a list of the
numbers we want to add, and we need a list of the numbers we want added to
the first list. More precisely, we want a list of the left-hand addends,
and a list of the right-hand addends. We can do this with "Drop[ ]".
First, the left-hand addends:
:[font = input; preserveAspect]
Drop[xlist,-1]
:[font = text; inactive; preserveAspect]
Then, the right-hand addends:
:[font = input; preserveAspect]
Drop[xlist,1]
:[font = text; inactive; preserveAspect]
Now we'll simply add the two lists and divide by 2! Note that lists are
added element-by- element, and the divide-by-2 operation is applied to each
element of the resulting list.
:[font = input; preserveAspect]
(Drop[xlist,-1] + Drop[xlist,1]) / 2
:[font = text; inactive; preserveAspect]
Let's make a function out of this:
:[font = input; preserveAspect]
midpoints[list_] :=
(Drop[list,-1] + Drop[list,1]) / 2
:[font = input; preserveAspect; endGroup]
midpoints[xlist]
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
Another Example: Riemann Sums
:[font = text; inactive; preserveAspect]
For another example, let's consider how we might evaluate some Riemann sums
of the function "x^2". The algorithm is somewhat complicated, but nothing
we cannot accomplish using the operations we have covered. A Riemann sum
for "x^2" with "n" divisions between x-values "a" and "b" (with "a"<"b") is
formed by finding "n" numbers equally spaced between "a" and "b",
evaluating "x^2" at each of them, multiplying these values by "(b-a)/n",
and adding up the results. As always, we approach the problem by taking
small steps first.
:[font = text; inactive; preserveAspect]
To find "n" numbers equally spaced between "a" and "b", we use a result
from our mathematics class that the numbers "a", "a+(b-a)/n", "a+2(b-a)/n",
"a+3(b-a)/n", up to "a+(n-1)(b-a)/n" are equally spaced from "a" to
"b"---being "(b-a)/n" apart, each time. How do we generate these numbers?
We can use "Range[ ]", with the appropriate iterator. Note that the
beginning value is "a", the ending value is "a+(n-1)(b-a)/n", and the step
value is "(b-a)/n". To aid us in working out the code, let's look at a
specific example first, say with 1 for "a", 2 for "b", and 10 for "n". We
want, then, "Range[1, 1+(9)(2-1)/10, (2-1)/10]":
:[font = input; preserveAspect]
Range[1,1+(9)(2-1)/10,(2-1)/10]
:[font = text; inactive; preserveAspect]
Now we want to evaluate "x^2" at each of them. Using list-based
operations, we can instruct Mathematica to raise each value to the second
power:
:[font = input; preserveAspect]
Range[1,1+(9)(2-1)/10,(2-1)/10] ^ 2
:[font = text; inactive; preserveAspect]
Now we must multiply each of these values by "(b-a)/n", or, in our case,
"(2-1)/10". We'll place the multiplication operation at the end of the
line:
:[font = input; preserveAspect]
Range[1,1+(9)(2-1)/10,(2-1)/10] ^ 2 * (2-1)/10
:[font = text; inactive; preserveAspect]
Finally, we must use "Apply[Plus, ]" to add up each of the values in the
list. ("Sum[ ]" requires an iterator, which we do not have; similarly,
"Apply[Sum, ]" would be incorrect.)
:[font = input; preserveAspect]
Apply[Plus,
Range[1,1+(9)(2-1)/10,(2-1)/10] ^ 2
* (2-1)/10
]
:[font = text; inactive; preserveAspect]
Our code is correct. Looking back, however, we find that we can improve
the speed of the code, using the distributive property of multiplication,
as follows. Instead of multiplying each element of the list by "(b-a)/n"
and then adding them up---which takes "n" multiplications followed by "n"-1
additions---we could add the elements up first and then multiply by
"(b-a)/n". We then save "n"-1 of those multiplications: adding the
elements up first and multiplying once takes "n"-1 additions and 1
multiplication. Let's rewrite our code to take advantage of this improved
method:
:[font = input; preserveAspect]
Apply[Plus,
Range[1,1+(9)(2-1)/10,(2-1)/10] ^ 2
] * (2-1)/10
:[font = text; inactive; preserveAspect]
Leaving the definition of "square[ ]", we can form the rest of the code into a function:
:[font = input; preserveAspect]
Clear[mySquareSum]
mySquareSum[a_,b_,n_] := Apply[
Plus,
Range[
a,
a+(n-1)(b-a)/n,
(b-a)/n
] ^ 2
] * (b-a)/n
:[font = text; inactive; preserveAspect]
Let's check that it works:
:[font = input; preserveAspect]
mySquareSum[1,2,10]
:[font = text; inactive; preserveAspect]
Finally, we point out that one can accomplish the same operations using
"Sum[ ]" instead of "Apply[ ]" and "Range[ ]". Consider the similarities
and differences between "mySquareSum[ ]" and the following function,
"newSquareSum[ ]":
:[font = input; preserveAspect]
Clear[newSquareSum]
newSquareSum[a_,b_,n_]:=
(b-a)/n * Sum[i^2,{i,a,a+(n-1)(b-a)/n,(b-a)/n}]
:[font = input; preserveAspect; endGroup; endGroup]
newSquareSum[1,2,10]
:[font = section; inactive; Cclosed; preserveAspect; startGroup]
Grammar and Examples of Adverbs (Inner, Outer, Apply, MapThread)
:[font = text; inactive; preserveAspect]
A good analogy which might be made between Mathematica code and language
grammar is as follows. A "noun" is anything defined with an equals sign
and no formal parameters (expressions followed by underscores inside
brackets); a "verb" is anything defined with colon-equals and with formal
parameters; and an "adverb" is a function which takes another function and
applies it in a certain fashion. In this section we want to consider some
"adverbs".
:[font = text; inactive; preserveAspect]
Suppose we have a function which takes two arguments and we wish to apply
it to two lists in the following fashion: we want to use the function with
the first argument taken from the first list and the second taken from the
second list, and we want to do so for all possible combinations. The
adverb which applies a function in this fashion is "Outer[ ]". Let's make
two lists and watch it work.
:[font = input; preserveAspect]
sideList = Range[.16,.20,.01]
:[font = input; preserveAspect]
topList = Range[10,15]
:[font = input; preserveAspect]
Outer[Times, sideList, topList]
:[font = text; inactive; preserveAspect]
Sometimes it's useful to show the output of a list of lists in a tabular
form. "Expression// TableForm" or "TableForm[expression]" will accomplish
this for us. Note that each innermost list is a row (not a column) of the
table.
:[font = input; Cclosed; preserveAspect; startGroup]
%//TableForm
:[font = output; output; inactive; preserveAspect]
TableForm[{{1.6, 1.76, 1.92, 2.08, 2.24,
2.4}, {1.7, 1.87, 2.04, 2.21, 2.38,
2.55}, {1.8, 1.98, 2.16, 2.34, 2.52,
2.7}, {1.9, 2.09, 2.28, 2.47,
2.660000000000001, 2.850000000000001}\
, {2., 2.200000000000001, 2.4, 2.6,
2.800000000000001, 3.}}]
;[o]
1.6 1.76 1.92 2.08 2.24 2.4
1.7 1.87 2.04 2.21 2.38 2.55
1.8 1.98 2.16 2.34 2.52 2.7
1.9 2.09 2.28 2.47 2.66 2.85
2. 2.2 2.4 2.6 2.8 3.
:[font = text; inactive; preserveAspect; endGroup]
Note: It's best not so save the lists with "TableForm[ ]", since this
isn't the easiest way for Mathematica to work with them.
:[font = text; inactive; preserveAspect]
What if we don't want a function so complicated as "Outer[ ]", but simply
want to apply a function to all elements of the list together, such as + or
*? Here "Apply[ ]" is the function (adverb) we want.
:[font = input; preserveAspect]
xlist
:[font = input; preserveAspect]
Apply[Plus,xlist]
:[font = input; preserveAspect]
Apply[Times,xlist]
:[font = text; inactive; preserveAspect]
What if we want a function like "Outer[ ]", except that we don't want all
possible combinations of taking a first argument from a first list and a
second from a second? Let's say we want to take arguments, one at a time,
from each list, say using the first elements of each list together, then
the second, and so forth. "MapThread[ ]" is the function (adverb) we want:
it takes a function of two or more arguments and matches up arguments from
a list of lists.
:[font = text; inactive; preserveAspect]
First we'll define an averaging function.
:[font = input; preserveAspect]
ave2[a_, b_] := (a+b)/2
:[font = input; preserveAspect]
ave2[3,7]
:[font = text; inactive; preserveAspect]
And now we'll use this function to average grades, say for students, where
the first list is the scores on the first test and the second list is the
scores on the second list (in the same order).
:[font = input; preserveAspect]
MapThread[ave2,{{3,4,5},{7,8,9}}]
:[font = input; preserveAspect]
xlist
:[font = text; inactive; preserveAspect]
This average function gives us a new way to find our midpoints; we simply
use "MapThread[ ]" with "ave2[ ]" and our two truncated lists. First we'll
need to make a list of the two lists:
:[font = input; preserveAspect]
twolists = {Drop[xlist,-1],Drop[xlist,1]}
:[font = text; inactive; preserveAspect]
And then here we go:
:[font = input; preserveAspect]
MapThread[ave2,twolists]
:[font = subsection; inactive; Cclosed; preserveAspect; startGroup]
A dot product
:[font = text; inactive; preserveAspect]
A certain operation in mathematics is to take two lists, multiply
corresponding entries, and add up all of the products. We will do this
with the Mathematica commands we know. First, though, let's note that
Mathematica already knows how to do this, with the "." operator:
:[font = input; preserveAspect]
Clear[a,b,c];
{1,2,3}.{a,b,c} (* Built-in dot product *)
:[font = input; preserveAspect]
Dot[{1,2,3},{a,b,c}]
:[font = text; inactive; preserveAspect]
First we want to multiply corresponding entries. This is accomplished just
by using "MapThread[ ]" with "Times[ ]".
:[font = input; preserveAspect]
MapThread[Times,{{1,2,3},{a,b,c}}]
:[font = text; inactive; preserveAspect]
Then we want to add up all the elements. Recalling that "%" denotes the
result of the previous expression, we try the following:
:[font = input; preserveAspect]
Apply[Plus,%]
:[font = text; inactive; preserveAspect]
The code appears to work, so we'll put the two lines together, programming
bottom-up:
:[font = input; preserveAspect]
Apply[Plus,
MapThread[Times,{{1,2,3},{a,b,c}}]
]
:[font = text; inactive; preserveAspect]
Finally, satisfied with our code thus far, we form the code into a function:
:[font = input; preserveAspect]
myDot[list1_,list2_] :=
Apply[ Plus, MapThread[ Times, {list1,list2} ] ]
:[font = text; inactive; preserveAspect; endGroup; endGroup]
Note how we had to put brackets around the two lists to make a
list-of-lists for "MapThread[ ]".
^*)