5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 Copyright (C) 1998-2004 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
5307
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
5164
|
20 |
|
21 */ |
|
22 |
|
23 // This is the octave interface to colamd, which bore the copyright given |
|
24 // in the help of the functions. |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include <cstdlib> |
|
31 |
|
32 #include <string> |
|
33 #include <vector> |
|
34 |
|
35 #include "ov.h" |
|
36 #include "defun-dld.h" |
|
37 #include "pager.h" |
|
38 #include "ov-re-mat.h" |
|
39 |
|
40 #include "ov-re-sparse.h" |
|
41 #include "ov-cx-sparse.h" |
|
42 |
5451
|
43 #include "oct-sparse.h" |
5164
|
44 |
5440
|
45 #ifdef IDX_TYPE_LONG |
|
46 #define COLAMD_NAME(name) colamd_l ## name |
|
47 #define SYMAMD_NAME(name) symamd_l ## name |
|
48 #else |
|
49 #define COLAMD_NAME(name) colamd ## name |
|
50 #define SYMAMD_NAME(name) symamd ## name |
|
51 #endif |
|
52 |
5164
|
53 // The symmetric column elimination tree code take from the Davis LDL code. |
|
54 // Copyright given elsewhere in this file. |
|
55 static |
5440
|
56 void symetree (const octave_idx_type *ridx, const octave_idx_type *cidx, |
|
57 octave_idx_type *Parent, octave_idx_type *P, octave_idx_type n) |
5164
|
58 { |
5440
|
59 OCTAVE_LOCAL_BUFFER (octave_idx_type, Flag, n); |
|
60 OCTAVE_LOCAL_BUFFER (octave_idx_type, Pinv, (P ? n : 0)); |
5164
|
61 if (P) |
|
62 // If P is present then compute Pinv, the inverse of P |
5440
|
63 for (octave_idx_type k = 0 ; k < n ; k++) |
5164
|
64 Pinv [P [k]] = k ; |
|
65 |
5440
|
66 for (octave_idx_type k = 0 ; k < n ; k++) |
5164
|
67 { |
|
68 // L(k,:) pattern: all nodes reachable in etree from nz in A(0:k-1,k) |
|
69 Parent [k] = n ; // parent of k is not yet known |
|
70 Flag [k] = k ; // mark node k as visited |
5440
|
71 octave_idx_type kk = (P) ? (P [k]) : (k) ; // kth original, or permuted, column |
|
72 octave_idx_type p2 = cidx [kk+1] ; |
|
73 for (octave_idx_type p = cidx [kk] ; p < p2 ; p++) |
5164
|
74 { |
|
75 // A (i,k) is nonzero (original or permuted A) |
5440
|
76 octave_idx_type i = (Pinv) ? (Pinv [ridx [p]]) : (ridx [p]) ; |
5164
|
77 if (i < k) |
|
78 { |
|
79 // follow path from i to root of etree, stop at flagged node |
|
80 for ( ; Flag [i] != k ; i = Parent [i]) |
|
81 { |
|
82 // find parent of i if not yet determined |
|
83 if (Parent [i] == n) |
|
84 Parent [i] = k ; |
|
85 Flag [i] = k ; // mark i as visited |
|
86 } |
|
87 } |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 // The elimination tree post-ordering code below is taken from SuperLU |
|
93 static inline |
5440
|
94 octave_idx_type make_set (octave_idx_type i, octave_idx_type *pp) |
5164
|
95 { |
|
96 pp[i] = i; |
|
97 return i; |
|
98 } |
|
99 |
|
100 static inline |
5440
|
101 octave_idx_type link (octave_idx_type s, octave_idx_type t, octave_idx_type *pp) |
5164
|
102 { |
|
103 pp[s] = t; |
|
104 return t; |
|
105 } |
|
106 |
|
107 static inline |
5440
|
108 octave_idx_type find (octave_idx_type i, octave_idx_type *pp) |
5164
|
109 { |
5440
|
110 register octave_idx_type p, gp; |
5164
|
111 |
|
112 p = pp[i]; |
|
113 gp = pp[p]; |
|
114 while (gp != p) { |
|
115 pp[i] = gp; |
|
116 i = gp; |
|
117 p = pp[i]; |
|
118 gp = pp[p]; |
|
119 } |
|
120 return (p); |
|
121 } |
|
122 |
|
123 static |
5440
|
124 octave_idx_type etdfs (octave_idx_type v, octave_idx_type *first_kid, |
|
125 octave_idx_type *next_kid, octave_idx_type *post, |
|
126 octave_idx_type postnum) |
5164
|
127 { |
5440
|
128 for (octave_idx_type w = first_kid[v]; w != -1; w = next_kid[w]) { |
5164
|
129 postnum = etdfs (w, first_kid, next_kid, post, postnum); |
|
130 } |
|
131 post[postnum++] = v; |
|
132 |
|
133 return postnum; |
|
134 } |
|
135 |
|
136 static |
5440
|
137 void TreePostorder(octave_idx_type n, octave_idx_type *parent, octave_idx_type *post) |
5164
|
138 { |
|
139 // Allocate storage for working arrays and results |
5440
|
140 OCTAVE_LOCAL_BUFFER (octave_idx_type, first_kid, n+1); |
|
141 OCTAVE_LOCAL_BUFFER (octave_idx_type, next_kid, n+1); |
5164
|
142 |
|
143 // Set up structure describing children |
5440
|
144 for (octave_idx_type v = 0; v <= n; first_kid[v++] = -1); |
|
145 for (octave_idx_type v = n-1; v >= 0; v--) |
5164
|
146 { |
5440
|
147 octave_idx_type dad = parent[v]; |
5164
|
148 next_kid[v] = first_kid[dad]; |
|
149 first_kid[dad] = v; |
|
150 } |
|
151 |
|
152 // Depth-first search from dummy root vertex #n |
|
153 etdfs (n, first_kid, next_kid, post, 0); |
|
154 } |
|
155 |
|
156 static |
5440
|
157 void coletree (const octave_idx_type *ridx, const octave_idx_type *colbeg, |
|
158 octave_idx_type *colend, octave_idx_type *parent, |
|
159 octave_idx_type nr, octave_idx_type nc) |
5164
|
160 { |
5440
|
161 OCTAVE_LOCAL_BUFFER (octave_idx_type, root, nc); |
|
162 OCTAVE_LOCAL_BUFFER (octave_idx_type, pp, nc); |
|
163 OCTAVE_LOCAL_BUFFER (octave_idx_type, firstcol, nr); |
5164
|
164 |
|
165 // Compute firstcol[row] = first nonzero column in row |
5440
|
166 for (octave_idx_type row = 0; row < nr; firstcol[row++] = nc); |
|
167 for (octave_idx_type col = 0; col < nc; col++) |
|
168 for (octave_idx_type p = colbeg[col]; p < colend[col]; p++) |
5164
|
169 { |
5440
|
170 octave_idx_type row = ridx[p]; |
5164
|
171 if (firstcol[row] > col) |
|
172 firstcol[row] = col; |
|
173 } |
|
174 |
|
175 // Compute etree by Liu's algorithm for symmetric matrices, |
|
176 // except use (firstcol[r],c) in place of an edge (r,c) of A. |
|
177 // Thus each row clique in A'*A is replaced by a star |
|
178 // centered at its first vertex, which has the same fill. |
5440
|
179 for (octave_idx_type col = 0; col < nc; col++) |
5164
|
180 { |
5440
|
181 octave_idx_type cset = make_set (col, pp); |
5164
|
182 root[cset] = col; |
|
183 parent[col] = nc; |
5440
|
184 for (octave_idx_type p = colbeg[col]; p < colend[col]; p++) |
5164
|
185 { |
5440
|
186 octave_idx_type row = firstcol[ridx[p]]; |
5164
|
187 if (row >= col) |
|
188 continue; |
5440
|
189 octave_idx_type rset = find (row, pp); |
|
190 octave_idx_type rroot = root[rset]; |
5164
|
191 if (rroot != col) |
|
192 { |
|
193 parent[rroot] = col; |
|
194 cset = link (cset, rset, pp); |
|
195 root[cset] = col; |
|
196 } |
|
197 } |
|
198 } |
|
199 } |
|
200 |
|
201 DEFUN_DLD (colamd, args, nargout, |
|
202 "-*- texinfo -*-\n\ |
|
203 @deftypefn {Loadable Function} {@var{p} =} colamd (@var{s})\n\ |
|
204 @deftypefnx {Loadable Function} {@var{p} =} colamd (@var{s}, @var{knobs})\n\ |
|
205 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} colamd (@var{s})\n\ |
|
206 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} colamd (@var{s}, @var{knobs})\n\ |
|
207 \n\ |
|
208 Column approximate minimum degree permutation. @code{@var{p} = colamd\n\ |
|
209 (@var{s})} returns the column approximate minimum degree permutation\n\ |
|
210 vector for the sparse matrix @var{s}. For a non-symmetric matrix @var{s},\n\ |
|
211 @code{@var{s} (:,@var{p})} tends to have sparser LU factors than @var{s}.\n\ |
|
212 The Cholesky factorization of @code{@var{s} (:,@var{p})' * @var{s}\n\ |
|
213 (:,@var{p})} also tends to be sparser than that of @code{@var{s}' *\n\ |
|
214 @var{s}}.\n\ |
|
215 \n\ |
5440
|
216 @var{knobs} is an optional one- to three-element input vector. If @var{s} is\n\ |
|
217 m-by-n, then rows with more than @code{max(16,@var{knobs}(1)*sqrt(n))} entries\n\ |
|
218 are ignored. Columns with more than @code{max(16,knobs(2)*sqrt(min(m,n)))}\n\ |
|
219 entries are removed prior to ordering, and ordered last in the output\n\ |
|
220 permutation @var{p}. Only completely dense rows or columns are removed\n\ |
|
221 if @code{@var{knobs} (1)} and @code{@var{knobs} (2)} are < 0, respectively.\n\ |
|
222 If @code{@var{knobs} (3)} is nonzero, @var{stats} and @var{knobs} are\n\ |
|
223 printed. The default is @code{@var{knobs} = [10 10 0]}. Note that\n\ |
|
224 @var{knobs} differs from earlier versions of colamd\n\ |
5164
|
225 \n\ |
|
226 @var{stats} is an optional 20-element output vector that provides data\n\ |
|
227 about the ordering and the validity of the input matrix @var{s}. Ordering\n\ |
|
228 statistics are in @code{@var{stats} (1:3)}. @code{@var{stats} (1)} and\n\ |
|
229 @code{@var{stats} (2)} are the number of dense or empty rows and columns\n\ |
|
230 ignored by COLAMD and @code{@var{stats} (3)} is the number of garbage\n\ |
|
231 collections performed on the internal data structure used by COLAMD\n\ |
|
232 (roughly of size @code{2.2 * nnz(@var{s}) + 4 * @var{m} + 7 * @var{n}}\n\ |
|
233 integers).\n\ |
|
234 \n\ |
|
235 Octave built-in functions are intended to generate valid sparse matrices,\n\ |
|
236 with no duplicate entries, with ascending row indices of the nonzeros\n\ |
|
237 in each column, with a non-negative number of entries in each column (!)\n\ |
|
238 and so on. If a matrix is invalid, then COLAMD may or may not be able\n\ |
|
239 to continue. If there are duplicate entries (a row index appears two or\n\ |
|
240 more times in the same column) or if the row indices in a column are out\n\ |
|
241 of order, then COLAMD can correct these errors by ignoring the duplicate\n\ |
|
242 entries and sorting each column of its internal copy of the matrix\n\ |
|
243 @var{s} (the input matrix @var{s} is not repaired, however). If a matrix\n\ |
|
244 is invalid in other ways then COLAMD cannot continue, an error message is\n\ |
|
245 printed, and no output arguments (@var{p} or @var{stats}) are returned.\n\ |
|
246 COLAMD is thus a simple way to check a sparse matrix to see if it's\n\ |
|
247 valid.\n\ |
|
248 \n\ |
|
249 @code{@var{stats} (4:7)} provide information if COLAMD was able to\n\ |
|
250 continue. The matrix is OK if @code{@var{stats} (4)} is zero, or 1 if\n\ |
|
251 invalid. @code{@var{stats} (5)} is the rightmost column index that is\n\ |
|
252 unsorted or contains duplicate entries, or zero if no such column exists.\n\ |
|
253 @code{@var{stats} (6)} is the last seen duplicate or out-of-order row\n\ |
|
254 index in the column index given by @code{@var{stats} (5)}, or zero if no\n\ |
|
255 such row index exists. @code{@var{stats} (7)} is the number of duplicate\n\ |
|
256 or out-of-order row indices. @code{@var{stats} (8:20)} is always zero in\n\ |
|
257 the current version of COLAMD (reserved for future use).\n\ |
|
258 \n\ |
|
259 The ordering is followed by a column elimination tree post-ordering.\n\ |
|
260 \n\ |
|
261 The authors of the code itself are Stefan I. Larimore and Timothy A.\n\ |
|
262 Davis (davis@@cise.ufl.edu), University of Florida. The algorithm was\n\ |
|
263 developed in collaboration with John Gilbert, Xerox PARC, and Esmond\n\ |
|
264 Ng, Oak Ridge National Laboratory. (see\n\ |
|
265 @url{http://www.cise.ufl.edu/research/sparse/colamd})\n\ |
|
266 @end deftypefn\n\ |
|
267 @seealso{colperm, symamd}") |
|
268 { |
|
269 octave_value_list retval; |
5451
|
270 |
|
271 #ifdef HAVE_COLAMD |
|
272 |
5164
|
273 int nargin = args.length (); |
|
274 int spumoni = 0; |
|
275 |
|
276 if (nargout < 0 || nargout > 2 || nargin < 0 || nargin > 2) |
5451
|
277 print_usage ("colamd"); |
5164
|
278 else |
|
279 { |
|
280 // Get knobs |
5440
|
281 OCTAVE_LOCAL_BUFFER (double, knobs, COLAMD_KNOBS); |
|
282 COLAMD_NAME (_set_defaults) (knobs); |
5164
|
283 |
|
284 // Check for user-passed knobs |
|
285 if (nargin == 2) |
|
286 { |
|
287 NDArray User_knobs = args(1).array_value (); |
|
288 int nel_User_knobs = User_knobs.length (); |
|
289 |
|
290 if (nel_User_knobs > 0) |
5440
|
291 knobs [COLAMD_DENSE_ROW] = User_knobs (0); |
5164
|
292 if (nel_User_knobs > 1) |
5440
|
293 knobs [COLAMD_DENSE_COL] = User_knobs (1) ; |
5164
|
294 if (nel_User_knobs > 2) |
|
295 spumoni = (int) User_knobs (2); |
5440
|
296 |
|
297 // print knob settings if spumoni is set |
|
298 if (spumoni) |
|
299 { |
|
300 |
|
301 octave_stdout << "\ncolamd version " << COLAMD_MAIN_VERSION << "." |
|
302 << COLAMD_SUB_VERSION << ", " << COLAMD_DATE << ":\n"; |
5164
|
303 |
5440
|
304 if (knobs [COLAMD_DENSE_ROW] >= 0) |
|
305 octave_stdout << "knobs(1): " << User_knobs (0) |
|
306 << ", rows with > max(16," |
|
307 << knobs [COLAMD_DENSE_ROW] << "*sqrt(size(A,2)))" |
|
308 << " entries removed\n"; |
|
309 else |
|
310 octave_stdout << "knobs(1): " << User_knobs (0) |
|
311 << ", only completely dense rows removed\n"; |
|
312 |
|
313 if (knobs [COLAMD_DENSE_COL] >= 0) |
|
314 octave_stdout << "knobs(2): " << User_knobs (1) |
|
315 << ", cols with > max(16," |
|
316 << knobs [COLAMD_DENSE_COL] << "*sqrt(size(A)))" |
|
317 << " entries removed\n"; |
|
318 else |
|
319 octave_stdout << "knobs(2): " << User_knobs (1) |
|
320 << ", only completely dense columns removed\n"; |
|
321 |
|
322 octave_stdout << "knobs(3): " << User_knobs (2) |
|
323 << ", statistics and knobs printed\n"; |
|
324 |
|
325 } |
5164
|
326 } |
|
327 |
5440
|
328 octave_idx_type n_row, n_col, nnz; |
|
329 octave_idx_type *ridx, *cidx; |
5164
|
330 SparseComplexMatrix scm; |
|
331 SparseMatrix sm; |
|
332 |
5631
|
333 if (args(0).is_sparse_type ()) |
5164
|
334 { |
|
335 if (args(0).is_complex_type ()) |
|
336 { |
|
337 scm = args(0). sparse_complex_matrix_value (); |
|
338 n_row = scm.rows (); |
|
339 n_col = scm.cols (); |
5604
|
340 nnz = scm.nzmax (); |
5164
|
341 ridx = scm.xridx (); |
|
342 cidx = scm.xcidx (); |
|
343 } |
|
344 else |
|
345 { |
|
346 sm = args(0).sparse_matrix_value (); |
|
347 |
|
348 n_row = sm.rows (); |
|
349 n_col = sm.cols (); |
5604
|
350 nnz = sm.nzmax (); |
5164
|
351 ridx = sm.xridx (); |
|
352 cidx = sm.xcidx (); |
|
353 } |
|
354 } |
|
355 else |
|
356 { |
|
357 if (args(0).is_complex_type ()) |
|
358 sm = SparseMatrix (real (args(0).complex_matrix_value ())); |
|
359 else |
|
360 sm = SparseMatrix (args(0).matrix_value ()); |
|
361 |
|
362 n_row = sm.rows (); |
|
363 n_col = sm.cols (); |
5604
|
364 nnz = sm.nzmax (); |
5164
|
365 ridx = sm.xridx (); |
|
366 cidx = sm.xcidx (); |
|
367 } |
|
368 |
|
369 // Allocate workspace for colamd |
5440
|
370 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, n_col+1); |
|
371 for (octave_idx_type i = 0; i < n_col+1; i++) |
5164
|
372 p[i] = cidx [i]; |
|
373 |
5440
|
374 octave_idx_type Alen = COLAMD_NAME (_recommended) (nnz, n_row, n_col); |
|
375 OCTAVE_LOCAL_BUFFER (octave_idx_type, A, Alen); |
|
376 for (octave_idx_type i = 0; i < nnz; i++) |
5164
|
377 A[i] = ridx [i]; |
|
378 |
|
379 // Order the columns (destroys A) |
5440
|
380 OCTAVE_LOCAL_BUFFER (octave_idx_type, stats, COLAMD_STATS); |
|
381 if (! COLAMD_NAME () (n_row, n_col, Alen, A, p, knobs, stats)) |
5164
|
382 { |
5440
|
383 COLAMD_NAME (_report) (stats) ; |
5164
|
384 error ("colamd: internal error!"); |
|
385 return retval; |
|
386 } |
|
387 |
|
388 // column elimination tree post-ordering (reuse variables) |
5440
|
389 OCTAVE_LOCAL_BUFFER (octave_idx_type, colbeg, n_col + 1); |
|
390 OCTAVE_LOCAL_BUFFER (octave_idx_type, colend, n_col + 1); |
|
391 OCTAVE_LOCAL_BUFFER (octave_idx_type, etree, n_col + 1); |
5164
|
392 |
5440
|
393 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
394 { |
|
395 colbeg[i] = cidx[p[i]]; |
|
396 colend[i] = cidx[p[i]+1]; |
|
397 } |
|
398 |
|
399 coletree (ridx, colbeg, colend, etree, n_row, n_col); |
|
400 |
|
401 // Calculate the tree post-ordering |
|
402 TreePostorder (n_col, etree, colbeg); |
|
403 |
|
404 // return the permutation vector |
|
405 NDArray out_perm (dim_vector (1, n_col)); |
5440
|
406 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
407 out_perm(i) = p [colbeg [i]] + 1; |
|
408 |
|
409 retval (0) = out_perm; |
|
410 |
|
411 // print stats if spumoni > 0 |
|
412 if (spumoni > 0) |
5440
|
413 COLAMD_NAME (_report) (stats) ; |
5164
|
414 |
|
415 // Return the stats vector |
|
416 if (nargout == 2) |
|
417 { |
|
418 NDArray out_stats (dim_vector (1, COLAMD_STATS)); |
5440
|
419 for (octave_idx_type i = 0 ; i < COLAMD_STATS ; i++) |
5164
|
420 out_stats (i) = stats [i] ; |
|
421 retval(1) = out_stats; |
|
422 |
|
423 // fix stats (5) and (6), for 1-based information on |
|
424 // jumbled matrix. note that this correction doesn't |
|
425 // occur if symamd returns FALSE |
|
426 out_stats (COLAMD_INFO1) ++ ; |
|
427 out_stats (COLAMD_INFO2) ++ ; |
|
428 } |
|
429 } |
|
430 |
5451
|
431 #else |
|
432 |
|
433 error ("colamd: not available in this version of Octave"); |
|
434 |
|
435 #endif |
|
436 |
5164
|
437 return retval; |
|
438 } |
|
439 |
|
440 DEFUN_DLD (symamd, args, nargout, |
|
441 "-*- texinfo -*-\n\ |
|
442 @deftypefn {Loadable Function} {@var{p} =} symamd (@var{s})\n\ |
|
443 @deftypefnx {Loadable Function} {@var{p} =} symamd (@var{s}, @var{knobs})\n\ |
|
444 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} symamd (@var{s})\n\ |
|
445 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} symamd (@var{s}, @var{knobs})\n\ |
|
446 \n\ |
|
447 For a symmetric positive definite matrix @var{s}, returns the permutation\n\ |
|
448 vector p such that @code{@var{s} (@var{p}, @var{p})} tends to have a\n\ |
|
449 sparser Cholesky factor than @var{s}. Sometimes SYMAMD works well for\n\ |
|
450 symmetric indefinite matrices too. The matrix @var{s} is assumed to be\n\ |
|
451 symmetric; only the strictly lower triangular part is referenced. @var{s}\n\ |
|
452 must be square.\n\ |
|
453 \n\ |
5440
|
454 @var{knobs} is an optional one- to two-element input vector. If @var{s} is\n\ |
|
455 n-by-n, then rows and columns with more than\n\ |
|
456 @code{max(16,@var{knobs}(1)*sqrt(n))} entries are removed prior to ordering,\n\ |
|
457 and ordered last in the output permutation @var{p}. No rows/columns are\n\ |
|
458 removed if @code{@var{knobs}(1) < 0}. If @code{@var{knobs} (2)} is nonzero,\n\ |
|
459 @code{stats} and @var{knobs} are printed. The default is @code{@var{knobs} \n\ |
|
460 = [10 0]}. Note that @var{knobs} differs from earlier versions of symamd.\n\ |
5164
|
461 \n\ |
|
462 @var{stats} is an optional 20-element output vector that provides data\n\ |
|
463 about the ordering and the validity of the input matrix @var{s}. Ordering\n\ |
|
464 statistics are in @code{@var{stats} (1:3)}. @code{@var{stats} (1) =\n\ |
|
465 @var{stats} (2)} is the number of dense or empty rows and columns\n\ |
|
466 ignored by SYMAMD and @code{@var{stats} (3)} is the number of garbage\n\ |
|
467 collections performed on the internal data structure used by SYMAMD\n\ |
|
468 (roughly of size @code{8.4 * nnz (tril (@var{s}, -1)) + 9 * @var{n}}\n\ |
|
469 integers).\n\ |
|
470 \n\ |
|
471 Octave built-in functions are intended to generate valid sparse matrices,\n\ |
|
472 with no duplicate entries, with ascending row indices of the nonzeros\n\ |
|
473 in each column, with a non-negative number of entries in each column (!)\n\ |
|
474 and so on. If a matrix is invalid, then SYMAMD may or may not be able\n\ |
|
475 to continue. If there are duplicate entries (a row index appears two or\n\ |
|
476 more times in the same column) or if the row indices in a column are out\n\ |
|
477 of order, then SYMAMD can correct these errors by ignoring the duplicate\n\ |
|
478 entries and sorting each column of its internal copy of the matrix S (the\n\ |
|
479 input matrix S is not repaired, however). If a matrix is invalid in\n\ |
|
480 other ways then SYMAMD cannot continue, an error message is printed, and\n\ |
|
481 no output arguments (@var{p} or @var{stats}) are returned. SYMAMD is\n\ |
|
482 thus a simple way to check a sparse matrix to see if it's valid.\n\ |
|
483 \n\ |
|
484 @code{@var{stats} (4:7)} provide information if SYMAMD was able to\n\ |
|
485 continue. The matrix is OK if @code{@var{stats} (4)} is zero, or 1\n\ |
|
486 if invalid. @code{@var{stats} (5)} is the rightmost column index that\n\ |
|
487 is unsorted or contains duplicate entries, or zero if no such column\n\ |
|
488 exists. @code{@var{stats} (6)} is the last seen duplicate or out-of-order\n\ |
|
489 row index in the column index given by @code{@var{stats} (5)}, or zero\n\ |
|
490 if no such row index exists. @code{@var{stats} (7)} is the number of\n\ |
|
491 duplicate or out-of-order row indices. @code{@var{stats} (8:20)} is\n\ |
|
492 always zero in the current version of SYMAMD (reserved for future use).\n\ |
|
493 \n\ |
|
494 The ordering is followed by a column elimination tree post-ordering.\n\ |
|
495 \n\ |
|
496 \n\ |
|
497 The authors of the code itself are Stefan I. Larimore and Timothy A.\n\ |
|
498 Davis (davis@@cise.ufl.edu), University of Florida. The algorithm was\n\ |
|
499 developed in collaboration with John Gilbert, Xerox PARC, and Esmond\n\ |
|
500 Ng, Oak Ridge National Laboratory. (see\n\ |
|
501 @url{http://www.cise.ufl.edu/research/sparse/colamd})\n\ |
|
502 @end deftypefn\n\ |
|
503 @seealso{colperm, colamd}") |
|
504 { |
|
505 octave_value_list retval; |
5451
|
506 |
|
507 #ifdef HAVE_COLAMD |
|
508 |
5164
|
509 int nargin = args.length (); |
|
510 int spumoni = 0; |
|
511 |
|
512 if (nargout < 0 || nargout > 2 || nargin < 0 || nargin > 2) |
5451
|
513 print_usage ("symamd"); |
5164
|
514 else |
|
515 { |
|
516 // Get knobs |
|
517 OCTAVE_LOCAL_BUFFER (double, knobs, COLAMD_KNOBS); |
5440
|
518 COLAMD_NAME (_set_defaults) (knobs); |
5164
|
519 |
|
520 // Check for user-passed knobs |
|
521 if (nargin == 2) |
|
522 { |
|
523 NDArray User_knobs = args(1).array_value (); |
|
524 int nel_User_knobs = User_knobs.length (); |
|
525 |
|
526 if (nel_User_knobs > 0) |
|
527 knobs [COLAMD_DENSE_ROW] = User_knobs (COLAMD_DENSE_ROW); |
|
528 if (nel_User_knobs > 1) |
|
529 spumoni = (int) User_knobs (1); |
|
530 } |
|
531 |
|
532 // print knob settings if spumoni is set |
|
533 if (spumoni > 0) |
|
534 octave_stdout << "symamd: dense row/col fraction: " |
|
535 << knobs [COLAMD_DENSE_ROW] << std::endl; |
|
536 |
5440
|
537 octave_idx_type n_row, n_col, nnz; |
|
538 octave_idx_type *ridx, *cidx; |
5164
|
539 SparseMatrix sm; |
|
540 SparseComplexMatrix scm; |
|
541 |
5631
|
542 if (args(0).is_sparse_type ()) |
5164
|
543 { |
|
544 if (args(0).is_complex_type ()) |
|
545 { |
|
546 scm = args(0).sparse_complex_matrix_value (); |
|
547 n_row = scm.rows (); |
|
548 n_col = scm.cols (); |
5604
|
549 nnz = scm.nzmax (); |
5164
|
550 ridx = scm.xridx (); |
|
551 cidx = scm.xcidx (); |
|
552 } |
|
553 else |
|
554 { |
|
555 sm = args(0).sparse_matrix_value (); |
|
556 n_row = sm.rows (); |
|
557 n_col = sm.cols (); |
5604
|
558 nnz = sm.nzmax (); |
5164
|
559 ridx = sm.xridx (); |
|
560 cidx = sm.xcidx (); |
|
561 } |
|
562 } |
|
563 else |
|
564 { |
|
565 if (args(0).is_complex_type ()) |
|
566 sm = SparseMatrix (real (args(0).complex_matrix_value ())); |
|
567 else |
|
568 sm = SparseMatrix (args(0).matrix_value ()); |
|
569 |
|
570 n_row = sm.rows (); |
|
571 n_col = sm.cols (); |
5604
|
572 nnz = sm.nzmax (); |
5164
|
573 ridx = sm.xridx (); |
|
574 cidx = sm.xcidx (); |
|
575 } |
|
576 |
|
577 if (n_row != n_col) |
|
578 { |
|
579 error ("symamd: matrix must be square"); |
|
580 return retval; |
|
581 } |
|
582 |
|
583 // Allocate workspace for symamd |
5440
|
584 OCTAVE_LOCAL_BUFFER (octave_idx_type, perm, n_col+1); |
|
585 OCTAVE_LOCAL_BUFFER (octave_idx_type, stats, COLAMD_STATS); |
|
586 if (!SYMAMD_NAME () (n_col, ridx, cidx, perm, knobs, stats, &calloc, &free)) |
5164
|
587 { |
5440
|
588 SYMAMD_NAME (_report) (stats) ; |
5164
|
589 error ("symamd: internal error!") ; |
|
590 return retval; |
|
591 } |
|
592 |
|
593 // column elimination tree post-ordering |
5440
|
594 OCTAVE_LOCAL_BUFFER (octave_idx_type, etree, n_col + 1); |
5164
|
595 symetree (ridx, cidx, etree, perm, n_col); |
|
596 |
|
597 // Calculate the tree post-ordering |
5440
|
598 OCTAVE_LOCAL_BUFFER (octave_idx_type, post, n_col + 1); |
5164
|
599 TreePostorder (n_col, etree, post); |
|
600 |
|
601 // return the permutation vector |
|
602 NDArray out_perm (dim_vector (1, n_col)); |
5440
|
603 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
604 out_perm(i) = perm [post [i]] + 1; |
|
605 |
|
606 retval (0) = out_perm; |
|
607 |
|
608 // print stats if spumoni > 0 |
|
609 if (spumoni > 0) |
5440
|
610 SYMAMD_NAME (_report) (stats) ; |
5164
|
611 |
|
612 // Return the stats vector |
|
613 if (nargout == 2) |
|
614 { |
|
615 NDArray out_stats (dim_vector (1, COLAMD_STATS)); |
5440
|
616 for (octave_idx_type i = 0 ; i < COLAMD_STATS ; i++) |
5164
|
617 out_stats (i) = stats [i] ; |
|
618 retval(1) = out_stats; |
|
619 |
|
620 // fix stats (5) and (6), for 1-based information on |
|
621 // jumbled matrix. note that this correction doesn't |
|
622 // occur if symamd returns FALSE |
|
623 out_stats (COLAMD_INFO1) ++ ; |
|
624 out_stats (COLAMD_INFO2) ++ ; |
|
625 } |
|
626 } |
|
627 |
5451
|
628 #else |
|
629 |
|
630 error ("symamd: not available in this version of Octave"); |
|
631 |
|
632 #endif |
|
633 |
5164
|
634 return retval; |
|
635 } |
|
636 |
|
637 DEFUN_DLD (etree, args, nargout, |
|
638 "-*- texinfo -*-\n\ |
|
639 @deftypefn {Loadable Function} {@var{p} =} etree (@var{s})\n\ |
|
640 @deftypefnx {Loadable Function} {@var{p} =} etree (@var{s}, @var{typ})\n\ |
|
641 @deftypefnx {Loadable Function} {[@var{p}, @var{q}] =} etree (@var{s}, @var{typ})\n\ |
|
642 \n\ |
|
643 Returns the elimination tree for the matrix @var{s}. By default @var{s}\n\ |
|
644 is assumed to be symmetric and the symmetric elimination tree is\n\ |
|
645 returned. The argument @var{typ} controls whether a symmetric or\n\ |
|
646 column elimination tree is returned. Valid values of @var{typ} are\n\ |
|
647 'sym' or 'col', for symmetric or column elimination tree respectively\n\ |
|
648 \n\ |
|
649 Called with a second argument, @dfn{etree} also returns the postorder\n\ |
|
650 permutations on the tree.\n\ |
|
651 @end deftypefn") |
|
652 { |
|
653 octave_value_list retval; |
5297
|
654 |
5164
|
655 int nargin = args.length (); |
|
656 |
|
657 if (nargout < 0 || nargout > 2 || nargin < 0 || nargin > 2) |
5451
|
658 print_usage ("etree"); |
5164
|
659 else |
|
660 { |
5440
|
661 octave_idx_type n_row, n_col, nnz; |
|
662 octave_idx_type *ridx, *cidx; |
5164
|
663 bool is_sym = true; |
|
664 SparseMatrix sm; |
|
665 SparseComplexMatrix scm; |
|
666 |
5631
|
667 if (args(0).is_sparse_type ()) |
5164
|
668 { |
|
669 if (args(0).is_complex_type ()) |
|
670 { |
|
671 scm = args(0).sparse_complex_matrix_value (); |
|
672 n_row = scm.rows (); |
|
673 n_col = scm.cols (); |
5604
|
674 nnz = scm.nzmax (); |
5164
|
675 ridx = scm.xridx (); |
|
676 cidx = scm.xcidx (); |
|
677 } |
|
678 else |
|
679 { |
|
680 sm = args(0).sparse_matrix_value (); |
|
681 n_row = sm.rows (); |
|
682 n_col = sm.cols (); |
5604
|
683 nnz = sm.nzmax (); |
5164
|
684 ridx = sm.xridx (); |
|
685 cidx = sm.xcidx (); |
|
686 } |
|
687 |
|
688 } |
|
689 else |
|
690 { |
|
691 error ("etree: must be called with a sparse matrix"); |
|
692 return retval; |
|
693 } |
|
694 |
|
695 if (nargin == 2) |
|
696 if (args(1).is_string ()) |
|
697 { |
|
698 std::string str = args(1).string_value (); |
|
699 if (str.find("C") == 0 || str.find("c") == 0) |
|
700 is_sym = false; |
|
701 } |
|
702 else |
|
703 { |
|
704 error ("etree: second argument must be a string"); |
|
705 return retval; |
|
706 } |
|
707 |
|
708 // column elimination tree post-ordering (reuse variables) |
5440
|
709 OCTAVE_LOCAL_BUFFER (octave_idx_type, etree, n_col + 1); |
5164
|
710 |
|
711 |
|
712 if (is_sym) |
|
713 { |
|
714 if (n_row != n_col) |
|
715 { |
|
716 error ("etree: matrix is marked as symmetric, but not square"); |
|
717 return retval; |
|
718 } |
|
719 symetree (ridx, cidx, etree, NULL, n_col); |
|
720 } |
|
721 else |
|
722 { |
5440
|
723 OCTAVE_LOCAL_BUFFER (octave_idx_type, colbeg, n_col); |
|
724 OCTAVE_LOCAL_BUFFER (octave_idx_type, colend, n_col); |
5164
|
725 |
5440
|
726 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
727 { |
|
728 colbeg[i] = cidx[i]; |
|
729 colend[i] = cidx[i+1]; |
|
730 } |
|
731 |
|
732 coletree (ridx, colbeg, colend, etree, n_row, n_col); |
|
733 } |
|
734 |
|
735 NDArray tree (dim_vector (1, n_col)); |
5440
|
736 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
737 // We flag a root with n_col while Matlab does it with zero |
|
738 // Convert for matlab compatiable output |
|
739 if (etree[i] == n_col) |
|
740 tree (i) = 0; |
|
741 else |
|
742 tree (i) = etree[i] + 1; |
|
743 |
|
744 retval (0) = tree; |
|
745 |
|
746 if (nargout == 2) |
|
747 { |
|
748 // Calculate the tree post-ordering |
5440
|
749 OCTAVE_LOCAL_BUFFER (octave_idx_type, post, n_col + 1); |
5164
|
750 TreePostorder (n_col, etree, post); |
|
751 |
|
752 NDArray postorder (dim_vector (1, n_col)); |
5440
|
753 for (octave_idx_type i = 0; i < n_col; i++) |
5164
|
754 postorder (i) = post[i] + 1; |
|
755 |
|
756 retval (1) = postorder; |
|
757 } |
|
758 } |
|
759 |
|
760 return retval; |
|
761 } |
|
762 |
|
763 /* |
|
764 ;;; Local Variables: *** |
|
765 ;;; mode: C++ *** |
|
766 ;;; End: *** |
|
767 */ |