(* - Lists*
1. How to generate:  Table command; nested lists 
2. Length and Dimensions
3. REARRANGING LISTS: Sort, Rotate, etc.
4. Restructuring Lists: Flatten, Drop, etc.
5. Combining Lists: Join, Union, Intersection
6. Operating on lists: the Map command
7. Lists as vectors and matrices

*)

(* 1. How to generate: 

Table[expr, {imax}] generates a list of imax copies of expr. Table[expr, {i, imax}] generates a list of the values of expr when i runs from 1 to imax. Table[expr, {i, imin, imax}] starts with i = imin. Table[expr, {i, imin, imax, di}] uses steps di. Table[expr, {i, imin, imax}, {j, jmin, jmax}, ... ] gives a nested list. 
	*)

Table[RandomInteger[],{10}]

Table[i, {i,10}]

Table[i, {i,10,21,3}]

Length[%]

ColumnForm[%%]

Table[Sin[n x], {n, 1, 7, 2}]

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

Dimensions[%]

TableForm[%%](*for a human*)

MatrixForm[%%%]

mf:=MatrixForm

vv={1,-1};mm={{a,b},{c,d}};mm//mf

mm.vv

vv.mm

(*both above are vectors*)

vv.mm.vv (*scalar*)

vv.vv  (*scalar*)

Transpose[mm]//mf

Inverse[mm]//mf

Det[mm]

Clear[vv,mm];

(* REARRANGING LISTS *)
Sort[{abd, abc, ab, a, d, 10, a, 10, 5.5,
     	       1/4, 23/2, ada}]

mylist =Table[i, {i,10}]

Permutations[{a, b, c}]

Length[%]==3!//Simplify

(* Restructuring Lists *)

Clear[mylist,c]

(*Drop[list, n]gives list with its first n elements dropped 
Drop[list, {n}]gives list with its n(\[Null]^("th"))element dropped.
Take[list, n]gives the first n elements of list
Take[list, {m, n}]gives elements m through n of list
HW: create a simple list; try all this*)

(*Element extraction*)

list={a,b,c,d};
list[[2]]

Clear[list]; list={{a,b},{c,d}}

list[[2]]

list[[1,2]]

MatrixForm[list]

?Select

list2=Table[i, {i,7}]

Select[list2,EvenQ]

(* Combining Lists *)
list1=Table[i, {i,3}]

list2=Table[i, {i,7}]

Join[list1,list2]

Union[list1,list2]

Intersection[list1,list2]

(*Operating on Lists*)

Clear[list]

list=Table[x^n, {n, 4}]

D[list, x]

Sin[list]

Sqrt[list]

%//PowerExpand (*x>0*)

(* Not all fuctions are listable! - Check *)

Clear[f];

Map[f, list]

f/@ list (*same thing*)

Clear[list];list={{1,1},{2,2},{3,3}};

Graphics[{PointSize->.1,
Point /@ list}]

(*Working with lists*)

list1

Plus @@ list1

(* HW: write a function av[y_] which would find an average of a list y of arbitrary length*)

Mean[list1]

%*Length[list1]

(*Pure function -reminder*)

(*Note: in f[x_]:=Sin[x] the variable x does not matter and can be replaced by anything. In fact, x_ indicates a slot in which the variable will be place when evaluating Sin. The idea is then to skip x alltogether - this leads to a "Pure function", analog of an operator*)

(* more useful for differential operators*)

der2:=D[#, x,x] &

der2[x^2]

Clear[a];der2[E^(a x)]

(*other examples: anything can go into the slot, not only numbers. Below are Lists and Graphics*)

myav:=(Plus @@ #)/Length[#] & (*average of a list*)

list={1,2,3,4};

myav[list]

Mean[list]

disp:=Export["t.ps", #, "EPS"]&

(*this creates a nice postscript outside Mathematica. E.g., if you like graphics in fig, you write disp[fig] and will get a postscript file t.ps with the picture*)

step:=#+RandomInteger[]&

Nest[step,0,10]

NestList[step,0,10]

(*can use in the coin experiment*)

list=Table[i^2,{i,7}]

Clear[lp];lp=ListPlot[list,PlotStyle->PointSize[.03]]

Show[Plot[x^2,{x,0,7}],lp]

Show[Plot[x^2,{x,0,7}],lp,PlotLabel->"x^2",AxesLabel->{"x", "y"},LabelStyle->Medium,GridLines->Automatic]

(*GRAPHICS with lists*)

Clear[a,b];a={2,1};b={1,2};o={0,0};

Graphics[{Arrow[{o,a}],Arrow[{o,b}]}]

Show[%,GridLines->Automatic]

(*equivalently*)

Graphics[Arrow/@ {{o,a},{o,b},{a,b}}]

(*similarly with other graphics primitives Point, Disk, etc.*)

(*Rotationall matrix*)

rot[p_]:={{Cos[p],Sin[p]},{-Sin[p],Cos[p]}}

rot[p]//MatrixForm

Det[rot[p]]//Simplify

rot[Pi/2].a

A=rot[Pi/4];Graphics[{Arrow[{o,A.a}],Arrow[{o,A.b}]}]

(*random walks and long lists*)

step:=#+RandomInteger[]&

Nest[step,0,4]

NestList[step,0,4]

Clear[list];list=NestList[step,0,500];ListPlot[list]

Length[list]

list0=Table[n/2,{n,0,500}];

newlist=list-list0;

ListPlot[newlist]

(*does not get small - grows as Sqrt[n] *)

ListPlot[newlist,Joined->True]