Class 4 : Programming in Mathematica Part #2

Note : Almost all of the examples below are taken from the Mathematica documentation center.

In this class we shall see how to handle, expressions, lists, functions, patterns (a bit). In the next class we shall see shall look at procedural programming in Mathematica.

Making Lists of Objects

{3,5,1}

{3,5,1}^2+1

{6,7,8}-{3.5,4,2.5}

Exp[%]//N

v={2,4,3.1}

v/(v-1)

Getting Pieces of Lists

t={a,b,c,d,e,f,g}

Last[t]

t[[3]]

t[[3;;6]]

t[[{1,4}]]

Take[t,3]

Take[t,-3]

Take[t,{3,7,2}]

Rest[t]

Drop[t,3]

Defining Functions

f[x_]:=x^2

f[a+1]

f[4]

?f

hump[x_,xmax_]:=(x-xmax)^2/xmax

2+hump[x,3.5]

hump[x_,xmax_]:=(x-xmax)^4

?hump

Clear[hump]

Applying Functions to Parts of Expressions

Map[f,{a,b,c}]

take2[list_]:=Take[list,2]

Map[take2,{{1,3,4},{5,6,7},{2,1,6,6}}]

Map[f,a+b+c]

Map[Sqrt,g[x^2,x^3]]

m={{a,b},{c,d}}

Map[f,m]

Map[f,m,{2}]

Scan[Print,{a,b,c}]

Scan[Print,1+x^2,Infinity]

Applying Functions to Lists and Other Expressions

Apply[f,{a,b,c}]

Apply[Times,{a,b,c}]

geom[list_]:=Apply[Times,list]^(1/Length[list])

Apply[List,a+b+c]

m={{a,b,c},{b,c,d}}

Apply[f,m]

Apply[f,m,{1}]

Apply[f,m,{0,1}]

Adding, Removing and Modifying List Elements

Prepend[{a,b,c},x]

Insert[{a,b,c},x,2]

Riffle[{a,b,c},x]

?Riffle

ReplacePart[{a,b,c,d},3->x]

ReplacePart[{{a,b},{c,d}},{1,2}->x]

v={a,b,c,d}

v[[3]]=x

v

m={{a,b},{c,d}}

m[[All,1]]={x,y};m

m[[All,1]]=0;m

Manipulating Lists by Their Indices

{a,b,c,d}[[{1,3}]]

(m={{a,b,c},{d,e,f},{g,h,i}})//TableForm

m[[{1,3},{1,2}]]//TableForm

Extract[m,{{1,3},{1,2}}]

m={{a[1],a[2],b[1]},{b[2],c[1]},{{b[3]}}};

Position[m,b[_]]

Extract[m,%]

Insert[{{a,b,c},{d,e}},x,{2,1}]

Delete[%,{2,1}]

IdentityMatrix[3]

ReplacePart[%,{2,2}->x]

Nested Lists

Table[x^i+j,{i,2},{j,3}]

Array[x^#1+#2&,{2,3}]

Table[x^i+j,{i,3},{j,i}]

Array[a,{2,3}]

Flatten[%]

ArrayFlatten[{{{{1}},{{2,3}}},{{{4},{7}},{{5,6},{8,9}}}}]

Array[a,{2,2,2}]

Transpose[%,{3,1,2}]

m={{{a,b},{c,d}},{{e,f},{g,h},{i}}};

Map[f,m,{2}]

Apply[f,m,{2}]

Pure Functions

h[x_]:=f[x]+g[x]

Map[h,{a,b,c}]

Map[f[#]+g[#]&,{a,b,c}]

Function[x,x^2]

%[n]

Map[Function[x,x^2],a+b+c]

Nest[Function[q,1/(1+q)],x,3]

Function[{x,y},x^2+y^3][a,b]

Map[#^2&,a+b+c]

f[##,##]&[x,y]

Apply[f[##2,#1]&,{{a,b,c},{ap,bp}},{1}]

fromdigits[digits_]:=Fold[(10#1+#2)&,0,digits]

fromdigits[{1,5,2,3,4}]

Applying Functions Repeatedly

?Fold

?FoldList

?Nest

?NestList

Nest[f,x,4]

NestList[f,x,4]

recip[x_]:=1/(1+x)

Nest[recip,x,3]

newton3[x_]:=N[1/2 (x+3/x)]

NestList[newton3,1.0,5]

FixedPoint[newton3,1.0]

FixedPointList[newton3,1.0]

divide2[n_]:=n/2

NestWhileList[divide2,123456,EvenQ]

NestWhileList[newton3,1.0,Unequal,2]

NestWhileList[Mod[5#,7]&,1,Unequal,All]

FoldList[f,x,{a,b,c}]

Fold[f,x,{a,b,c}]

FoldList[Plus,0,{a,b,c}]

Testing and Searching List Elements

Position[{a,b,c,a,b},a]

Count[{a,b,c,a,b},a]

MemberQ[{a,b,c},a]

MemberQ[{a,b,c},d]

m=IdentityMatrix[3]

FreeQ[m,0]

Position[m,0]

Building Lists from Functions

Array[p,5]

Table[p[i],{i,5}]

Map[p,Range[5]]

p[x_]:=(p^2)
Array[p,5]
Table[p[i],{i,5}]
Map[p,Range[5]]

Array[#+#^2&,5]

Array[m,{2,3}]

Array[Plus[##]^2&,{3,3}]

NestList[D[#,x]&,x^n,3]

Selecting Parts of Expressions with Functions

Select[{2,15,1,a,16,17},#>4&]

t=Expand[(x+y+z)^2]

Select[t,FreeQ[#,x]&]

Select[{-1,3,10,12,14},#>3&,1]

Everything Is an Expression

x+y+z

FullForm[%]

1+x^2+(y+z)^2

FullForm[%]

Head[f[x,y]]

?Head

Head[a+b+c]

Head[{a,b,c}]

Head[23432]

Head[345.6]

Part[{a,b,c},0]

Part[{a,b,c},1]

Part[{a,b,c},3]

Part[{a,b,c},4]

Part[23242,0]

Part[23242,1]

Immediate and Delayed Definitions

ex[x_]:=Expand[(1+x)^2]

?ex

iex[x_]=Expand[(1+x)^2]

?iex

ex[y+2]

iex[y+2]

r1=RandomReal[]

r2:=RandomReal[]

{r1,r2}

{r1,r2}

Applying Transformation Rules

x+y/.x->3

x+y/.{x->a,y->b}

x+y/.{{x->1,y->2},{x->4,y->2}}

Solve[x^3-5x^2+2x+8==0,x]

x^2+6/.%

{x^2,x^3,x^4}/.{x^3->u,x^n_->p[n]}

h[x+h[y]]/.h[u_]->u^2

{x^2,y^3}/.{x->y,y->x}

x^2/.x->(1+y)/.y->b

x^2+y^6/.{x->2+a,a->3}

x^2+y^6//.{x->2+a,a->3}

log[a b c d]/.log[x_ y_]->log[x]+log[y]

log[a b c d]//.log[x_ y_]->log[x]+log[y]

THIS WAS A LOT OF MATERIAL - YOU MUST NOW PRACTICE.