Mathematica II.

Functions

From the previous class

A function definition looks like this:

f[x_]:=(x^3)-x

A normal function call looks like this:

f[x]

Prefix apply

f@x

Postfix apply

x//f

4!
Factorial[4]

There are some infix operators: a+b as well.

Precedence of  @ and // differ, @ is stronger:

2Pi+1//f

f@2Pi+1

The bracket function call order is determined by the brackets.

f[x,g[y,z]]

The prefix notation is evaluated right-to-left

f@g@x

But you can specify with parenthesis

(f@g)@x

The postfix notation is evaluated left-to-right

x//f//g

But you can specify with parenthesis

x//(f//g)

Map

/@ applies a function to every element of a list.

Sin/@{Pi/2,Pi/3,Pi/5}

Cos/@{.25,.75}

Notice the difference between @ and /@

Length/@{{1},{a,b},{b,c,d,e},{22,23}}

Length@{{1},{a,b},{b,c,d,e},{22,23}}

Reverse@{{1},{a,b},{b,c,d,e},{22,23}}

Reverse/@{{1},{a,b},{b,c,d,e},{22,23}}

There us a function called Map which does exactly this:

Map[Cos,{Pi,2Pi}]

Some functions do this automatically for a list:

Sqrt@{2,3}

N[Sqrt[{2,3}]]

Prime@{10,1000,100000}
Prime/@{10,1000,100000}

Sum is treated as a list of summands

f/@(a+b c+d)

Product is treated as a list of coefficients

f/@(a (b+c)d)

In case of Map the outermost operation is treated as a list

Map[g,h[x,y,z]]

Levels of Apply

The @@ treats the elements of a list as arguments. Note that in this case the function should be a multivariate function, not one list variable.

g@{a,b,c}

g@@{a,b,c}

g@@{{a,b},{c,d}}

g@{{a,b},{c,d}}

Pure, anonymous, lambda function

The followings are equivalent

g[x_]:=x-x^2
g/@{a,b,c}

Map[#-#^2&,{a,b,c}]

Now watch this :

((#)^2)&@x

(((#)^2)&)[x]

Map[Function[x,x-x^2],{a,b,c}]

The pre/post-fix notation can use a pure function, but only with one variable.

Range[10]

Range[10]//((#)/(Total[#]))&

Mod[#,3]&/@Range[10]

One can store a function in a variable.

f=((#)^2)&;
f=Function[x,(x^2)];
f[3]

f+g

Map[f, Range[10]]

Operators

Function valued functions: operators.

Derivative[1][f]

The Derivative is an operator-valued function.

∂:ℕ↦(∂_n)
(∂_n):(ℝ↦ℝ)↦(ℝ↦ℝ)

Derivative[n][F][x]

You can differentiate formulas with D

D[f,x](*nem jó*)
D[f[x],x](*jó*)
D[f[x],{x,2}](*második derivált*)

f//Derivative[1]
x//(f//Derivative[1])

Iterative applications

Nest applies a function to the result of the previous result, iteratively. NestList stores the previous values too.

Nest[F,x,3]
NestList[F,x,3]

Nest[f,x,3]

Nest[#^2&,3,5]

3^32

NestList[#^2&,3,5]

Nest[1+1/#&,x,4]

NestList[1+1/#&,1,6]

FixedPoint applies the function until the result is unchaned:

NestList[Cos[#]&,1.0,10]

FixedPoint[Cos[#]&,1.0]

The Fold FoldList does a similar thing, but the  applied function has two arguments and the second argument is supplied by a list.

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

Range[2,10]
FoldList[Times,1,Range[2,10]]

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

FoldList[Times,1,{a,b,c}]

Times[a,b,c]

Times[]

Select

Test functions

Usually the functions ending with Q give a boolean result (True or False).

EvenQ[11]
OddQ[11]

IntegerQ[12.2]
NumberQ[x]

Select

Selects from a list with a given criteria. The critera should be a test function!

Select[{1,2,3,4,5},EvenQ]
Select[{1,2,3,4,5},OddQ]
Select[Range[100], PrimeQ]

You can use pure function in the criteria:

Select[Range[10], Mod[10,#]==0&]

Select[Range[100], 10≤#≤99&&Mod[#,3]==0&]

Range[12,99,3]