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 |
|
17 along with this program; see the file COPYING. If not, write to the Free |
|
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
19 |
|
20 */ |
|
21 |
|
22 #ifdef HAVE_CONFIG_H |
|
23 #include <config.h> |
|
24 #endif |
|
25 |
|
26 #include <vector> |
|
27 |
|
28 #include "lo-error.h" |
|
29 |
|
30 #include "SparseCmplxLU.h" |
|
31 #include "oct-spparms.h" |
|
32 |
|
33 // Instantiate the base LU class for the types we need. |
|
34 |
|
35 #include "sparse-base-lu.h" |
|
36 #include "sparse-base-lu.cc" |
|
37 |
|
38 template class sparse_base_lu <SparseComplexMatrix, Complex, SparseMatrix, double>; |
|
39 |
5203
|
40 #ifdef HAVE_UMFPACK |
5164
|
41 // Include the UMFPACK functions |
|
42 extern "C" { |
5203
|
43 #include <umfpack/umfpack.h> |
5164
|
44 } |
5203
|
45 #endif |
5164
|
46 |
|
47 SparseComplexLU::SparseComplexLU (const SparseComplexMatrix& a, |
|
48 double piv_thres) |
|
49 { |
5203
|
50 #ifdef HAVE_UMFPACK |
5275
|
51 octave_idx_type nr = a.rows (); |
|
52 octave_idx_type nc = a.cols (); |
5164
|
53 |
|
54 // Setup the control parameters |
|
55 Matrix Control (UMFPACK_CONTROL, 1); |
|
56 double *control = Control.fortran_vec (); |
|
57 umfpack_zi_defaults (control); |
|
58 |
|
59 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
60 if (!xisnan (tmp)) |
|
61 Control (UMFPACK_PRL) = tmp; |
|
62 if (piv_thres >= 0.) |
|
63 { |
|
64 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
65 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
66 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
67 } |
|
68 else |
|
69 { |
|
70 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
71 if (!xisnan (tmp)) |
|
72 { |
|
73 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
74 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
75 } |
|
76 } |
|
77 |
|
78 // Set whether we are allowed to modify Q or not |
|
79 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
80 if (!xisnan (tmp)) |
|
81 Control (UMFPACK_FIXQ) = tmp; |
|
82 |
|
83 // Turn-off UMFPACK scaling for LU |
|
84 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
85 |
|
86 umfpack_zi_report_control (control); |
|
87 |
5275
|
88 const octave_idx_type *Ap = a.cidx (); |
|
89 const octave_idx_type *Ai = a.ridx (); |
5164
|
90 const Complex *Ax = a.data (); |
|
91 |
|
92 umfpack_zi_report_matrix (nr, nc, Ap, Ai, X_CAST (const double *, Ax), |
|
93 NULL, 1, control); |
|
94 |
|
95 void *Symbolic; |
|
96 Matrix Info (1, UMFPACK_INFO); |
|
97 double *info = Info.fortran_vec (); |
|
98 int status = umfpack_zi_qsymbolic (nr, nc, Ap, Ai, |
|
99 X_CAST (const double *, Ax), NULL, NULL, |
|
100 &Symbolic, control, info); |
|
101 |
|
102 if (status < 0) |
|
103 { |
|
104 (*current_liboctave_error_handler) |
|
105 ("SparseComplexLU::SparseComplexLU symbolic factorization failed"); |
|
106 |
|
107 umfpack_zi_report_status (control, status); |
|
108 umfpack_zi_report_info (control, info); |
|
109 |
|
110 umfpack_zi_free_symbolic (&Symbolic) ; |
|
111 } |
|
112 else |
|
113 { |
|
114 umfpack_zi_report_symbolic (Symbolic, control); |
|
115 |
|
116 void *Numeric; |
|
117 status = umfpack_zi_numeric (Ap, Ai, X_CAST (const double *, Ax), NULL, |
|
118 Symbolic, &Numeric, control, info) ; |
|
119 umfpack_zi_free_symbolic (&Symbolic) ; |
|
120 |
|
121 cond = Info (UMFPACK_RCOND); |
|
122 |
|
123 if (status < 0) |
|
124 { |
|
125 (*current_liboctave_error_handler) |
|
126 ("SparseComplexLU::SparseComplexLU numeric factorization failed"); |
|
127 |
|
128 umfpack_zi_report_status (control, status); |
|
129 umfpack_zi_report_info (control, info); |
|
130 |
|
131 umfpack_zi_free_numeric (&Numeric); |
|
132 } |
|
133 else |
|
134 { |
|
135 umfpack_zi_report_numeric (Numeric, control); |
|
136 |
|
137 int lnz, unz, ignore1, ignore2, ignore3; |
|
138 status = umfpack_zi_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
139 &ignore3, Numeric) ; |
|
140 |
|
141 if (status < 0) |
|
142 { |
|
143 (*current_liboctave_error_handler) |
|
144 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
145 |
|
146 umfpack_zi_report_status (control, status); |
|
147 umfpack_zi_report_info (control, info); |
|
148 |
|
149 umfpack_zi_free_numeric (&Numeric); |
|
150 } |
|
151 else |
|
152 { |
|
153 int n_inner = (nr < nc ? nr : nc); |
|
154 |
|
155 if (lnz < 1) |
5275
|
156 Lfact = SparseComplexMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
157 static_cast<octave_idx_type> (1)); |
5164
|
158 else |
5275
|
159 Lfact = SparseComplexMatrix (static_cast<octave_idx_type> (n_inner), nr, |
|
160 static_cast<octave_idx_type> (lnz)); |
5164
|
161 |
5275
|
162 octave_idx_type *Ltp = Lfact.cidx (); |
|
163 octave_idx_type *Ltj = Lfact.ridx (); |
5164
|
164 Complex *Ltx = Lfact.data (); |
|
165 |
|
166 if (unz < 1) |
5275
|
167 Ufact = SparseComplexMatrix (static_cast<octave_idx_type> (n_inner), nc, |
|
168 static_cast<octave_idx_type> (1)); |
5164
|
169 else |
5275
|
170 Ufact = SparseComplexMatrix (static_cast<octave_idx_type> (n_inner), nc, unz); |
5164
|
171 |
5275
|
172 octave_idx_type *Up = Ufact.cidx (); |
|
173 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
174 Complex *Ux = Ufact.data (); |
|
175 |
|
176 P.resize (nr); |
|
177 int *p = P.fortran_vec (); |
|
178 |
|
179 Q.resize (nc); |
|
180 int *q = Q.fortran_vec (); |
|
181 |
|
182 int do_recip; |
|
183 status = umfpack_zi_get_numeric (Ltp, Ltj, X_CAST (double *, Ltx), |
|
184 NULL, Up, Uj, |
|
185 X_CAST (double *, Ux), NULL, p, |
|
186 q, NULL, NULL, &do_recip, |
|
187 NULL, Numeric) ; |
|
188 |
|
189 umfpack_zi_free_numeric (&Numeric) ; |
|
190 |
|
191 if (status < 0 || do_recip) |
|
192 { |
|
193 (*current_liboctave_error_handler) |
|
194 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
195 |
|
196 umfpack_zi_report_status (control, status); |
|
197 } |
|
198 else |
|
199 { |
|
200 Lfact = Lfact.transpose (); |
|
201 |
|
202 umfpack_zi_report_matrix (nr, n_inner, Lfact.cidx (), |
|
203 Lfact.ridx (), |
|
204 X_CAST (double *, Lfact.data()), |
|
205 NULL, 1, control); |
|
206 |
|
207 umfpack_zi_report_matrix (n_inner, nc, Ufact.cidx (), |
|
208 Ufact.ridx (), |
|
209 X_CAST (double *, Ufact.data()), |
|
210 NULL, 1, control); |
|
211 umfpack_zi_report_perm (nr, p, control); |
|
212 umfpack_zi_report_perm (nc, q, control); |
|
213 } |
|
214 |
|
215 umfpack_zi_report_info (control, info); |
|
216 } |
|
217 } |
|
218 } |
5203
|
219 #else |
|
220 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
221 #endif |
5164
|
222 } |
|
223 |
|
224 SparseComplexLU::SparseComplexLU (const SparseComplexMatrix& a, |
|
225 const ColumnVector& Qinit, |
5282
|
226 double piv_thres, bool FixedQ, |
|
227 double droptol, bool milu, bool udiag) |
5164
|
228 { |
5203
|
229 #ifdef HAVE_UMFPACK |
5282
|
230 if (milu) |
|
231 (*current_liboctave_error_handler) |
|
232 ("Modified incomplete LU not implemented"); |
5164
|
233 else |
|
234 { |
5282
|
235 octave_idx_type nr = a.rows (); |
|
236 octave_idx_type nc = a.cols (); |
5164
|
237 |
5282
|
238 // Setup the control parameters |
|
239 Matrix Control (UMFPACK_CONTROL, 1); |
|
240 double *control = Control.fortran_vec (); |
|
241 umfpack_zi_defaults (control); |
5164
|
242 |
5282
|
243 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
244 if (!xisnan (tmp)) |
|
245 Control (UMFPACK_PRL) = tmp; |
|
246 if (piv_thres >= 0.) |
|
247 { |
|
248 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
249 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
250 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
251 } |
|
252 else |
|
253 { |
|
254 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
255 if (!xisnan (tmp)) |
|
256 { |
|
257 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
258 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
259 } |
|
260 } |
5164
|
261 |
5282
|
262 if (droptol >= 0.) |
|
263 Control (UMFPACK_DROPTOL) = droptol; |
5164
|
264 |
5282
|
265 // Set whether we are allowed to modify Q or not |
|
266 if (FixedQ) |
|
267 Control (UMFPACK_FIXQ) = 1.0; |
|
268 else |
|
269 { |
|
270 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
271 if (!xisnan (tmp)) |
|
272 Control (UMFPACK_FIXQ) = tmp; |
|
273 } |
5164
|
274 |
5282
|
275 // Turn-off UMFPACK scaling for LU |
|
276 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
5164
|
277 |
5282
|
278 umfpack_zi_report_control (control); |
|
279 |
|
280 const octave_idx_type *Ap = a.cidx (); |
|
281 const octave_idx_type *Ai = a.ridx (); |
|
282 const Complex *Ax = a.data (); |
5164
|
283 |
5282
|
284 umfpack_zi_report_matrix (nr, nc, Ap, Ai, |
|
285 X_CAST (const double *, Ax), NULL, |
|
286 1, control); |
|
287 |
|
288 void *Symbolic; |
|
289 Matrix Info (1, UMFPACK_INFO); |
|
290 double *info = Info.fortran_vec (); |
|
291 int status; |
5164
|
292 |
5282
|
293 // Null loop so that qinit is imediately deallocated when not |
|
294 // needed |
|
295 do { |
|
296 OCTAVE_LOCAL_BUFFER (int, qinit, nc); |
5164
|
297 |
5282
|
298 for (int i = 0; i < nc; i++) |
|
299 qinit [i] = static_cast<int> (Qinit (i)); |
5164
|
300 |
5282
|
301 status = umfpack_zi_qsymbolic (nr, nc, Ap, Ai, |
|
302 X_CAST (const double *, Ax), |
|
303 NULL, qinit, &Symbolic, control, |
|
304 info); |
|
305 } while (0); |
5164
|
306 |
|
307 if (status < 0) |
|
308 { |
|
309 (*current_liboctave_error_handler) |
5282
|
310 ("SparseComplexLU::SparseComplexLU symbolic factorization failed"); |
5164
|
311 |
|
312 umfpack_zi_report_status (control, status); |
|
313 umfpack_zi_report_info (control, info); |
|
314 |
5282
|
315 umfpack_zi_free_symbolic (&Symbolic) ; |
5164
|
316 } |
|
317 else |
|
318 { |
5282
|
319 umfpack_zi_report_symbolic (Symbolic, control); |
5164
|
320 |
5282
|
321 void *Numeric; |
|
322 status = umfpack_zi_numeric (Ap, Ai, |
|
323 X_CAST (const double *, Ax), NULL, |
|
324 Symbolic, &Numeric, control, info) ; |
|
325 umfpack_zi_free_symbolic (&Symbolic) ; |
|
326 |
|
327 cond = Info (UMFPACK_RCOND); |
|
328 |
5164
|
329 if (status < 0) |
|
330 { |
|
331 (*current_liboctave_error_handler) |
5282
|
332 ("SparseComplexLU::SparseComplexLU numeric factorization failed"); |
5164
|
333 |
|
334 umfpack_zi_report_status (control, status); |
|
335 umfpack_zi_report_info (control, info); |
|
336 |
|
337 umfpack_zi_free_numeric (&Numeric); |
|
338 } |
|
339 else |
|
340 { |
5282
|
341 umfpack_zi_report_numeric (Numeric, control); |
5164
|
342 |
5282
|
343 int lnz, unz, ignore1, ignore2, ignore3; |
|
344 status = umfpack_zi_get_lunz (&lnz, &unz, &ignore1, |
|
345 &ignore2, &ignore3, Numeric); |
|
346 |
|
347 if (status < 0) |
5164
|
348 { |
|
349 (*current_liboctave_error_handler) |
|
350 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
351 |
|
352 umfpack_zi_report_status (control, status); |
5282
|
353 umfpack_zi_report_info (control, info); |
|
354 |
|
355 umfpack_zi_free_numeric (&Numeric); |
5164
|
356 } |
|
357 else |
|
358 { |
5282
|
359 int n_inner = (nr < nc ? nr : nc); |
|
360 |
|
361 if (lnz < 1) |
|
362 Lfact = SparseComplexMatrix |
|
363 (static_cast<octave_idx_type> (n_inner), nr, |
|
364 static_cast<octave_idx_type> (1)); |
|
365 else |
|
366 Lfact = SparseComplexMatrix |
|
367 (static_cast<octave_idx_type> (n_inner), nr, |
|
368 static_cast<octave_idx_type> (lnz)); |
|
369 |
|
370 octave_idx_type *Ltp = Lfact.cidx (); |
|
371 octave_idx_type *Ltj = Lfact.ridx (); |
|
372 Complex *Ltx = Lfact.data (); |
5164
|
373 |
5282
|
374 if (unz < 1) |
|
375 Ufact = SparseComplexMatrix |
|
376 (static_cast<octave_idx_type> (n_inner), nc, |
|
377 static_cast<octave_idx_type> (1)); |
|
378 else |
|
379 Ufact = SparseComplexMatrix |
|
380 (static_cast<octave_idx_type> (n_inner), nc, unz); |
|
381 |
|
382 octave_idx_type *Up = Ufact.cidx (); |
|
383 octave_idx_type *Uj = Ufact.ridx (); |
|
384 Complex *Ux = Ufact.data (); |
|
385 |
|
386 P.resize (nr); |
|
387 int *p = P.fortran_vec (); |
|
388 |
|
389 Q.resize (nc); |
|
390 int *q = Q.fortran_vec (); |
5164
|
391 |
5282
|
392 int do_recip; |
|
393 status = |
|
394 umfpack_zi_get_numeric (Ltp, Ltj, |
|
395 X_CAST (double *, Ltx), |
|
396 NULL, Up, Uj, |
|
397 X_CAST (double *, Ux), |
|
398 NULL, p, q, NULL, NULL, |
|
399 &do_recip, NULL, Numeric) ; |
|
400 |
|
401 umfpack_zi_free_numeric (&Numeric) ; |
|
402 |
|
403 if (status < 0 || do_recip) |
|
404 { |
|
405 (*current_liboctave_error_handler) |
|
406 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
407 |
|
408 umfpack_zi_report_status (control, status); |
|
409 } |
|
410 else |
|
411 { |
|
412 Lfact = Lfact.transpose (); |
|
413 |
|
414 umfpack_zi_report_matrix (nr, n_inner, |
|
415 Lfact.cidx (), |
|
416 Lfact.ridx (), |
|
417 X_CAST (double *, Lfact.data()), |
|
418 NULL, 1, control); |
|
419 |
|
420 umfpack_zi_report_matrix (n_inner, nc, |
|
421 Ufact.cidx (), |
|
422 Ufact.ridx (), |
|
423 X_CAST (double *, Ufact.data()), |
|
424 NULL, 1, control); |
|
425 umfpack_zi_report_perm (nr, p, control); |
|
426 umfpack_zi_report_perm (nc, q, control); |
|
427 } |
|
428 |
|
429 umfpack_zi_report_info (control, info); |
5164
|
430 } |
|
431 } |
|
432 } |
5282
|
433 |
|
434 if (udiag) |
|
435 (*current_liboctave_error_handler) |
|
436 ("Option udiag of incomplete LU not implemented"); |
5164
|
437 } |
5203
|
438 #else |
|
439 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
440 #endif |
5164
|
441 } |
|
442 |
|
443 /* |
|
444 ;;; Local Variables: *** |
|
445 ;;; mode: C++ *** |
|
446 ;;; End: *** |
|
447 */ |
|
448 |