06-03-2015, 01:20 PM
hi everybody,
please, help to debug and improve this program to calculate pivots of a matrix (Gauss-Jordan elimination).
It is important to me to create also another program to calculate LDLt factorization (decomposition) [this is the thread], where for a symmetric matrix L is a lower triangular (Lt its transpose) and D diagonal with pivots...
The program for pivots use the function pivot(), finding recursively the minors of the matrix and applying this function to them, saving pivots and rows in a matrix, and at end giving upper triangular matrix with pivots in diagonal and a list with pivots.
The program can, if a pivot is 0, to swap two rows (this function could be improved) and retry for a new calculation...
But my algorithm is not so good...
I'd like to receive your approach, tips, advice...
I wasn't able to find pivots directly with pivot() function, without using it more times...
Or help to find pivots and reduced form with them in diagonal with ref(), RREF(), pivot(), lu(), diag()...
Salvo
The code:
this seems to be good with matrices like [[2,1,1,5],[4,-6,0,-2],[-2,7,2,9]] (or [[2,1,1],[4,-6,0],[-2,7,2]], pivots 2,-8,1), [[2,-1,0],[-1,2,-1],[0,-1,1]] (pivots 2, 3/2 and 4/3)
but has little wrong results with [[1,2,3],[2,4,3],[3,2,-1]]: the pivots should be (1,-4,-3) but I get (1,-4,3/4)
and isn't good for echelon matrices (not RREF reduced), like [[1,3,3,2],[2,6,9,7],[-1,-3,3,4]] that gives [[1,3,3,2],[0,0,0,0],[0,0,0,0]] (pivots 1,0,0?)
EDIT: please, help whit last rows of program: why difference in the last pivot? about what am I wrong? I need a more general solution...
please, help to debug and improve this program to calculate pivots of a matrix (Gauss-Jordan elimination).
It is important to me to create also another program to calculate LDLt factorization (decomposition) [this is the thread], where for a symmetric matrix L is a lower triangular (Lt its transpose) and D diagonal with pivots...
The program for pivots use the function pivot(), finding recursively the minors of the matrix and applying this function to them, saving pivots and rows in a matrix, and at end giving upper triangular matrix with pivots in diagonal and a list with pivots.
The program can, if a pivot is 0, to swap two rows (this function could be improved) and retry for a new calculation...
But my algorithm is not so good...
I'd like to receive your approach, tips, advice...
I wasn't able to find pivots directly with pivot() function, without using it more times...
Or help to find pivots and reduced form with them in diagonal with ref(), RREF(), pivot(), lu(), diag()...
Salvo
The code:
Code:
calcPivot();
zeropivot:=0;
numpivot:=1;
gj:=[0,0];
EXPORT gaussJordan(m)
// Gauss-Jordan elimination and pivots
// Salvo Micciché 2015
BEGIN
local r, c, piv, temp;
r:=rowDim(m);
c:=colDim(m);
gj:=MAKEMAT(0,r,c);
M1:= m;
L1:={0,0};
calcPivot(c,r);
IF (zeropivot==1) THEN
M1:=m;
// M1:= CAS.SWAPROW(M1, numpivot, (numpivot+1));
temp:= M1(numpivot); M1(numpivot):=M1(numpivot+1); M1(numpivot+1):=temp;
calcPivot(c,r);
END; // if
piv:= list2mat(L1, r);
M1:= gj;
// Return pivot, matrix reduced and RREF form
RETURN {piv, M1, ref(m)};
END;
calcPivot(c,r)
BEGIN
local j, k;
zeropivot:=0;
FOR j FROM 1 TO r DO
M1:=CAS.pivot(M1,1,1);
L1(j):= M1(1,1);
IF (L1(j)==0) THEN zeropivot:=1; numpivot:=j; RETURN {zeropivot, numpivot}; END;
FOR k FROM 1 TO colDim(M1) DO
gj(j, c-k+1):= M1(1, colDim(M1)-k+1);
END; // inner for
IF (j<r) THEN
M1:= delrows(M1,1);
M1:= delcols(M1,1);
END; // if
M1:= exact(M1./L1(j));
IF L1(j)==0 THEN RETURN "Pivot is 0, division by 0"; END;
END; // for
END;
this seems to be good with matrices like [[2,1,1,5],[4,-6,0,-2],[-2,7,2,9]] (or [[2,1,1],[4,-6,0],[-2,7,2]], pivots 2,-8,1), [[2,-1,0],[-1,2,-1],[0,-1,1]] (pivots 2, 3/2 and 4/3)
but has little wrong results with [[1,2,3],[2,4,3],[3,2,-1]]: the pivots should be (1,-4,-3) but I get (1,-4,3/4)
and isn't good for echelon matrices (not RREF reduced), like [[1,3,3,2],[2,6,9,7],[-1,-3,3,4]] that gives [[1,3,3,2],[0,0,0,0],[0,0,0,0]] (pivots 1,0,0?)
EDIT: please, help whit last rows of program: why difference in the last pivot? about what am I wrong? I need a more general solution...