Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
6/29/2016
The Map command, and its shortcut version, /@, is one of the most powerful
and versatile functions in Mathematica. Using Map can greatly simplify the
code, make it more readable, and make it run more efficiently. Many of the
code examples at the Wolfram Demonstrations site and elsewhere involve some
version of Map (usually in the shortcut form, /@). If you want to read and
understand this code, and to do some programming with Mathemat-
ica on your own, you should become thoroughly familiar with this
command.
Map in a Nutshell
The Map command applies a function to each element of a list, and it returns a list of the outputs.
The basic syntax is:
• Map[function,list] (explicit form) or function /@ list (shortcut form, using prefix no-
tation)
• a pure function (e.g., #^2&), where the symbol # represents an element of the list.
Basic Examples
Start by generating two test lists, a simple numerical list, e.g.,
list1=RandomInteger[{1,6},10]
list2=RandomReal[1,{10,2}].
1
Mathematica Workshop IV A.J. Hildebrand
6. Map[Reverse,list2] (reverses the order of each element in list2, i.e., converts (xi , yi ) to
(yi , xi ))
Applications
Again, generate two test lists, a simple numerical list list1, and a simple 2-dimensional list list2,
as above, and a 2-dimensional list whose elements have variable lengths and positive coordinates,
e.g.,
list3={{4,3,2,4},{3,4},{4,3,4},{2,3}}
(For example, such a list might represent the list of grades of students at a university. Each sublist
represents the grades of a particular student.)
1. Computing averages:
(a) Write a function avg[list_] that takes a list, say list, and computes its numerical av-
erage (Hint: Use Total, Length, and N). Test this function on a single (one-dimensional)
list.
(Remark: The built-in function, Mean, accomplishes the same, but for practice try to
code this without using Mean.)
(b) Apply this function to the 2-dimenstional list list3 above, using the Map function.
Map[avg,list3]. The result should be a list of the averages of the sublists.
(c) Rewrite the latter as a standalone one-line command, using the pure function and /@
notations, i.e., something of the form .../@list3. (The is the form most commonly
used in demonstrations at the Wolfram Demonstrations site, so you need to be familiar
with it.)
2. Scaling lists.
(a) Write a function scale[list_] that takes a list and scales it so that the sum of the
elements add up to 1. For example, {1,3} should turn into {0.25,0.75}, {1,7,2}
should turn into {0.1,0,7,0.2}
(b) Now use Map to apply this function to a two-dimensional list like list3.
2
Mathematica Workshop IV A.J. Hildebrand
(c) Rewrite it as a standalone function, using the pure function and /@ notations.
3. Extracting first digits.
(a) Write a function firstdigit[n_] that takes a positive integer, and outputs its first
digit. (Hint: IntegerDigits.)
(b) Create a test list of integers, e.g., primelist=Prime[Range[20], or powerlist=2^Range[20],
then use Map to apply the first digit function to each element in the list.
(c) Create functions to calculate the numerical frequencies of first digits in a list of integers.
(Hint: First assume the list contains only digits 1, . . . , 9. Use a combination of Tally,
Sort, Length, N) to get the frequencies of these digits. Then apply this to the list
produced by the firstdigit function.)
The final result should be a function, say firstdigitfrequencies[list_], whose input
is a list of integers and whose output is the list of frequencies of first digits in these
integers. The output should be a list such as {0.1,0.13,...,0.21} with 9 elements
representing the frequencies of digits 1, 2, . . . , 9.
Test this function on some large lists, e.g., the first 104 powers of 2, the first 104 primes,
the first 104 squares, etc. Display the resulting frequencies using BarChart.
(a) Using Nest, write a function, logistic[r_,x0_,n_] that applies the logistic map x →
rx(1 − x) to a starting value x0 n times. Test your function on some concrete values of
r, x0 , n, e.g., r = 3.8 and n = 5, x0 = .3.
(b) Use Map to apply the function with specific r and n choices (e.g., r = 3.8, and n = 100) to
a list of k random real numbers in [0, 1] as initial values x0 generated by RandomReal[1,
k]. Test this out with small values of k first (e.g., k = 10), then try larger values (e.g.,
k = 105 ).
(c) Plot the resulting list using Histogram. Is the distribution approximately uniform? Does
it converge to some other distribution if you let n get larger and larger?