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