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