5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "defun-dld.h" |
|
29 #include "error.h" |
|
30 #include "gripes.h" |
|
31 #include "oct-obj.h" |
|
32 #include "utils.h" |
|
33 |
|
34 #include "SparseCmplxLU.h" |
|
35 #include "SparsedbleLU.h" |
|
36 #include "ov-re-sparse.h" |
|
37 #include "ov-cx-sparse.h" |
|
38 |
5534
|
39 // PKG_ADD: dispatch ("lu", "splu", "sparse matrix"); |
|
40 // PKG_ADD: dispatch ("lu", "splu", "sparse complex matrix"); |
|
41 // PKG_ADD: dispatch ("lu", "splu", "sparse bool matrix"); |
5164
|
42 DEFUN_DLD (splu, args, nargout, |
|
43 "-*- texinfo -*-\n\ |
|
44 @deftypefn {Loadable Function} {[@var{l}, @var{u}] =} splu (@var{a})\n\ |
|
45 @deftypefnx {Loadable Function} {[@var{l}, @var{u}, @var{P}] =} splu (@var{a})\n\ |
|
46 @deftypefnx {Loadable Function} {[@var{l}, @var{u}, @var{P}, @var{Q}] =} splu (@var{a})\n\ |
|
47 @deftypefnx {Loadable Function} {[@var{l}, @var{u}, @var{P}, @var{Q}] =} splu (@dots{}, @var{thres})\n\ |
|
48 @deftypefnx {Loadable Function} {[@var{l}, @var{u}, @var{P}] =} splu (@dots{}, @var{Q})\n\ |
|
49 @cindex LU decomposition\n\ |
|
50 Compute the LU decomposition of the sparse matrix @var{a}, using\n\ |
|
51 subroutines from UMFPACK. The result is returned in a permuted\n\ |
|
52 form, according to the optional return values @var{P} and @var{Q}.\n\ |
|
53 \n\ |
|
54 Called with two or three output arguments and a single input argument,\n\ |
|
55 @dfn{splu} is a replacement for @dfn{lu}, and therefore the sparsity\n\ |
|
56 preserving column permutations @var{Q} are not performed. Called with\n\ |
|
57 a fourth output argument, the sparsity preserving column transformation\n\ |
|
58 @var{Q} is returned, such that @code{@var{P} * @var{a} * @var{Q} =\n\ |
|
59 @var{l} * @var{u}}.\n\ |
|
60 \n\ |
|
61 An additional input argument @var{thres}, that defines the pivoting\n\ |
|
62 threshold can be given. Alternatively, the desired sparsity preserving\n\ |
|
63 column permutations @var{Q} can be passed. Note that @var{Q} is assumed\n\ |
7001
|
64 to be fixed if there are fewer than four output arguments. Otherwise,\n\ |
5164
|
65 the updated column permutations are returned as the fourth argument.\n\ |
|
66 \n\ |
|
67 With two output arguments, returns the permuted forms of the upper and\n\ |
|
68 lower triangular matrices, such that @code{@var{a} = @var{l} * @var{u}}.\n\ |
|
69 With two or three output arguments, if a user-defined @var{Q} is given,\n\ |
|
70 then @code{@var{u} * @var{Q}'} is returned. The matrix is not required to\n\ |
|
71 be square.\n\ |
5642
|
72 @seealso{sparse, spinv, colamd, symamd}\n\ |
|
73 @end deftypefn") |
5164
|
74 { |
|
75 octave_value_list retval; |
|
76 |
|
77 int nargin = args.length (); |
|
78 |
|
79 if (nargin < 1 || nargin > 3 || nargout > 4) |
|
80 { |
5823
|
81 print_usage (); |
5164
|
82 return retval; |
|
83 } |
|
84 |
|
85 octave_value arg = args(0); |
|
86 |
5282
|
87 octave_idx_type nr = arg.rows (); |
|
88 octave_idx_type nc = arg.columns (); |
5164
|
89 |
|
90 int arg_is_empty = empty_arg ("splu", nr, nc); |
|
91 |
|
92 if (arg_is_empty < 0) |
|
93 return retval; |
|
94 else if (arg_is_empty > 0) |
|
95 return octave_value_list (3, SparseMatrix ()); |
|
96 |
|
97 ColumnVector Qinit; |
|
98 bool have_Qinit = false; |
|
99 double thres = -1.; |
|
100 |
|
101 for (int k = 1; k < nargin; k++) |
|
102 { |
5631
|
103 if (args(k).is_sparse_type ()) |
5164
|
104 { |
|
105 SparseMatrix tmp = args (k).sparse_matrix_value (); |
|
106 |
|
107 if (error_state) |
|
108 { |
|
109 error ("splu: Not a valid permutation/threshold"); |
|
110 return retval; |
|
111 } |
|
112 |
|
113 dim_vector dv = tmp.dims (); |
|
114 |
|
115 if (dv(0) == 1 && dv(1) == 1) |
|
116 thres = tmp (0); |
|
117 else if (dv(0) == 1 || dv(1) == 1) |
|
118 { |
5282
|
119 octave_idx_type nel = tmp.numel (); |
5164
|
120 Qinit.resize (nel); |
5282
|
121 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
122 Qinit (i) = tmp (i) - 1; |
|
123 have_Qinit = true; |
|
124 } |
|
125 else |
|
126 { |
5282
|
127 octave_idx_type t_nc = tmp.cols (); |
5164
|
128 |
5604
|
129 if (tmp.nzmax () != t_nc) |
5164
|
130 error ("splu: Not a valid permutation matrix"); |
|
131 else |
|
132 { |
5282
|
133 for (octave_idx_type i = 0; i < t_nc + 1; i++) |
5164
|
134 if (tmp.cidx(i) != i) |
|
135 { |
|
136 error ("splu: Not a valid permutation matrix"); |
|
137 break; |
|
138 } |
|
139 } |
|
140 |
|
141 if (!error_state) |
|
142 { |
5282
|
143 for (octave_idx_type i = 0; i < t_nc; i++) |
5164
|
144 if (tmp.data (i) != 1.) |
|
145 { |
|
146 error ("splu: Not a valid permutation matrix"); |
|
147 break; |
|
148 } |
|
149 else |
|
150 Qinit (i) = tmp.ridx (i) - 1; |
|
151 } |
|
152 |
|
153 if (! error_state) |
|
154 have_Qinit = true; |
|
155 } |
|
156 } |
|
157 else |
|
158 { |
|
159 NDArray tmp = args(k).array_value (); |
|
160 |
|
161 if (error_state) |
|
162 return retval; |
|
163 |
|
164 dim_vector dv = tmp.dims (); |
|
165 if (dv.length () > 2) |
|
166 { |
|
167 error ("splu: second argument must be a vector/matrix or a scalar"); |
|
168 } |
|
169 else if (dv(0) == 1 && dv(1) == 1) |
|
170 thres = tmp (0); |
|
171 else if (dv(0) == 1 || dv(1) == 1) |
|
172 { |
5282
|
173 octave_idx_type nel = tmp.numel (); |
5164
|
174 Qinit.resize (nel); |
5282
|
175 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
176 Qinit (i) = tmp (i) - 1; |
|
177 have_Qinit = true; |
|
178 } |
|
179 else |
|
180 { |
|
181 SparseMatrix tmp2 (tmp); |
|
182 |
5282
|
183 octave_idx_type t_nc = tmp2.cols (); |
5164
|
184 |
5604
|
185 if (tmp2.nzmax () != t_nc) |
5164
|
186 error ("splu: Not a valid permutation matrix"); |
|
187 else |
|
188 { |
5282
|
189 for (octave_idx_type i = 0; i < t_nc + 1; i++) |
5164
|
190 if (tmp2.cidx(i) != i) |
|
191 { |
|
192 error ("splu: Not a valid permutation matrix"); |
|
193 break; |
|
194 } |
|
195 } |
|
196 |
|
197 if (!error_state) |
|
198 { |
5282
|
199 for (octave_idx_type i = 0; i < t_nc; i++) |
5164
|
200 if (tmp2.data (i) != 1.) |
|
201 { |
|
202 error ("splu: Not a valid permutation matrix"); |
|
203 break; |
|
204 } |
|
205 else |
|
206 Qinit (i) = tmp2.ridx (i) - 1; |
|
207 } |
|
208 |
|
209 if (! error_state) |
|
210 have_Qinit = true; |
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 if (error_state) |
|
216 return retval; |
|
217 |
|
218 if (arg.is_real_type ()) |
|
219 { |
|
220 SparseMatrix m = arg.sparse_matrix_value (); |
|
221 |
|
222 if (nargout < 4 && ! have_Qinit) |
|
223 { |
5282
|
224 octave_idx_type m_nc = m.cols (); |
5164
|
225 Qinit.resize (m_nc); |
5282
|
226 for (octave_idx_type i = 0; i < m_nc; i++) |
5164
|
227 Qinit (i) = i; |
|
228 } |
|
229 |
|
230 if (! error_state) |
|
231 { |
|
232 switch (nargout) |
|
233 { |
|
234 case 0: |
|
235 case 1: |
|
236 case 2: |
|
237 { |
|
238 SparseLU fact (m, Qinit, thres, true); |
|
239 |
|
240 SparseMatrix P = fact.Pr (); |
|
241 SparseMatrix L = P.transpose () * fact.L (); |
|
242 if (have_Qinit) |
5322
|
243 retval(1) = octave_value (fact.U () * fact.Pc ().transpose (), |
5785
|
244 MatrixType (MatrixType::Permuted_Upper, nc, fact.col_perm ())); |
5164
|
245 else |
5322
|
246 retval(1) = octave_value (fact.U (), |
5785
|
247 MatrixType (MatrixType::Upper)); |
5164
|
248 |
5322
|
249 retval(0) = octave_value (L, |
5785
|
250 MatrixType (MatrixType::Permuted_Lower, nr, fact.row_perm ())); |
5164
|
251 } |
|
252 break; |
|
253 |
|
254 case 3: |
|
255 { |
|
256 SparseLU fact (m, Qinit, thres, true); |
|
257 |
|
258 retval(2) = fact.Pr (); |
|
259 if (have_Qinit) |
5322
|
260 retval(1) = octave_value (fact.U () * fact.Pc ().transpose (), |
5785
|
261 MatrixType (MatrixType::Permuted_Upper, nc, fact.col_perm ())); |
5164
|
262 else |
5322
|
263 retval(1) = octave_value (fact.U (), |
5785
|
264 MatrixType (MatrixType::Upper)); |
5322
|
265 |
|
266 retval(0) = octave_value (fact.L (), |
5785
|
267 MatrixType (MatrixType::Lower)); |
5164
|
268 } |
|
269 break; |
|
270 |
|
271 case 4: |
|
272 default: |
|
273 { |
|
274 if (have_Qinit) |
|
275 { |
|
276 SparseLU fact (m, Qinit, thres, false); |
|
277 |
|
278 retval(3) = fact.Pc (); |
|
279 retval(2) = fact.Pr (); |
5322
|
280 retval(1) = octave_value (fact.U (), |
5785
|
281 MatrixType (MatrixType::Upper)); |
5322
|
282 retval(0) = octave_value (fact.L (), |
5785
|
283 MatrixType (MatrixType::Lower)); |
5164
|
284 } |
|
285 else |
|
286 { |
|
287 SparseLU fact (m, thres); |
|
288 |
|
289 retval(3) = fact.Pc (); |
|
290 retval(2) = fact.Pr (); |
5322
|
291 retval(1) = octave_value (fact.U (), |
5785
|
292 MatrixType (MatrixType::Upper)); |
5322
|
293 retval(0) = octave_value (fact.L (), |
5785
|
294 MatrixType (MatrixType::Lower)); |
5164
|
295 } |
|
296 } |
|
297 break; |
|
298 } |
|
299 } |
|
300 } |
|
301 else if (arg.is_complex_type ()) |
|
302 { |
|
303 SparseComplexMatrix m = arg.sparse_complex_matrix_value (); |
|
304 |
|
305 if (nargout < 4 && ! have_Qinit) |
|
306 { |
5282
|
307 octave_idx_type m_nc = m.cols (); |
5164
|
308 Qinit.resize (m_nc); |
5282
|
309 for (octave_idx_type i = 0; i < m_nc; i++) |
5164
|
310 Qinit (i) = i; |
|
311 } |
|
312 |
|
313 if (! error_state) |
|
314 { |
|
315 switch (nargout) |
|
316 { |
|
317 case 0: |
|
318 case 1: |
|
319 case 2: |
|
320 { |
|
321 SparseComplexLU fact (m, Qinit, thres, true); |
|
322 |
|
323 SparseMatrix P = fact.Pr (); |
|
324 SparseComplexMatrix L = P.transpose () * fact.L (); |
|
325 |
|
326 if (have_Qinit) |
5322
|
327 retval(1) = octave_value (fact.U () * fact.Pc ().transpose (), |
5785
|
328 MatrixType (MatrixType::Permuted_Upper, nc, fact.col_perm ())); |
5164
|
329 else |
5322
|
330 retval(1) = octave_value (fact.U (), |
5785
|
331 MatrixType (MatrixType::Upper)); |
5322
|
332 |
|
333 retval(0) = octave_value (L, |
5785
|
334 MatrixType (MatrixType::Permuted_Lower, nr, fact.row_perm ())); |
5164
|
335 } |
|
336 break; |
|
337 |
|
338 case 3: |
|
339 { |
|
340 SparseComplexLU fact (m, Qinit, thres, true); |
|
341 |
|
342 retval(2) = fact.Pr (); |
|
343 if (have_Qinit) |
5322
|
344 retval(1) = octave_value (fact.U () * fact.Pc ().transpose (), |
5785
|
345 MatrixType (MatrixType::Permuted_Upper, nc, fact.col_perm ())); |
5164
|
346 else |
5322
|
347 retval(1) = octave_value (fact.U (), |
5785
|
348 MatrixType (MatrixType::Upper)); |
5322
|
349 |
|
350 retval(0) = octave_value (fact.L (), |
5785
|
351 MatrixType (MatrixType::Lower)); |
5164
|
352 } |
|
353 break; |
|
354 |
|
355 case 4: |
|
356 default: |
|
357 { |
|
358 if (have_Qinit) |
|
359 { |
|
360 SparseComplexLU fact (m, Qinit, thres, false); |
5322
|
361 |
5164
|
362 retval(3) = fact.Pc (); |
|
363 retval(2) = fact.Pr (); |
5322
|
364 retval(1) = octave_value (fact.U (), |
5785
|
365 MatrixType (MatrixType::Upper)); |
5322
|
366 retval(0) = octave_value (fact.L (), |
5785
|
367 MatrixType (MatrixType::Lower)); |
5164
|
368 } |
|
369 else |
|
370 { |
|
371 SparseComplexLU fact (m, thres); |
|
372 |
|
373 retval(3) = fact.Pc (); |
|
374 retval(2) = fact.Pr (); |
5322
|
375 retval(1) = octave_value (fact.U (), |
5785
|
376 MatrixType (MatrixType::Upper)); |
5322
|
377 retval(0) = octave_value (fact.L (), |
5785
|
378 MatrixType (MatrixType::Lower)); |
5164
|
379 } |
|
380 } |
|
381 break; |
|
382 } |
|
383 } |
|
384 } |
|
385 else |
|
386 { |
|
387 gripe_wrong_type_arg ("splu", arg); |
|
388 } |
|
389 |
|
390 return retval; |
|
391 } |
|
392 |
5534
|
393 // PKG_ADD: dispatch ("inv", "spinv", "sparse matrix"); |
|
394 // PKG_ADD: dispatch ("inv", "spinv", "sparse complex matrix"); |
|
395 // PKG_ADD: dispatch ("inv", "spinv", "sparse bool matrix"); |
|
396 // PKG_ADD: dispatch ("inverse", "spinv", "sparse matrix"); |
|
397 // PKG_ADD: dispatch ("inverse", "spinv", "sparse complex matrix"); |
|
398 // PKG_ADD: dispatch ("inverse", "spinv", "sparse bool matrix"); |
5164
|
399 DEFUN_DLD (spinv, args, nargout, |
|
400 "-*- texinfo -*-\n\ |
|
401 @deftypefn {Loadable Function} {[@var{x}, @var{rcond}] = } spinv (@var{a}, @var{Q})\n\ |
5506
|
402 Compute the inverse of the sparse square matrix @var{a}. Return an estimate\n\ |
5164
|
403 of the reciprocal condition number if requested, otherwise warn of an\n\ |
|
404 ill-conditioned matrix if the reciprocal condition number is small.\n\ |
5506
|
405 This function takes advantage of the sparsity of the matrix to accelerate\n\ |
|
406 the calculation of the inverse.\n\ |
5164
|
407 \n\ |
5506
|
408 In general @var{x} will be a full matrix, and so if possible forming the\n\ |
|
409 inverse of a sparse matrix should be avoided. It is significantly more\n\ |
|
410 accurate and faster to do @code{@var{y} = @var{a} \\ @var{b}}, rather\n\ |
|
411 than @code{@var{y} = spinv (@var{a}) * @var{b}}.\n\ |
5164
|
412 @end deftypefn") |
|
413 { |
5506
|
414 octave_value_list retval; |
|
415 |
|
416 int nargin = args.length (); |
|
417 |
|
418 if (nargin != 1) |
|
419 { |
5823
|
420 print_usage (); |
5506
|
421 return retval; |
|
422 } |
|
423 |
|
424 octave_value arg = args(0); |
|
425 |
|
426 octave_idx_type nr = arg.rows (); |
|
427 octave_idx_type nc = arg.columns (); |
|
428 |
|
429 int arg_is_empty = empty_arg ("spinverse", nr, nc); |
|
430 |
|
431 if (arg_is_empty < 0) |
|
432 return retval; |
|
433 else if (arg_is_empty > 0) |
|
434 return octave_value (Matrix ()); |
|
435 |
|
436 if (nr != nc) |
|
437 { |
|
438 gripe_square_matrix_required ("spinverse"); |
|
439 return retval; |
|
440 } |
|
441 |
|
442 if (arg.is_real_type ()) |
|
443 { |
|
444 |
|
445 SparseMatrix m = arg.sparse_matrix_value (); |
|
446 |
|
447 if (! error_state) |
|
448 { |
5785
|
449 MatrixType mattyp = args(0).matrix_type (); |
5760
|
450 |
5506
|
451 octave_idx_type info; |
|
452 double rcond = 0.0; |
|
453 SparseMatrix result = m.inverse (mattyp, info, rcond, 1); |
5760
|
454 |
5785
|
455 args(0).matrix_type (mattyp); |
5506
|
456 |
|
457 if (nargout > 1) |
|
458 retval(1) = rcond; |
|
459 |
|
460 retval(0) = result; |
|
461 |
|
462 volatile double xrcond = rcond; |
|
463 xrcond += 1.0; |
|
464 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
|
465 warning ("spinverse: matrix singular to machine precision,\ |
|
466 rcond = %g", rcond); |
|
467 } |
|
468 } |
|
469 else if (arg.is_complex_type ()) |
|
470 { |
|
471 SparseComplexMatrix m = arg.sparse_complex_matrix_value (); |
|
472 |
|
473 if (! error_state) |
|
474 { |
5785
|
475 MatrixType mattyp = args(0).matrix_type (); |
5760
|
476 |
5506
|
477 octave_idx_type info; |
|
478 double rcond = 0.0; |
|
479 |
|
480 SparseComplexMatrix result = m.inverse (mattyp, info, rcond, 1); |
5760
|
481 |
5785
|
482 args(0).matrix_type (mattyp); |
5506
|
483 |
|
484 if (nargout > 1) |
|
485 retval(1) = rcond; |
|
486 |
|
487 retval(0) = result; |
|
488 |
|
489 volatile double xrcond = rcond; |
|
490 xrcond += 1.0; |
|
491 if (nargout < 2 && (info == -1 || xrcond == 1.0)) |
|
492 warning ("spinverse: matrix singular to machine precision,\ |
|
493 rcond = %g", rcond); |
|
494 } |
|
495 } |
|
496 else |
|
497 gripe_wrong_type_arg ("spinverse", arg); |
|
498 |
|
499 return retval; |
5164
|
500 } |
|
501 |
|
502 /* |
|
503 ;;; Local Variables: *** |
|
504 ;;; mode: C++ *** |
|
505 ;;; End: *** |
|
506 */ |