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