Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/amd.cc @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | be150a172010 |
children | 40dfc0c99116 |
rev | line source |
---|---|
7619 | 1 /* |
2 | |
9245 | 3 Copyright (C) 2008, 2009 David Bateman |
7619 | 4 |
5 This file is part of Octave. | |
6 | |
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 | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
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 | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 // This is the octave interface to amd, 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 #include "oct-map.h" | |
43 | |
44 #include "oct-sparse.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8333
diff
changeset
|
45 #include "oct-locbuf.h" |
7619 | 46 |
47 #ifdef IDX_TYPE_LONG | |
48 #define AMD_NAME(name) amd_l ## name | |
49 #else | |
50 #define AMD_NAME(name) amd ## name | |
51 #endif | |
52 | |
53 DEFUN_DLD (amd, args, nargout, | |
54 "-*- texinfo -*-\n\ | |
55 @deftypefn {Loadable Function} {@var{p} =} amd (@var{s})\n\ | |
56 @deftypefnx {Loadable Function} {@var{p} =} amd (@var{s}, @var{opts})\n\ | |
57 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8377
diff
changeset
|
58 Returns the approximate minimum degree permutation of a matrix. This\n\ |
7619 | 59 permutation such that the Cholesky factorization of @code{@var{s} (@var{p},\n\ |
60 @var{p})} tends to be sparser than the Cholesky factorization of @var{s}\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8377
diff
changeset
|
61 itself. @code{amd} is typically faster than @code{symamd} but serves a\n\ |
7619 | 62 similar purpose.\n\ |
63 \n\ | |
64 The optional parameter @var{opts} is a structure that controls the\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8377
diff
changeset
|
65 behavior of @code{amd}. The fields of these structure are\n\ |
7619 | 66 \n\ |
67 @table @asis\n\ | |
68 @item opts.dense\n\ | |
69 Determines what @code{amd} considers to be a dense row or column of the\n\ | |
9066
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
70 input matrix. Rows or columns with more than @code{max(16, (dense *\n\ |
7619 | 71 sqrt (@var{n})} entries, where @var{n} is the order of the matrix @var{s},\n\ |
9066
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
72 are ignored by @code{amd} during the calculation of the permutation\n\ |
7619 | 73 The value of dense must be a positive scalar and its default value is 10.0\n\ |
74 \n\ | |
75 @item opts.aggressive\n\ | |
9066
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
76 If this value is a non zero scalar, then @code{amd} performs aggressive\n\ |
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9064
diff
changeset
|
77 absorption. The default is not to perform aggressive absorption.\n\ |
7619 | 78 @end table\n\ |
79 \n\ | |
80 The author of the code itself is Timothy A. Davis (davis@@cise.ufl.edu),\n\ | |
81 University of Florida (see @url{http://www.cise.ufl.edu/research/sparse/amd}).\n\ | |
82 @seealso{symamd, colamd}\n\ | |
83 @end deftypefn") | |
84 { | |
85 octave_value_list retval; | |
86 | |
87 #ifdef HAVE_AMD | |
88 int nargin = args.length (); | |
89 | |
90 if (nargin < 1 || nargin > 2) | |
91 print_usage (); | |
92 else | |
93 { | |
94 octave_idx_type n_row, n_col, nnz; | |
95 const octave_idx_type *ridx, *cidx; | |
96 SparseMatrix sm; | |
97 SparseComplexMatrix scm; | |
98 | |
99 if (args(0).is_sparse_type ()) | |
100 { | |
101 if (args(0).is_complex_type ()) | |
102 { | |
103 scm = args(0).sparse_complex_matrix_value (); | |
104 n_row = scm.rows (); | |
105 n_col = scm.cols (); | |
106 nnz = scm.nzmax (); | |
107 ridx = scm.xridx (); | |
108 cidx = scm.xcidx (); | |
109 } | |
110 else | |
111 { | |
112 sm = args(0).sparse_matrix_value (); | |
113 n_row = sm.rows (); | |
114 n_col = sm.cols (); | |
115 nnz = sm.nzmax (); | |
116 ridx = sm.xridx (); | |
117 cidx = sm.xcidx (); | |
118 } | |
119 } | |
120 else | |
121 { | |
122 if (args(0).is_complex_type ()) | |
123 sm = SparseMatrix (real (args(0).complex_matrix_value ())); | |
124 else | |
125 sm = SparseMatrix (args(0).matrix_value ()); | |
126 | |
127 n_row = sm.rows (); | |
128 n_col = sm.cols (); | |
129 nnz = sm.nzmax (); | |
130 ridx = sm.xridx (); | |
131 cidx = sm.xcidx (); | |
132 } | |
133 | |
134 if (!error_state && n_row != n_col) | |
135 error ("amd: input matrix must be square"); | |
136 | |
137 if (!error_state) | |
138 { | |
139 OCTAVE_LOCAL_BUFFER (double, Control, AMD_CONTROL); | |
140 AMD_NAME (_defaults) (Control) ; | |
141 if (nargin > 1) | |
142 { | |
143 Octave_map arg1 = args(1).map_value (); | |
144 | |
145 if (!error_state) | |
146 { | |
147 if (arg1.contains ("dense")) | |
148 { | |
149 Cell c = arg1.contents ("dense"); | |
150 if (c.length() == 1) | |
151 Control[AMD_DENSE] = c.elem(0).double_value (); | |
152 else | |
153 error ("amd: invalid options structure"); | |
154 } | |
155 if (arg1.contains ("aggressive")) | |
156 { | |
157 Cell c = arg1.contents ("aggressive"); | |
158 if (c.length() == 1) | |
159 Control[AMD_AGGRESSIVE] = c.elem(0).double_value (); | |
160 else | |
161 error ("amd: invalid options structure"); | |
162 } | |
163 } | |
164 } | |
165 | |
166 if (!error_state) | |
167 { | |
168 OCTAVE_LOCAL_BUFFER (octave_idx_type, P, n_col); | |
169 Matrix xinfo (AMD_INFO, 1); | |
170 double *Info = xinfo.fortran_vec (); | |
171 | |
8333 | 172 // FIXME -- how can we manage the memory allocation of |
173 // amd in a cleaner manner? | |
7619 | 174 amd_malloc = malloc; |
175 amd_free = free; | |
176 amd_calloc = calloc; | |
177 amd_realloc = realloc; | |
178 amd_printf = printf; | |
179 | |
180 octave_idx_type result = AMD_NAME (_order) (n_col, cidx, ridx, P, | |
181 Control, Info); | |
182 | |
183 switch (result) | |
184 { | |
185 case AMD_OUT_OF_MEMORY: | |
186 error ("amd: out of memory"); | |
187 break; | |
188 case AMD_INVALID: | |
189 error ("amd: input matrix is corrupted"); | |
190 break; | |
191 default: | |
192 { | |
193 if (nargout > 1) | |
194 retval(1) = xinfo; | |
195 | |
196 Matrix Pout (1, n_col); | |
197 for (octave_idx_type i = 0; i < n_col; i++) | |
198 Pout.xelem (i) = P[i] + 1; | |
199 | |
200 retval (0) = Pout; | |
201 } | |
202 } | |
203 } | |
204 } | |
205 } | |
206 #else | |
207 | |
208 error ("amd: not available in this version of Octave"); | |
209 | |
210 #endif | |
211 | |
212 return retval; | |
213 } |