xref: /petsc/src/mat/impls/aij/seq/ij.c (revision 70f55243aafb320636e2a54ff30cab5d1e8d3d7b)
1cb512458SBarry Smith #ifndef lint
2*70f55243SBarry Smith static char vcid[] = "$Id: ij.c,v 1.17 1996/03/08 05:47:13 bsmith Exp bsmith $";
3cb512458SBarry Smith #endif
4d6dfbf8fSBarry Smith 
5*70f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
62d9e4a2aSBarry Smith 
72d9e4a2aSBarry Smith /*
8ec8511deSBarry Smith   MatToSymmetricIJ_SeqAIJ - Convert a sparse AIJ matrix to IJ format
9d5d45c9bSBarry Smith            (ignore the "A" part) Allocates the space needed. Uses only
10d5d45c9bSBarry Smith            the lower triangular part of the matrix.
112d9e4a2aSBarry Smith 
122d9e4a2aSBarry Smith     Description:
132d9e4a2aSBarry Smith     Take the data in the row-oriented sparse storage and build the
142d9e4a2aSBarry Smith     IJ data for the Matrix.  Return 0 on success, row + 1 on failure
152d9e4a2aSBarry Smith     at that row. Produces the ij for a symmetric matrix by only using
162d9e4a2aSBarry Smith     the lower triangular part of the matrix.
172d9e4a2aSBarry Smith 
182d9e4a2aSBarry Smith     Input Parameters:
192d9e4a2aSBarry Smith .   Matrix - matrix to convert
20bcd2baecSBarry Smith .   shiftin - the shift for the original matrix (0 or 1)
21bcd2baecSBarry Smith .   shiftout - the shift required for the reordering routine (0 or 1)
222d9e4a2aSBarry Smith 
232d9e4a2aSBarry Smith     Output Parameters:
242d9e4a2aSBarry Smith .   ia     - ia part of IJ representation (row information)
252d9e4a2aSBarry Smith .   ja     - ja part (column indices)
262d9e4a2aSBarry Smith 
272d9e4a2aSBarry Smith     Notes:
280452661fSBarry Smith $    Both ia and ja may be freed with PetscFree();
29464493b3SBarry Smith $    This routine is provided for ordering routines that require a
30bcd2baecSBarry Smith $    symmetric structure.  It is required since those routines call
31bcd2baecSBarry Smith $    SparsePak routines that expect a symmetric  matrix.
322d9e4a2aSBarry Smith */
33bcd2baecSBarry Smith int MatToSymmetricIJ_SeqAIJ(int n,int *ai,int *aj,int shiftin, int shiftout,
34bcd2baecSBarry Smith                             int **iia, int **jja )
352d9e4a2aSBarry Smith {
3635aab85fSBarry Smith   int *work,*ia,*ja,*j,i, nz, row, col;
372d9e4a2aSBarry Smith 
382d9e4a2aSBarry Smith   /* allocate space for row pointers */
390452661fSBarry Smith   *iia = ia = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ia);
40cddf8d76SBarry Smith   PetscMemzero(ia,(n+1)*sizeof(int));
410452661fSBarry Smith   work = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(work);
422d9e4a2aSBarry Smith 
432d9e4a2aSBarry Smith   /* determine the number of columns in each row */
44bcd2baecSBarry Smith   ia[0] = -shiftout;
452d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
463439631bSBarry Smith     nz = ai[row+1] - ai[row];
473439631bSBarry Smith     j  = aj + ai[row];
482d9e4a2aSBarry Smith     while (nz--) {
49bcd2baecSBarry Smith        col = *j++ + shiftin;
50416022c9SBarry Smith        if (col > row) { break;}
512d9e4a2aSBarry Smith        if (col != row) ia[row+1]++;
522d9e4a2aSBarry Smith        ia[col+1]++;
532d9e4a2aSBarry Smith     }
542d9e4a2aSBarry Smith   }
552d9e4a2aSBarry Smith 
56bcd2baecSBarry Smith   /* shiftin ia[i] to point to next row */
572d9e4a2aSBarry Smith   for ( i=1; i<n+1; i++ ) {
582d9e4a2aSBarry Smith     row       = ia[i-1];
592d9e4a2aSBarry Smith     ia[i]     += row;
602d9e4a2aSBarry Smith     work[i-1] = row - 1;
612d9e4a2aSBarry Smith   }
622d9e4a2aSBarry Smith 
632d9e4a2aSBarry Smith   /* allocate space for column pointers */
64bcd2baecSBarry Smith   nz = ia[n] + (!shiftin);
650452661fSBarry Smith   *jja = ja = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(ja);
662d9e4a2aSBarry Smith 
672d9e4a2aSBarry Smith   /* loop over lower triangular part putting into ja */
682d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
693439631bSBarry Smith     nz = ai[row+1] - ai[row];
703439631bSBarry Smith     j  = aj + ai[row];
712d9e4a2aSBarry Smith     while (nz--) {
72bcd2baecSBarry Smith       col = *j++ + shiftin;
73416022c9SBarry Smith       if (col > row) { break;}
74bcd2baecSBarry Smith       if (col != row) {ja[work[col]++] = row - shiftout; }
75bcd2baecSBarry Smith       ja[work[row]++] = col - shiftout;
762d9e4a2aSBarry Smith     }
772d9e4a2aSBarry Smith   }
780452661fSBarry Smith   PetscFree(work);
792d9e4a2aSBarry Smith   return 0;
802d9e4a2aSBarry Smith }
812d9e4a2aSBarry Smith 
82d5d45c9bSBarry Smith 
83d5d45c9bSBarry Smith 
84