Actual source code: dgefa5.c
petsc-3.6.1 2015-08-06
2: /*
3: Inverts 5 by 5 matrix using 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>
16: PETSC_EXTERN PetscErrorCode PetscKernel_A_gets_inverse_A_5(MatScalar *a,PetscInt *ipvt,MatScalar *work,PetscReal shift)
17: {
18: PetscInt i__2,i__3,kp1,j,k,l,ll,i,kb,k3;
19: PetscInt k4,j3;
20: MatScalar *aa,*ax,*ay,stmp;
21: MatReal tmp,max;
23: /* gaussian elimination with partial pivoting */
26: shift = .25*shift*(1.e-12 + PetscAbsScalar(a[0]) + PetscAbsScalar(a[6]) + PetscAbsScalar(a[12]) + PetscAbsScalar(a[18]) + PetscAbsScalar(a[24]));
27: /* Parameter adjustments */
28: a -= 6;
30: for (k = 1; k <= 4; ++k) {
31: kp1 = k + 1;
32: k3 = 5*k;
33: k4 = k3 + k;
34: /* find l = pivot index */
36: i__2 = 6 - k;
37: aa = &a[k4];
38: max = PetscAbsScalar(aa[0]);
39: l = 1;
40: for (ll=1; ll<i__2; ll++) {
41: tmp = PetscAbsScalar(aa[ll]);
42: if (tmp > max) { max = tmp; l = ll+1;}
43: }
44: l += k - 1;
45: ipvt[k-1] = l;
47: if (a[l + k3] == 0.0) {
48: if (shift == 0.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",k-1);
49: else {
50: /* SHIFT is applied to SINGLE diagonal entry; does this make any sense? */
51: a[l + k3] = shift;
52: }
53: }
55: /* interchange if necessary */
57: if (l != k) {
58: stmp = a[l + k3];
59: a[l + k3] = a[k4];
60: a[k4] = stmp;
61: }
63: /* compute multipliers */
65: stmp = -1. / a[k4];
66: i__2 = 5 - k;
67: aa = &a[1 + k4];
68: for (ll=0; ll<i__2; ll++) aa[ll] *= stmp;
70: /* 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) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",4);
89: /*
90: Now form the inverse
91: */
93: /* compute inverse(u) */
95: for (k = 1; k <= 5; ++k) {
96: k3 = 5*k;
97: k4 = k3 + k;
98: a[k4] = 1.0 / a[k4];
99: stmp = -a[k4];
100: i__2 = k - 1;
101: aa = &a[k3 + 1];
102: for (ll=0; ll<i__2; ll++) aa[ll] *= stmp;
103: kp1 = k + 1;
104: if (5 < kp1) continue;
105: ax = aa;
106: for (j = kp1; j <= 5; ++j) {
107: j3 = 5*j;
108: stmp = a[k + j3];
109: a[k + j3] = 0.0;
110: ay = &a[j3 + 1];
111: for (ll=0; ll<k; ll++) ay[ll] += stmp*ax[ll];
112: }
113: }
115: /* form inverse(u)*inverse(l) */
117: for (kb = 1; kb <= 4; ++kb) {
118: k = 5 - kb;
119: k3 = 5*k;
120: kp1 = k + 1;
121: aa = a + k3;
122: for (i = kp1; i <= 5; ++i) {
123: work[i-1] = aa[i];
124: aa[i] = 0.0;
125: }
126: for (j = kp1; j <= 5; ++j) {
127: stmp = work[j-1];
128: ax = &a[5*j + 1];
129: ay = &a[k3 + 1];
130: ay[0] += stmp*ax[0];
131: ay[1] += stmp*ax[1];
132: ay[2] += stmp*ax[2];
133: ay[3] += stmp*ax[3];
134: ay[4] += stmp*ax[4];
135: }
136: l = ipvt[k-1];
137: if (l != k) {
138: ax = &a[k3 + 1];
139: ay = &a[5*l + 1];
140: stmp = ax[0]; ax[0] = ay[0]; ay[0] = stmp;
141: stmp = ax[1]; ax[1] = ay[1]; ay[1] = stmp;
142: stmp = ax[2]; ax[2] = ay[2]; ay[2] = stmp;
143: stmp = ax[3]; ax[3] = ay[3]; ay[3] = stmp;
144: stmp = ax[4]; ax[4] = ay[4]; ay[4] = stmp;
145: }
146: }
147: return(0);
148: }