xref: /petsc/src/mat/impls/aij/seq/ij.c (revision 63c41f6a1560bbb6cf7ee09697a660f5641fb9ab)
1 #ifndef lint
2 static char vcid[] = "$Id: ij.c,v 1.24 1996/10/16 21:08:02 balay Exp balay $";
3 #endif
4 
5 #include "src/mat/impls/aij/seq/aij.h"
6 
7 #undef __FUNCTION__
8 #define __FUNCTION__ "MatToSymmetricIJ_SeqAIJ"
9 /*
10   MatToSymmetricIJ_SeqAIJ - Convert a (generally nonsymmetric) sparse AIJ matrix
11            to IJ format (ignore the "A" part) Allocates the space needed. Uses only
12            the lower triangular part of the matrix.
13 
14     Description:
15     Take the data in the row-oriented sparse storage and build the
16     IJ data for the Matrix.  Return 0 on success, row + 1 on failure
17     at that row. Produces the ij for a symmetric matrix by only using
18     the lower triangular part of the matrix.
19 
20     Input Parameters:
21 .   Matrix - matrix to convert
22 .   shiftin - the shift for the original matrix (0 or 1)
23 .   shiftout - the shift required for the reordering routine (0 or 1)
24 
25     Output Parameters:
26 .   ia     - ia part of IJ representation (row information)
27 .   ja     - ja part (column indices)
28 
29     Notes:
30 $    Both ia and ja may be freed with PetscFree();
31 $    This routine is provided for ordering routines that require a
32 $    symmetric structure.  It is required since those routines call
33 $    SparsePak routines that expect a symmetric  matrix.
34 */
35 int MatToSymmetricIJ_SeqAIJ(int m,int *ai,int *aj,int shiftin, int shiftout,
36                             int **iia, int **jja )
37 {
38   int *work,*ia,*ja,*j,i, nz, row, col;
39 
40   /* allocate space for row pointers */
41   *iia = ia = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(ia);
42   PetscMemzero(ia,(m+1)*sizeof(int));
43   work = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(work);
44 
45   /* determine the number of columns in each row */
46   ia[0] = shiftout;
47   for (row = 0; row < m; row++) {
48     nz = ai[row+1] - ai[row];
49     j  = aj + ai[row] + shiftin;
50     while (nz--) {
51        col = *j++ + shiftin;
52        if (col > row) { break;}
53        if (col != row) ia[row+1]++;
54        ia[col+1]++;
55     }
56   }
57 
58   /* shiftin ia[i] to point to next row */
59   for ( i=1; i<m+1; i++ ) {
60     row       = ia[i-1];
61     ia[i]     += row;
62     work[i-1] = row - shiftout;
63   }
64 
65   /* allocate space for column pointers */
66   nz = ia[m] + (!shiftin);
67   *jja = ja = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(ja);
68 
69   /* loop over lower triangular part putting into ja */
70   for (row = 0; row < m; row++) {
71     nz = ai[row+1] - ai[row];
72     j  = aj + ai[row] + shiftin;
73     while (nz--) {
74       col = *j++ + shiftin;
75       if (col > row) { break;}
76       if (col != row) {ja[work[col]++] = row + shiftout; }
77       ja[work[row]++] = col + shiftout;
78     }
79   }
80   PetscFree(work);
81   return 0;
82 }
83 
84 
85 
86