xref: /petsc/src/mat/impls/baij/seq/dgefa4.c (revision 71c5468d42d5edee53e3cd1296ec0437567e1ffb)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*71c5468dSBarry Smith static char vcid[] = "$Id: dgefa4.c,v 1.6 1998/12/17 22:10:39 bsmith Exp bsmith $";
34224c193SBarry Smith #endif
44224c193SBarry Smith /*
58a36c062SBarry Smith        Inverts 4 by 4 matrix using partial pivoting.
6*71c5468dSBarry Smith 
7*71c5468dSBarry Smith        Used by the sparse factorization routines in
8*71c5468dSBarry Smith      src/mat/impls/baij/seq and src/mat/impls/bdiag/seq
9*71c5468dSBarry Smith 
10*71c5468dSBarry Smith        See also src/inline/ilu.h
11*71c5468dSBarry Smith 
12*71c5468dSBarry Smith        This is a combination of the Linpack routines
13*71c5468dSBarry Smith     dgefa() and dgedi() specialized for a size of 4.
14*71c5468dSBarry Smith 
154224c193SBarry Smith */
164224c193SBarry Smith #include "petsc.h"
174224c193SBarry Smith 
184224c193SBarry Smith #undef __FUNC__
198a36c062SBarry Smith #define __FUNC__ "Kernel_A_gets_inverse_A_4"
203f1db9ecSBarry Smith int Kernel_A_gets_inverse_A_4(MatScalar *a)
214224c193SBarry Smith {
228a36c062SBarry Smith     int        i__2, i__3, kp1, j, k, l,ll,i,ipvt_l[4],*ipvt = ipvt_l-1,kb,k3;
234224c193SBarry Smith     int        k4,j3;
243f1db9ecSBarry Smith     MatScalar  *aa,*ax,*ay,work_l[16],*work = work_l-1,stmp;
253f1db9ecSBarry Smith     MatFloat   tmp,max;
264224c193SBarry Smith 
274224c193SBarry Smith /*     gaussian elimination with partial pivoting */
284224c193SBarry Smith 
293a40ed3dSBarry Smith     PetscFunctionBegin;
304224c193SBarry Smith     /* Parameter adjustments */
318a36c062SBarry Smith     a       -= 5;
324224c193SBarry Smith 
338a36c062SBarry Smith     for (k = 1; k <= 3; ++k) {
344224c193SBarry Smith 	kp1 = k + 1;
358a36c062SBarry Smith         k3  = 4*k;
364224c193SBarry Smith         k4  = k3 + k;
374224c193SBarry Smith /*        find l = pivot index */
384224c193SBarry Smith 
394224c193SBarry Smith 	i__2 = 4 - k;
404224c193SBarry Smith         aa = &a[k4];
414224c193SBarry Smith         max = PetscAbsScalar(aa[0]);
424224c193SBarry Smith         l = 1;
434224c193SBarry Smith         for ( ll=1; ll<i__2; ll++ ) {
444224c193SBarry Smith           tmp = PetscAbsScalar(aa[ll]);
454224c193SBarry Smith           if (tmp > max) { max = tmp; l = ll+1;}
464224c193SBarry Smith         }
474224c193SBarry Smith         l       += k - 1;
484224c193SBarry Smith 	ipvt[k] = l;
494224c193SBarry Smith 
504224c193SBarry Smith 	if (a[l + k3] == 0.) {
514224c193SBarry Smith 	  SETERRQ(k,0,"Zero pivot");
524224c193SBarry Smith 	}
534224c193SBarry Smith 
544224c193SBarry Smith /*           interchange if necessary */
554224c193SBarry Smith 
564224c193SBarry Smith 	if (l != k) {
574224c193SBarry Smith 	  stmp      = a[l + k3];
584224c193SBarry Smith 	  a[l + k3] = a[k4];
594224c193SBarry Smith 	  a[k4]     = stmp;
604224c193SBarry Smith         }
614224c193SBarry Smith 
624224c193SBarry Smith /*           compute multipliers */
634224c193SBarry Smith 
644224c193SBarry Smith 	stmp = -1. / a[k4];
658a36c062SBarry Smith 	i__2 = 4 - k;
664224c193SBarry Smith         aa = &a[1 + k4];
674224c193SBarry Smith         for ( ll=0; ll<i__2; ll++ ) {
684224c193SBarry Smith           aa[ll] *= stmp;
694224c193SBarry Smith         }
704224c193SBarry Smith 
714224c193SBarry Smith /*           row elimination with column indexing */
724224c193SBarry Smith 
734224c193SBarry Smith 	ax = &a[k4+1];
748a36c062SBarry Smith         for (j = kp1; j <= 4; ++j) {
758a36c062SBarry Smith             j3   = 4*j;
764224c193SBarry Smith 	    stmp = a[l + j3];
774224c193SBarry Smith 	    if (l != k) {
784224c193SBarry Smith 	      a[l + j3] = a[k + j3];
794224c193SBarry Smith 	      a[k + j3] = stmp;
804224c193SBarry Smith             }
814224c193SBarry Smith 
828a36c062SBarry Smith 	    i__3 = 4 - k;
834224c193SBarry Smith             ay = &a[1+k+j3];
844224c193SBarry Smith             for ( ll=0; ll<i__3; ll++ ) {
854224c193SBarry Smith               ay[ll] += stmp*ax[ll];
864224c193SBarry Smith             }
874224c193SBarry Smith 	}
884224c193SBarry Smith     }
898a36c062SBarry Smith     ipvt[4] = 4;
908a36c062SBarry Smith     if (a[20] == 0.) {
914224c193SBarry Smith 	SETERRQ(3,0,"Zero pivot,final row");
924224c193SBarry Smith     }
934224c193SBarry Smith 
944224c193SBarry Smith     /*
954224c193SBarry Smith          Now form the inverse
964224c193SBarry Smith     */
974224c193SBarry Smith 
984224c193SBarry Smith    /*     compute inverse(u) */
994224c193SBarry Smith 
1008a36c062SBarry Smith     for (k = 1; k <= 4; ++k) {
1018a36c062SBarry Smith         k3    = 4*k;
1024224c193SBarry Smith         k4    = k3 + k;
1034224c193SBarry Smith 	a[k4] = 1.0 / a[k4];
1044224c193SBarry Smith 	stmp  = -a[k4];
1054224c193SBarry Smith 	i__2  = k - 1;
1064224c193SBarry Smith         aa    = &a[k3 + 1];
1074224c193SBarry Smith         for ( ll=0; ll<i__2; ll++ ) aa[ll] *= stmp;
1084224c193SBarry Smith 	kp1 = k + 1;
1098a36c062SBarry Smith 	if (4 < kp1) continue;
1104224c193SBarry Smith         ax = aa;
1118a36c062SBarry Smith         for (j = kp1; j <= 4; ++j) {
1128a36c062SBarry Smith             j3        = 4*j;
1134224c193SBarry Smith 	    stmp      = a[k + j3];
1144224c193SBarry Smith 	    a[k + j3] = 0.0;
1154224c193SBarry Smith             ay        = &a[j3 + 1];
1164224c193SBarry Smith             for ( ll=0; ll<k; ll++ ) {
1174224c193SBarry Smith               ay[ll] += stmp*ax[ll];
1184224c193SBarry Smith             }
1194224c193SBarry Smith 	}
1204224c193SBarry Smith     }
1214224c193SBarry Smith 
1224224c193SBarry Smith    /*    form inverse(u)*inverse(l) */
1234224c193SBarry Smith 
1248a36c062SBarry Smith     for (kb = 1; kb <= 3; ++kb) {
1258a36c062SBarry Smith 	k   = 4 - kb;
1268a36c062SBarry Smith         k3  = 4*k;
1274224c193SBarry Smith 	kp1 = k + 1;
1284224c193SBarry Smith         aa  = a + k3;
1298a36c062SBarry Smith 	for (i = kp1; i <= 4; ++i) {
130f3f07278SSatish Balay             work_l[i-1] = aa[i];
131f3f07278SSatish Balay             /* work[i] = aa[i]; Fix for -O3 error on Origin 2000 */
1324224c193SBarry Smith 	    aa[i]   = 0.0;
1334224c193SBarry Smith 	}
1348a36c062SBarry Smith 	for (j = kp1; j <= 4; ++j) {
1354224c193SBarry Smith 	    stmp  = work[j];
1368a36c062SBarry Smith             ax    = &a[4*j + 1];
1374224c193SBarry Smith             ay    = &a[k3 + 1];
1384224c193SBarry Smith             ay[0] += stmp*ax[0];
1394224c193SBarry Smith             ay[1] += stmp*ax[1];
1404224c193SBarry Smith             ay[2] += stmp*ax[2];
1418a36c062SBarry Smith             ay[3] += stmp*ax[3];
1424224c193SBarry Smith 	}
1434224c193SBarry Smith 	l = ipvt[k];
1444224c193SBarry Smith 	if (l != k) {
1454224c193SBarry Smith             ax = &a[k3 + 1];
1468a36c062SBarry Smith             ay = &a[4*l + 1];
1474224c193SBarry Smith             stmp = ax[0]; ax[0] = ay[0]; ay[0] = stmp;
1484224c193SBarry Smith             stmp = ax[1]; ax[1] = ay[1]; ay[1] = stmp;
1494224c193SBarry Smith             stmp = ax[2]; ax[2] = ay[2]; ay[2] = stmp;
1508a36c062SBarry Smith             stmp = ax[3]; ax[3] = ay[3]; ay[3] = stmp;
1514224c193SBarry Smith 	}
1524224c193SBarry Smith     }
1533a40ed3dSBarry Smith     PetscFunctionReturn(0);
1544224c193SBarry Smith }
1554224c193SBarry Smith 
156