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