You are on page 1of 18

ECM6 Computational Methods :

Slide 1 of 11

Lecture 4
Introduction to Functional Programming
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture4Vitenam_2014.nb

Topics for Lecture


In this lecture we give a brief over view of the following topics
Overeview of functional programming
Concept of a pure function
Using the Map function
Using the Apply function
Using the Nest function

ECM6Lecture4Vitenam_2014.nb

Functional Programming
In this lecture we will give a brief overview of functional programming in Mathematica. Here is a definition of functional programming (http://www.cs.nott.ac.uk/~gmh//faq.html)
"Functional programming is a style of programming that emphasizes the evaluation of expressions,
rather than execution of commands. The expressions in these language are formed by using functions
to combine basic values. A functional language is a language that supports and encourages programming in a functional style."
Programming languages such as FORTRAN, BASIC are often referred to as procedural languages.
The use of For loops, While loops are common constructs in procedural programming.
In procedural programming one specifies the steps the program must take to reach the desired goal.
Procedures, also known as routines, subroutines, methods, contain a series of computational steps to
be carried out.
In this lecture we will discuss the following functions: Pure functions, Map, Apply, and related functions
such Nest (NestList), FixedPoint.
To avoid function clashes evalute the following:
In[1]:=

Remove@g1, name, pfunc, g, f, dataPoints, myfunc, myFunc1,


myFunc2, integralValues, myData, myList, iterates, finalValueD;

ECM6Lecture4Vitenam_2014.nb

Pure Functions
In writing a Mathematica program it is sometimes convenient to be able to assign an operation with no
name to it. This might be useful if the operation is used infrequently.
A function without a name assigned to it is called an anonymous or pure function. In Mathematica you
specify a pure function with Function. The definition and attributes of Function are given below:
In[2]:=

? Function
Function@bodyD or body & is a pure function. The formal parameters are Hor 1L, 2, etc.
Function@x, bodyD is a pure function with a single formal parameter x.
Function@8x1 , x2 , <, bodyD is a pure function with a list of formal parameters.

In this section we will show how one defines and uses anonymous function.

Conventional User-defined function in Mathematica


We start our discussion by first using the conventional way to define a function in Mathematica to
compute the cube of a number:
In[3]:=

g1@x_D := x3

To evaluate the function g1, we specify the argument, e.g.,


In[4]:=
Out[4]=

g1@3D
27

Anonymous(Pure) Function
We can also use the anonymous function (which is specified using Function[]) to do the same thing
In[5]:=

FunctionAw, w3 E

Out[5]=

FunctionAw, w3 E

The variable in the anonymous function is the first argument (in this case w). The second argument is
the body of the function (in this case w 3 ). To apply this function to a value (or list of values ) of w we
must enclose its value in brackets after the function:
In[6]:=
Out[6]=

FunctionAw, w3 E@3D
27

We can also give the anonymous function a name (this is actually redundant), as follows:
In[7]:=
Out[7]=

name = FunctionAw, w3 E
FunctionAw, w3 E

Now if we want to evaluate the function name with w=5, say, we proceed as follows

ECM6Lecture4Vitenam_2014.nb

In[8]:=
Out[8]=

name@4D
64

We can apply this function to a list of values


In[9]:=
Out[9]=

name@82, 3, 4, 5<D
88, 27, 64, 125<

ECM6Lecture4Vitenam_2014.nb

Short-Hand Notation for Pure Function


There is a convenient short hand version for Function.
The variable is represented by # ( a slot) and the end of the function is denoted by &.
Thus in our previous example we can write Function[w,w^3] as #^3&. Here # refers to the argument w.
To make the program more readable one often encloses the pure function in parentheses, i.e. (#^3)&.
Thus if we wanted to evaluate the function with an argument value of 5, we write
In[10]:=
Out[10]=

H ^ 3L &@5D
125

Here are a few simple examples that show how pure functions can be implemented:

Example 1:
In[11]:=
Out[11]=

HCos@1D + RandomReal@D &L@5D


0.369898

Example 2:
Pure functions can also be defined to have more than one variable. When that is the case we use #1,
#2, etc. to denote the variables. Here is an example:
Cos@1D
In[12]:=

1 + Sin@2D

&@3, 4D

Cos@3D
Out[12]=

1 + Sin@4D

Example 3:
Now take our function g that we defined in a previous lecture:
In[13]:=

g@a_, b_, x_D :=

1
a

Ha - 1L x +

b
x

For a fixed value of a and b, we can write g(a, b, x) as a pure function and call it pfunc
In[14]:=
Out[14]=

pfunc = g@1.5, 78.4, D &


g@1.5, 78.4, 1D &

Note now that by defining pfunc we have transformed our original function of 3 variables to one that
takes a single argument. Thus if we want to evaluate this function for a specific value of x, say x=0.2,
all we do is

ECM6Lecture4Vitenam_2014.nb

Note now that by defining pfunc we have transformed our original function of 3 variables to one that
takes a single argument. Thus if we want to evaluate this function for a specific value of x, say x=0.2,
all we do is
In[15]:=
Out[15]=

pfunc@0.2D
261.4

Or equivalently we can write


In[16]:=
Out[16]=

g@1.5, 78.4, D &@0.2D


261.4

In this way we do not have to ever define the variable pfunc!

ECM6Lecture4Vitenam_2014.nb

Map Function
In[17]:=

Remove@f, gD

The definition for Map is given as


In[18]:=

? Map
Map@ f , exprD or f expr applies f to each element on the first level in expr.
Map@ f , expr, levelspecD applies f to parts of expr specified by levelspec.

The basic operation of Map is to wrap a given function around each element of a list and returns a list.
Thus
In[19]:=
Out[19]=

Map@f, 8x, y, z<D


8f@xD, f@yD, f@zD<

The output is a list but with the first argument of Map applied to each element of our original list. Thus
we have produced a new list in which each element is a function.
Mathematica did not evaluate these functions f[x],f[y], since no rule was specified for f. In this
case f is stored in the kernel as a Symbol, one of six atomic types used by Mathematica.
We can extend this example by defining f to be some function with a rule, say g[t_]:=t^2.
In[20]:=

g@t_D := t2

In[21]:=

Map@g, 8x, y<D

Out[21]=

9x2 , y2 =

The sequence of events is as follows.


First Map wraps g around the elements of the list.
Then the function g is evaluated according to the rule we defined for the function g.
However, if we had specified the first argument of Map as g[t],then the output would be not the
same.
For example, Map[g[t],{x,y}] computes the following
In[22]:=
Out[22]=

Map@g@tD, 8x, y<D


9t2 @xD, t2 @yD=

ECM6Lecture4Vitenam_2014.nb

Example 1: Application of Map Function


In[23]:=

Remove@dataPoints, myFunc1D

Here is another simple example of how Map can be used.

Creating Random Data Points


Suppose we had a set of {x,y} data points
In[24]:=
Out[24]=

dataPoints = Table@RandomReal@8- 10, 10<D, 8i, 1, 5<, 82<D


886.51307, - 6.2803<, 87.28604, 5.34146<,
8- 9.9164, 8.05597<, 85.01953, 6.70978<, 8- 4.72962, - 6.6576<<

Applying a Function to the Data Points


We now want to apply a function to each data point
Our function we will use is defined below. Note that the argument of the function is a list and that the
elements of the list are pattern matched:
In[25]:=

myFunc1@8x_, y_<D :=

Abs@yD
x2 + y2

We can use Map to apply that data points to our function


In[26]:=
Out[26]=

Map@myFunc1, dataPointsD
80.199385, 0.206318, - 0.172426, 0.185172, - 0.182981<

We can do the same calculation without ever defining the function myFunc1
In[27]:=

Out[27]=

MapB

@@1DD

Abs@@@2DDD

@@1DD2 + @@2DD2

&, dataPointsF

80.199385, 0.206318, - 0.172426, 0.185172, - 0.182981<

10

ECM6Lecture4Vitenam_2014.nb

Example 2 : Application of Map and a Pure function


In[28]:=

Remove@myFunc2, integralValuesD

In this example we illustrates the use of Map for efficient calculations on list of data. Consider the following list of functions:
In[29]:=

myFunc2 = TableASin@RandomReal@D xD3 RandomReal@D x , 81000<E;

We have suppressed the output by using the semi-colon after the expression. We have generated a list
of 1000 functions. Here is what the first 5 functions look :
In[30]:=
Out[30]=

myFunc2@@Range@5DDD
90.768993 x Sin@0.661181 xD3 , 0.627862 x Sin@0.570485 xD3 ,
0.0468615 x Sin@0.113841 xD3 , 0.35992 x Sin@0.50649 xD3 , 0.945703 x Sin@0.896221 xD3 =

Integrating a List of Functions


Next we are going to integrate these functions using NIntegrate making use of Map
In[31]:=

integralValues = Map@NIntegrate@, 8x, 0, .5<D &, myFunc2D;

Note 1: Map takes each element of the the list myFunc2, and substitutes it into the first argument of
NIntegrate, and then the numerical integration is done.
Note 2: Ending the RHS of the expression with a the semicolon (;) supresses the output.
Here are the first 5 value of the integral
In[32]:=
Out[32]=

integralValues@@Range@5DDD
80.00592975, 0.00363239, 0.0000234634, 0.00229541, 0.0153726<

Here are all the values stored in the list integralValues


In[33]:=
Out[33]=

integralValues
90.00592975, 0.00363239, 0.0000234634, 0.00229541, 0.0153726, 0.00118521,
0.000326834, 0.000824181, 0.013277, 2.86702 10-8 , 0.00101488, 0.00906668,
0.00103362, 0.00101666, 0.00029176, 0.0000828636, 0.0183311, 0.00804102,
0.000305338, 0.0142143, 0.0140254, 0.00135766, 0.0153095, 0.000894754, 0.0124342,
0.00209115, 2.78754 10-10 , 0.00164677, 0.00827162, 0.000429945, 0.00148678,
3.45489 10-6 , 0.0106541, 0.0131496, 0.0000287917, 0.00113331, 9.0539 10-6 ,
0.00616589, 0.0103695, 0.000405898, 0.00018057, 0.0000791592, 7.56686 10-6 ,
0.00457373, 0.000551122, 0.00859689, 0.0152631, 0.00760289, 1.94124 10-6 ,
0.0000975954, 7.08544 10-7 , 0.000504507, 0.015131, 0.014122, 0.000520485,
0.0110242, 0.00586971, 0.00199233, 0.0169856, 0.000911403, 9.90629 10-8 ,
0.000537615, 0.00279646, 0.0000208286, 0.0145136, 0.00465274, 0.00661498,
0.00853557, 0.0000588386, 0.00062723, 0.0117508, 0.0109272, 0.0127529, 0.0026466,
0.0113174, 0.0117951, 0.00932683, 0.00189065, 0.016707, 0.00077999, 0.0108555,
0.00299123, 0.00164571, 0.00633398, 0.00803565, 0.00985574, 0.00871247, 0.00405964,
0.00979525, 2.03099 10-7 , 0.00121812, 0.0131072, 0.00887896, 0.00431722,
0.00709209, 0.000477385, 0.000347967, 0.00471188, 0.00657172, 0.000884978,

ECM6Lecture4Vitenam_2014.nb

11

0.00979525, 2.03099 10 , 0.00121812, 0.0131072, 0.00887896, 0.00431722,


0.00709209, 0.000477385, 0.000347967, 0.00471188, 0.00657172, 0.000884978,
4.95097 10-7 , 0.000206233, 0.00368363, 7.51103 10-6 , 0.00485686, 0.00505586,
0.00190015, 1.37122 10-7 , 0.0160588, 0.0000663441, 0.000189603, 0.000425461,
1.33312 10-6 , 0.00317278, 0.0117819, 0.0196703, 0.00567092, 0.00827905, 0.00281126,
0.00110953, 0.000031318, 0.0000316425, 0.0111845, 0.00295442, 0.00352864,
0.0122585, 0.00794784, 0.000857446, 0.00774484, 0.00367454, 0.000607817,
0.0124247, 0.000289802, 0.000361805, 0.0119545, 0.0117952, 0.000579224,
2.83024 10-6 , 0.00157352, 0.00111166, 0.00506094, 0.0126712, 0.0000553609,
0.016548, 0.000276045, 0.000288508, 0.00048027, 0.0140305, 0.0124178, 0.0157531,
0.00316331, 0.0000342267, 0.00224217, 0.00156108, 0.00146288, 0.00491824,
0.00178084, 0.00100635, 0.0174432, 0.00220944, 0.00283341, 0.0000498935,
0.00826277, 0.00100665, 0.00214509, 0.00423096, 0.0131884, 0.0000949664,
0.0000583554, 4.93179 10-6 , 0.0099982, 0.00180399, 0.000158891, 0.00521338,
0.0105775, 0.0000222785, 0.0141196, 0.0000981443, 0.0024158, 0.0000326884,
0.0000673846, 1.01821 10-10 , 0.00376595, 0.0000326024, 0.000334447, 0.0000405804,
0.00109744, 0.000218206, 1.94448 10-6 , 0.00822198, 0.000140408, 0.00033855,
0.0108393, 4.5142 10-6 , 0.00124033, 0.0194542, 0.00476511, 0.0000459428,
0.000134485, 0.00205361, 0.0025077, 0.00159756, 0.0000189271, 0.00431094,
0.0024297, 0.00565154, 0.0000264337, 0.00362462, 0.00128786, 0.0104169,
0.0153881, 0.000150959, 0.0101595, 0.0000391054, 0.00697663, 0.000135607,
0.00596834, 0.00196813, 0.000128995, 0.00387855, 0.00588067, 0.000962794,
0.00443856, 0.0073437, 0.00183967, 1.22504 10-6 , 0.00201008, 0.00554012,
0.00045508, 0.000342589, 0.0162352, 0.0000492702, 0.00107983, 0.0000435502,
0.000244655, 0.0000105425, 0.00622943, 0.000558594, 0.00574489, 0.00392331,
0.000807148, 0.000730863, 0.0010365, 0.0152894, 6.61845 10-6 , 0.000200309,
0.0141578, 0.00264936, 0.00114312, 0.0056939, 0.00846002, 0.000994262, 0.00844807,
0.0000519488, 0.00502099, 0.00961028, 0.00859044, 0.00476375, 0.0117947, 0.0160187,
0.000419733, 0.0135895, 1.75975 10-7 , 0.000517912, 0.000068158, 3.45246 10-6 ,
0.0126821, 0.0000104559, 0.0170619, 0.0035929, 0.0165844, 0.00532236, 0.00109018,
0.0000249902, 0.000393511, 1.1866 10-6 , 0.014566, 2.58471 10-6 , 0.00187643,
0.0000432235, 0.00583125, 0.00575982, 3.50824 10-6 , 0.00272052, 0.0120881,
0.00263635, 0.0115913, 0.0039552, 1.53046 10-10 , 0.00747756, 0.00473032,
0.00353219, 0.00326945, 0.0105741, 0.0084629, 0.0000124332, 0.00581665,
0.014627, 0.00589033, 0.00449632, 0.0126783, 0.00633762, 0.000967758, 0.0181437,
0.000567988, 0.00500958, 0.0013562, 0.000579789, 0.0112825, 2.50887 10-6 ,
2.16722 10-7 , 0.00049282, 0.00766097, 0.0000335631, 0.0000244194, 0.000447865,
0.0000210202, 0.000388116, 0.000882604, 0.000297069, 0.0077189, 0.0139981,
0.000596376, 0.0000580394, 0.000434341, 0.000967612, 0.000395068, 0.00112824,
0.00293323, 0.0000522139, 0.00156941, 0.00680069, 0.000101842, 0.00203769,
0.0000903871, 0.0187704, 1.06093 10-6 , 0.00287117, 0.00112923, 0.00212782,
0.0000200171, 0.00225212, 0.00867706, 0.0000106293, 0.000107484, 0.0155393,
0.000395441, 0.00490158, 0.000239062, 0.00110742, 0.00139576, 0.00102867,
0.00489862, 0.0017106, 0.00131243, 0.000139571, 0.00063665, 0.0181766,
0.00330533, 0.00282335, 0.00748895, 0.00465378, 0.01685, 0.000673278, 0.00986643,
0.00946505, 0.00171654, 0.00870859, 0.00131833, 0.000199519, 0.00121186,
0.00156812, 0.0128188, 0.0138604, 0.00675051, 0.00869148, 0.0173669, 0.00065959,
0.0000309451, 0.00286425, 0.0161171, 0.0003528, 0.00749464, 0.000191138,
0.00147359, 0.00972992, 0.000366842, 0.0136166, 0.00635113, 0.00147412,
0.0000258744, 0.0000177056, 2.552 10-7 , 0.00531208, 0.0100643, 0.00502177,
0.0142494, 0.0146613, 0.00813104, 0.0000123125, 0.000330372, 0.00132723,
0.000844079, 0.000130583, 0.00747575, 0.00270824, 0.00151212, 0.00887436,
0.00290024, 0.0000902185, 0.0015688, 8.82965 10-7 , 0.0000953011, 0.00188874,
0.00499153, 0.00239396, 0.0130714, 0.000419518, 0.00350885, 0.0000672115,

12

ECM6Lecture4Vitenam_2014.nb

0.00290024, 0.0000902185, 0.0015688, 8.82965 10 , 0.0000953011, 0.00188874,


0.00499153, 0.00239396, 0.0130714, 0.000419518, 0.00350885, 0.0000672115,
0.0143129, 0.0102586, 0.000388227, 0.00414798, 0.0000334843, 5.10143 10-7 ,
0.00545308, 0.000680595, 0.000774817, 0.00478531, 0.00440509, 0.00694935,
0.00523704, 0.00641403, 0.0146276, 0.0000680415, 0.000792623, 0.0000601091,
0.004759, 0.0114314, 0.0000163664, 0.00267548, 0.000423085, 0.0058813,
0.000278751, 0.0111049, 0.0107631, 1.17614 10-6 , 0.000437119, 0.0170596,
0.00128347, 0.0000562874, 0.000390929, 0.0134204, 0.00146401, 0.00764174,
0.00315139, 0.000122476, 0.00809797, 0.00588624, 0.0000544486, 6.31893 10-6 ,
0.0000683318, 0.00172383, 0.000517713, 0.00142271, 5.47515 10-7 , 0.000214842,
0.000342781, 0.00049415, 0.0114536, 0.000510842, 0.000411314, 0.0120859,
0.000436484, 0.0014185, 0.01457, 0.0160765, 0.00382305, 0.0162903, 3.26418 10-7 ,
0.0000436615, 0.000243007, 4.33933 10-8 , 0.0192279, 0.00284635, 0.00153306,
0.00819535, 0.0034428, 0.0197126, 0.00482823, 8.23273 10-7 , 0.0000545067,
0.00701351, 0.0000479378, 0.0000700458, 6.88368 10-7 , 0.00691365, 0.000243012,
0.0000223037, 0.000859213, 0.00482632, 0.0122153, 0.0000775853, 0.000516708,
6.91127 10-12 , 6.75159 10-6 , 0.015857, 0.0000305389, 0.0000360585, 0.0155152,
0.000882024, 0.000585037, 0.00169279, 0.00838745, 6.371 10-6 , 4.34718 10-7 ,
0.0100133, 0.000268619, 0.000273726, 0.00423902, 0.0000350834, 0.00344404,
0.00987266, 7.55343 10-6 , 0.00338204, 0.000385288, 0.0104041, 0.00275358,
0.000563346, 0.0000907107, 0.00346795, 0.0111393, 0.00853541, 0.0167433,
0.00757538, 0.00557889, 0.0000202664, 0.000117992, 0.00237549, 0.0133118,
0.0193418, 0.00489404, 0.00363789, 0.00195775, 0.00563255, 0.000948934,
0.0116847, 0.00030752, 1.80975 10-6 , 0.00201697, 0.00422466, 0.000352697,
0.00663692, 0.00227377, 0.00101754, 0.0000499529, 0.00112176, 0.0003131,
0.0000818408, 0.00930908, 0.00901082, 0.000341212, 0.0000823915, 0.000499237,
0.00793249, 0.0000816295, 0.0000308951, 0.00188781, 0.000540266, 0.0000719551,
0.00305381, 0.00863212, 0.000467352, 0.0154775, 0.010503, 0.00715929,
0.000230859, 0.00705536, 0.0000961097, 0.00450776, 0.00460654, 0.00114116,
0.0173252, 0.00440915, 0.0000909107, 0.000520004, 0.0000599745, 0.00569918,
0.00808324, 0.00115777, 0.000163226, 0.00267609, 0.000410107, 0.00257728,
3.16166 10-8 , 0.0158662, 0.00192133, 0.000015619, 0.00351193, 0.00682917,
4.38002 10-6 , 0.00263304, 0.00187274, 0.0125162, 0.0125142, 0.00402207,
6.18014 10-6 , 0.00200052, 0.00347697, 0.0033558, 0.0150423, 0.00221408,
0.000395923, 0.00909366, 0.0116341, 1.75319 10-7 , 0.000225353, 0.00505918,
0.00803067, 0.000182184, 0.0154791, 0.000489031, 0.00467235, 0.00196031,
0.0115585, 0.000103708, 0.0000172788, 0.00174393, 0.00117431, 0.00100211,
0.0000138415, 0.0000679561, 0.00684416, 0.0121925, 0.0183212, 0.000709556,
0.000947271, 0.000372853, 0.000337164, 0.00203402, 0.00562084, 0.00659284,
0.00952207, 0.000282856, 0.00239846, 0.00142958, 0.0185204, 0.0000883534,
0.00120038, 0.0175743, 0.00843076, 0.0000312315, 0.000169168, 0.000167887,
0.0120778, 0.00229855, 0.00167689, 0.000378679, 0.000109158, 0.00391562,
0.000210506, 7.47072 10-8 , 0.0121928, 1.71313 10-6 , 0.0000112009, 0.000109664,
1.57682 10-8 , 0.00778603, 0.000984636, 0.00742809, 0.0164639, 0.0000437568,
0.00270469, 0.0000121681, 0.00109347, 0.00395542, 0.000707639, 0.00761288,
0.0154583, 0.00422806, 0.0154944, 0.0133073, 0.000122812, 0.00490095,
0.0000347095, 0.000856304, 0.00167134, 4.79923 10-6 , 0.00241362, 0.000182347,
0.00962113, 6.92904 10-6 , 8.95362 10-7 , 0.000746703, 0.00301382, 0.01245,
0.000750642, 0.00887798, 0.00153503, 0.0188354, 1.53452 10-6 , 0.0000242767,
0.0000981494, 0.0145522, 0.00282383, 0.00418248, 0.00343161, 0.00479166,
0.000491287, 9.44034 10-7 , 0.0079082, 0.0103636, 1.52991 10-6 , 0.0022231,
0.0000204518, 0.000329357, 0.00167135, 0.0162235, 0.0025317, 0.00637365,
0.00306003, 0.00937322, 0.00224227, 0.0000820269, 0.00939526, 0.000554295,

ECM6Lecture4Vitenam_2014.nb

0.0000204518, 0.000329357, 0.00167135, 0.0162235, 0.0025317, 0.00637365,


0.00306003, 0.00937322, 0.00224227, 0.0000820269, 0.00939526, 0.000554295,
0.00100977, 0.00489915, 0.00113154, 0.0073677, 0.00116331, 0.000016701,
0.0127756, 0.01424, 0.00080138, 0.00378623, 0.0000743093, 0.0101466, 0.00236877,
0.00030836, 0.0000778939, 0.00587854, 0.000152314, 0.0177107, 0.0104239,
0.000111377, 0.00860227, 0.00326438, 0.00165201, 0.000683681, 0.0119767,
0.00293031, 0.0131516, 0.0151297, 0.0113093, 0.00204908, 0.00138678, 1.0489 10-6 ,
0.000447294, 0.00867256, 0.00172755, 1.60083 10-6 , 0.003007, 0.00587169,
0.00255049, 0.000125475, 0.00288903, 0.0177567, 0.00384598, 0.00140704,
0.00947227, 0.0114073, 0.000673029, 0.000265183, 0.00134905, 0.00242405,
0.0170736, 0.0101802, 0.0137087, 0.0063508, 0.000565843, 0.000722318, 0.0141471,
0.0000122117, 0.00823419, 0.0205398, 0.0016322, 0.00201376, 0.0131773,
0.0000472101, 0.0085921, 0.0131084, 0.0000164358, 0.0000106505, 0.00139875,
0.00890288, 0.00582573, 0.00195287, 0.000123264, 0.00114172, 0.000102327,
0.00919197, 0.000330562, 0.00832795, 0.00804993, 0.00273547, 0.0107238,
0.00397541, 0.000239417, 0.0120868, 0.000318885, 0.00507542, 0.0000341067,
0.000296369, 0.00556374, 0.0143331, 0.00145291, 0.0113909, 0.00816484, 0.0160693,
0.00883642, 0.0106156, 0.000687501, 0.00325885, 0.0118112, 0.00664698,
0.000407558, 0.014736, 0.0177285, 0.00978723, 9.08388 10-9 , 0.00055554,
0.0140515, 0.00112774, 0.0000841486, 0.0123762, 0.0109141, 0.0144134, 0.00808699,
0.00444522, 0.00590321, 0.0016758, 0.0175467, 0.00290958, 0.0165155, 0.0024997,
0.0116149, 0.00793368, 0.00340189, 0.00897392, 1.33557 10-6 , 0.00241276,
0.000136441, 0.00321707, 0.00196128, 0.00851136, 0.0174654, 0.000106382,
0.00632255, 0.0159495, 0.00214144, 0.0180288, 0.000196069, 0.00923088, 0.010447,
0.00035127, 0.00198197, 0.00530604, 0.00917657, 0.000020421, 0.000235211,
0.00211722, 8.29399 10-7 , 1.07396 10-6 , 0.00307003, 0.00922396, 0.011149,
0.00243651, 0.00178787, 0.00353512, 5.81772 10-7 , 0.0037465, 0.0111118,
0.00532739, 0.00251458, 0.00831541, 0.00209085, 0.00512705, 0.000207399,
0.00957291, 0.00216826, 3.50516 10-6 , 0.0105957, 0.000663509, 0.00235411,
0.00159781, 0.0000154793, 0.00723576, 0.000064546, 0.0175519, 0.00813381,
3.34547 10-6 , 0.000146651, 0.000700072, 0.00143534, 0.0128461, 0.0109876,
0.00791897, 3.0685 10-7 , 0.00261465, 0.000810813, 0.00410632, 0.00861096,
0.00757444, 0.0061126, 0.000204546, 0.000124328, 9.04753 10-8 , 0.0000957394,
0.000467976, 0.0120681, 0.00125741, 0.0000153384, 0.00661716, 0.0137432,
0.00759746, 5.84889 10-7 , 0.0007011, 0.0000167875, 0.00978237, 0.0000364716,
0.015433, 0.00661937, 0.000596054, 0.000638668, 0.000342587, 0.00295997,
0.00362435, 0.000138358, 0.00117219, 0.0000415257, 0.0154944, 0.012147,
0.0138884, 0.00548946, 0.00336359, 0.0068739, 0.00801741, 0.0153127, 0.000171052,
0.0000219547, 0.015858, 0.0138246, 3.3257 10-7 , 0.000222541, 0.0182823,
0.00448602, 0.00102166, 1.30594 10-6 , 0.00585061, 0.000633497, 0.0110836,
1.33888 10-6 , 0.00219768, 0.0176011, 0.00499999, 0.01745, 0.0000457487,
0.00039521, 0.00826984, 0.000209906, 0.00470269, 0.00125793, 0.0112319,
0.00759208, 0.00520124, 0.0000168424, 0.0000136774, 0.0000308458, 0.00472378,
0.000497195, 0.0134015, 0.00175843, 4.44559 10-6 , 7.2396 10-6 , 0.00863988,
0.00143561, 0.00276572, 0.0195765, 0.0000209013, 0.00981014, 0.00588812,
0.0078628, 0.00799487, 0.00293786, 0.00210265, 0.00229545, 0.0135832, 0.00151429=

13

14

ECM6Lecture4Vitenam_2014.nb

Apply Function
In[34]:=

Remove@myDataD

The function Apply allows one to replace the Head of expressions


In[35]:=

? Apply
Apply@ f , exprD or f expr replaces the head of expr by f .
Apply@ f , expr, 81<D or f expr replaces heads at level 1 of expr by f .
Apply@ f , expr, levelspecD replaces heads in parts of expr specified by levelspec.

What is the Head of a function?


In[36]:=
Out[36]=

8Head@Sin@xDD, Head@81, 2, 3, 4<D, Head@3.5678D, Head@3 + 4 D<


8Sin, List, Real, Complex<

Example 1:
Consider the following list of 10 real data points
In[37]:=
Out[37]=

myData = RandomReal@8- 10, 10<, 10D


85.84686, 3.97765, - 4.76625, 7.20701, 6.74128,
- 5.49718, - 7.04753, - 1.38867, - 0.981835, 0.33432<

We can check what the head is of the expression mydata using the function Head
In[38]:=
Out[38]=

Head@myDataD
List

As expected it is a list.
Suppose now we change the head of mydata to Plus, using Apply
In[39]:=
Out[39]=

Apply@Plus, myDataD
4.42566

By changing the head to Plus we effectively sum up the data.

Example 2:
If we change the head to Times we multiply all the elements in the list together.
In[40]:=
Out[40]=

Apply@Times, myDataD
- 95 104.3

Example 3:
To find the mean of the data in our list we could do the following

ECM6Lecture4Vitenam_2014.nb

In[41]:=
Out[41]=

Apply@Plus, myDataD Length@myDataD


0.442566

This is the same as using the function Mean.


In[42]:=
Out[42]=

Mean@myDataD
0.442566

15

16

ECM6Lecture4Vitenam_2014.nb

Nest Function
In[43]:=

Remove@g, iterates, finalValueD

As we saw in a previous lecture, many algorithms are iterative or recursive, having the form
xi+1 = f Hxi L
The function Nest is the basic function for doing iteration. The function NestList gives all the intermediate values during the iteration.
In[44]:=

? Nest
Nest@ f , expr, nD gives an expression with f applied n times to expr.

In[45]:=

? NestList
NestList@ f , expr, nD gives a list of the results of applying f to expr 0 through n times.

Thus NestList@f , x0 , nD does the iteration


xi+1 = f Hxi L
n times starting with the value x0 . Often we use a pure function for f.

Example 1:
In[46]:=

Out[46]=

NestListB

&, 1, 6F
1+
1 2 3 5
8
13
:1, , , , ,
,
>
2 3 5 8 13 21

We can use NestList to generate continued fractions


In[47]:=

Out[47]=

NestListB
:x,

1
1+x

1
1+
1
1+

&, x, 5F

1
1+x

,
1+

1+

1
1

1+

1+x

1+

1+

1
1+

1
1+x

>

1
1

1+

1+
1+

1
1+x

Example 2:
With this in mind we use NestList with a pure function to generate the iterates of our function g@1.5, xD.
As our starting value of x we take x0 = 5 and generate 20 iterates
Consider our previous function
In[48]:=

g@a_, b_, x_D :=

1
a

Ha - 1L x +

b
x

ECM6Lecture4Vitenam_2014.nb

Let us determine the fixed point of this function by iterating using NestList
In[49]:=
Out[49]=

In[50]:=
Out[50]=

iterates = NestList@g@1.5, 78.4, D &, 5, 20D


85, 12.12, 8.35243, 9.0418, 8.79449, 8.87461, 8.84766,
8.85662, 8.85363, 8.85463, 8.85429, 8.85441, 8.85437, 8.85438,
8.85438, 8.85438, 8.85438, 8.85438, 8.85438, 8.85438, 8.85438<
finalValue = Nest@g@1.5, 78.4, D &, 5, 20D
8.85438

17

18

ECM6Lecture4Vitenam_2014.nb

Fixed Point Function


In[51]:=

Remove@gD

Mathematica has a built-in function called FixedPoint that can determine the fixed points of a function
In[52]:=

? FixedPoint
FixedPoint@ f , exprD starts with expr, then applies f repeatedly until the result no longer changes.

Let us apply it to the function g defined previously


In[53]:=

In[54]:=
Out[54]=

g@a_, b_, x_D :=

1
a

Ha - 1L x +

b
x

FixedPoint@g@1.5, 78.4, D &, 5D


8.85438