xref: /petsc/src/mat/impls/aij/seq/aijfact.c (revision cb51245867d6a442a15a98eb5f86fd750dfb6de4)
1*cb512458SBarry Smith #ifndef lint
2*cb512458SBarry Smith static char vcid[] = "$Id: $";
3*cb512458SBarry Smith #endif
4289bc588SBarry Smith 
5289bc588SBarry Smith 
6289bc588SBarry Smith #include "aij.h"
78c37ef55SBarry Smith #include "inline/spops.h"
8289bc588SBarry Smith /*
9289bc588SBarry Smith     Factorization code for AIJ format.
10289bc588SBarry Smith */
11289bc588SBarry Smith 
12289bc588SBarry Smith int MatiAIJLUFactorSymbolic(Mat mat,IS isrow,IS iscol,Mat *fact)
13289bc588SBarry Smith {
14289bc588SBarry Smith   Matiaij *aij = (Matiaij *) mat->data, *aijnew;
15289bc588SBarry Smith   IS      isicol;
166abc6512SBarry Smith   int     *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j;
176abc6512SBarry Smith   int     *ainew,*ajnew, jmax,*fill, *ajtmp, nz;
186abc6512SBarry Smith   int     *idnew, idx, row,m,fm, nnz, nzi,len;
19289bc588SBarry Smith 
20289bc588SBarry Smith   if (n != aij->n) SETERR(1,"Mat must be square");
21289bc588SBarry Smith   if (!isrow) {SETERR(1,"Must have row permutation");}
22289bc588SBarry Smith   if (!iscol) {SETERR(1,"Must have column permutation");}
23289bc588SBarry Smith 
246abc6512SBarry Smith   if ((ierr = ISInvertPermutation(iscol,&isicol))) SETERR(ierr,0);
25289bc588SBarry Smith   ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic);
26289bc588SBarry Smith 
27289bc588SBarry Smith   /* get new row pointers */
28289bc588SBarry Smith   ainew = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(ainew);
29289bc588SBarry Smith   ainew[0] = 1;
30289bc588SBarry Smith   /* don't know how many column pointers are needed so estimate */
31289bc588SBarry Smith   jmax = 2*ai[n];
32289bc588SBarry Smith   ajnew = (int *) MALLOC( (jmax)*sizeof(int) ); CHKPTR(ajnew);
33289bc588SBarry Smith   /* fill is a linked list of nonzeros in active row */
34289bc588SBarry Smith   fill = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(fill);
35289bc588SBarry Smith   /* idnew is location of diagonal in factor */
36289bc588SBarry Smith   idnew = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(idnew);
37289bc588SBarry Smith   idnew[0] = 1;
38289bc588SBarry Smith 
39289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
40289bc588SBarry Smith     /* first copy previous fill into linked list */
41289bc588SBarry Smith     nnz = nz    = ai[r[i]+1] - ai[r[i]];
42289bc588SBarry Smith     ajtmp = aj + ai[r[i]] - 1;
43289bc588SBarry Smith     fill[n] = n;
44289bc588SBarry Smith     while (nz--) {
45289bc588SBarry Smith       fm = n;
46289bc588SBarry Smith       idx = ic[*ajtmp++ - 1];
47289bc588SBarry Smith       do {
48289bc588SBarry Smith         m = fm;
49289bc588SBarry Smith         fm = fill[m];
50289bc588SBarry Smith       } while (fm < idx);
51289bc588SBarry Smith       fill[m] = idx;
52289bc588SBarry Smith       fill[idx] = fm;
53289bc588SBarry Smith     }
54289bc588SBarry Smith     row = fill[n];
55289bc588SBarry Smith     while ( row < i ) {
56289bc588SBarry Smith       ajtmp = ajnew + idnew[row] - 1;
57289bc588SBarry Smith       nz = ainew[row+1] - idnew[row];
58289bc588SBarry Smith       fm = row;
59289bc588SBarry Smith       while (nz--) {
60289bc588SBarry Smith         fm = n;
61289bc588SBarry Smith         idx = *ajtmp++ - 1;
62289bc588SBarry Smith         do {
63289bc588SBarry Smith           m = fm;
64289bc588SBarry Smith           fm = fill[m];
65289bc588SBarry Smith         } while (fm < idx);
66289bc588SBarry Smith         if (fm != idx) {
67289bc588SBarry Smith           fill[m] = idx;
68289bc588SBarry Smith           fill[idx] = fm;
69289bc588SBarry Smith           fm = idx;
70289bc588SBarry Smith           nnz++;
71289bc588SBarry Smith         }
72289bc588SBarry Smith       }
73289bc588SBarry Smith       row = fill[row];
74289bc588SBarry Smith     }
75289bc588SBarry Smith     /* copy new filled row into permanent storage */
76289bc588SBarry Smith     ainew[i+1] = ainew[i] + nnz;
77289bc588SBarry Smith     if (ainew[i+1] > jmax+1) {
78289bc588SBarry Smith       /* allocate a longer ajnew */
79289bc588SBarry Smith       jmax += nnz*(n-i);
80289bc588SBarry Smith       ajtmp = (int *) MALLOC( jmax*sizeof(int) );CHKPTR(ajtmp);
81289bc588SBarry Smith       MEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int));
82289bc588SBarry Smith       FREE(ajnew);
83289bc588SBarry Smith       ajnew = ajtmp;
84289bc588SBarry Smith     }
85289bc588SBarry Smith     ajtmp = ajnew + ainew[i] - 1;
86289bc588SBarry Smith     fm = fill[n];
87289bc588SBarry Smith     nzi = 0;
88289bc588SBarry Smith     while (nnz--) {
89289bc588SBarry Smith       if (fm < i) nzi++;
90289bc588SBarry Smith       *ajtmp++ = fm + 1;
91289bc588SBarry Smith       fm = fill[fm];
92289bc588SBarry Smith     }
93289bc588SBarry Smith     idnew[i] = ainew[i] + nzi;
94289bc588SBarry Smith   }
95289bc588SBarry Smith 
96289bc588SBarry Smith   ISDestroy(isicol); FREE(fill);
97289bc588SBarry Smith 
98289bc588SBarry Smith   /* put together the new matrix */
9911d228e4SBarry Smith   ierr = MatCreateSequentialAIJ(n, n, 0, 0, fact); CHKERR(ierr);
100289bc588SBarry Smith   aijnew = (Matiaij *) (*fact)->data;
101289bc588SBarry Smith   FREE(aijnew->imax);
102289bc588SBarry Smith   aijnew->singlemalloc = 0;
103f0479e8cSBarry Smith   len = (ainew[n] - 1)*sizeof(Scalar);
104e8d4e0b9SBarry Smith   /* the next line frees the default space generated by the Create() */
105e8d4e0b9SBarry Smith   FREE(aijnew->a); FREE(aijnew->ilen);
106289bc588SBarry Smith   aijnew->a         = (Scalar *) MALLOC( len ); CHKPTR(aijnew->a);
107289bc588SBarry Smith   aijnew->j         = ajnew;
108289bc588SBarry Smith   aijnew->i         = ainew;
1098c37ef55SBarry Smith   aijnew->diag      = idnew;
110e8d4e0b9SBarry Smith   aijnew->ilen      = 0;
11120563c6bSBarry Smith   aijnew->imax      = 0;
112289bc588SBarry Smith   (*fact)->row      = isrow;
113289bc588SBarry Smith   (*fact)->col      = iscol;
114289bc588SBarry Smith   (*fact)->factor   = FACTOR_LU;
115289bc588SBarry Smith   return 0;
116289bc588SBarry Smith }
117289bc588SBarry Smith 
11820563c6bSBarry Smith int MatiAIJLUFactorNumeric(Mat mat,Mat *infact)
119289bc588SBarry Smith {
12020563c6bSBarry Smith   Mat     fact = *infact;
121289bc588SBarry Smith   Matiaij *aij = (Matiaij *) mat->data, *aijnew = (Matiaij *)fact->data;
122289bc588SBarry Smith   IS      iscol = fact->col, isrow = fact->row, isicol;
123289bc588SBarry Smith   int     *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j;
1246abc6512SBarry Smith   int     *ajtmpold, *ajtmp, nz, row,*pj;
1256abc6512SBarry Smith   Scalar  *rtmp,*v, *pv, *pc, multiplier;
126289bc588SBarry Smith 
1276abc6512SBarry Smith   if ((ierr = ISInvertPermutation(iscol,&isicol))) SETERR(ierr,0);
1288c37ef55SBarry Smith   ierr = ISGetIndices(isrow,&r); CHKERR(ierr);
1298c37ef55SBarry Smith   ierr = ISGetIndices(isicol,&ic); CHKERR(ierr);
130289bc588SBarry Smith   rtmp = (Scalar *) MALLOC( (n+1)*sizeof(Scalar) ); CHKPTR(rtmp);
131289bc588SBarry Smith 
132289bc588SBarry Smith   for ( i=0; i<n; i++ ) {
133289bc588SBarry Smith     nz = ai[i+1] - ai[i];
134289bc588SBarry Smith     ajtmp = aj + ai[i] - 1;
135289bc588SBarry Smith     for  ( j=0; j<nz; j++ ) rtmp[ajtmp[j]-1] = 0.0;
136289bc588SBarry Smith 
137289bc588SBarry Smith     /* load in initial (unfactored row) */
1388c37ef55SBarry Smith     nz = aij->i[r[i]+1] - aij->i[r[i]];
1398c37ef55SBarry Smith     ajtmpold = aij->j + aij->i[r[i]] - 1;
1408c37ef55SBarry Smith     v  = aij->a + aij->i[r[i]] - 1;
1418c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) rtmp[ic[ajtmpold[j]-1]] =  v[j];
142289bc588SBarry Smith 
1438c37ef55SBarry Smith     row = *ajtmp++ - 1;
144289bc588SBarry Smith     while (row < i) {
1458c37ef55SBarry Smith       pc = rtmp + row;
1468c37ef55SBarry Smith       if (*pc != 0.0) {
1478c37ef55SBarry Smith         nz = aijnew->diag[row] - ai[row];
1488c37ef55SBarry Smith         pv = aijnew->a + aijnew->diag[row] - 1;
1498c37ef55SBarry Smith         pj = aijnew->j + aijnew->diag[row];
1508c37ef55SBarry Smith         multiplier = *pc * *pv++;
1518c37ef55SBarry Smith         *pc = multiplier;
1528c37ef55SBarry Smith         nz = ai[row+1] - ai[row] - 1 - nz;
1538c37ef55SBarry Smith         while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++;
154289bc588SBarry Smith       }
1558c37ef55SBarry Smith       row = *ajtmp++ - 1;
156289bc588SBarry Smith     }
1578c37ef55SBarry Smith     /* finished row so stick it into aijnew->a */
1588c37ef55SBarry Smith     pv = aijnew->a + ai[i] - 1;
1598c37ef55SBarry Smith     pj = aijnew->j + ai[i] - 1;
1608c37ef55SBarry Smith     nz = ai[i+1] - ai[i];
1618c37ef55SBarry Smith     rtmp[i] = 1.0/rtmp[i];
1628c37ef55SBarry Smith     for ( j=0; j<nz; j++ ) {pv[j] = rtmp[pj[j]-1];}
1638c37ef55SBarry Smith   }
1648c37ef55SBarry Smith   FREE(rtmp);
165f0479e8cSBarry Smith   ierr = ISRestoreIndices(isicol,&ic); CHKERR(ierr);
166f0479e8cSBarry Smith   ierr = ISRestoreIndices(isrow,&r); CHKERR(ierr);
1678c37ef55SBarry Smith   ierr = ISDestroy(isicol); CHKERR(ierr);
1688c37ef55SBarry Smith   fact->factor = FACTOR_LU;
169289bc588SBarry Smith 
170289bc588SBarry Smith   return 0;
171289bc588SBarry Smith }
172da3a660dSBarry Smith int MatiAIJLUFactor(Mat matin,IS row,IS col)
173da3a660dSBarry Smith {
174da3a660dSBarry Smith   Matiaij *mat = (Matiaij *) matin->data;
1756abc6512SBarry Smith   int     ierr;
176da3a660dSBarry Smith   Mat     fact;
177da3a660dSBarry Smith   ierr = MatiAIJLUFactorSymbolic(matin,row,col,&fact); CHKERR(ierr);
178da3a660dSBarry Smith   ierr = MatiAIJLUFactorNumeric(matin,&fact); CHKERR(ierr);
179da3a660dSBarry Smith 
180da3a660dSBarry Smith   /* free all the data structures from mat */
181da3a660dSBarry Smith   FREE(mat->a);
182da3a660dSBarry Smith   if (!mat->singlemalloc) {FREE(mat->i); FREE(mat->j);}
183da3a660dSBarry Smith   if (mat->diag) FREE(mat->diag);
184da3a660dSBarry Smith   if (mat->ilen) FREE(mat->ilen);
185da3a660dSBarry Smith   if (mat->imax) FREE(mat->imax);
186da3a660dSBarry Smith   if (matin->row && matin->col && matin->row != matin->col) {
187da3a660dSBarry Smith     ISDestroy(matin->row);
188da3a660dSBarry Smith   }
189da3a660dSBarry Smith   if (matin->col) ISDestroy(matin->col);
190da3a660dSBarry Smith   FREE(mat);
191da3a660dSBarry Smith 
192da3a660dSBarry Smith   MEMCPY(matin,fact,sizeof(struct _Mat));
193da3a660dSBarry Smith   FREE(fact);
194da3a660dSBarry Smith   return 0;
195da3a660dSBarry Smith }
196da3a660dSBarry Smith 
1978c37ef55SBarry Smith int MatiAIJSolve(Mat mat,Vec bb, Vec xx)
1988c37ef55SBarry Smith {
1998c37ef55SBarry Smith   Matiaij *aij = (Matiaij *) mat->data;
2008c37ef55SBarry Smith   IS      iscol = mat->col, isrow = mat->row;
2016abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
2028c37ef55SBarry Smith   int     nz;
2038c37ef55SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
2048c37ef55SBarry Smith 
2056abc6512SBarry Smith   if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0);
2066abc6512SBarry Smith   if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0);
2078c37ef55SBarry Smith   tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp);
2088c37ef55SBarry Smith 
2096abc6512SBarry Smith   if ((ierr = ISGetIndices(isrow,&r))) SETERR(ierr,0);
2106abc6512SBarry Smith   if ((ierr = ISGetIndices(iscol,&c))) SETERR(ierr,0); c = c + (n-1);
2118c37ef55SBarry Smith 
2128c37ef55SBarry Smith   /* forward solve the lower triangular */
2138c37ef55SBarry Smith   tmp[0] = b[*r++];
2148c37ef55SBarry Smith   for ( i=1; i<n; i++ ) {
2158c37ef55SBarry Smith     v   = aa + ai[i] - 1;
2168c37ef55SBarry Smith     vi  = aj + ai[i] - 1;
2178c37ef55SBarry Smith     nz  = aij->diag[i] - ai[i];
2188c37ef55SBarry Smith     sum = b[*r++];
2198c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2208c37ef55SBarry Smith     tmp[i] = sum;
2218c37ef55SBarry Smith   }
2228c37ef55SBarry Smith 
2238c37ef55SBarry Smith   /* backward solve the upper triangular */
2248c37ef55SBarry Smith   for ( i=n-1; i>=0; i-- ){
2258c37ef55SBarry Smith     v   = aa + aij->diag[i];
2268c37ef55SBarry Smith     vi  = aj + aij->diag[i];
2278c37ef55SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
2288c37ef55SBarry Smith     sum = tmp[i];
2298c37ef55SBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
2308c37ef55SBarry Smith     x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1];
2318c37ef55SBarry Smith   }
2328c37ef55SBarry Smith 
2338c37ef55SBarry Smith   FREE(tmp);
2348c37ef55SBarry Smith   return 0;
2358c37ef55SBarry Smith }
236da3a660dSBarry Smith int MatiAIJSolveAdd(Mat mat,Vec bb, Vec yy, Vec xx)
237da3a660dSBarry Smith {
238da3a660dSBarry Smith   Matiaij *aij = (Matiaij *) mat->data;
239da3a660dSBarry Smith   IS      iscol = mat->col, isrow = mat->row;
2406abc6512SBarry Smith   int     *r,*c, ierr, i,  n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
241da3a660dSBarry Smith   int     nz;
242da3a660dSBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, sum, *v;
243da3a660dSBarry Smith 
244da3a660dSBarry Smith   if (yy != xx) {ierr = VecCopy(yy,xx); CHKERR(ierr);}
245da3a660dSBarry Smith 
2466abc6512SBarry Smith   if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0);
2476abc6512SBarry Smith   if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0);
248da3a660dSBarry Smith   tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp);
249da3a660dSBarry Smith 
2506abc6512SBarry Smith   if ((ierr = ISGetIndices(isrow,&r))) SETERR(ierr,0);
2516abc6512SBarry Smith   if ((ierr = ISGetIndices(iscol,&c))) SETERR(ierr,0); c = c + (n-1);
252da3a660dSBarry Smith 
253da3a660dSBarry Smith   /* forward solve the lower triangular */
254da3a660dSBarry Smith   tmp[0] = b[*r++];
255da3a660dSBarry Smith   for ( i=1; i<n; i++ ) {
256da3a660dSBarry Smith     v   = aa + ai[i] - 1;
257da3a660dSBarry Smith     vi  = aj + ai[i] - 1;
258da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
259da3a660dSBarry Smith     sum = b[*r++];
260da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
261da3a660dSBarry Smith     tmp[i] = sum;
262da3a660dSBarry Smith   }
263da3a660dSBarry Smith 
264da3a660dSBarry Smith   /* backward solve the upper triangular */
265da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
266da3a660dSBarry Smith     v   = aa + aij->diag[i];
267da3a660dSBarry Smith     vi  = aj + aij->diag[i];
268da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
269da3a660dSBarry Smith     sum = tmp[i];
270da3a660dSBarry Smith     while (nz--) sum -= *v++ * tmp[*vi++ - 1];
271da3a660dSBarry Smith     tmp[i] = sum*aa[aij->diag[i]-1];
272da3a660dSBarry Smith     x[*c--] += tmp[i];
273da3a660dSBarry Smith   }
274da3a660dSBarry Smith 
275da3a660dSBarry Smith   FREE(tmp);
276da3a660dSBarry Smith   return 0;
277da3a660dSBarry Smith }
278da3a660dSBarry Smith /* -------------------------------------------------------------------*/
279da3a660dSBarry Smith int MatiAIJSolveTrans(Mat mat,Vec bb, Vec xx)
280da3a660dSBarry Smith {
281da3a660dSBarry Smith   Matiaij *aij = (Matiaij *) mat->data;
282da3a660dSBarry Smith   IS      iscol = mat->col, isrow = mat->row, invisrow,inviscol;
2836abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
284da3a660dSBarry Smith   int     nz;
2856abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
286da3a660dSBarry Smith 
2876abc6512SBarry Smith   if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0);
2886abc6512SBarry Smith   if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0);
289da3a660dSBarry Smith   tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp);
290da3a660dSBarry Smith 
291da3a660dSBarry Smith   /* invert the permutations */
292da3a660dSBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr);
293da3a660dSBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr);
294da3a660dSBarry Smith 
295da3a660dSBarry Smith 
2966abc6512SBarry Smith   if ((ierr = ISGetIndices(invisrow,&r))) SETERR(ierr,0);
2976abc6512SBarry Smith   if ((ierr = ISGetIndices(inviscol,&c))) SETERR(ierr,0);
298da3a660dSBarry Smith 
299da3a660dSBarry Smith   /* copy the b into temp work space according to permutation */
300da3a660dSBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
301da3a660dSBarry Smith 
302da3a660dSBarry Smith   /* forward solve the U^T */
303da3a660dSBarry Smith   for ( i=0; i<n; i++ ) {
304da3a660dSBarry Smith     v   = aa + aij->diag[i] - 1;
305da3a660dSBarry Smith     vi  = aj + aij->diag[i];
306da3a660dSBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
307da3a660dSBarry Smith     tmp[i] *= *v++;
308da3a660dSBarry Smith     while (nz--) {
309da3a660dSBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
310da3a660dSBarry Smith     }
311da3a660dSBarry Smith   }
312da3a660dSBarry Smith 
313da3a660dSBarry Smith   /* backward solve the L^T */
314da3a660dSBarry Smith   for ( i=n-1; i>=0; i-- ){
315da3a660dSBarry Smith     v   = aa + aij->diag[i] - 2;
316da3a660dSBarry Smith     vi  = aj + aij->diag[i] - 2;
317da3a660dSBarry Smith     nz  = aij->diag[i] - ai[i];
318da3a660dSBarry Smith     while (nz--) {
319da3a660dSBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
320da3a660dSBarry Smith     }
321da3a660dSBarry Smith   }
322da3a660dSBarry Smith 
323da3a660dSBarry Smith   /* copy tmp into x according to permutation */
324da3a660dSBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] = tmp[i];
325da3a660dSBarry Smith 
326da3a660dSBarry Smith   ISDestroy(invisrow); ISDestroy(inviscol);
327da3a660dSBarry Smith 
328da3a660dSBarry Smith   FREE(tmp);
329da3a660dSBarry Smith   return 0;
330da3a660dSBarry Smith }
331da3a660dSBarry Smith 
3326abc6512SBarry Smith int MatiAIJSolveTransAdd(Mat mat,Vec bb, Vec zz,Vec xx)
333da3a660dSBarry Smith {
334da3a660dSBarry Smith   Matiaij *aij = (Matiaij *) mat->data;
3356abc6512SBarry Smith   IS      iscol = mat->col, isrow = mat->row, invisrow,inviscol;
3366abc6512SBarry Smith   int     *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j;
3376abc6512SBarry Smith   int     nz;
3386abc6512SBarry Smith   Scalar  *x,*b,*tmp, *aa = aij->a, *v;
3396abc6512SBarry Smith 
3406abc6512SBarry Smith   if (zz != xx) VecCopy(zz,xx);
3416abc6512SBarry Smith 
3426abc6512SBarry Smith   if ((ierr = VecGetArray(bb,&b))) SETERR(ierr,0);
3436abc6512SBarry Smith   if ((ierr = VecGetArray(xx,&x))) SETERR(ierr,0);
3446abc6512SBarry Smith   tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp);
3456abc6512SBarry Smith 
3466abc6512SBarry Smith   /* invert the permutations */
3476abc6512SBarry Smith   ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr);
3486abc6512SBarry Smith   ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr);
3496abc6512SBarry Smith 
3506abc6512SBarry Smith 
3516abc6512SBarry Smith   if ((ierr = ISGetIndices(invisrow,&r))) SETERR(ierr,0);
3526abc6512SBarry Smith   if ((ierr = ISGetIndices(inviscol,&c))) SETERR(ierr,0);
3536abc6512SBarry Smith 
3546abc6512SBarry Smith   /* copy the b into temp work space according to permutation */
3556abc6512SBarry Smith   for ( i=0; i<n; i++ ) tmp[c[i]] = b[i];
3566abc6512SBarry Smith 
3576abc6512SBarry Smith   /* forward solve the U^T */
3586abc6512SBarry Smith   for ( i=0; i<n; i++ ) {
3596abc6512SBarry Smith     v   = aa + aij->diag[i] - 1;
3606abc6512SBarry Smith     vi  = aj + aij->diag[i];
3616abc6512SBarry Smith     nz  = ai[i+1] - aij->diag[i] - 1;
3626abc6512SBarry Smith     tmp[i] *= *v++;
3636abc6512SBarry Smith     while (nz--) {
3646abc6512SBarry Smith       tmp[*vi++ - 1] -= (*v++)*tmp[i];
3656abc6512SBarry Smith     }
3666abc6512SBarry Smith   }
3676abc6512SBarry Smith 
3686abc6512SBarry Smith   /* backward solve the L^T */
3696abc6512SBarry Smith   for ( i=n-1; i>=0; i-- ){
3706abc6512SBarry Smith     v   = aa + aij->diag[i] - 2;
3716abc6512SBarry Smith     vi  = aj + aij->diag[i] - 2;
3726abc6512SBarry Smith     nz  = aij->diag[i] - ai[i];
3736abc6512SBarry Smith     while (nz--) {
3746abc6512SBarry Smith       tmp[*vi-- - 1] -= (*v--)*tmp[i];
3756abc6512SBarry Smith     }
3766abc6512SBarry Smith   }
3776abc6512SBarry Smith 
3786abc6512SBarry Smith   /* copy tmp into x according to permutation */
3796abc6512SBarry Smith   for ( i=0; i<n; i++ ) x[r[i]] += tmp[i];
3806abc6512SBarry Smith 
3816abc6512SBarry Smith   ISDestroy(invisrow); ISDestroy(inviscol);
3826abc6512SBarry Smith 
3836abc6512SBarry Smith   FREE(tmp);
3846abc6512SBarry Smith   return 0;
3856abc6512SBarry Smith 
386da3a660dSBarry Smith }
387