Calcolatore elettronico

Calcoli di base

4+5

4+5;

23784 78921

66/12

Sqrt[25]

Sqrt[26]

N[66/12]

66/12  //N

N[Sqrt[26]]

N[Sqrt[26],30]

Funzioni elementari e costanti

Log[-6]

Log[-6]//N

N[E,40]

N[Pi,50]

Sin[30]

Sin[30.]

Degree//N

Sin[N[30 Degree]]

Infinity

Infinity> 50

Infinity == Infinity

4 + Infinity

Indeterminate==Infinity-Infinity

Valutazioni numeriche

Abs[-4]

Abs[3 + 4 I]

Sign[-4]

Sign[0]

Sign[1]

Round[3.2]

Round[3.6]

Floor[3.6]

Floor[-3.6]

Ceiling[2.1]

Ceiling[-2.1]

Chop[10.^{-10}]

Chop[10.^{-11}]

Chop[0.0003,0.001]

Random[]

Random[Integer,{3,10}]

Random[Integer,{3,10}]

SeedRandom[4]

Random[Integer,{3,10}]

SeedRandom[4]

Random[Integer,{3,10}]

Max[3,6,-1]

Max[{3,6,-1}]

Teoria dei numeri  e combinatorica

Mod[113,7]

Quotient[113,7]

IntegerDigits[113,10]

IntegerDigits[113,2]

IntegerDigits[113,7]

Factorial[10]

Binomial[90,2]/Binomial[5,2]

FactorInteger[2346767787]

Prime[201]

PrimeQ[3453453898545745453]

GCD[84,12,100]

LCM[84,12,100]

n=100000000

PrimePi[n] //N

n/Log[N[n]]//N

Calcolo simbolico

a+b

a=2

a+b

Clear[a]

a+b

a+b+a

(a+b)(a-b)

Expand[%]

Expand[(a+b)^5]

(a^2-b^2)/(a+b)

Simplify[(a^2-b^2)/(a+b)]

Simplify[Sqrt[a^2-2 a b +b^2]]

repl=Sqrt[x_^2] -> Abs[x]

Sqrt[Factor[a^2-2 a b +b^2]]

Sqrt[Factor[a^2-2 a b +b^2]]/.repl

Mod[a,a]

p=x^2+ 2 x +5-x^4-2 x^3

m=x^2  -3

PolynomialQ[m,x]

s=PolynomialRemainder[p,m,x]

q=PolynomialQuotient[p,m,x]

q m + r

Expand[%]

Factor[p]

Factor[p,Modulus->5]

Liste, matrici e insiemi

Costruzione di liste

a={1,3,2,2,6}

b=Table[i^2,{i,6}]

c=Range[6]

c^2

c+3

1/c

1/c^2

Array[f,{5}]

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

MatrixForm[%]

z={1 ,{2,a},{4,{5,{2}},0}}

Flatten[z]

IdentityMatrix[6]

DiagonalMatrix[c^3]

Estrazione da liste

a

Take[a,3]

Drop[a,2]

Reverse[a]

Take[a,-2]

a[[3]]

a[[{2,5}]]

Select[a,EvenQ]

Select[a,Mod[#,3]==0 &]

Select[a, # > 2 &]

Valutazioni di liste

Length[a]

Length[z ]

z[[1]]

Length[z[[1]]]

z[[2]]

Length[z[[2]]]

Depth[z]

MatrixQ[z]

Position[a,2]

z

Position[z,2]

Operazioni su liste

Append[a,8]

a

AppendTo[a,8]

a

Prepend[a,0]

a

a=Insert[a,Pi,3]

a= DeleteCases[a,Pi]

a=Delete[a,-1]

Join[a,b]

Union[a,b]

Intersection[a,b]

Complement[Range[7],a]

Sort[Join[a,b]]

Operazioni sulla struttura

Outer[Plus,a,b]

Outer[Times,a,b]

Transpose[{{1,2},{3,4}}]

Permutations[{1,d,7}]

Operazioni matriciali

{3,4,2}.{1,2,1}

A={{2,3,4},{1,1,2},{4,5,2}}

MatrixForm[A]

Det[A]

MatrixForm[Inverse[A]]

LinearSolve[A,{3,0,1}]

Clear[b];Clear[c];

B={{b,0,1,0},{0,c,0,1},{1,0,c,0},{0,b,0,1}}

MatrixForm[B]

Det[B]

Factor[%]

Simplify[LinearSolve[B,{1,b,1,c}]]

b=5;c=1/5;

NullSpace[B]

b=c=1

NullSpace[B]

Eigenvalues[A]

Eigenvalues[N[A]]

X=Eigenvectors[N[A]]

XT=Transpose[X];

Inverse[XT].A.XT

Clear[b];Clear[c];

Expand[MatrixPower[B,6]]

MatrixPower[A,10]

MatrixPower[N[A],0.5]

Eigenvalues[%]

%^2

MatrixPower[N[A],0.2]

MatrixPower[%,5]

A

MatrixExp[N[A]]

CharacteristicPolynomial[A,x]

Factor[N[%]]

CharacteristicPolynomial[A.A,x]

Factor[N[%]]

sv=SingularValues[N[A]]

sv[[2,1]] Outer[Times,sv[[1,1]],sv[[3,1]]]+
sv[[2,2]] Outer[Times,sv[[1,2]],sv[[3,2]]]+
sv[[2,3]] Outer[Times,sv[[1,3]],sv[[3,3]]]

AA=Table[Random[Integer,{0,5}],{10},{10}]

Timing[Inverse[AA]]

Timing[Inverse[N[AA]]]

Funzioni e grafici

f[x_]:=x^3-3 x+2

f[z]

f[5]

f2[x_,y_]:=If[x>y, x^2-y^2, y^2-x^2]

f2[4,5]

fact[i_]:=fact[i-1] i;

fact[0]=1;

fact[7]

fib[i_]:=fib[i-1]+fib[i-2];

fib[0]=1;
fib[1]=1;

fib[40]

fib[i_]:=fib[i]=fib[i-1]+fib[i-2];

fib[40]

Plot[f[x],{x,-4,3}]

ContourPlot[f2[x,y],{x,-1,1},{y,-1,1}]

Plot3D[f2[x,y],{x,-1,1},{y,-1,1}]

ParametricPlot[{10 Sin[t],Cos[t]},{t,0, 2 Pi}]

ParametricPlot[{10 Sin[t],Cos[t]},{t,0, 2 Pi},AspectRatio->Automatic]

ParametricPlot3D[{Cos[t](3+Cos[u]),Sin[t](3+Cos[u]), Sin[u]},
              {t,0, 2 Pi},{u,0,2 Pi}]

p=Table[Random[Integer,{-100,100}],{100}]

ListPlot[p]

Sort[p]

ListPlot[Sort[p]]

ListPlot[Sort[p],PlotJoined->True]

lambda=1;

poisson=Table[Log[1-Random[]]/(-lambda),{100}]

sp=Sort[poisson]

values=Range[Length[sp]]/Length[sp]//N

distribution=Transpose[{sp,values}]

g1=ListPlot[distribution,PlotJoined->True]

g2=Plot[1-Exp[-lambda t],{t,0,4}]

Show[g1,g2]

Grafica

g1=Graphics[Point[{2,3}]]

Show[g1]

Show[g1,Axes->True]

g2=Graphics[Line[{{1,3},{2,4},{0,1}}]]

Show[g2,Axes->True]

Show[g2,Axes->True,AspectRatio->Automatic]

g3=Graphics[Table[Point[{Random[],Random[]}],{20}]]

Show[g3,Axes->True,AspectRatio->Automatic]

s=0;

randomwalk=Table[s+={Random[],Random[]};s,{10}]

Show[Graphics[{Map[Point, randomwalk],Line[randomwalk]}]
             ,Axes->True]

g5=Graphics[{PointSize[0.02],RGBColor[1,0,0],Map[Point, randomwalk]}]

g6=Graphics[{Thickness[0.005],RGBColor[0,0,1],Line[randomwalk]}];

Show[g6,g5,Axes->True,AspectRatio->Automatic]

Programmazione

Programmazione dichiarativa

counter=0;
sum=0;
While[sum < 50
      ,
      counter++;
      sum+=Random[];
      ];
counter

For[counter=0;
    sum=0;
    ,
    sum < 50
    ,
    counter++;
    ,
    sum+=Random[];
     ];
counter

Do[Print[{i,j}],{i,3},{j,i,4}]

ranvar[a_]:=Block[
{counter=0,sum=0}
,
While[sum < a
      ,
      counter++;
      sum+=Random[];
      ];
counter
]

ranvar[1]

nextcollatz[n_]:=If[EvenQ[n]
                   ,n/2
                   ,3 n + 1]

collatzsequence[n_]:=Block[{seq={},m}
,
m=n;
AppendTo[seq,m];
While[m!=1,
      m=nextcollatz[m];
      AppendTo[seq,m];
      ];
seq
]

collatzsequence[1530]

f[x_]:=Which[x<=1, x,
             x<=2, 2 x-1,
             x<=3, 5 - x,
             True, 2
             ]

Plot[f[x],{x,0,6}]

Programmazione funzionale

f=Function[# + 1]

Map[f,{2,4,5}]

f= #1 > 4 &

Map[f,{2,4,5}]

points=Table[{Random[Integer,{0,100}],
              Random[Integer,{0,100}]},{5}]

labels={Range[5],points+2}

MapThread[Text,labels]

Show[Graphics[{Map[Point,points],MapThread[Text,labels]}
             ]
    ]

Apply[Plus,{2,4,5,4}]

Apply[Times,{2,4,5,4}]

x

f=1+ x # &

Nest[f,1,4]

coeff={4,5,2,3}

ff=#2 + x #1 &

Fold[ff,1,coeff ]

g=#2 +1/(#1) &

Clear[a];Clear[b];Clear[c];Clear[d];

cc={a,b,c,d};

Fold[g,1,cc ]

Select[coeff,EvenQ]

squaretest=IntegerQ[Sqrt[#]] &

numbers=Table[Random[Integer,{1,100}],{10}]

Select[ numbers,squaretest]

Array[h,{5}]

Map[h,Range[5]]

Stringhe

str1="abcd"

str2="efgh"

StringJoin[str1,str2]

StringReverse[%]

StringDrop[%,2]

StringTake[%,-3]

StringInsert[%,"**",2]

Characters[%]

StringJoin[%]

ToCharacterCode[%]

FromCharacterCode[%]

a=Random[Integer,{1,20}];
b=Random[Integer,{1,20}];
s=Input[StringJoin[ToString[a]," volte ", ToString[b]]];
If[s==a b,Print["OK"],Print["Errore !!"]]

Scoping

Block[{x=x0},x+y]

Module[{x=x0},x+y]

y=x

Block[{x=x0},x+y]

Module[{x=x0},x+y]

Analisi matematica

Limit[(x^2-x)/x,x->0]

Limit[(x^2-(a+b) x+a b)/(x^2-(a+c) x+a c),x->a]

D[x^2,x]

D[Log[Sin[x]]/Sqrt[x],x]

D[Log[Sin[x]]/Sqrt[x],{x,4}]

Dt[x^2+y^2,x]

v[x_,y_]:=Sqrt[2(1-y)]

y[x_]:=1-E^x

Dt[v[x,y[x]],x]

Clear[f];Clear[function];

Clear[x];

n=3;

function[x_]:= Apply[Plus,Map[Log,Array[x,{n}]]]

g=Array[D[function[x],x[#]] &,n]

H=Array[D[function[x],x[#1],x[#2]] &,{n,n}]

setxvalue[xx_]:=Do[x[i]=xx[[i]],{i,3}]

x[2]

g

H

f[x_,y_]:=x^2-y^3+x y

D[f[x,y],y]

gradient={D[f[x,y],x],D[f[x,y],y]}

D[f[x,y],{y,2}]

D[f[x,y],y,x]

hessian={{D[f[x,y],{x,2}],D[f[x,y],x,y]},
         {D[f[x,y],y,x],D[f[x,y],{y,2}]}}

Integrate[x^2,x]

Integrate[Log[1+x^2],x]

Integrate[Log[1+x^2],{x,0,1}]

g[x_]:=Integrate[Log[1+t^2],{t,0,x}]

Timing[g[1]]

Timing[g[2]]

gg[x_]=Integrate[Log[1+t^2],{t,0,x}]

Timing[gg[1]]

Timing[gg[2]]

Timing[gg[2.]]

Timing[g[2.]]

DSolve[y[x]+y''[x]==0,y[x],x]

DSolve[4 y[x]-(y'[x])^2==0,y[x],x]

DSolve[(y'[x])^2==0,y[x],x]

Series[Log[x],{x,1,7}]

Varie

signal[t_]:=Sin[t] +0.8 Sin[2 t +0.1] +0.3 Sin[3 t +2]

Plot[signal[t],{t,0,2 Pi}]

sample=Table[signal[N[j 2 Pi/10]],{j,100}];

transform=Abs[Fourier[sample]];

ListPlot[Take[transform,50],PlotJoined->True,PlotRange->All]

noise=Table[3 Random[]-0.5,{100}];

ListPlot[sample,PlotJoined->True]

ListPlot[sample+noise,PlotJoined->True]

transform=Abs[Fourier[sample+noise]];

ListPlot[Take[transform,50],PlotJoined->True,PlotRange->All]

values={1,3,3.4,2,0}

ListPlot[values]

h[x_]=InterpolatingPolynomial[values,x]

h1=Plot[h[x],{x,-3,8}]

values={1,3,3.2,2,0}

h[x_]=InterpolatingPolynomial[values,x]

h2=Plot[h[x],{x,-3,8}]

Show[h1,h2]