Date: 2021-03-03T22:26:17+00:00

Actual source code: dgefa5.c
petsc-3.14.5 2021-03-03
Report Typos and Errors

      2: /*
      3:       Inverts 5 by 5 matrix using gaussian elimination with partial pivoting.

      5:        Used by the sparse factorization routines in
      6:      src/mat/impls/baij/seq

      8:        This is a combination of the Linpack routines
      9:     dgefa() and dgedi() specialized for a size of 5.

     11: */
     12: #include <petscsys.h>

     14: PETSC_EXTERN PetscErrorCode PetscKernel_A_gets_inverse_A_5(MatScalar *a,PetscInt *ipvt,MatScalar *work,PetscReal shift,PetscBool allowzeropivot,PetscBool *zeropivotdetected)
     15: {
     16:   PetscInt  i__2,i__3,kp1,j,k,l,ll,i,kb,k3;
     17:   PetscInt  k4,j3;
     18:   MatScalar *aa,*ax,*ay,stmp;
     19:   MatReal   tmp,max;

     22:   if (zeropivotdetected) *zeropivotdetected = PETSC_FALSE;
     23:   shift = .25*shift*(1.e-12 + PetscAbsScalar(a[0]) + PetscAbsScalar(a[6]) + PetscAbsScalar(a[12]) + PetscAbsScalar(a[18]) + PetscAbsScalar(a[24]));

     25:   /* Parameter adjustments */
     26:   a -= 6;

     28:   for (k = 1; k <= 4; ++k) {
     29:     kp1 = k + 1;
     30:     k3  = 5*k;
     31:     k4  = k3 + k;

     33:     /* find l = pivot index */
     34:     i__2 = 6 - k;
     35:     aa   = &a[k4];
     36:     max  = PetscAbsScalar(aa[0]);
     37:     l    = 1;
     38:     for (ll=1; ll<i__2; ll++) {
     39:       tmp = PetscAbsScalar(aa[ll]);
     40:       if (tmp > max) { max = tmp; l = ll+1;}
     41:     }
     42:     l        += k - 1;
     43:     ipvt[k-1] = l;

     45:     if (a[l + k3] == 0.0) {
     46:       if (shift == 0.0) {
     47:         if (allowzeropivot) {
     49:           PetscInfo1(NULL,"Zero pivot, row %D\n",k-1);
     50:           *zeropivotdetected = PETSC_TRUE;
     51:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",k-1);
     52:       } else {
     53:         /* SHIFT is applied to SINGLE diagonal entry; does this make any sense? */
     54:         a[l + k3] = shift;
     55:       }
     56:     }

     58:     /* interchange if necessary */
     59:     if (l != k) {
     60:       stmp      = a[l + k3];
     61:       a[l + k3] = a[k4];
     62:       a[k4]     = stmp;
     63:     }

     65:     /* compute multipliers */
     66:     stmp = -1. / a[k4];
     67:     i__2 = 5 - k;
     68:     aa   = &a[1 + k4];
     69:     for (ll=0; ll<i__2; ll++) aa[ll] *= stmp;

     71:     /* row elimination with column indexing */
     72:     ax = &a[k4+1];
     73:     for (j = kp1; j <= 5; ++j) {
     74:       j3   = 5*j;
     75:       stmp = a[l + j3];
     76:       if (l != k) {
     77:         a[l + j3] = a[k + j3];
     78:         a[k + j3] = stmp;
     79:       }

     81:       i__3 = 5 - k;
     82:       ay   = &a[1+k+j3];
     83:       for (ll=0; ll<i__3; ll++) ay[ll] += stmp*ax[ll];
     84:     }
     85:   }
     86:   ipvt[4] = 5;
     87:   if (a[30] == 0.0) {
     88:     if (allowzeropivot) {
     90:       PetscInfo1(NULL,"Zero pivot, row %D\n",4);
     91:       *zeropivotdetected = PETSC_TRUE;
     92:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",4);
     93:   }

     95:   /* Now form the inverse */
     96:   /* compute inverse(u) */
     97:   for (k = 1; k <= 5; ++k) {
     98:     k3    = 5*k;
     99:     k4    = k3 + k;
    100:     a[k4] = 1.0 / a[k4];
    101:     stmp  = -a[k4];
    102:     i__2  = k - 1;
    103:     aa    = &a[k3 + 1];
    104:     for (ll=0; ll<i__2; ll++) aa[ll] *= stmp;
    105:     kp1 = k + 1;
    106:     if (5 < kp1) continue;
    107:     ax = aa;
    108:     for (j = kp1; j <= 5; ++j) {
    109:       j3        = 5*j;
    110:       stmp      = a[k + j3];
    111:       a[k + j3] = 0.0;
    112:       ay        = &a[j3 + 1];
    113:       for (ll=0; ll<k; ll++) ay[ll] += stmp*ax[ll];
    114:     }
    115:   }

    117:   /* form inverse(u)*inverse(l) */
    118:   for (kb = 1; kb <= 4; ++kb) {
    119:     k   = 5 - kb;
    120:     k3  = 5*k;
    121:     kp1 = k + 1;
    122:     aa  = a + k3;
    123:     for (i = kp1; i <= 5; ++i) {
    124:       work[i-1] = aa[i];
    125:       aa[i]     = 0.0;
    126:     }
    127:     for (j = kp1; j <= 5; ++j) {
    128:       stmp   = work[j-1];
    129:       ax     = &a[5*j + 1];
    130:       ay     = &a[k3 + 1];
    131:       ay[0] += stmp*ax[0];
    132:       ay[1] += stmp*ax[1];
    133:       ay[2] += stmp*ax[2];
    134:       ay[3] += stmp*ax[3];
    135:       ay[4] += stmp*ax[4];
    136:     }
    137:     l = ipvt[k-1];
    138:     if (l != k) {
    139:       ax   = &a[k3 + 1];
    140:       ay   = &a[5*l + 1];
    141:       stmp = ax[0]; ax[0] = ay[0]; ay[0] = stmp;
    142:       stmp = ax[1]; ax[1] = ay[1]; ay[1] = stmp;
    143:       stmp = ax[2]; ax[2] = ay[2]; ay[2] = stmp;
    144:       stmp = ax[3]; ax[3] = ay[3]; ay[3] = stmp;
    145:       stmp = ax[4]; ax[4] = ay[4]; ay[4] = stmp;
    146:     }
    147:   }
    148:   return(0);
    149: }