5451
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2005, 2006, 2007 David Bateman |
5451
|
4 |
7016
|
5 This file is part of Octave. |
|
6 |
5451
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
5451
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
5451
|
20 |
|
21 */ |
|
22 |
|
23 // This is the octave interface to ccolamd, 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 |
|
43 #include "oct-sparse.h" |
|
44 |
|
45 #ifdef IDX_TYPE_LONG |
|
46 #define CCOLAMD_NAME(name) ccolamd_l ## name |
|
47 #define CSYMAMD_NAME(name) csymamd_l ## name |
|
48 #else |
|
49 #define CCOLAMD_NAME(name) ccolamd ## name |
|
50 #define CSYMAMD_NAME(name) csymamd ## name |
|
51 #endif |
|
52 |
|
53 DEFUN_DLD (ccolamd, args, nargout, |
|
54 "-*- texinfo -*-\n\ |
|
55 @deftypefn {Loadable Function} {@var{p} =} ccolamd (@var{s})\n\ |
|
56 @deftypefnx {Loadable Function} {@var{p} =} ccolamd (@var{s}, @var{knobs})\n\ |
|
57 @deftypefnx {Loadable Function} {@var{p} =} ccolamd (@var{s}, @var{knobs}, @var{cmember})\n\ |
|
58 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} ccolamd (@dots{})\n\ |
|
59 \n\ |
|
60 Constrained column approximate minimum degree permutation. @code{@var{p} =\n\ |
|
61 ccolamd (@var{s})} returns the column approximate minimum degree permutation\n\ |
|
62 vector for the sparse matrix @var{s}. For a non-symmetric matrix @var{s},\n\ |
7107
|
63 @code{@var{s} (:, @var{p})} tends to have sparser LU factors than @var{s}.\n\ |
|
64 @code{chol (@var{s} (:, @var{p})' * @var{s} (:, @var{p}))} also tends to be\n\ |
|
65 sparser than @code{chol (@var{s}' * @var{s})}. @code{@var{p} = ccolamd\n\ |
|
66 (@var{s}, 1)} optimizes the ordering for @code{lu (@var{s} (:, @var{p}))}.\n\ |
5451
|
67 The ordering is followed by a column elimination tree post-ordering.\n\ |
|
68 \n\ |
|
69 @var{knobs} is an optional one- to five-element input vector, with a default\n\ |
|
70 value of @code{[0 10 10 1 0]} if not present or empty. Entries not present\n\ |
|
71 are set to their defaults.\n\ |
|
72 \n\ |
|
73 @table @code\n\ |
|
74 @item @var{knobs}(1)\n\ |
7107
|
75 if nonzero, the ordering is optimized for @code{lu (S (:, p))}. It will be a\n\ |
|
76 poor ordering for @code{chol (@var{s} (:, @var{p})' * @var{s} (:,\n\ |
|
77 @var{p}))}. This is the most important knob for ccolamd.\n\ |
5451
|
78 \n\ |
|
79 @item @var{knob}(2)\n\ |
7107
|
80 if @var{s} is m-by-n, rows with more than @code{max (16, @var{knobs} (2) *\n\ |
|
81 sqrt (n))} entries are ignored.\n\ |
5451
|
82 \n\ |
|
83 @item @var{knob}(3)\n\ |
7107
|
84 columns with more than @code{max (16, @var{knobs} (3) * sqrt (min (@var{m},\n\ |
|
85 @var{n})))} entries are ignored and ordered last in the output permutation\n\ |
|
86 (subject to the cmember constraints).\n\ |
5451
|
87 \n\ |
|
88 @item @var{knob}(4)\n\ |
|
89 if nonzero, aggressive absorption is performed.\n\ |
|
90 \n\ |
|
91 @item @var{knob}(5)\n\ |
|
92 if nonzero, statistics and knobs are printed.\n\ |
|
93 \n\ |
|
94 @end table\n\ |
|
95 \n\ |
|
96 @var{cmember} is an optional vector of length n. It defines the constraints\n\ |
7107
|
97 on the column ordering. If @code{@var{cmember} (j) = @var{c}}, then column\n\ |
|
98 @var{j} is in constraint set @var{c} (@var{c} must be in the range 1 to\n\ |
|
99 @var{n}). In the output permutation @var{p}, all columns in set 1 appear\n\ |
|
100 first, followed by all columns in set 2, and so on. @code{@var{cmember} =\n\ |
|
101 ones(1,n)} if not present or empty. @code{ccolamd (@var{s}, [], 1 :\n\ |
|
102 @var{n})} returns @code{1 : @var{n}}\n\ |
5451
|
103 \n\ |
7107
|
104 @code{@var{p} = ccolamd (@var{s})} is about the same as @code{@var{p} =\n\ |
|
105 colamd (@var{s})}. @var{knobs} and its default values differ. @code{colamd}\n\ |
5451
|
106 always does aggressive absorption, and it finds an ordering suitable for\n\ |
7107
|
107 both @code{lu (@var{s} (:, @var{p}))} and @code{chol (@var{S} (:, @var{p})'\n\ |
|
108 * @var{s} (:, @var{p}))}; it cannot optimize its ordering for\n\ |
|
109 @code{lu (@var{s} (:, @var{p}))} to the extent that\n\ |
|
110 @code{ccolamd (@var{s}, 1)} can.\n\ |
5451
|
111 \n\ |
|
112 @var{stats} is an optional 20-element output vector that provides data\n\ |
|
113 about the ordering and the validity of the input matrix @var{s}. Ordering\n\ |
7107
|
114 statistics are in @code{@var{stats} (1 : 3)}. @code{@var{stats} (1)} and\n\ |
5451
|
115 @code{@var{stats} (2)} are the number of dense or empty rows and columns\n\ |
|
116 ignored by CCOLAMD and @code{@var{stats} (3)} is the number of garbage\n\ |
|
117 collections performed on the internal data structure used by CCOLAMD\n\ |
7107
|
118 (roughly of size @code{2.2 * nnz (@var{s}) + 4 * @var{m} + 7 * @var{n}}\n\ |
5451
|
119 integers).\n\ |
|
120 \n\ |
7107
|
121 @code{@var{stats} (4 : 7)} provide information if CCOLAMD was able to\n\ |
5451
|
122 continue. The matrix is OK if @code{@var{stats} (4)} is zero, or 1 if\n\ |
|
123 invalid. @code{@var{stats} (5)} is the rightmost column index that is\n\ |
|
124 unsorted or contains duplicate entries, or zero if no such column exists.\n\ |
|
125 @code{@var{stats} (6)} is the last seen duplicate or out-of-order row\n\ |
|
126 index in the column index given by @code{@var{stats} (5)}, or zero if no\n\ |
|
127 such row index exists. @code{@var{stats} (7)} is the number of duplicate\n\ |
7107
|
128 or out-of-order row indices. @code{@var{stats} (8 : 20)} is always zero in\n\ |
5451
|
129 the current version of CCOLAMD (reserved for future use).\n\ |
|
130 \n\ |
|
131 The authors of the code itself are S. Larimore, T. Davis (Uni of Florida)\n\ |
|
132 and S. Rajamanickam in collaboration with J. Bilbert and E. Ng. Supported\n\ |
|
133 by the National Science Foundation (DMS-9504974, DMS-9803599, CCR-0203270),\n\ |
|
134 and a grant from Sandia National Lab. See\n\ |
|
135 @url{http://www.cise.ufl.edu/research/sparse} for ccolamd, csymamd, amd,\n\ |
|
136 colamd, symamd, and other related orderings.\n\ |
5642
|
137 @seealso{colamd, csymamd}\n\ |
|
138 @end deftypefn") |
5451
|
139 { |
5771
|
140 octave_value_list retval; |
|
141 |
5451
|
142 #ifdef HAVE_CCOLAMD |
5771
|
143 |
5451
|
144 int nargin = args.length (); |
|
145 int spumoni = 0; |
|
146 |
|
147 if (nargout < 0 || nargout > 2 || nargin < 0 || nargin > 3) |
|
148 usage ("ccolamd: incorrect number of input and/or output arguments"); |
|
149 else |
|
150 { |
|
151 // Get knobs |
|
152 OCTAVE_LOCAL_BUFFER (double, knobs, CCOLAMD_KNOBS); |
|
153 CCOLAMD_NAME (_set_defaults) (knobs); |
|
154 |
|
155 // Check for user-passed knobs |
|
156 if (nargin > 1) |
|
157 { |
|
158 NDArray User_knobs = args(1).array_value (); |
|
159 int nel_User_knobs = User_knobs.length (); |
|
160 |
|
161 if (nel_User_knobs > 0) |
|
162 knobs [CCOLAMD_LU] = (User_knobs (0) != 0); |
|
163 if (nel_User_knobs > 1) |
|
164 knobs [CCOLAMD_DENSE_ROW] = User_knobs (1); |
|
165 if (nel_User_knobs > 2) |
|
166 knobs [CCOLAMD_DENSE_COL] = User_knobs (2); |
|
167 if (nel_User_knobs > 3) |
|
168 knobs [CCOLAMD_AGGRESSIVE] = (User_knobs (3) != 0); |
|
169 if (nel_User_knobs > 4) |
|
170 spumoni = (User_knobs (4) != 0); |
|
171 |
|
172 // print knob settings if spumoni is set |
|
173 if (spumoni) |
|
174 { |
|
175 octave_stdout << "\nccolamd version " << CCOLAMD_MAIN_VERSION << "." |
|
176 << CCOLAMD_SUB_VERSION << ", " << CCOLAMD_DATE |
|
177 << ":\nknobs(1): " << User_knobs (0) << ", order for "; |
|
178 if ( knobs [CCOLAMD_LU] != 0) |
|
179 octave_stdout << "lu(A)\n"; |
|
180 else |
|
181 octave_stdout << "chol(A'*A)\n"; |
|
182 |
|
183 if (knobs [CCOLAMD_DENSE_ROW] >= 0) |
|
184 octave_stdout << "knobs(2): " << User_knobs (1) |
|
185 << ", rows with > max(16," |
|
186 << knobs [CCOLAMD_DENSE_ROW] << "*sqrt(size(A,2)))" |
|
187 << " entries removed\n"; |
|
188 else |
|
189 octave_stdout << "knobs(2): " << User_knobs (1) |
|
190 << ", no dense rows removed\n"; |
|
191 |
|
192 if (knobs [CCOLAMD_DENSE_COL] >= 0) |
|
193 octave_stdout << "knobs(3): " << User_knobs (2) |
|
194 << ", cols with > max(16," |
|
195 << knobs [CCOLAMD_DENSE_COL] << "*sqrt(size(A)))" |
|
196 << " entries removed\n"; |
|
197 else |
|
198 octave_stdout << "knobs(3): " << User_knobs (2) |
|
199 << ", no dense columns removed\n"; |
|
200 |
|
201 if (knobs [CCOLAMD_AGGRESSIVE] != 0) |
|
202 octave_stdout << "knobs(4): " << User_knobs(3) |
|
203 << ", aggressive absorption: yes"; |
|
204 else |
|
205 octave_stdout << "knobs(4): " << User_knobs(3) |
|
206 << ", aggressive absorption: no"; |
|
207 |
|
208 octave_stdout << "knobs(5): " << User_knobs (4) |
|
209 << ", statistics and knobs printed\n"; |
|
210 } |
|
211 } |
|
212 |
|
213 octave_idx_type n_row, n_col, nnz; |
|
214 octave_idx_type *ridx, *cidx; |
|
215 SparseComplexMatrix scm; |
|
216 SparseMatrix sm; |
|
217 |
5631
|
218 if (args(0).is_sparse_type ()) |
5451
|
219 { |
|
220 if (args(0).is_complex_type ()) |
|
221 { |
|
222 scm = args(0). sparse_complex_matrix_value (); |
|
223 n_row = scm.rows (); |
|
224 n_col = scm.cols (); |
5604
|
225 nnz = scm.nzmax (); |
5451
|
226 ridx = scm.xridx (); |
|
227 cidx = scm.xcidx (); |
|
228 } |
|
229 else |
|
230 { |
|
231 sm = args(0).sparse_matrix_value (); |
|
232 |
|
233 n_row = sm.rows (); |
|
234 n_col = sm.cols (); |
5604
|
235 nnz = sm.nzmax (); |
5451
|
236 ridx = sm.xridx (); |
|
237 cidx = sm.xcidx (); |
|
238 } |
|
239 } |
|
240 else |
|
241 { |
|
242 if (args(0).is_complex_type ()) |
|
243 sm = SparseMatrix (real (args(0).complex_matrix_value ())); |
|
244 else |
|
245 sm = SparseMatrix (args(0).matrix_value ()); |
|
246 |
|
247 n_row = sm.rows (); |
|
248 n_col = sm.cols (); |
5604
|
249 nnz = sm.nzmax (); |
5451
|
250 ridx = sm.xridx (); |
|
251 cidx = sm.xcidx (); |
|
252 } |
|
253 |
|
254 // Allocate workspace for ccolamd |
|
255 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, n_col+1); |
|
256 for (octave_idx_type i = 0; i < n_col+1; i++) |
|
257 p[i] = cidx [i]; |
|
258 |
|
259 octave_idx_type Alen = CCOLAMD_NAME (_recommended) (nnz, n_row, n_col); |
|
260 OCTAVE_LOCAL_BUFFER (octave_idx_type, A, Alen); |
|
261 for (octave_idx_type i = 0; i < nnz; i++) |
|
262 A[i] = ridx [i]; |
|
263 |
|
264 OCTAVE_LOCAL_BUFFER (octave_idx_type, stats, CCOLAMD_STATS); |
|
265 |
|
266 if (nargin > 2) |
|
267 { |
|
268 NDArray in_cmember = args(2).array_value(); |
|
269 octave_idx_type cslen = in_cmember.length(); |
|
270 OCTAVE_LOCAL_BUFFER (octave_idx_type, cmember, cslen); |
|
271 for (octave_idx_type i = 0; i < cslen; i++) |
|
272 // convert cmember from 1-based to 0-based |
|
273 cmember[i] = static_cast<octave_idx_type>(in_cmember(i) - 1); |
|
274 |
|
275 if (cslen != n_col) |
|
276 error ("ccolamd: cmember must be of length equal to #cols of A"); |
|
277 else |
|
278 // Order the columns (destroys A) |
|
279 if (! CCOLAMD_NAME () (n_row, n_col, Alen, A, p, knobs, stats, cmember)) |
|
280 { |
|
281 CCOLAMD_NAME (_report) (stats) ; |
|
282 error ("ccolamd: internal error!"); |
|
283 return retval; |
|
284 } |
|
285 } |
|
286 else |
|
287 { |
|
288 // Order the columns (destroys A) |
7520
|
289 if (! CCOLAMD_NAME () (n_row, n_col, Alen, A, p, knobs, stats, 0)) |
5451
|
290 { |
|
291 CCOLAMD_NAME (_report) (stats) ; |
|
292 error ("ccolamd: internal error!"); |
|
293 return retval; |
|
294 } |
|
295 } |
|
296 |
|
297 // return the permutation vector |
|
298 NDArray out_perm (dim_vector (1, n_col)); |
|
299 for (octave_idx_type i = 0; i < n_col; i++) |
|
300 out_perm(i) = p [i] + 1; |
|
301 |
|
302 retval (0) = out_perm; |
|
303 |
|
304 // print stats if spumoni > 0 |
|
305 if (spumoni > 0) |
|
306 CCOLAMD_NAME (_report) (stats) ; |
|
307 |
|
308 // Return the stats vector |
|
309 if (nargout == 2) |
|
310 { |
|
311 NDArray out_stats (dim_vector (1, CCOLAMD_STATS)); |
|
312 for (octave_idx_type i = 0 ; i < CCOLAMD_STATS ; i++) |
|
313 out_stats (i) = stats [i] ; |
|
314 retval(1) = out_stats; |
|
315 |
|
316 // fix stats (5) and (6), for 1-based information on |
|
317 // jumbled matrix. note that this correction doesn't |
|
318 // occur if symamd returns FALSE |
|
319 out_stats (CCOLAMD_INFO1) ++ ; |
|
320 out_stats (CCOLAMD_INFO2) ++ ; |
|
321 } |
|
322 } |
|
323 |
|
324 #else |
|
325 |
|
326 error ("ccolamd: not available in this version of Octave"); |
|
327 |
|
328 #endif |
5771
|
329 |
|
330 return retval; |
5451
|
331 } |
|
332 |
|
333 DEFUN_DLD (csymamd, args, nargout, |
|
334 "-*- texinfo -*-\n\ |
|
335 @deftypefn {Loadable Function} {@var{p} =} csymamd (@var{s})\n\ |
|
336 @deftypefnx {Loadable Function} {@var{p} =} csymamd (@var{s}, @var{knobs})\n\ |
|
337 @deftypefnx {Loadable Function} {@var{p} =} csymamd (@var{s}, @var{knobs}, @var{cmember})\n\ |
|
338 @deftypefnx {Loadable Function} {[@var{p}, @var{stats}] =} csymamd (@dots{})\n\ |
|
339 \n\ |
|
340 For a symmetric positive definite matrix @var{s}, returns the permutation\n\ |
|
341 vector @var{p} such that @code{@var{s}(@var{p},@var{p})} tends to have a\n\ |
|
342 sparser Cholesky factor than @var{s}. Sometimes @code{csymamd} works well\n\ |
|
343 for symmetric indefinite matrices too. The matrix @var{s} is assumed to\n\ |
|
344 be symmetric; only the strictly lower triangular part is referenced.\n\ |
|
345 @var{s} must be square. The ordering is followed by an elimination tree\n\ |
|
346 post-ordering.\n\ |
|
347 \n\ |
|
348 @var{knobs} is an optional one- to three-element input vector, with a\n\ |
|
349 default value of @code{[10 1 0]} if present or empty. Entries not\n\ |
|
350 present are set to their defaults.\n\ |
|
351 \n\ |
|
352 @table @code\n\ |
|
353 @item @var{knobs}(1)\n\ |
|
354 If @var{s} is n-by-n, then rows and columns with more than\n\ |
|
355 @code{max(16,@var{knobs}(1)*sqrt(n))} entries are ignored, and ordered\n\ |
|
356 last in the output permutation (subject to the cmember constraints).\n\ |
|
357 \n\ |
|
358 @item @var{knobs}(2)\n\ |
|
359 If nonzero, aggressive absorption is performed.\n\ |
|
360 \n\ |
|
361 @item @var{knobs}(3)\n\ |
|
362 If nonzero, statistics and knobs are printed.\n\ |
|
363 \n\ |
|
364 @end table\n\ |
|
365 \n\ |
|
366 @var{cmember} is an optional vector of length n. It defines the constraints\n\ |
|
367 on the ordering. If @code{@var{cmember}(j) = @var{s}}, then row/column j is\n\ |
|
368 in constraint set @var{c} (@var{c} must be in the range 1 to n). In the\n\ |
|
369 output permutation @var{p}, rows/columns in set 1 appear first, followed\n\ |
|
370 by all rows/columns in set 2, and so on. @code{@var{cmember} = ones(1,n)}\n\ |
|
371 if not present or empty. @code{csymamd(@var{s},[],1:n)} returns @code{1:n}.\n\ |
|
372 \n\ |
|
373 @code{@var{p} = csymamd(@var{s})} is about the same as @code{@var{p} =\n\ |
|
374 symamd(@var{s})}. @var{knobs} and its default values differ.\n\ |
|
375 \n\ |
|
376 @code{@var{stats} (4:7)} provide information if CCOLAMD was able to\n\ |
|
377 continue. The matrix is OK if @code{@var{stats} (4)} is zero, or 1 if\n\ |
|
378 invalid. @code{@var{stats} (5)} is the rightmost column index that is\n\ |
|
379 unsorted or contains duplicate entries, or zero if no such column exists.\n\ |
|
380 @code{@var{stats} (6)} is the last seen duplicate or out-of-order row\n\ |
|
381 index in the column index given by @code{@var{stats} (5)}, or zero if no\n\ |
|
382 such row index exists. @code{@var{stats} (7)} is the number of duplicate\n\ |
|
383 or out-of-order row indices. @code{@var{stats} (8:20)} is always zero in\n\ |
|
384 the current version of CCOLAMD (reserved for future use).\n\ |
|
385 \n\ |
|
386 The authors of the code itself are S. Larimore, T. Davis (Uni of Florida)\n\ |
|
387 and S. Rajamanickam in collaboration with J. Bilbert and E. Ng. Supported\n\ |
|
388 by the National Science Foundation (DMS-9504974, DMS-9803599, CCR-0203270),\n\ |
|
389 and a grant from Sandia National Lab. See\n\ |
|
390 @url{http://www.cise.ufl.edu/research/sparse} for ccolamd, csymamd, amd,\n\ |
|
391 colamd, symamd, and other related orderings.\n\ |
5642
|
392 @seealso{symamd, ccolamd}\n\ |
|
393 @end deftypefn") |
5451
|
394 { |
5771
|
395 octave_value_list retval; |
|
396 |
5451
|
397 #if HAVE_CCOLAMD |
5771
|
398 |
5451
|
399 int nargin = args.length (); |
|
400 int spumoni = 0; |
|
401 |
|
402 if (nargout < 0 || nargout > 2 || nargin < 0 || nargin > 3) |
|
403 usage ("ccolamd: incorrect number of input and/or output arguments"); |
|
404 else |
|
405 { |
|
406 // Get knobs |
|
407 OCTAVE_LOCAL_BUFFER (double, knobs, CCOLAMD_KNOBS); |
|
408 CCOLAMD_NAME (_set_defaults) (knobs); |
|
409 |
|
410 // Check for user-passed knobs |
|
411 if (nargin > 1) |
|
412 { |
|
413 NDArray User_knobs = args(1).array_value (); |
|
414 int nel_User_knobs = User_knobs.length (); |
|
415 |
|
416 if (nel_User_knobs > 0) |
|
417 knobs [CCOLAMD_DENSE_ROW] = User_knobs (0); |
|
418 if (nel_User_knobs > 0) |
|
419 knobs [CCOLAMD_AGGRESSIVE] = User_knobs (1); |
|
420 if (nel_User_knobs > 1) |
5760
|
421 spumoni = static_cast<int> (User_knobs (2)); |
5451
|
422 |
|
423 // print knob settings if spumoni is set |
|
424 if (spumoni) |
|
425 { |
|
426 octave_stdout << "\ncsymamd version " << CCOLAMD_MAIN_VERSION << "." |
|
427 << CCOLAMD_SUB_VERSION << ", " << CCOLAMD_DATE << "\n"; |
|
428 |
|
429 if (knobs [CCOLAMD_DENSE_ROW] >= 0) |
|
430 octave_stdout << "knobs(1): " << User_knobs (0) |
|
431 << ", rows/cols with > max(16," |
|
432 << knobs [CCOLAMD_DENSE_ROW] << "*sqrt(size(A,2)))" |
|
433 << " entries removed\n"; |
|
434 else |
|
435 octave_stdout << "knobs(1): " << User_knobs (0) |
|
436 << ", no dense rows/cols removed\n"; |
|
437 |
|
438 if (knobs [CCOLAMD_AGGRESSIVE] != 0) |
|
439 octave_stdout << "knobs(2): " << User_knobs(1) |
|
440 << ", aggressive absorption: yes"; |
|
441 else |
|
442 octave_stdout << "knobs(2): " << User_knobs(1) |
|
443 << ", aggressive absorption: no"; |
|
444 |
|
445 |
|
446 octave_stdout << "knobs(3): " << User_knobs (2) |
|
447 << ", statistics and knobs printed\n"; |
|
448 } |
|
449 } |
|
450 |
|
451 octave_idx_type n_row, n_col, nnz; |
|
452 octave_idx_type *ridx, *cidx; |
|
453 SparseMatrix sm; |
|
454 SparseComplexMatrix scm; |
|
455 |
5631
|
456 if (args(0).is_sparse_type ()) |
5451
|
457 { |
|
458 if (args(0).is_complex_type ()) |
|
459 { |
|
460 scm = args(0).sparse_complex_matrix_value (); |
|
461 n_row = scm.rows (); |
|
462 n_col = scm.cols (); |
5604
|
463 nnz = scm.nzmax (); |
5451
|
464 ridx = scm.xridx (); |
|
465 cidx = scm.xcidx (); |
|
466 } |
|
467 else |
|
468 { |
|
469 sm = args(0).sparse_matrix_value (); |
|
470 n_row = sm.rows (); |
|
471 n_col = sm.cols (); |
5604
|
472 nnz = sm.nzmax (); |
5451
|
473 ridx = sm.xridx (); |
|
474 cidx = sm.xcidx (); |
|
475 } |
|
476 } |
|
477 else |
|
478 { |
|
479 if (args(0).is_complex_type ()) |
|
480 sm = SparseMatrix (real (args(0).complex_matrix_value ())); |
|
481 else |
|
482 sm = SparseMatrix (args(0).matrix_value ()); |
|
483 |
|
484 n_row = sm.rows (); |
|
485 n_col = sm.cols (); |
5604
|
486 nnz = sm.nzmax (); |
5451
|
487 ridx = sm.xridx (); |
|
488 cidx = sm.xcidx (); |
|
489 } |
|
490 |
|
491 if (n_row != n_col) |
|
492 { |
|
493 error ("symamd: matrix must be square"); |
|
494 return retval; |
|
495 } |
|
496 |
|
497 // Allocate workspace for symamd |
|
498 OCTAVE_LOCAL_BUFFER (octave_idx_type, perm, n_col+1); |
|
499 OCTAVE_LOCAL_BUFFER (octave_idx_type, stats, CCOLAMD_STATS); |
|
500 |
|
501 if (nargin > 2) |
|
502 { |
|
503 NDArray in_cmember = args(2).array_value(); |
|
504 octave_idx_type cslen = in_cmember.length(); |
|
505 OCTAVE_LOCAL_BUFFER (octave_idx_type, cmember, cslen); |
|
506 for (octave_idx_type i = 0; i < cslen; i++) |
|
507 // convert cmember from 1-based to 0-based |
|
508 cmember[i] = static_cast<octave_idx_type>(in_cmember(i) - 1); |
|
509 |
|
510 if (cslen != n_col) |
|
511 error ("ccolamd: cmember must be of length equal to #cols of A"); |
|
512 else |
|
513 if (!CSYMAMD_NAME () (n_col, ridx, cidx, perm, knobs, stats, |
|
514 &calloc, &free, cmember, -1)) |
|
515 { |
|
516 CSYMAMD_NAME (_report) (stats) ; |
|
517 error ("symamd: internal error!") ; |
|
518 return retval; |
|
519 } |
|
520 } |
|
521 else |
|
522 { |
|
523 if (!CSYMAMD_NAME () (n_col, ridx, cidx, perm, knobs, stats, |
7520
|
524 &calloc, &free, 0, -1)) |
5451
|
525 { |
|
526 CSYMAMD_NAME (_report) (stats) ; |
|
527 error ("symamd: internal error!") ; |
|
528 return retval; |
|
529 } |
|
530 } |
|
531 |
|
532 // return the permutation vector |
|
533 NDArray out_perm (dim_vector (1, n_col)); |
|
534 for (octave_idx_type i = 0; i < n_col; i++) |
|
535 out_perm(i) = perm [i] + 1; |
|
536 |
|
537 retval (0) = out_perm; |
|
538 |
|
539 // Return the stats vector |
|
540 if (nargout == 2) |
|
541 { |
|
542 NDArray out_stats (dim_vector (1, CCOLAMD_STATS)); |
|
543 for (octave_idx_type i = 0 ; i < CCOLAMD_STATS ; i++) |
|
544 out_stats (i) = stats [i] ; |
|
545 retval(1) = out_stats; |
|
546 |
|
547 // fix stats (5) and (6), for 1-based information on |
|
548 // jumbled matrix. note that this correction doesn't |
|
549 // occur if symamd returns FALSE |
|
550 out_stats (CCOLAMD_INFO1) ++ ; |
|
551 out_stats (CCOLAMD_INFO2) ++ ; |
|
552 } |
|
553 |
|
554 // print stats if spumoni > 0 |
|
555 if (spumoni > 0) |
|
556 CSYMAMD_NAME (_report) (stats) ; |
|
557 |
|
558 // Return the stats vector |
|
559 if (nargout == 2) |
|
560 { |
|
561 NDArray out_stats (dim_vector (1, CCOLAMD_STATS)); |
|
562 for (octave_idx_type i = 0 ; i < CCOLAMD_STATS ; i++) |
|
563 out_stats (i) = stats [i] ; |
|
564 retval(1) = out_stats; |
|
565 |
|
566 // fix stats (5) and (6), for 1-based information on |
|
567 // jumbled matrix. note that this correction doesn't |
|
568 // occur if symamd returns FALSE |
|
569 out_stats (CCOLAMD_INFO1) ++ ; |
|
570 out_stats (CCOLAMD_INFO2) ++ ; |
|
571 } |
|
572 } |
|
573 |
|
574 #else |
|
575 |
|
576 error ("csymamd: not available in this version of Octave"); |
|
577 |
|
578 #endif |
5771
|
579 |
|
580 return retval; |
5451
|
581 } |
|
582 |
|
583 /* |
|
584 ;;; Local Variables: *** |
|
585 ;;; mode: C++ *** |
|
586 ;;; End: *** |
|
587 */ |