(* NORMALE *)

Clear[a,b,x,n];
a={{1.,2.,3.},{2.,1.,1.},{3.,-4.,5.}};
b={1.,-2.,-1.};
x={x1,x2,x3};
n=Length[b];
MatrixForm[a]
MatrixForm[b]
sys=Dot[a,x]==b;
sol=Solve[sys,x][[1]];
MatrixForm[x/.sol]

Clear[ab,x];
ab=Table[Join[a[[i]],{b[[i]]}],{i,1,n}];
MatrixForm[ab]
Do[piv=ab[[i]][[i]];Print["i = ", i, "  piv = ", piv];Do[m[j]=ab[[j]][[i]]/piv;ab[[j]]=ab[[j]]-m[j]*ab[[i]],{j,i+1,n}],{i,1,n-1}];
MatrixForm[ab]

xx[n]=ab[[n,n+1]]/ab[[n,n]];
Do[xx[i]=(ab[[i,n+1]]-Sum[xx[j]*ab[[i,j]],{j,i+1,n}])/ab[[i,i]],{i,n-1,1,-1}];
x=Table[xx[i],{i,1,n}];
MatrixForm[x]

(* CON SCAMBIO DI RIGHE *)

Clear[a,b,x,n];
a={{1.,2.,3.},{2.,4.,1.},{3.,-4.,5.}};
b={1.,-2.,-1.};
x={x1,x2,x3};
n=Length[b];
MatrixForm[a]
MatrixForm[b]
sys=Dot[a,x]==b;
sol=Solve[sys,x][[1]];
MatrixForm[x/.sol]

Clear[ab,x];ϵ=1.0 10^(-8);
ab=Table[Join[a[[i]],{b[[i]]}],{i,1,n}];
MatrixForm[ab]
Do[piv=ab[[i,i]];
If[Abs[piv]≤ϵ,
{mm=0.0;Do[If[Abs[ab[[j,i]]]≥mm,mm=Abs[ab[[j,i]]];jmax=j],{j,i+1,n}];a1=ab[[i]];ab[[i]]=ab[[jmax]];ab[[jmax]]=a1;piv=ab[[i,i]]},Continue];
Do[m[j]=ab[[j]][[i]]/piv;ab[[j]]=ab[[j]]-m[j]*ab[[i]],{j,i+1,n}],{i,1,n-1}];
MatrixForm[ab]
xx[n]=ab[[n,n+1]]/ab[[n,n]];
Do[xx[i]=(ab[[i,n+1]]-Sum[xx[j]*ab[[i,j]],{j,i+1,n}])/ab[[i,i]],{i,n-1,1,-1}];
x=Table[xx[i],{i,1,n}];
MatrixForm[x]

(* CON PIVOTING PARZIALE *)

Clear[a,b,x,n];
a={{1.,2.,3.},{2.,4.,1.},{3.,-4.,5.}};
b={1.,-2.,-1.};
x={x1,x2,x3};
n=Length[b];
MatrixForm[a]
MatrixForm[b]
sys=Dot[a,x]==b;
sol=Solve[sys,x][[1]];
MatrixForm[x/.sol]

Clear[ab,x];ϵ=1.0 10^(-8);
ab=Table[Join[a[[i]],{b[[i]]}],{i,1,n}];
MatrixForm[ab]
Do[piv=ab[[i,i]];
mm=Abs[piv];Do[If[Abs[ab[[j,i]]]≥mm,mm=Abs[ab[[j,i]]];jmax=j],{j,i+1,n}];a1=ab[[i]];ab[[i]]=ab[[jmax]];ab[[jmax]]=a1;piv=ab[[i,i]];
Do[m[j]=ab[[j]][[i]]/piv;ab[[j]]=ab[[j]]-m[j]*ab[[i]],{j,i+1,n}],{i,1,n-1}];
MatrixForm[ab]
xx[n]=ab[[n,n+1]]/ab[[n,n]];
Do[xx[i]=(ab[[i,n+1]]-Sum[xx[j]*ab[[i,j]],{j,i+1,n}])/ab[[i,i]],{i,n-1,1,-1}];
x=Table[xx[i],{i,1,n}];
MatrixForm[x]