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