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 #include "defun-dld.h" |
|
28 #include "error.h" |
|
29 #include "gripes.h" |
|
30 #include "oct-obj.h" |
|
31 #include "utils.h" |
|
32 |
5616
|
33 #include "oct-sparse.h" |
5610
|
34 #include "ov-re-sparse.h" |
|
35 #include "ov-cx-sparse.h" |
|
36 #include "SparseQR.h" |
|
37 #include "SparseCmplxQR.h" |
|
38 |
|
39 #ifdef IDX_TYPE_LONG |
5648
|
40 #define CXSPARSE_NAME(name) cs_dl ## name |
5610
|
41 #else |
5648
|
42 #define CXSPARSE_NAME(name) cs_di ## name |
5610
|
43 #endif |
|
44 |
|
45 // PKG_ADD: dispatch ("qr", "spqr", "sparse matrix"); |
|
46 // PKG_ADD: dispatch ("qr", "spqr", "sparse complex matrix"); |
|
47 // PKG_ADD: dispatch ("qr", "spqr", "sparse bool matrix"); |
|
48 DEFUN_DLD (spqr, args, nargout, |
|
49 "-*- texinfo -*-\n\ |
|
50 @deftypefn {Loadable Function} {@var{r} =} spqr (@var{a})\n\ |
|
51 @deftypefnx {Loadable Function} {@var{r} =} spqr (@var{a},0)\n\ |
|
52 @deftypefnx {Loadable Function} {[@var{c}, @var{r}] =} spqr (@var{a},@var{b})\n\ |
|
53 @deftypefnx {Loadable Function} {[@var{c}, @var{r}] =} spqr (@var{a},@var{b},0)\n\ |
|
54 @cindex QR factorization\n\ |
|
55 Compute the sparse QR factorization of @var{a}, using @sc{CSparse}.\n\ |
|
56 As the matrix @var{Q} is in general a full matrix, this function returns\n\ |
|
57 the @var{Q}-less factorization @var{r} of @var{a}, such that\n\ |
|
58 @code{@var{r} = chol (@var{a}' * @var{a})}.\n\ |
|
59 \n\ |
|
60 If the final argument is the scalar @code{0} and the number of rows is\n\ |
|
61 larger than the number of columns, then an economy factorization is\n\ |
|
62 returned. That is @var{r} will have only @code{size (@var{a},1)} rows.\n\ |
|
63 \n\ |
|
64 If an additional matrix @var{b} is supplied, then @code{spqr} returns\n\ |
|
65 @var{c}, where @code{@var{c} = @var{q}' * @var{b}}. This allows the\n\ |
|
66 least squares approximation of @code{@var{a} \\ @var{b}} to be calculated\n\ |
|
67 as\n\ |
|
68 \n\ |
|
69 @example\n\ |
|
70 [@var{c},@var{r}] = spqr (@var{a},@var{b})\n\ |
|
71 @var{x} = @var{r} \\ @var{c}\n\ |
|
72 @end example\n\ |
5642
|
73 @seealso{spchol, qr}\n\ |
|
74 @end deftypefn") |
5610
|
75 { |
|
76 int nargin = args.length (); |
|
77 octave_value_list retval; |
|
78 bool economy = false; |
|
79 bool is_cmplx = false; |
|
80 bool have_b = false; |
|
81 |
|
82 if (nargin < 1 || nargin > 3) |
5823
|
83 print_usage (); |
5610
|
84 else |
|
85 { |
|
86 if (args(0).is_complex_type ()) |
|
87 is_cmplx = true; |
|
88 if (nargin > 1) |
|
89 { |
|
90 have_b = true; |
|
91 if (args(nargin-1).is_scalar_type ()) |
|
92 { |
|
93 int val = args(nargin-1).int_value (); |
|
94 if (val == 0) |
|
95 { |
|
96 economy = true; |
|
97 have_b = (nargin > 2); |
|
98 } |
|
99 } |
|
100 if (have_b && args(1).is_complex_type ()) |
|
101 is_cmplx = true; |
|
102 } |
|
103 |
|
104 if (!error_state) |
|
105 { |
|
106 if (have_b && nargout < 2) |
|
107 error ("spqr: incorrect number of output arguments"); |
|
108 else if (is_cmplx) |
|
109 { |
|
110 SparseComplexQR q (args(0).sparse_complex_matrix_value ()); |
|
111 if (!error_state) |
|
112 { |
|
113 if (have_b) |
|
114 { |
|
115 retval(1) = q.R (economy); |
|
116 retval(0) = q.C (args(1).complex_matrix_value ()); |
5681
|
117 if (args(0).rows() < args(0).columns()) |
|
118 warning ("spqr: non minimum norm solution for under-determined problem"); |
5610
|
119 } |
|
120 else |
|
121 retval(0) = q.R (economy); |
|
122 } |
|
123 } |
|
124 else |
|
125 { |
|
126 SparseQR q (args(0).sparse_matrix_value ()); |
|
127 if (!error_state) |
|
128 { |
|
129 if (have_b) |
|
130 { |
|
131 retval(1) = q.R (economy); |
|
132 retval(0) = q.C (args(1).matrix_value ()); |
5681
|
133 if (args(0).rows() < args(0).columns()) |
|
134 warning ("spqr: non minimum norm solution for under-determined problem"); |
5610
|
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 |
6066
|
233 #if HAVE_CXSPARSE |
|
234 static octave_value_list |
|
235 dmperm_internal (bool rank, const octave_value arg, int nargout) |
5610
|
236 { |
|
237 octave_value_list retval; |
|
238 octave_idx_type nr = arg.rows (); |
|
239 octave_idx_type nc = arg.columns (); |
|
240 SparseMatrix m; |
|
241 SparseComplexMatrix cm; |
5648
|
242 CXSPARSE_NAME () csm; |
5610
|
243 csm.m = nr; |
|
244 csm.n = nc; |
|
245 csm.x = NULL; |
|
246 csm.nz = -1; |
|
247 |
|
248 if (arg.is_real_type ()) |
|
249 { |
|
250 m = arg.sparse_matrix_value (); |
|
251 csm.nzmax = m.nnz(); |
|
252 csm.p = m.xcidx (); |
|
253 csm.i = m.xridx (); |
|
254 } |
|
255 else |
|
256 { |
|
257 cm = arg.sparse_complex_matrix_value (); |
|
258 csm.nzmax = cm.nnz(); |
|
259 csm.p = cm.xcidx (); |
|
260 csm.i = cm.xridx (); |
|
261 } |
|
262 |
|
263 if (!error_state) |
|
264 { |
6066
|
265 if (nargout <= 1 || rank) |
5610
|
266 { |
5792
|
267 #if defined(CS_VER) && (CS_VER >= 2) |
|
268 octave_idx_type *jmatch = CXSPARSE_NAME (_maxtrans) (&csm, 0); |
|
269 #else |
5648
|
270 octave_idx_type *jmatch = CXSPARSE_NAME (_maxtrans) (&csm); |
5792
|
271 #endif |
6066
|
272 if (rank) |
|
273 { |
|
274 octave_idx_type r = 0; |
|
275 for (octave_idx_type i = 0; i < nc; i++) |
|
276 if (jmatch[nr+i] >= 0) |
|
277 r++; |
|
278 retval(0) = static_cast<double>(r); |
|
279 } |
|
280 else |
|
281 retval(0) = put_int (jmatch + nr, nc); |
5648
|
282 CXSPARSE_NAME (_free) (jmatch); |
5610
|
283 } |
|
284 else |
|
285 { |
5792
|
286 #if defined(CS_VER) && (CS_VER >= 2) |
|
287 CXSPARSE_NAME (d) *dm = CXSPARSE_NAME(_dmperm) (&csm, 0); |
|
288 #else |
5648
|
289 CXSPARSE_NAME (d) *dm = CXSPARSE_NAME(_dmperm) (&csm); |
5792
|
290 #endif |
6066
|
291 |
5610
|
292 //retval(5) = put_int (dm->rr, 5); |
|
293 //retval(4) = put_int (dm->cc, 5); |
5792
|
294 #if defined(CS_VER) && (CS_VER >= 2) |
|
295 retval(3) = put_int (dm->s, dm->nb+1); |
|
296 retval(2) = put_int (dm->r, dm->nb+1); |
|
297 retval(1) = put_int (dm->q, nc); |
|
298 retval(0) = put_int (dm->p, nr); |
|
299 #else |
5610
|
300 retval(3) = put_int (dm->S, dm->nb+1); |
|
301 retval(2) = put_int (dm->R, dm->nb+1); |
|
302 retval(1) = put_int (dm->Q, nc); |
|
303 retval(0) = put_int (dm->P, nr); |
5792
|
304 #endif |
5648
|
305 CXSPARSE_NAME (_dfree) (dm); |
5610
|
306 } |
|
307 } |
6066
|
308 return retval; |
|
309 } |
|
310 #endif |
|
311 |
|
312 DEFUN_DLD (dmperm, args, nargout, |
|
313 "-*- texinfo -*-\n\ |
|
314 @deftypefn {Loadable Function} {@var{p} =} dmperm (@var{s})\n\ |
|
315 @deftypefnx {Loadable Function} {[@var{p}, @var{q}, @var{r}, @var{s}] =} dmperm (@var{s})\n\ |
|
316 \n\ |
|
317 @cindex Dulmage-Mendelsohn decomposition\n\ |
|
318 Perform a Dulmage-Mendelsohn permutation on the sparse matrix @var{s}.\n\ |
|
319 With a single output argument @dfn{dmperm} performs the row permutations\n\ |
|
320 @var{p} such that @code{@var{s} (@var{p},:)} has no zero elements on the\n\ |
|
321 diagonal.\n\ |
|
322 \n\ |
|
323 Called with two or more output arguments, returns the row and column\n\ |
|
324 permutations, such that @code{@var{s} (@var{p}, @var{q})} is in block\n\ |
|
325 triangular form. The values of @var{r} and @var{s} define the boundaries\n\ |
|
326 of the blocks. If @var{s} is square then @code{@var{r} == @var{s}}.\n\ |
|
327 \n\ |
|
328 The method used is described in: A. Pothen & C.-J. Fan. Computing the block\n\ |
|
329 triangular form of a sparse matrix. ACM Trans. Math. Software,\n\ |
|
330 16(4):303-324, 1990.\n\ |
|
331 @seealso{colamd, ccolamd}\n\ |
|
332 @end deftypefn") |
|
333 { |
|
334 int nargin = args.length(); |
|
335 octave_value_list retval; |
|
336 |
|
337 if (nargin != 1) |
|
338 { |
|
339 print_usage (); |
|
340 return retval; |
|
341 } |
|
342 |
|
343 #if HAVE_CXSPARSE |
|
344 retval = dmperm_internal (false, args(0), nargout); |
5610
|
345 #else |
|
346 error ("dmperm: not available in this version of Octave"); |
|
347 #endif |
|
348 |
|
349 return retval; |
|
350 } |
|
351 |
6066
|
352 /* |
5610
|
353 |
|
354 %!test |
|
355 %! n=20; |
|
356 %! a=speye(n,n);a=a(randperm(n),:); |
|
357 %! assert(a(dmperm(a),:),speye(n)) |
|
358 |
|
359 %!test |
|
360 %! n=20; |
|
361 %! d=0.2; |
|
362 %! a=tril(sprandn(n,n,d),-1)+speye(n,n); |
|
363 %! a=a(randperm(n),randperm(n)); |
|
364 %! [p,q,r,s]=dmperm(a); |
|
365 %! assert(tril(a(p,q),-1),sparse(n,n)) |
|
366 |
|
367 */ |
|
368 |
6066
|
369 DEFUN_DLD (sprank, args, nargout, |
|
370 "-*- texinfo -*-\n\ |
|
371 @deftypefn {Loadable Function} {@var{p} =} sprank (@var{s})\n\ |
|
372 \n\ |
|
373 @cindex Structural Rank\n\ |
|
374 Calculates the structural rank of a sparse matrix @var{s}. Note that\n\ |
|
375 only the structure of the matrix is used in this calculation based on\n\ |
|
376 a Dulmage-Mendelsohn to block triangular form. As such the numerical\n\ |
|
377 rank of the matrix @var{s} is bounded by @code{sprank (@var{s}) >=\n\ |
|
378 rank (@var{s})}. Ignoring floating point errors @code{sprank (@var{s}) ==\n\ |
|
379 rank (@var{s})}.\n\ |
|
380 @seealso{dmperm}\n\ |
|
381 @end deftypefn") |
|
382 { |
|
383 int nargin = args.length(); |
|
384 octave_value_list retval; |
|
385 |
|
386 if (nargin != 1) |
|
387 { |
|
388 print_usage (); |
|
389 return retval; |
|
390 } |
|
391 |
|
392 #if HAVE_CXSPARSE |
|
393 retval = dmperm_internal (true, args(0), nargout); |
|
394 #else |
|
395 error ("sprank: not available in this version of Octave"); |
|
396 #endif |
|
397 |
|
398 return retval; |
|
399 } |
|
400 |
|
401 /* |
|
402 |
|
403 %!error(sprank(1,2)); |
|
404 %!assert(sprank(speye(20)), 20) |
|
405 %!assert(sprank([1,0,2,0;2,0,4,0]),2) |
|
406 |
|
407 */ |
5610
|
408 /* |
|
409 ;;; Local Variables: *** |
|
410 ;;; mode: C++ *** |
|
411 ;;; End: *** |
|
412 */ |