5610
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
4 Copyright (C) 1998-2005 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #if HAVE_CXSPARSE |
|
28 #include <cxsparse/cxs.h> |
|
29 #endif |
|
30 |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
|
37 #include "ov-re-sparse.h" |
|
38 #include "ov-cx-sparse.h" |
|
39 #include "SparseQR.h" |
|
40 #include "SparseCmplxQR.h" |
|
41 |
|
42 #ifdef IDX_TYPE_LONG |
|
43 #define CSSPARSE_NAME(name) name ## _dl |
|
44 #else |
|
45 #define CSSPARSE_NAME(name) name ## _di |
|
46 #endif |
|
47 |
|
48 // PKG_ADD: dispatch ("qr", "spqr", "sparse matrix"); |
|
49 // PKG_ADD: dispatch ("qr", "spqr", "sparse complex matrix"); |
|
50 // PKG_ADD: dispatch ("qr", "spqr", "sparse bool matrix"); |
|
51 DEFUN_DLD (spqr, args, nargout, |
|
52 "-*- texinfo -*-\n\ |
|
53 @deftypefn {Loadable Function} {@var{r} =} spqr (@var{a})\n\ |
|
54 @deftypefnx {Loadable Function} {@var{r} =} spqr (@var{a},0)\n\ |
|
55 @deftypefnx {Loadable Function} {[@var{c}, @var{r}] =} spqr (@var{a},@var{b})\n\ |
|
56 @deftypefnx {Loadable Function} {[@var{c}, @var{r}] =} spqr (@var{a},@var{b},0)\n\ |
|
57 @cindex QR factorization\n\ |
|
58 Compute the sparse QR factorization of @var{a}, using @sc{CSparse}.\n\ |
|
59 As the matrix @var{Q} is in general a full matrix, this function returns\n\ |
|
60 the @var{Q}-less factorization @var{r} of @var{a}, such that\n\ |
|
61 @code{@var{r} = chol (@var{a}' * @var{a})}.\n\ |
|
62 \n\ |
|
63 If the final argument is the scalar @code{0} and the number of rows is\n\ |
|
64 larger than the number of columns, then an economy factorization is\n\ |
|
65 returned. That is @var{r} will have only @code{size (@var{a},1)} rows.\n\ |
|
66 \n\ |
|
67 If an additional matrix @var{b} is supplied, then @code{spqr} returns\n\ |
|
68 @var{c}, where @code{@var{c} = @var{q}' * @var{b}}. This allows the\n\ |
|
69 least squares approximation of @code{@var{a} \\ @var{b}} to be calculated\n\ |
|
70 as\n\ |
|
71 \n\ |
|
72 @example\n\ |
|
73 [@var{c},@var{r}] = spqr (@var{a},@var{b})\n\ |
|
74 @var{x} = @var{r} \\ @var{c}\n\ |
|
75 @end example\n\ |
|
76 \n\ |
|
77 @end deftypefn\n\ |
|
78 @seealso{spchol, qr}") |
|
79 { |
|
80 int nargin = args.length (); |
|
81 octave_value_list retval; |
|
82 bool economy = false; |
|
83 bool is_cmplx = false; |
|
84 bool have_b = false; |
|
85 |
|
86 if (nargin < 1 || nargin > 3) |
|
87 print_usage ("spqr"); |
|
88 else |
|
89 { |
|
90 if (args(0).is_complex_type ()) |
|
91 is_cmplx = true; |
|
92 if (nargin > 1) |
|
93 { |
|
94 have_b = true; |
|
95 if (args(nargin-1).is_scalar_type ()) |
|
96 { |
|
97 int val = args(nargin-1).int_value (); |
|
98 if (val == 0) |
|
99 { |
|
100 economy = true; |
|
101 have_b = (nargin > 2); |
|
102 } |
|
103 } |
|
104 if (have_b && args(1).is_complex_type ()) |
|
105 is_cmplx = true; |
|
106 } |
|
107 |
|
108 if (!error_state) |
|
109 { |
|
110 if (have_b && nargout < 2) |
|
111 error ("spqr: incorrect number of output arguments"); |
|
112 else if (is_cmplx) |
|
113 { |
|
114 SparseComplexQR q (args(0).sparse_complex_matrix_value ()); |
|
115 if (!error_state) |
|
116 { |
|
117 if (have_b) |
|
118 { |
|
119 retval(1) = q.R (economy); |
|
120 retval(0) = q.C (args(1).complex_matrix_value ()); |
|
121 } |
|
122 else |
|
123 retval(0) = q.R (economy); |
|
124 } |
|
125 } |
|
126 else |
|
127 { |
|
128 SparseQR q (args(0).sparse_matrix_value ()); |
|
129 if (!error_state) |
|
130 { |
|
131 if (have_b) |
|
132 { |
|
133 retval(1) = q.R (economy); |
|
134 retval(0) = q.C (args(1).matrix_value ()); |
|
135 } |
|
136 else |
|
137 retval(0) = q.R (economy); |
|
138 } |
|
139 } |
|
140 } |
|
141 } |
|
142 return retval; |
|
143 } |
|
144 |
|
145 /* |
|
146 |
|
147 The deactivated tests below can't be tested till rectangular back-subs is |
|
148 implemented for sparse matrices. |
|
149 |
|
150 %!test |
|
151 %! n = 20; d= 0.2; |
|
152 %! a = sprandn(n,n,d)+speye(n,n); |
|
153 %! r = spqr(a); |
|
154 %! assert(r'*r,a'*a,1e-10) |
|
155 |
|
156 %!test |
|
157 %! n = 20; d= 0.2; |
|
158 %! a = sprandn(n,n,d)+speye(n,n); |
|
159 %! q = symamd(a); |
|
160 %! a = a(q,q); |
|
161 %! r = spqr(a); |
|
162 %! assert(r'*r,a'*a,1e-10) |
|
163 |
|
164 %!test |
|
165 %! n = 20; d= 0.2; |
|
166 %! a = sprandn(n,n,d)+speye(n,n); |
|
167 %! [c,r] = spqr(a,ones(n,1)); |
|
168 %! assert (r\c,full(a)\ones(n,1),10e-10) |
|
169 |
|
170 %!test |
|
171 %! n = 20; d= 0.2; |
|
172 %! a = sprandn(n,n,d)+speye(n,n); |
|
173 %! b = randn(n,2); |
|
174 %! [c,r] = spqr(a,b); |
|
175 %! assert (r\c,full(a)\b,10e-10) |
|
176 |
|
177 %% Test under-determined systems!! |
|
178 %!#test |
|
179 %! n = 20; d= 0.2; |
|
180 %! a = sprandn(n,n+1,d)+speye(n,n+1); |
|
181 %! b = randn(n,2); |
|
182 %! [c,r] = spqr(a,b); |
|
183 %! assert (r\c,full(a)\b,10e-10) |
|
184 |
|
185 %!test |
|
186 %! n = 20; d= 0.2; |
|
187 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
188 %! r = spqr(a); |
|
189 %! assert(r'*r,a'*a,1e-10) |
|
190 |
|
191 %!test |
|
192 %! n = 20; d= 0.2; |
|
193 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
194 %! q = symamd(a); |
|
195 %! a = a(q,q); |
|
196 %! r = spqr(a); |
|
197 %! assert(r'*r,a'*a,1e-10) |
|
198 |
|
199 %!test |
|
200 %! n = 20; d= 0.2; |
|
201 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
202 %! [c,r] = spqr(a,ones(n,1)); |
|
203 %! assert (r\c,full(a)\ones(n,1),10e-10) |
|
204 |
|
205 %!test |
|
206 %! n = 20; d= 0.2; |
|
207 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
208 %! b = randn(n,2); |
|
209 %! [c,r] = spqr(a,b); |
|
210 %! assert (r\c,full(a)\b,10e-10) |
|
211 |
|
212 %% Test under-determined systems!! |
|
213 %!#test |
|
214 %! n = 20; d= 0.2; |
|
215 %! a = 1i*sprandn(n,n+1,d)+speye(n,n+1); |
|
216 %! b = randn(n,2); |
|
217 %! [c,r] = spqr(a,b); |
|
218 %! assert (r\c,full(a)\b,10e-10) |
|
219 |
|
220 %!error spqr(sprandn(10,10,0.2),ones(10,1)); |
|
221 |
|
222 */ |
|
223 |
|
224 static RowVector |
|
225 put_int (octave_idx_type *p, octave_idx_type n) |
|
226 { |
|
227 RowVector ret (n); |
|
228 for (octave_idx_type i = 0; i < n; i++) |
|
229 ret.xelem(i) = p[i] + 1; |
|
230 return ret; |
|
231 } |
|
232 |
|
233 DEFUN_DLD (dmperm, args, nargout, |
|
234 "-*- texinfo -*-\n\ |
|
235 @deftypefn {Loadable Function} {@var{p} =} dmperm (@var{s})\n\ |
|
236 @deftypefnx {Loadable Function} {[@var{p}. @var{q}. @var{r}, @var{s}] =} dmperm (@var{s})\n\ |
|
237 \n\ |
|
238 @cindex Dulmage-Mendelsohn decomposition\n\ |
|
239 Perform a Deulmage-Mendelsohn permutation on the sparse matrix @var{s}.\n\ |
|
240 With a single output argument @dfn{dmperm} performs the row permutations\n\ |
|
241 @var{p} such that @code{@var{s} (@var{p},:)} has no zero elements on the\n\ |
|
242 diagonal.\n\ |
|
243 \n\ |
|
244 Called with two or more output arguments, returns the row and column\n\ |
|
245 permutations, such that @code{@var{s} (@var{p}, @var{q})} is in block\n\ |
|
246 triangular form. The values of @var{r} and @var{s} define the boundaries\n\ |
|
247 of the blocks. If @var{s} is square then @code{@var{r} == @var{s}}.\n\ |
|
248 \n\ |
|
249 The method used is described in: A. Pothen & C.-J. Fan. Computing the block\n\ |
|
250 triangular form of a sparse matrix. ACM Trans. Math. Software,\n\ |
|
251 16(4):303-324, 1990.\n\ |
|
252 @end deftypefn\n\ |
|
253 @seealso{colamd,ccolamd}") |
|
254 { |
|
255 int nargin = args.length(); |
|
256 octave_value_list retval; |
|
257 |
|
258 #if HAVE_CXSPARSE |
|
259 if (nargin != 1) |
|
260 { |
|
261 print_usage ("dmperm"); |
|
262 return retval; |
|
263 } |
|
264 |
|
265 octave_value arg = args(0); |
|
266 octave_idx_type nr = arg.rows (); |
|
267 octave_idx_type nc = arg.columns (); |
|
268 SparseMatrix m; |
|
269 SparseComplexMatrix cm; |
|
270 CSSPARSE_NAME (cs) csm; |
|
271 csm.m = nr; |
|
272 csm.n = nc; |
|
273 csm.x = NULL; |
|
274 csm.nz = -1; |
|
275 |
|
276 if (arg.is_real_type ()) |
|
277 { |
|
278 m = arg.sparse_matrix_value (); |
|
279 csm.nzmax = m.nnz(); |
|
280 csm.p = m.xcidx (); |
|
281 csm.i = m.xridx (); |
|
282 } |
|
283 else |
|
284 { |
|
285 cm = arg.sparse_complex_matrix_value (); |
|
286 csm.nzmax = cm.nnz(); |
|
287 csm.p = cm.xcidx (); |
|
288 csm.i = cm.xridx (); |
|
289 } |
|
290 |
|
291 if (!error_state) |
|
292 { |
|
293 if (nargout <= 1) |
|
294 { |
|
295 octave_idx_type *jmatch = CSSPARSE_NAME (cs_maxtrans) (&csm); |
|
296 retval(0) = put_int (jmatch + nr, nc); |
|
297 CSSPARSE_NAME (cs_free) (jmatch); |
|
298 } |
|
299 else |
|
300 { |
|
301 CSSPARSE_NAME (csd) *dm = CSSPARSE_NAME(cs_dmperm) (&csm); |
|
302 //retval(5) = put_int (dm->rr, 5); |
|
303 //retval(4) = put_int (dm->cc, 5); |
|
304 retval(3) = put_int (dm->S, dm->nb+1); |
|
305 retval(2) = put_int (dm->R, dm->nb+1); |
|
306 retval(1) = put_int (dm->Q, nc); |
|
307 retval(0) = put_int (dm->P, nr); |
|
308 CSSPARSE_NAME (cs_dfree) (dm); |
|
309 } |
|
310 } |
|
311 #else |
|
312 error ("dmperm: not available in this version of Octave"); |
|
313 #endif |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 /* |
|
319 |
|
320 %!test |
|
321 %! n=20; |
|
322 %! a=speye(n,n);a=a(randperm(n),:); |
|
323 %! assert(a(dmperm(a),:),speye(n)) |
|
324 |
|
325 %!test |
|
326 %! n=20; |
|
327 %! d=0.2; |
|
328 %! a=tril(sprandn(n,n,d),-1)+speye(n,n); |
|
329 %! a=a(randperm(n),randperm(n)); |
|
330 %! [p,q,r,s]=dmperm(a); |
|
331 %! assert(tril(a(p,q),-1),sparse(n,n)) |
|
332 |
|
333 */ |
|
334 |
|
335 /* |
|
336 ;;; Local Variables: *** |
|
337 ;;; mode: C++ *** |
|
338 ;;; End: *** |
|
339 */ |