1 /* ========================================================================== */
2 /* === colamd and symamd example ============================================ */
3 /* ========================================================================== */
4 
5 /* COLAMD / SYMAMD example
6 
7     colamd example of use, to order the columns of a 5-by-4 matrix with
8     11 nonzero entries in the following nonzero pattern, with default knobs.
9 
10        x 0 x 0
11        x 0 x x
12        0 x x 0
13        0 0 x x
14        x x 0 0
15 
16     symamd example of use, to order the rows and columns of a 5-by-5
17     matrix with 13 nonzero entries in the following nonzero pattern,
18     with default knobs.
19 
20        x x 0 0 0
21        x x x x 0
22        0 x x 0 0
23        0 x 0 x x
24        0 0 0 x x
25 
26     (where x denotes a nonzero value).
27 */
28 
29 /* ========================================================================== */
30 
31 #include <stdio.h>
32 #include "colamd.h"
33 
34 #define A_NNZ 11
35 #define A_NROW 5
36 #define A_NCOL 4
37 #define ALEN 150
38 
39 #define B_NNZ 4
40 #define B_N 5
41 
main(void)42 int main (void)
43 {
44 
45     /* ====================================================================== */
46     /* input matrix A definition */
47     /* ====================================================================== */
48 
49     int A [ALEN] = {
50 
51     	0, 1, 4,		/* row indices of nonzeros in column 0 */
52 	2, 4,			/* row indices of nonzeros in column 1 */
53 	0, 1, 2, 3,		/* row indices of nonzeros in column 2 */
54 	1, 3} ;			/* row indices of nonzeros in column 3 */
55 
56     int p [ ] = {
57 
58     	0,			/* column 0 is in A [0..2] */
59 	3,			/* column 1 is in A [3..4] */
60 	5,			/* column 2 is in A [5..8] */
61 	9,			/* column 3 is in A [9..10] */
62 	A_NNZ} ;		/* number of nonzeros in A */
63 
64     /* ====================================================================== */
65     /* input matrix B definition */
66     /* ====================================================================== */
67 
68     int B [ ] = {		/* Note: only strictly lower triangular part */
69     				/* is included, since symamd ignores the */
70 				/* diagonal and upper triangular part of B. */
71 
72     	1,			/* row indices of nonzeros in column 0 */
73     	2, 3,			/* row indices of nonzeros in column 1 */
74     				/* row indices of nonzeros in column 2 (none) */
75     	4			/* row indices of nonzeros in column 3 */
76     	} ;			/* row indices of nonzeros in column 4 (none) */
77 
78     int q [ ] = {
79 
80     	0,			/* column 0 is in B [0] */
81 	1,			/* column 1 is in B [1..2] */
82 	3,			/* column 2 is empty */
83 	3,			/* column 3 is in B [3] */
84 	4,			/* column 4 is empty */
85 	B_NNZ} ;		/* number of nonzeros in strictly lower B */
86 
87     /* ====================================================================== */
88     /* other variable definitions */
89     /* ====================================================================== */
90 
91     int perm [B_N+1] ;		/* note the size is N+1 */
92     int stats [COLAMD_STATS] ;	/* for colamd and symamd output statistics */
93 
94     int row, col, pp, length, ok ;
95 
96     /* ====================================================================== */
97     /* dump the input matrix A */
98     /* ====================================================================== */
99 
100     printf ("colamd %d-by-%d input matrix:\n", A_NROW, A_NCOL) ;
101     for (col = 0 ; col < A_NCOL ; col++)
102     {
103 	length = p [col+1] - p [col] ;
104     	printf ("Column %d, with %d entries:\n", col, length) ;
105 	for (pp = p [col] ; pp < p [col+1] ; pp++)
106 	{
107 	    row = A [pp] ;
108 	    printf ("    row %d\n", row) ;
109 	}
110     }
111 
112     /* ====================================================================== */
113     /* order the matrix.  Note that this destroys A and overwrites p */
114     /* ====================================================================== */
115 
116     ok = colamd (A_NROW, A_NCOL, ALEN, A, p, (double *) NULL, stats) ;
117     colamd_report (stats) ;
118 
119     if (!ok)
120     {
121 	printf ("colamd error!\n") ;
122 	exit (1) ;
123     }
124 
125     /* ====================================================================== */
126     /* print the column ordering */
127     /* ====================================================================== */
128 
129     printf ("colamd column ordering:\n") ;
130     printf ("1st column: %d\n", p [0]) ;
131     printf ("2nd column: %d\n", p [1]) ;
132     printf ("3rd column: %d\n", p [2]) ;
133     printf ("4th column: %d\n", p [3]) ;
134 
135     /* ====================================================================== */
136     /* dump the strictly lower triangular part of symmetric input matrix B */
137     /* ====================================================================== */
138 
139     printf ("\n\nsymamd %d-by-%d input matrix:\n", B_N, B_N) ;
140     printf ("Entries in strictly lower triangular part:\n") ;
141     for (col = 0 ; col < B_N ; col++)
142     {
143 	length = q [col+1] - q [col] ;
144     	printf ("Column %d, with %d entries:\n", col, length) ;
145 	for (pp = q [col] ; pp < q [col+1] ; pp++)
146 	{
147 	    row = B [pp] ;
148 	    printf ("    row %d\n", row) ;
149 	}
150     }
151 
152     /* ====================================================================== */
153     /* order the matrix B.  Note that this does not modify B or q. */
154     /* ====================================================================== */
155 
156     ok = symamd (B_N, B, q, perm, (double *) NULL, stats, &calloc, &free) ;
157     symamd_report (stats) ;
158 
159     if (!ok)
160     {
161 	printf ("symamd error!\n") ;
162 	exit (1) ;
163     }
164 
165     /* ====================================================================== */
166     /* print the symmetric ordering */
167     /* ====================================================================== */
168 
169     printf ("symamd column ordering:\n") ;
170     printf ("1st row/column: %d\n", perm [0]) ;
171     printf ("2nd row/column: %d\n", perm [1]) ;
172     printf ("3rd row/column: %d\n", perm [2]) ;
173     printf ("4th row/column: %d\n", perm [3]) ;
174     printf ("5th row/column: %d\n", perm [4]) ;
175 
176     exit (0) ;
177 }
178 
179