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 |
|
40 #define CSSPARSE_NAME(name) name ## _dl |
|
41 #else |
|
42 #define CSSPARSE_NAME(name) name ## _di |
|
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\ |
|
73 \n\ |
|
74 @end deftypefn\n\ |
|
75 @seealso{spchol, qr}") |
|
76 { |
|
77 int nargin = args.length (); |
|
78 octave_value_list retval; |
|
79 bool economy = false; |
|
80 bool is_cmplx = false; |
|
81 bool have_b = false; |
|
82 |
|
83 if (nargin < 1 || nargin > 3) |
|
84 print_usage ("spqr"); |
|
85 else |
|
86 { |
|
87 if (args(0).is_complex_type ()) |
|
88 is_cmplx = true; |
|
89 if (nargin > 1) |
|
90 { |
|
91 have_b = true; |
|
92 if (args(nargin-1).is_scalar_type ()) |
|
93 { |
|
94 int val = args(nargin-1).int_value (); |
|
95 if (val == 0) |
|
96 { |
|
97 economy = true; |
|
98 have_b = (nargin > 2); |
|
99 } |
|
100 } |
|
101 if (have_b && args(1).is_complex_type ()) |
|
102 is_cmplx = true; |
|
103 } |
|
104 |
|
105 if (!error_state) |
|
106 { |
|
107 if (have_b && nargout < 2) |
|
108 error ("spqr: incorrect number of output arguments"); |
|
109 else if (is_cmplx) |
|
110 { |
|
111 SparseComplexQR q (args(0).sparse_complex_matrix_value ()); |
|
112 if (!error_state) |
|
113 { |
|
114 if (have_b) |
|
115 { |
|
116 retval(1) = q.R (economy); |
|
117 retval(0) = q.C (args(1).complex_matrix_value ()); |
|
118 } |
|
119 else |
|
120 retval(0) = q.R (economy); |
|
121 } |
|
122 } |
|
123 else |
|
124 { |
|
125 SparseQR q (args(0).sparse_matrix_value ()); |
|
126 if (!error_state) |
|
127 { |
|
128 if (have_b) |
|
129 { |
|
130 retval(1) = q.R (economy); |
|
131 retval(0) = q.C (args(1).matrix_value ()); |
|
132 } |
|
133 else |
|
134 retval(0) = q.R (economy); |
|
135 } |
|
136 } |
|
137 } |
|
138 } |
|
139 return retval; |
|
140 } |
|
141 |
|
142 /* |
|
143 |
|
144 The deactivated tests below can't be tested till rectangular back-subs is |
|
145 implemented for sparse matrices. |
|
146 |
|
147 %!test |
|
148 %! n = 20; d= 0.2; |
|
149 %! a = sprandn(n,n,d)+speye(n,n); |
|
150 %! r = spqr(a); |
|
151 %! assert(r'*r,a'*a,1e-10) |
|
152 |
|
153 %!test |
|
154 %! n = 20; d= 0.2; |
|
155 %! a = sprandn(n,n,d)+speye(n,n); |
|
156 %! q = symamd(a); |
|
157 %! a = a(q,q); |
|
158 %! r = spqr(a); |
|
159 %! assert(r'*r,a'*a,1e-10) |
|
160 |
|
161 %!test |
|
162 %! n = 20; d= 0.2; |
|
163 %! a = sprandn(n,n,d)+speye(n,n); |
|
164 %! [c,r] = spqr(a,ones(n,1)); |
|
165 %! assert (r\c,full(a)\ones(n,1),10e-10) |
|
166 |
|
167 %!test |
|
168 %! n = 20; d= 0.2; |
|
169 %! a = sprandn(n,n,d)+speye(n,n); |
|
170 %! b = randn(n,2); |
|
171 %! [c,r] = spqr(a,b); |
|
172 %! assert (r\c,full(a)\b,10e-10) |
|
173 |
|
174 %% Test under-determined systems!! |
|
175 %!#test |
|
176 %! n = 20; d= 0.2; |
|
177 %! a = sprandn(n,n+1,d)+speye(n,n+1); |
|
178 %! b = randn(n,2); |
|
179 %! [c,r] = spqr(a,b); |
|
180 %! assert (r\c,full(a)\b,10e-10) |
|
181 |
|
182 %!test |
|
183 %! n = 20; d= 0.2; |
|
184 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
185 %! r = spqr(a); |
|
186 %! assert(r'*r,a'*a,1e-10) |
|
187 |
|
188 %!test |
|
189 %! n = 20; d= 0.2; |
|
190 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
191 %! q = symamd(a); |
|
192 %! a = a(q,q); |
|
193 %! r = spqr(a); |
|
194 %! assert(r'*r,a'*a,1e-10) |
|
195 |
|
196 %!test |
|
197 %! n = 20; d= 0.2; |
|
198 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
199 %! [c,r] = spqr(a,ones(n,1)); |
|
200 %! assert (r\c,full(a)\ones(n,1),10e-10) |
|
201 |
|
202 %!test |
|
203 %! n = 20; d= 0.2; |
|
204 %! a = 1i*sprandn(n,n,d)+speye(n,n); |
|
205 %! b = randn(n,2); |
|
206 %! [c,r] = spqr(a,b); |
|
207 %! assert (r\c,full(a)\b,10e-10) |
|
208 |
|
209 %% Test under-determined systems!! |
|
210 %!#test |
|
211 %! n = 20; d= 0.2; |
|
212 %! a = 1i*sprandn(n,n+1,d)+speye(n,n+1); |
|
213 %! b = randn(n,2); |
|
214 %! [c,r] = spqr(a,b); |
|
215 %! assert (r\c,full(a)\b,10e-10) |
|
216 |
|
217 %!error spqr(sprandn(10,10,0.2),ones(10,1)); |
|
218 |
|
219 */ |
|
220 |
|
221 static RowVector |
|
222 put_int (octave_idx_type *p, octave_idx_type n) |
|
223 { |
|
224 RowVector ret (n); |
|
225 for (octave_idx_type i = 0; i < n; i++) |
|
226 ret.xelem(i) = p[i] + 1; |
|
227 return ret; |
|
228 } |
|
229 |
|
230 DEFUN_DLD (dmperm, args, nargout, |
|
231 "-*- texinfo -*-\n\ |
|
232 @deftypefn {Loadable Function} {@var{p} =} dmperm (@var{s})\n\ |
|
233 @deftypefnx {Loadable Function} {[@var{p}. @var{q}. @var{r}, @var{s}] =} dmperm (@var{s})\n\ |
|
234 \n\ |
|
235 @cindex Dulmage-Mendelsohn decomposition\n\ |
|
236 Perform a Deulmage-Mendelsohn permutation on the sparse matrix @var{s}.\n\ |
|
237 With a single output argument @dfn{dmperm} performs the row permutations\n\ |
|
238 @var{p} such that @code{@var{s} (@var{p},:)} has no zero elements on the\n\ |
|
239 diagonal.\n\ |
|
240 \n\ |
|
241 Called with two or more output arguments, returns the row and column\n\ |
|
242 permutations, such that @code{@var{s} (@var{p}, @var{q})} is in block\n\ |
|
243 triangular form. The values of @var{r} and @var{s} define the boundaries\n\ |
|
244 of the blocks. If @var{s} is square then @code{@var{r} == @var{s}}.\n\ |
|
245 \n\ |
|
246 The method used is described in: A. Pothen & C.-J. Fan. Computing the block\n\ |
|
247 triangular form of a sparse matrix. ACM Trans. Math. Software,\n\ |
|
248 16(4):303-324, 1990.\n\ |
|
249 @end deftypefn\n\ |
|
250 @seealso{colamd,ccolamd}") |
|
251 { |
|
252 int nargin = args.length(); |
|
253 octave_value_list retval; |
|
254 |
|
255 #if HAVE_CXSPARSE |
|
256 if (nargin != 1) |
|
257 { |
|
258 print_usage ("dmperm"); |
|
259 return retval; |
|
260 } |
|
261 |
|
262 octave_value arg = args(0); |
|
263 octave_idx_type nr = arg.rows (); |
|
264 octave_idx_type nc = arg.columns (); |
|
265 SparseMatrix m; |
|
266 SparseComplexMatrix cm; |
|
267 CSSPARSE_NAME (cs) csm; |
|
268 csm.m = nr; |
|
269 csm.n = nc; |
|
270 csm.x = NULL; |
|
271 csm.nz = -1; |
|
272 |
|
273 if (arg.is_real_type ()) |
|
274 { |
|
275 m = arg.sparse_matrix_value (); |
|
276 csm.nzmax = m.nnz(); |
|
277 csm.p = m.xcidx (); |
|
278 csm.i = m.xridx (); |
|
279 } |
|
280 else |
|
281 { |
|
282 cm = arg.sparse_complex_matrix_value (); |
|
283 csm.nzmax = cm.nnz(); |
|
284 csm.p = cm.xcidx (); |
|
285 csm.i = cm.xridx (); |
|
286 } |
|
287 |
|
288 if (!error_state) |
|
289 { |
|
290 if (nargout <= 1) |
|
291 { |
|
292 octave_idx_type *jmatch = CSSPARSE_NAME (cs_maxtrans) (&csm); |
|
293 retval(0) = put_int (jmatch + nr, nc); |
|
294 CSSPARSE_NAME (cs_free) (jmatch); |
|
295 } |
|
296 else |
|
297 { |
|
298 CSSPARSE_NAME (csd) *dm = CSSPARSE_NAME(cs_dmperm) (&csm); |
|
299 //retval(5) = put_int (dm->rr, 5); |
|
300 //retval(4) = put_int (dm->cc, 5); |
|
301 retval(3) = put_int (dm->S, dm->nb+1); |
|
302 retval(2) = put_int (dm->R, dm->nb+1); |
|
303 retval(1) = put_int (dm->Q, nc); |
|
304 retval(0) = put_int (dm->P, nr); |
|
305 CSSPARSE_NAME (cs_dfree) (dm); |
|
306 } |
|
307 } |
|
308 #else |
|
309 error ("dmperm: not available in this version of Octave"); |
|
310 #endif |
|
311 |
|
312 return retval; |
|
313 } |
|
314 |
|
315 /* |
|
316 |
|
317 %!test |
|
318 %! n=20; |
|
319 %! a=speye(n,n);a=a(randperm(n),:); |
|
320 %! assert(a(dmperm(a),:),speye(n)) |
|
321 |
|
322 %!test |
|
323 %! n=20; |
|
324 %! d=0.2; |
|
325 %! a=tril(sprandn(n,n,d),-1)+speye(n,n); |
|
326 %! a=a(randperm(n),randperm(n)); |
|
327 %! [p,q,r,s]=dmperm(a); |
|
328 %! assert(tril(a(p,q),-1),sparse(n,n)) |
|
329 |
|
330 */ |
|
331 |
|
332 /* |
|
333 ;;; Local Variables: *** |
|
334 ;;; mode: C++ *** |
|
335 ;;; End: *** |
|
336 */ |