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