Functional Constructs: Map, Apply...

l={"a","b",3,4,{-5,x,Exp[Pi]},f[6],t[5]}

l1=Map[h,l]

l2=Map[h,l,{2}]

TreeForm[l1]

TreeForm[l2]

l=RandomInteger[{10^1,10^2},{15}]

Map[Mod[#,17]==0&,l]

Map[{#,Mod[#,17]==0}&,l]

l1={a,b,c,d,e};
l2={1,2,3,4,5};

?-> {a,(b^2),(c^3),(d^4),(e^5)}

{l1,l2}//Transpose

Map[#[[1]]^#[[2]]&,Transpose[{l1,l2}]]

MapIndexed[]

Map[v,{a,b,c,d,e}]
MapIndexed[v,{a,b,c,d,e}]

MapIndexed[v,{{a,b,c},{d,e,f}}]

MapIndexed[v,{{a,b,c},{d,e,f}},{2}]

MapIndexed[v,{a,b,c,d,e}]

MapIndexed[ #1^#2[[1]]&, {a,b,c,d,e}]

l

Clear[l]

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

MapIndexed[ #1[[#2[[1]]]]&, {{a,b,c,d},{e,f,g,h},{i,j,k,l}}]

TreeForm[{{a,b},{c,d},e}]

MapIndexed[ v, {{a,b},{c,d},e},{2}]

MapThread[]

l1={1,2,3,4};
l2={a,b,c,d};
l3=Table[Random[],{4}];

MapThread[ t, {l1,l2,l3}]//ColumnForm

Clear[f,g,h]

functions^power[argument]

f={Sin,#^-2&,Exp,#^3-#&};
g={2,3,2,1};
h={Pi,E,I,4};

MapThread[t,{f,g,h}]

MapThread[#1[#3]^#2&,{f,g,h}]

Map[t,Transpose[{g,f,h}]]

Map[#[[1]][#[[3]]]^#[[2]]&,Transpose[{f,g,h}]]

Apply[] passes all the arguments to a function

Apply[ff,{1,2,3,4,5}]

Apply[Times,{a,2,b,4}]

Apply[Plus,{{1,a},{2,b},{4,d}}]

Apply[Join,{{2,3},{1},{2,3},{4,5}}]

Apply[Union,{{2,3},{1},{2,3},{4,5}}]

Syntax

Clear[f,g,h]

Map[g,{arg0,arg1,arg2}]
g/@{arg0,arg1,arg2}

Apply[g,{arg0,arg1,arg2}]
g@@{arg0,arg1,arg2}

Map[t,l1]

t@l1

t/@l1

t@@l1

Select[], Cases[], Position[], Count[]...

?Select

Select[
Table[Random[],{20}],
#>.5&]

Cases[{a, a^b, 3^Pi, Pi^3}, _^_]

Position[{a, a^b, 3^Pi, Pi^3}, _^_]

More:  Inner[], Outer[], Thread[]...

Inner[Times,{1,2,3,4},{a,b,c,d},Plus]  (* inner product *)

Inner[Plus,{1,2,3,4},{a,b,c,d},Times]

Inner[{#1,#2}& ,{1,2,3,4},{a,b,c,d},Plus]

Replacement Rules

Clear[a,x,y,z];

FullForm[exp1->exp2]

exp/.rule

x=3+a

?x

x/.a->5

x

x/.{3->a}

(x/.{3->a})/.{a->5}

x

x/.3->a

x/..3->a

x

x/.{x->-10}

(x+10)/.{x->-10}

a

a/.x->5

3+a/.x->10

x/.{3+a->10}

x/.a->x

x+y/.a->y

?Replace

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

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

?//.

?ReplaceRepeated

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

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

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

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

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

??ReplaceRepeated

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

Successive replacement

(x+y/.{a->y})/.y->a

Multiple rules

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

Clear[a,b,x,y,z]

{3a,a^2,3^a,a^a,a!}/.a->z

{3a,a^2,3^a,a^a,a!}/.a->4

?Replace

The replacement rule contains a pattern.

{3a,a^2,3^a,a^a,a!}/.a^_->Infinity

{3a,a^2,3^a,a^a,a!}/._^_->Infinity

{3a,a^2,3^a,a^a,a!}/.a^x_->x^a

{3a,a^(2+Pi),3^a,(a+b)^a,a!}/.a_^exp_->exp^a

replacement rules and patterns

simplification

log[a b c d]//FullForm

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

log[a b c d]//.{log[Times[a_, b_]]->log[a]+log[b]}

the solutions of equations are provided in the form of replacement rules

p[x_]:=x^3-Pi x^2-Pi^2 x-3

p[x]

Plot[p[x],{x,-Pi/2,3/2Pi}]

sol=Solve[p'[x]==0,x]

p'[x]

p'[x]/.sol

p[x]/.sol

p[x]/.sol//N

options to Mathematica functions

ParametricPlot[
{Sin[u]u ,Cos[u]u}, 
{u,0,100}
]

Options[ParametricPlot]

ParametricPlot[
{Sin[u]u ,Cos[u]u}, 
{u,0,100},
PlotPoints->125,
Axes->False,
ColorFunction->(ColorData["Rainbow"][#3]&),
MaxRecursion->0,
PlotStyle->Thick
]

a program that accepts replacement rules as optional arguments

options[program]={opt1->val1, opt2->val2};

program[x_,y_,opt___]:=
definition/.{opt}/.options[program]

Example

l=RandomInteger[20,{8}]

l/.{a_,b_}/;a>b->{b,a}

l/.{___,a_,b_,___}/;a>b->{b,a}

l

l//.
{left___,a_,b_,right___}/;a>b->{left,b,a,right}

Out[143]

Out[146]

l={1,3,4,6,8}

l/.{left___,a_,b_,right___}/;b==2+a->{a,b}

A little bit of list analysis. Are there two successive entries (a, b) such that b is 2+a?

l/.{a_,b_}/;b==a+2->b

{1,3,4,6,8}/.{l1___,l2_,l3_,l4___}/;l3==l2+2->{l3,l4}

Only one result. How to get all the possible results?

ReplaceList[
{3,1,5,2,8,6},
{l1___,l2_,l3_,l4___}/;l2>l3->{l3,l2}
]

Very useful for debugging

ReplaceList[
{1,3,4,6,8},
{l1___,l2_,l3_,l4___}->{{l1},l2,l3,{l4}}
]//ColumnForm

Again, list analysis. Finding all the sequences whose left and right elements add up to 6

ReplaceList[
{3,6,-1,4,3,2,1,0,3,6},
{___,l1_,l2__,l3_,___}/;l1+l3==6->{l2}
]//ColumnForm

Example: Encoding

How to transform a list {a,a,a,a,b,b,a} to{{a,4},{b,2},{a,1}} ?

randL[n_]:=
Table[
If[Random[]>.5,a,b],
{n}]

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

randL[n_]:={a,b}[[RandomInteger[{1,2},n]]]

l=randL[25]

Another way of generating a random list of a's and b's (* ignore for now, just use it *)

randL[n_]:=
{a,b}[[#]]&/@(1+RandomInteger[1,{n}])

IDEA: {...{a,n},{a,m},...}->{...{a,n+m},...}

ll=Map[{#,1}&,l]

ll//.{left___,{x_,n_},{x_,m_},right___}->{left,{x,n+m},right}

replacement rules?
ll

Map[{#,1}&,l]//.{left__,{c_,v1_},{c_,v2_},r__}->{left,{c,v1+v2},r}

Map[{#,1}&,l]//.{left___,{c_,v1_},{c_,v2_},r___}->{left,{c,v1+v2},r}

Lists

{a,b,c,d,e,f}

l = {a,b,c,4,Pi,,{},-3.5}

ColumnForm[l]

Column[l]

FullForm[l]

l/.List->Plus

TreeForm[l]

(* make sure that you have evaluated the previous list definition *)

l                  (* this is the whole list *)

l[[All]]   (* same story *)

l[[1]]       (* first element *)

First[l]  (* first element again *)

l[[0]]      (* the head of the expression, in this case, List *)

Rest[l]    (* all the elements of the list except the first one *)

l[[-1]]    (* last element *)

Last[l]    (* last element *)

l[[Length[l]]]  (* again, last element *)

Table[0,{9}]

Table[Sqrt[i],{i,10}]

Table[i^i,{i,5,10}]

Table[i,{i,15,-10,-1}]

Table[i^2,{i,{"t","h","e","s","e",,"a","r","e",,Sqrt[12],"entries"}}]

Table[i,{i,Pi/3.,N[Sqrt[Pi]],Pi/E^3}]

Table[Sin[i^2],{i,-5,14,.2}]//Length

l0=Table[RandomInteger[10],{6}]

?RandomInteger

RandomInteger[{0,10},6]

Table[Exp[-i Pi j],{j,l0}]

Table[f[Pi],{f,{Sin,Cos,Exp,g,#^2&}}]

Range[5]

Range[Pi,3/2Pi,.1]

Array[g,6]

?Range

?Array

Table[i+j,{i,3},{j,Pi,2Pi,Pi/4}]//MatrixForm

Table[i+j,{i,3},{j,Pi,2Pi,Pi/4}]//TableForm

Table[i+j,{i,Pi,2Pi,Pi/4},{j,3}]//MatrixForm

Table[{i,j,i+j},{i,Pi,2Pi,Pi/4},{j,3}]//MatrixForm

M={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

M//MatrixForm

M=Table[(Subscript[A, i,j]),{i,1,5},{j,1,6}]
M2=Table[10i+j,{i,1,5},{j,1,6}]

MatrixForm[M]

M2//MatrixForm

M[[1]]

M[[-2]]

M[[1]][[3]]

M[[1,3]]

Part[M,1,3]

M[[{1,3}]]//MatrixForm

M[[1;;3]]

M[[3,All]]

M[[All,3]]

M[[2;;-2]]

M[[2;;3,2;;3]]

Transpose[M]//MatrixForm

Length[M]

Length[M//Transpose]

Dimensions[M]

Quick detour into visualization.

ListPlot[
Table[Sin[i^2],{i,-5,4,.02}]
]

Table[{i,Sin[i^2]},{i,-5,4,1}]

ListPlot[%]

ListPlot[
Table[{i,Sin[i^2]},{i,-5,4,.02}]
]

Plot[Sin[x^2],{x,-5,4}]

l = {a,b,c,4,Pi,,{},-3.5}

Take[l,-3]

Take[l,{-3}]

Drop[l,-3]

Reverse[l]

Clear[m]

m = {Pi,2,{1,Pi},c,d}

Join[l,m]

Join[m,l]

Union[l,m]

Length[l]+Length[m]==Length[Union[l,m]]

Intersection[l,m]

Append[l,m]

Prepend[l,m]

m//ColumnForm

ReplacePart[m,{x,y,z},2]//ColumnForm

m

m//ColumnForm

(m = ReplacePart[m,{x,y,z},{3,1}])//ColumnForm

m//ColumnForm

(m1=Insert[m,{1,2,3,4},-1])//ColumnForm

(m2=Insert[m1,{-1,-2,-3,-4},1])//ColumnForm

Delete[m2,{4,2}]//ColumnForm

m2

TreeForm[m2]

Flatten[m2]

Flatten[m2]//TreeForm

Flatten[m2,1]

Flatten[m2,1]//TreeForm

m2

FlattenAt[m2,{4,1}]//TreeForm

FlattenAt[m2,{4,1}]

Count,Tally

l=RandomInteger[6,10]

Tally[%]

Count[l,x_/;x>2]

p=3

{3,2,1,3,4,5,6}/.p_/;p>3->Sin[p]

Block[{p},
{3,2,1,3,4,5,6}/.p_/;p>3->Sin[p]
]

{3,2,1,3,4,5,6}/.p_/;p>3:>Sin[p]