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