Introduction to Mathematica

Julius, EIMI

Lecture 2 - based on lectures by N. Gromov (King’s College London) and J.Harris  (Wolfram Research)

More on Functions

We briefly saw query functions last time. Such functions usually end with the letter “Q” and return a Boolean value.

EvenQ[2]

OddQ[2]

?AtomQ

AtomQ[a+b]

AtomQ[a]

?NumberQ

?SameQ

a===b

a==b

?FreeQ

LinearFunction[a+b]

FreeQ[LinearFunction[a+b],c1_[a1_+b1_]]

LinearFunction[a+b]/.LinearFunction[a1_+b1_]:>LinearFunction[a1]+LinearFunction[b1]
FreeQ[%,c1_[a1_+b1_]]

LinearFunction[a+b+c+d]/.LinearFunction[a1_+b1_]:>LinearFunction[a1]+LinearFunction[b1]
FreeQ[%,c1_[a1_+b1_]]

LinearFunction[a+b+c+d]//.LinearFunction[a1_+b1_]:>LinearFunction[a1]+LinearFunction[b1]
FreeQ[%,c1_[a1_+b1_]]

?MatchQ

MatchQ[b[(x^7),a,γ,t],a1_[(x^(a2_)),a3_,a4_,a5_]]

MatchQ[(1/2),((a_)/(b_))]

(1/2)//FullForm

(a/b)//FullForm

Other ways of defining functions.

(a|->(a^3))/@ {2,f,Sin[x]}

Function[a,(a^2)][t]

There are also so called “pure” functions, similar to “lambda” functions if you are familiar with Python, for example.

(# &)[t]

((((#)^3)/(Sin[#]))&)[t+r]

Let us use them to sort a list. There are two sorting functions, let us look at their documentation below.

?Sort

lst ={f[1,4],f[4,2],h[6,1],g[2,3]};

Sort[lst]

CheckLess[a1_[a2_,a_],a3_[a4_,b_]]:=a<b

Sort[lst,CheckLess]

Sort[lst,#1[[2]]<#2[[2]]&]

?SortBy

Table[lst[[i]][[2]],{i,Length@lst}]

SortBy[lst,#[[2]]&]

lst

Table[lst[[i]]//Last,{i,Length@lst}]

SortBy[lst,Last]

Length@bar[(x^2),(x^3),(x^6),(y^9),(x^7)]

?Cases

Cases[{(x^2),(x^3),(x^6),(y^9),(x^7)},((a_)^(b_?OddQ))]

Cases[{(x^2),(x^3),(x^6),(y^9),(x^7)},a_?OddQ]

{(x^2),(x^3),(x^6),(y^9),(x^7)}//FullForm

Cases[{(x^2),(x^3),(x^6),(y^9),(x^7)},a_?OddQ,2]

{{1,4,a,0},{b,3,2,2},{c,c,5,5}}

Cases[{{1,4,a,0},{b,3,2,2},{c,c,5,5}},_Integer]

{{1,4,a,0},{b,3,2,2},{c,c,5,5}}//FullForm

Cases[{{1,4,a,0},{b,3,2,2},{c,c,5,5}},_Integer,1]

Cases[{{1,4,a,0},{b,3,2,2},{c,c,5,5}},_Integer,2]

Cases[{{1,4,a,0},{b,3,2,2},{c,c,5,5}},_Integer,∞]

Finally, for pattern matching, we can use more general patterns than just “x_”. Namely, “x__” will match one or more arguments, and “x___” will match zero or more arguments.

Clear[foo]
foo[a_,c___,b_]:=g[a,b]

foo[5,6]

foo[5,7,6]

foo[5,7,8,9,10,6]

Problems

Exercise 1

Generate a list of square numbers from 1 to 100, use the “/@” construction. Replace the head of the list to obtain the sum of squares.

Solution

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

?Range

Range[10]

#^2&/@Range[100]

Range[100]^2

Head[(Range[100]^2)]

Plus@@(Range[100]^2)

?Total

Total[(Range[100]^2)]

Exercise 2

Construct a 4 x 4 Vandermonde matrix and obtain its determinant.

Solution

vdm = Table[((Subscript[α, i])^j),{i,1,4},{j,0,3}]
vdm//MatrixForm

Det[vdm]//Simplify

Exercise 3

Expand (1 + a + ((x))^10), collect in terms of x and Simplify each coefficient.

Solution

?Collect

Collect[Expand[(1 + a + x)^10],x,Simplify]

Expand[(1 + a + x)^10];
Collect[%,x,Simplify]

Exercise 4

Generate the sum (∑_(i=1)^5)((Subscript[a, i])/(Subscript[b, i])) and make it have a common denominator.

Solution

(∑_(i=1)^5)((Subscript[a, i])/(Subscript[b, i]))

(∑_(i=1)^5)((Subscript[a, i])/(Subscript[b, i]))//Together

Problem 1

Define a function “MyLog” which works as follows:

MyLog[a b c 5] = MyLog[a] + MyLog[b] + MyLog[c] + MyLog[5]

Solution

MyLog[a_ b_]:=MyLog[a] + MyLog[b]

MyLog[a b c 5]

Problem 2

Define a function which computes the mean of a list

Solution

MyMean[ls_]:=(1/(Length[ls]))Plus@@ls

MyMean[{1,2,3}]

Problem 3

Implement a function “FirstAndLast”, which returns the first and last element of the input.

Solution

FirstAndLast[a_,b___,c_]:={a,c}

FirstAndLast[a,b,c,d,e,f,x,y,z]

FirstAndLast[a,z]

Problem 4

Expand (1 + a + ((x))^10) and produce a list of terms with only odd powers of x.

Solution

List@@Collect[Expand[(1 + a + x)^10]/.(x^(a1_?EvenQ))->0,x,Simplify]

Problem 5

Do a series expansion of the exponential function up to the third order and plot it against the real function.

Solution

srs = Series[Exp[x],{x,0,3}]//Normal

Plot[{srs,Exp[x]},{x,0,5}]

Problem 6

Plot a 3d parabola.

Solution

?Plot3D

Plot3D[x^2 + y^2,{x,-10,10},{y,-10,10}]

Problem 7

Plot the real and imaginary parts of (Sqrt[(x^2)-1]) on the complex plane. Make sure that it has only 1 cut going from -1 to 1.

Solution

Table[Plot3D[(Sqrt[x - 1])(Sqrt[x+1])/.x->a + I b//ff//Evaluate,{a,-2,2},{b,-2,2}],{ff,{Re,Im}}]

Problem 8

Use only patterns to transform 
		M, a, p, {}, l, e, S, {y, {r}}, u, p 
to 
			M, a, p, l, e, S, y, r, u, p.

Solution

{{M,a,{p,{},{{l},e}}},S,{y,{r}},u,p}/.{a___,{b___},c___}:>{a,b,c}

{{M,a,{p,{},{{l},e}}},S,{y,{r}},u,p} //.{a___,{b___},c___}:>{a,b,c}

Problem 9

Define a function that symmetrises the arguments.

Solution

Sym[f_[a__]]:=Sum[f@@xx,{xx,Permutations[{a}]}]/Length[Permutations[{a}]]

Sym[G[x,y,z]]

Sym[G[x,x,z]]

Problem 10

Write a function that deletes duplicates in a list.

Solution

DropDups[h_[l___,a_,m___,a_,r___]]:=DropDups[h[l,a,m,r]]
DropDups[any_]:=any

Table[RandomInteger[10],{30}]
DropDups[%]

Problem 11

Write a function to sort a list.

Solution

?OrderedQ

MySort[{l___,a_,b_,r___}]/;!OrderedQ[{a,b}]:=MySort[{l,b,a,r}]
MySort[any_]:=any

MySort @ {1,a,2,c,3,b}

MySort @ {2,a,1,c,3,b}

Problem 12

Write a function to implement the function Riffle.

Solution

?Riffle

ls = {1,2,3,4}

xs = Table[x,{i,Length[ls]}]

Flatten[(({ls,xs})^\[Transpose])]

MyRiffle[a_List,x_]:=Drop[Flatten[{a,Table[x,{Length @a}]}\[Transpose],1],-1]

MyRiffle[{1,2,3,4,5},x]

Problem 13

Implement Sum though recursion.

Solution

MySum[f_,{i_,b_,b_}]:=f/.i->b
MySum[f_,{i_,a_Integer,b_Integer}]:=f/.i->a+MySum[f,{i,a+1,b}]

MySum[i,{i,1,100}]

Problem 14

Implement a function “ExtractKth[expr,k]” which will keep every kth element of a structure.

Solution

?Part

ls

ls[[3]]

Part[ls,3]

Part[ls,{3,4}]

ExtractKth[expr_,k_]:=Part[expr,Range[k,Length@expr, k]]

ExtractKth[{a,b,c,d,e,f,g,h,i,j,k,l},3]

Problem 15

Given a list of functions, find the multiplier in the power x of any terms at any level.

Solution

?Cases

coefPowerOfX[a_]:=Cases[a,((f_)^(c_ x))->c,∞]

coefPowerOfX[{a (E^(-I x))+3,Sin[(x^2)],Sin[(2^(3 x))],f}]