Math 350 Mathematical Software

Lecture 2: Anonymous functions and lists

Anonymous functions

Formula as a function

f[x_]:=x^2 + 1

f[2]

Anonymous or “pure” function has no name

#^2 + 1&[2]

#^2 + 1&@2

f@2

2 // f

2//PrimeQ

2//#^2 + 1&

Lists

A = {a, {a, b, c}, {a, b}, {{a}}}

Length[A]

A[[1]]

A[[2]]

A[[3]]

A[[4]]

A[[2]]

A[[2]][[2]]

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

A

A[[4]][[1]][[1]]

?Range

Range[5]

Range[3,5]

Range[6, 27, 3]

Table[i^2, {i, 1, 5}]

Table[PrimeQ[i], {i, 1, 100}]

Table[x^i, {i, 1, 10}]

Lists and functions

Listable function can be applied to a list and it acts elementwise

B = 2 Table[i, {i, 1, 10}] + 3

PrimeQ[B]

B//PrimeQ

Not every command is listable

Table[(1+x)^n, {n, 1, 5}]

Map (/@) command can make a function act entrywise

Map[Expand, Out[39]]

Expand /@Out[39]

Select

Select generates a subset of a list based on a rule (Boolean)

Select[Range[100], PrimeQ]

Divisible[34, 17]

Select[Range[300], Divisible[# , 17]&]

Select[Range[300], Mod[#, 17]==0&]

What did we do

Anonymous functions (evaluate functions [], //, @)
Table, generation of lists
Range
Select and the construction of boolean conditions using anonymous functions