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