/**************************************************** * Calculate the determinant of a real square matrix * * A(n,n) by Gauss method with full pivoting. * * ------------------------------------------------- * * Ref.: "Algèbre - Algorithmes et programmes en * * Pascal By Jean-Louis Jardrin, Dunod - * * Bordas Paris, 1988 p. 76-79" [BIBLI 10]. * * ------------------------------------------------- * * SAMPLE RUN: * * (Calculate the determinant of matrix: * * 10 18 1 14 22 * * 4 12 25 8 16 * * 23 6 19 2 15 * * 17 5 13 21 9 * * 11 24 7 20 3 ) * * * * Input size of square real matrix: 5 * * * * Line 1 * * Element 1: 10 * * Element 2: 18 * * Element 3: 1 * * Element 4: 14 * * Element 5: 22 * * * * Line 2 * * Element 1: 4 * * Element 2: 12 * * Element 3: 25 * * Element 4: 8 * * Element 5: 16 * * * * Line 3 * * Element 1: 23 * * Element 2: 6 * * Element 3: 19 * * Element 4: 2 * * Element 5: 15 * * * * Line 4 * * Element 1: 17 * * Element 2: 5 * * Element 3: 13 * * Element 4: 21 * * Element 5: 9 * * * * Line 5 * * Element 1: 11 * * Element 2: 24 * * Element 3: 7 * * Element 4: 20 * * Element 5: 3 * * * * Determinant = -4680000.000000 * * * * C++ version with dynamic allocations * * By Jean-Pierre Moreau, Paris. * * (www.jpmoreau.fr) * * * * (To link with basis_r.cpp and vmblock.cpp). * ****************************************************/ #include #include int n; //size of matrix A REAL **A; //pointer to input matrix REAL det; //determinant of matrix A REAL eps; //desired precision void *vmblock; //for dynamic allocation of A(n,n) /*The function TSRGT applies to input real square matrix A(n,n) the upper triangularization algorithm of Gauss method with full pivoting and keeps trace of successive transformations done in integer vectors KP and LP. ---------------------------------------------------------------------------- Input parameters: eps precision (real) n size of A matrix (integer) A pointer to input real square matrix (REAL**) Output parameters: it flag=1 if A matrix ok, =0 if A matrix is singular (integer) C pointer to table storing main diagonal elements and supra- diagonal elements of upper triangular matrix and the multi- plying coefficients used during triangularization process (REAL**) KP table storing informations concerning the column exchanges during process (int*) LP table storing informations concerning the line exchanges during process (int*) ----------------------------------------------------------------------------- The table C is first initialized to A matrix, then receives at each step k of the triangularization process, usefull elements of A matrix at step k for k=1,2,...n. The variables po(real), lo and ko(integer) store respectively pivot at step k, its line number and its column number. -----------------------------------------------------------------------------*/ void TSRGT(REAL eps,int n,REAL **A,int *it,REAL **C, int *Kp,int *Lp) { int i,j,k,ko,lo; REAL po,t0; //set matrix C to A for (i=1; i<=n; i++) for (j=1; j<=n; j++) C[i][j] = A[i][j]; *it=1; k=1; while (*it==1 && kfabs(po)) { po=C[i][j]; lo=i; ko=j; } Lp[k]=lo; Kp[k]=ko; if (fabs(po)=i) the corresponding element of the upper triangular matrix. Tables Lp and Kp contain informations relative to exchanges of line or column that occured during the process. For instance, the element number k of Lp is an integer <> k if an exchange of line has been made at step k (k=1,2,...,n). The number of exchanges of lines and columns is stored in L(integer). the determinant of A matrix is stored in d0(real). Note: types pMat and pVecI must be declared and allocated for by main program, except local variables C,Kp,Lp allocated (and disposed of) here. -----------------------------------------------------------------------------*/ REAL DMGT(REAL eps,int n,REAL **A) { int it,k,l; REAL d0; REAL **C; int *Kp, *Lp; void *vmblock1; //allocate local matrix and vectors vmblock1 = vminit(); C = (REAL **)vmalloc(vmblock1, MATRIX, n+1, n+1); Kp = (int *) vmalloc(vmblock1, VEKTOR, n+1, 0); Lp = (int *) vmalloc(vmblock1, VEKTOR, n+1, 0); if (! vmcomplete(vmblock1)) { LogError ("Memory full !", 0, __FILE__, __LINE__); return 0; } TSRGT(eps,n,A,&it,C,Kp,Lp); //call triangularization procedure if (it==0) return 0.0; //matrix singular, det=0 else { //matrix regular, det<>0 d0=1.0; for (k=1; k<=n; k++) d0 *= C[k][k]; l=0; for (k=1; k