xref: /petsc/src/mat/impls/aij/seq/ij.c (revision d5d45c9bad5d66633d6ef030bee5689b038db93c)
1cb512458SBarry Smith #ifndef lint
2*d5d45c9bSBarry Smith static char vcid[] = "$Id: ij.c,v 1.13 1995/11/01 23:17:58 bsmith Exp bsmith $";
3cb512458SBarry Smith #endif
4d6dfbf8fSBarry Smith 
52d9e4a2aSBarry Smith #include "aij.h"
62d9e4a2aSBarry Smith 
72d9e4a2aSBarry Smith /*
8ec8511deSBarry Smith   MatToSymmetricIJ_SeqAIJ - Convert a sparse AIJ matrix to IJ format
9*d5d45c9bSBarry Smith            (ignore the "A" part) Allocates the space needed. Uses only
10*d5d45c9bSBarry 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
202d9e4a2aSBarry Smith 
212d9e4a2aSBarry Smith     Output Parameters:
222d9e4a2aSBarry Smith .   ia     - ia part of IJ representation (row information)
232d9e4a2aSBarry Smith .   ja     - ja part (column indices)
242d9e4a2aSBarry Smith 
252d9e4a2aSBarry Smith     Notes:
260452661fSBarry Smith $    Both ia and ja may be freed with PetscFree();
27464493b3SBarry Smith $    This routine is provided for ordering routines that require a
28464493b3SBarry Smith $    symmetric structure.  It is used in SpOrder (and derivatives) since
29464493b3SBarry Smith $    those routines call SparsePak routines that expect a symmetric
30464493b3SBarry Smith $    matrix.
312d9e4a2aSBarry Smith */
32416022c9SBarry Smith int MatToSymmetricIJ_SeqAIJ( Mat_SeqAIJ *A, int **iia, int **jja )
332d9e4a2aSBarry Smith {
34416022c9SBarry Smith   int *work,*ia,*ja,*j,i, nz, n = A->m, row, wr, col, shift = A->indexshift;
352d9e4a2aSBarry Smith 
362d9e4a2aSBarry Smith   /* allocate space for row pointers */
370452661fSBarry Smith   *iia = ia = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ia);
38cddf8d76SBarry Smith   PetscMemzero(ia,(n+1)*sizeof(int));
390452661fSBarry Smith   work = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(work);
402d9e4a2aSBarry Smith 
412d9e4a2aSBarry Smith   /* determine the number of columns in each row */
422d9e4a2aSBarry Smith   ia[0] = 1;
432d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
44416022c9SBarry Smith     nz = A->i[row+1] - A->i[row];
45416022c9SBarry Smith     j  = A->j + A->i[row] + shift;
462d9e4a2aSBarry Smith     while (nz--) {
47dbb450caSBarry Smith        col = *j++ + shift;
48416022c9SBarry Smith        if ( col > row ) { break;}
492d9e4a2aSBarry Smith        if (col != row) ia[row+1]++;
502d9e4a2aSBarry Smith        ia[col+1]++;
512d9e4a2aSBarry Smith     }
522d9e4a2aSBarry Smith   }
532d9e4a2aSBarry Smith 
542d9e4a2aSBarry Smith   /* shift ia[i] to point to next row */
552d9e4a2aSBarry Smith   for ( i=1; i<n+1; i++ ) {
562d9e4a2aSBarry Smith     row       = ia[i-1];
572d9e4a2aSBarry Smith     ia[i]     += row;
582d9e4a2aSBarry Smith     work[i-1] = row - 1;
592d9e4a2aSBarry Smith   }
602d9e4a2aSBarry Smith 
612d9e4a2aSBarry Smith   /* allocate space for column pointers */
62dbb450caSBarry Smith   nz = ia[n] + (!shift);
630452661fSBarry Smith   *jja = ja = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(ja);
642d9e4a2aSBarry Smith 
652d9e4a2aSBarry Smith   /* loop over lower triangular part putting into ja */
662d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
67416022c9SBarry Smith     nz = A->i[row+1] - A->i[row];
68416022c9SBarry Smith     j  = A->j + A->i[row] + shift;
692d9e4a2aSBarry Smith     while (nz--) {
70dbb450caSBarry Smith       col = *j++ + shift;
71416022c9SBarry Smith       if ( col > row ) { break;}
72416022c9SBarry Smith       if (col != row) {wr = work[col]; work[col] = wr + 1; ja[wr] = row + 1; }
732d9e4a2aSBarry Smith       wr = work[row]; work[row] = wr + 1;
742d9e4a2aSBarry Smith       ja[wr] = col + 1;
752d9e4a2aSBarry Smith     }
762d9e4a2aSBarry Smith   }
770452661fSBarry Smith   PetscFree(work);
782d9e4a2aSBarry Smith   return 0;
792d9e4a2aSBarry Smith }
802d9e4a2aSBarry Smith 
81*d5d45c9bSBarry Smith 
82*d5d45c9bSBarry Smith 
83