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