data=Table[i^2,{i,0,10}]
data1=Table[i^2,{i,1,2,0.1}]

novadata=Table[data[[i]]*data[[i+1]],{i,4}]

??Power

??OneIdentity

novadata^2

%
%4

%6

hilb[n_]:=Table[1/(i+j-1),{i,n},{j,n}]
h3=hilb[3]//N
h3^2
Det[h3]

m35=MatrixPower[h3,3.5]
m1=MatrixPower[m35,1/3.5]-h3

c=Table[Random[],{5}]

c=Table[Random[],{3},{3}]
g=Transpose[c].c

Eigenvalues[g]

(* Odmocninova matice *)
g12=MatrixPower[g,.5]
g-g12.g12//Chop

(* Nasobeni matic a vektoru *)
a={{a11,a12},{a21,a22}};
x={x1,x2};
x.x
x.a
a.x
x.a.x//Expand
Outer[Times,x,x]

??Outer

afl=Flatten[a]

r9=Range[9]
mat9=Partition[r9,3]
mat10=Partition[r9,4]

aa={a,a}
Flatten[aa]
Flatten[aa,1]
strom={aaa,
	{b,ccc,
		{d,e,
			{f,ggg}
		}
	}
	  }
strom //TreeForm
Flatten[strom]
Flatten[strom,1]
Flatten[strom,2]

FactorInteger[171171171171171]

Symbolicke pocitani

e1=y^3+3y
e2=e1+3y^2
e3=Factor[e2+1]
e4=Expand[e3]

Clear[a,b,c,x]
rovnice=e4==0
Solve[rovnice,y]
Solve[a*x^2+b*x+c==0,x]
Solve[a*x^2+b*x+c==0,a]
Solve[x^2+x+1==0,x]
Solve[x^2+x+1.==0,x]

eq44=Table[
	Random[Real,{-2,2}],{7}].(x^Range[7])==0

reseni44=NSolve[eq44,x]

mojereseni=x /. reseni44
mojereseni[[1]]

??/.

Coefficient[1+x+3x^2+y^2*x^2,x^2]

Analyza

ex=D[x^n,x]
en=D[x^n,n]

iex=Integrate[ex,x]
ien=Integrate[en,n]
ienx=Integrate[en,x]

derenx=D[ienx,x]

simpl=Simplify[derenx]

D[x^3*y^2+x^2,x,y]

D[x^3*y^2+x^2,{x,3},y]

Integrate[Sin[x]^2,x]
Integrate[Sin[x^2],x]

Integrate[Exp[-x^2],x]

ix=Integrate[Sin[1/x],x]

Limit[x*Sin[1/x],x->0]

Series[CosIntegral[1/x],{x,0,1}]

(* Diferencialni rovnice *)
ds=DSolve[y''[x]+y[x]==0,y[x],x]
dp=DSolve[{y''[x]+y[x]==0,y[0]==1,
	y[1]==1},y[x],x]

y[x] /. ds

iss=Integrate[Sin[Sin[x]],{x,0,Pi}]

iss=NIntegrate[Sin[Sin[x]],{x,0,Pi}]

N[iss,20]

iss=NIntegrate[Sin[Sin[x]],{x,0,1,2,Pi}]

(* Funkce *)
h[x_,y_]:=x/y^2

h[3,z]

NestList[f,x,3]
NestList[Sin,x,3]

addrandom1[x_]:=x+Random[]-.5
addrandom2[x_]=x+Random[]-.5

addrandom2[y]

addrandom1[x]

proch1=NestList[addrandom1,0,100];
proch2=NestList[addrandom2,0,100];
ListPlot[proch1,PlotJoined->True]
ListPlot[proch2,PlotJoined->True]

(* Ryzi funkce, Pure Functions *)
Clear[g]
g[x_]:=x^2
g[3]

f=Function[x,x^2]
f[3]

f=#^2&
f[3]

(#^2&)[3]

(#1+#2^2&)[a,b]

proch3=NestList[(#+Random[]-.5)&,0,100];

ListPlot[proch3,PlotJoined->True]

Map[f,{1,2,3,4}]

?f

Map[fun,{1,2,3,4}]

??Select

Select[Range[5],#>3&]

Select[Range[10],(3<= # <8)&]

Select[Range[10],(#>=3 && #<8)&]

1+1

(* Packages *)
mean[list_]:=Apply[Plus,list]/Length[list]
l1=Range[11]
mean[l1]

mean[{l1,{a,b}}]
mean[{}]
mean[5]

mean1[list_]:=Apply[Plus,list]/Length[list] /;
	VectorQ[list] && Length[list]>0
mean1[Range[11]]
mean1[{l1,{a,b}}]
mean1[5]
mean1[{Range[11]}]
mean1[{Range[11]}//Flatten]

var[list_]:=mean1[(list-mean1[list])^2] /;
	VectorQ[list] && Length[list]>0
mean1[Range[11]]
var[Range[5]]

Needs["Statistics`Master`"]

??Mean

Needs["Statistics`DescriptiveStatistics`"]

??Mean

Needs["DiscreteMath`Master`"]

Table[RandomPermutation[3],{10}]

KSubsets[{1,2,3,4,5},3]

Compositions[6,3]

(* Diferencni rovnice *)
Needs["DiscreteMath`RSolve`"]
?RSolve

res1=RSolve[{f[n]==f[n-1]+f[n-2],f[1]==1,f[2]==1},
	f[n],n]

res2=f[n]/.res1

Table[res2 /. {n->j},{j,6}]// Simplify//Flatten

res2/.{n->100}//Simplify

f1[n_]:=f1[n-1]+f1[n-2]
f1[1]=1
f1[2]=1
?f1
t1=Timing[Table[f1[n],{n,15}]]

f2[n_]:=f2[n]=f2[n-1]+f2[n-2]
f2[1]=1; f2[2]=1;
t2=Timing[Table[f2[n],{n,15}]]

?f2

Needs["Statistics`ContinuousDistributions`"]
?*Distribution

??NormalDistribution

a={s,d}
AppendTo[a,e]
a1=Append[a,g]
a

Needs["c:\wnmath22\packages\programm`skeleton`"]

MaxMemoryUsed[]
MemoryInUse[]

Clear[m,s]
distr1=NormalDistribution[m,s];

PDF[distr1,x]

distr2=NormalDistribution[0,1];
Plot[PDF[distr2,x],{x,-3,3}];

CDF[distr1,x]

{Mean[distr1],Variance[distr1],Kurtosis[distr1],
	Skewness[distr1]}
CharacteristicFunction[distr1,t]

Table[Quantile[distr2,p],{p,.1,.9,.1}]

tran=Table[Random[distr2],{1000}];
{Mean[tran],Variance[tran]}

MeanCI[tran]
MeanCI[tran,ConfidenceLevel->.99]
MeanCI[tran,ConfidenceLevel->.9]

MeanDifferenceCI[tran,tran+.1]

distr3=ChiSquareDistribution[4.6];
PDF[distr3,x]

Clear[a,b]
distr4=WeibullDistribution[a,b]
CDF[distr4,x]

??WeibullDistribution

{Mean[distr4],Variance[distr4]}

(* Histogram *)
Needs["Graphics`Graphics`"]

cetnosti=BinCounts[tran,{-3,3,.2}]
Apply[Plus,cetnosti]

BarChart[cetnosti]

Short[s1=Sort[tran]]
Last[s1]

transf=CDF[distr2,tran];

Short[transf]

{Mean[transf],Variance[transf]}

cetnf=BinCounts[transf,{0,1,.05}]
Apply[Plus,cetnf]
BarChart[cetnf]
1-CDF[ChiSquareDistribution[19],
	Apply[Plus,(cetnf-50)^2/50]]

(* Import a export souboru *)

stream8=OpenRead["c:\masata\CAIdestr.txt"];
data8=ReadList[stream8,
	{Number,Number,Number,Number,Number},
    RecordLists->True]
 data8=Flatten[data8,1] 
 Close[stream8];

Table[Number,{5}]

Dimensions[data8]

ToExpression["2 000"]

ToExpression[
	StringReplace[{"2,000","3,000"},{","->"."}]]^2

StringReplace[{"2,000","3,000"},{","->"."}]^2

ToString[{3,4}]^2

{3,4}^2

stream9=OpenRead["c:\masata\CAIdestr.txt"];
data9=ReadList[stream9,	Number]
  Close[stream9];

Partition[data9,5]

table1=Table[1/(i+j),{i,5},{j,5}];
Save["mojetab",table1]

Clear[table1]
table1

Get["mojetab"]

Clear["mojetab"]
tab2=ToString[{2.3,3;4,5}]

Save["moje3",tab2]

ReadList["moje3",Word,RecordLists->True,
	RecordSeparators->{".",","}]

tab3=Table[Random[],{3},{2}]
file3=OpenWrite["c:\\masata\\tab3.txt"];
Write[file3,tab3];
Close[file3];

stream6=OpenRead["c:\\masata\\tab3.txt"];
tab4=Read[stream6];
Close[stream6];

Remove[BinCounts,CDF,ChiSquareDistribution]
Needs["Statistics`Master`"]

testchi[list_,k_(* pocet trid *),
	m_(* pocet odhadovanych parametru *)]:=
	Module[{fr,n=Length[list],krit},
		fr=BinCounts[list,{0,1,1./k}];
		krit=Apply[Plus,(fr-n/k)^2/(n/k)];
		Print["Pocet pozorovani = ",n];
		Print["Pocet trid       = ",k];
		Print["Pocet st. volnosti = ",
							k-m-1];
		Print["Hodnota kriteria = ",krit//N];
	    1-CDF[ChiSquareDistribution[k-m-1],
									krit]
	      ]

data7=Table[Random[],{500}];

testchi[data7,20,0]

?CDF

{1,2,3}/{2,3,4}

cf=Table[Random[],{10}]-.5;
cfrum={-1000,1000,2000};

pvrum[cf_,i_]:=Table[1/(1+i)^k,{k,0,Length[cf]-1}].
	cf

pvrum[cfrum,.1]

pvsedl[cf_,i_]:=Module[{j,n},
		n=Length[cf];
		j=Range[0,n-1];
		Apply[Plus,cf/(1+i)^j]]

pvsedl[cfrum,.1]

pvjh[cf_,i_]:=cf.
	Table[1/(1+i)^(Range[Length[cf]]-1)]
pvjh[cfrum,.1]

Timing[pvjh[cf,.1]]
Timing[pvrum[cf,.1]]
Timing[pvsedl[cf,.1]]

cf=Table[Random[],{1000}]-.5;
cfrum={-1000,1000,2000};

pv[cf_,i_]:=Module[{j,n},
		n=Length[cf];
		j=Range[0,n-1];
		Apply[Plus,cf/(1+i)^j]]

pv[cf,.1]//Timing

durbar[cf_,i_]:=pv[cf*Range[0,Length[cf]-1],i]/
	pv[cf,i]
dursedl[cf_,i_]:=Module[{n=Length[cf]-1,t},
	t=Range[0,n];
	Apply[Plus,t*cf/(1+i)^t]/	
		Apply[Plus,cf/(1+i)^t]]

pv[cfrum,.1]

durbar[cfrum,.1]
dursedl[cfrum,.1]

durbar[cf,.1]//Timing
dursedl[cf,.1]//Timing

??Solve

??NSolve

??FindRoot

cf1={-1000,400,400,300}
Plot[pv[cf1,i],{i,0,.1}]

rovnice=Table[Random[],{10}].(x^Range[10])

NSolve[rovnice==0,x]

FindRoot[rovnice==0,{x,3}]

irrmar[cf_]:=Module[{j,n},
	n=Length[cf];
	j=Range[0,n-1];
	j=NSolve[Apply[Plus,cf/(1+x)^j]==0,x];
	Cases[x/.j,z_Real/;z>=0]]

reseni=x/.irrmar[cf1]

irrrou[cf_]:=NSolve[pv[cf,i]==0,i]
	irrrou[cf1]

Select[reseni,Re[#]>0 && Im[#]==0&]

??Cases

reseni

Cases[reseni,z_Real /;z>=0]

irrmar[cf_]:=Module[{j,n},
	n=Length[cf];
	j=Range[0,n-1];
	j=NSolve[Apply[Plus,cf/(1+x)^j]==0,x];
	Cases[x/.j,z_Real/;z>=0]]
irrmar[cf1]

cf2={-1000,3600,-4310,1716}
Plot[pv[cf2,i],{i,0,.5},PlotRange->All];
irrmar[cf2]

??ConstrainedMax

??LinearProgramming

ConstrainedMax[17x-20y+18z,{x-y+z<10,x<5,
	x+z>20},{x,y,z}]

c={-17,20,-18};
m={{-1,1,-1},
   {-1,0,0},
   {1,0,1}};
b={-10,-5,20};
x0=LinearProgramming[c,m,b]
Print["Hodnota ucelove funkce = ",c.x0]

??Random

(* Dalsi uzitecne veci *)
tab3=Table[Random[Integer,{1,4}],{8}]

Join[{3.4,2},tab3]

Union[tab3]

Intersection[tab3,{4,6}]

tab4=Union[tab3,{4,6}]

Complement[tab4,tab3] 
Complement[tab3,tab4]

tab5=Table[Random[],{5},{2}]

Sort[tab5]

Sort[tab5]//Reverse
Sort[tab5,#2[[1]]<#1[[1]]&]
Sort[tab5,#1[[2]]<#2[[2]]&]

Reverse[{1,4,6,2}]

vyber=Table[Random[],{7}]
dvojice=Transpose[{vyber,Range[Length[vyber]]}]//
							Sort

(* Rotate, Drop, Take, Append,Prepend *)

Or[3<2,1==2,2<0]
3<2 || 1==2||2<0

Select[tab5,#[[2]]<0.5&]

(* Regrese *)
data1=Table[{x,x^2(*+Random[]*)},{x,6}];
ListPlot[data1,PlotJoined->True];

Fit[data1,{1,x,x^2},x]//Chop

Needs["Statistics`Master`"]

(* Vyrovnat data funkci a*x*Exp[-c*x] *)
data2=Table[{x,3.1*x*Exp[-2.1x]+
0.05*(Random[]-0.5)},{x,0.02,2,0.02}];
ListPlot[data2,PlotJoined->True];

Map[f,{1,2,3}]
Map[#&,{1,2,3}]

trdata2=Map[{#[[1]],Log[#[[2]]]}&,data2];
graf1=ListPlot[trdata2,PlotJoined->True]

fun2=Fit[trdata2,{1,Log[x],x},x]
graf2=Plot[fun2,{x,0.02,2}]

Show[graf1,graf2]

regranal=Regress[trdata2,{1,Log[x],x},x]

Remove[a,x,c]

subst=NonlinearFit[data2,a*x*Exp[-c*x],x,{a,c}]

{a,c}={a,c}/.subst

Plot[a*x*Exp[-c*x],{x,0.02,2}]

cf=Table[Random[],{1000}]-.5;
cfrum={-1000,1000,2000};

pv[cf_,i_]:=Module[{j,n},
		n=Length[cf];
		j=Range[0,n-1];
		Apply[Plus,cf/(1+i)^j]]

pv[cf,.1]//Timing

durbar[cf_,i_]:=pv[cf*Range[0,Length[cf]-1],i]/
	pv[cf,i]
dursedl[cf_,i_]:=Module[{n=Length[cf]-1,t},
	t=Range[0,n];
	Apply[Plus,t*cf/(1+i)^t]/	
		Apply[Plus,cf/(1+i)^t]]

pv[cfrum,.1]

durbar[cfrum,.1]
dursedl[cfrum,.1]

durbar[cf,.1]//Timing
dursedl[cf,.1]//Timing

??Solve

??NSolve

??FindRoot

cf1={-1000,400,400,300}
Plot[pv[cf1,i],{i,0,.1}]

rovnice=Table[Random[],{10}].(x^Range[10])

NSolve[rovnice==0,x]

FindRoot[rovnice==0,{x,3}]

irrmar[cf_]:=Module[{j,n},
	n=Length[cf];
	j=Range[0,n-1];
	j=NSolve[Apply[Plus,cf/(1+x)^j]==0,x];
	Cases[x/.j,z_Real/;z>=0]]

reseni=x/.irrmar[cf1]

irrrou[cf_]:=NSolve[pv[cf,i]==0,i]
	irrrou[cf1]

Select[reseni,Re[#]>0 && Im[#]==0&]

??Cases

reseni

Cases[reseni,z_Real /;z>=0]

irrmar[cf_]:=Module[{j,n},
	n=Length[cf];
	j=Range[0,n-1];
	j=NSolve[Apply[Plus,cf/(1+x)^j]==0,x];
	Cases[x/.j,z_Real/;z>=0]]
irrmar[cf1]

cf2={-1000,3600,-4310,1716}
Plot[pv[cf2,i],{i,0,.5},PlotRange->All];
irrmar[cf2]

??ConstrainedMax

??LinearProgramming

ConstrainedMax[17x-20y+18z,{x-y+z<10,x<5,
	x+z>20},{x,y,z}]

c={-17,20,-18};
m={{-1,1,-1},
   {-1,0,0},
   {1,0,1}};
b={-10,-5,20};
x0=LinearProgramming[c,m,b]
Print["Hodnota ucelove funkce = ",c.x0]

??Random

(* Dalsi uzitecne veci *)
tab3=Table[Random[Integer,{1,4}],{8}]

Join[{3.4,2},tab3]

Union[tab3]

Intersection[tab3,{4,6}]

tab4=Union[tab3,{4,6}]

Complement[tab4,tab3] 
Complement[tab3,tab4]

tab5=Table[Random[],{5},{2}]

Sort[tab5]

Sort[tab5]//Reverse
Sort[tab5,#2[[1]]<#1[[1]]&]
Sort[tab5,#1[[2]]<#2[[2]]&]

Reverse[{1,4,6,2}]

vyber=Table[Random[],{7}]
dvojice=Transpose[{vyber,Range[Length[vyber]]}]//
							Sort

(* Rotate, Drop, Take, Append,Prepend *)

Or[3<2,1==2,2<0]
3<2 || 1==2||2<0

Select[tab5,#[[2]]<0.5&]

(* Regrese *)
data1=Table[{x,x^2(*+Random[]*)},{x,6}];
ListPlot[data1,PlotJoined->True];

Fit[data1,{1,x,x^2},x]//Chop

Needs["Statistics`Master`"]

(* Vyrovnat data funkci a*x*Exp[-c*x] *)
data2=Table[{x,3.1*x*Exp[-2.1x]+
0.05*(Random[]-0.5)},{x,0.02,2,0.02}];
ListPlot[data2,PlotJoined->True];

Map[f,{1,2,3}]
Map[#&,{1,2,3}]

trdata2=Map[{#[[1]],Log[#[[2]]]}&,data2];
graf1=ListPlot[trdata2,PlotJoined->True]

fun2=Fit[trdata2,{1,Log[x],x},x]
graf2=Plot[fun2,{x,0.02,2}]

Show[graf1,graf2]

regranal=Regress[trdata2,{1,Log[x],x},x]

Remove[a,x,c]

subst=NonlinearFit[data2,a*x*Exp[-c*x],x,{a,c}]

{a,c}={a,c}/.subst

Plot[a*x*Exp[-c*x],{x,0.02,2}]

t1=Table[{1,2,3},{4}]

t2=FoldList[Plus,{1,2,3},t1]

{1,2,3}>={1,1,1}

3>=4

m1=Map[#>=0&,{1,2,3}-{2,2,2}]

m1.m1

bool[x_]:=If[True==x,1,If[x==False,0,ho]]

?bool

Map[bool,m1]

bool[False]

Needs["LinearAlgebra`MatrixManipulation`"]
?UpperDiagonalMatrix

Clear[matrum]
matrum=UpperDiagonalMatrix[v^#&,5]
matrum2=UpperDiagonalMatrix[1^#&,5]

Table[1,{i,5},{j,i,5}]

rumfun[i_,j_]:=If[i<=j,1,0]
rummat3=Table[rumfun[i,j],{i,5},{j,5}]

minjar[cc_,b0_,b_,r_]:=Module[
	{mm,c,t,bb},
	{t,c}=Dimensions[cc];
	mm=FoldList[Plus,cc[[1]],Drop[cc,1]];
	bb=b-b0;
	Map[Apply[Plus,#]&,Transpose[cc]].
	(1/(1+r)^Range[t]);

?Drop

cc={{-10,1,11},{-10,1.2,10.5},{10,-11.3,0}}//
	Transpose
Map[Apply[Plus,#]&,Transpose[cc]].
	(1/(1+r)^Range[3])
r=.11;
g={100,10,10,10};