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