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 <vector> |
|
28 |
|
29 #include "lo-error.h" |
|
30 |
|
31 #include "SparseCmplxLU.h" |
|
32 #include "oct-spparms.h" |
|
33 |
|
34 // Instantiate the base LU class for the types we need. |
|
35 |
|
36 #include "sparse-base-lu.h" |
|
37 #include "sparse-base-lu.cc" |
|
38 |
|
39 template class sparse_base_lu <SparseComplexMatrix, Complex, SparseMatrix, double>; |
|
40 |
5451
|
41 #include "oct-sparse.h" |
5164
|
42 |
|
43 SparseComplexLU::SparseComplexLU (const SparseComplexMatrix& a, |
|
44 double piv_thres) |
|
45 { |
5203
|
46 #ifdef HAVE_UMFPACK |
5275
|
47 octave_idx_type nr = a.rows (); |
|
48 octave_idx_type nc = a.cols (); |
5164
|
49 |
|
50 // Setup the control parameters |
|
51 Matrix Control (UMFPACK_CONTROL, 1); |
|
52 double *control = Control.fortran_vec (); |
5322
|
53 UMFPACK_ZNAME (defaults) (control); |
5164
|
54 |
5893
|
55 double tmp = octave_sparse_params::get_key ("spumoni"); |
5164
|
56 if (!xisnan (tmp)) |
|
57 Control (UMFPACK_PRL) = tmp; |
|
58 if (piv_thres >= 0.) |
|
59 { |
|
60 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
61 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
62 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
63 } |
|
64 else |
|
65 { |
5893
|
66 tmp = octave_sparse_params::get_key ("piv_tol"); |
5164
|
67 if (!xisnan (tmp)) |
|
68 { |
|
69 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
70 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
71 } |
|
72 } |
|
73 |
|
74 // Set whether we are allowed to modify Q or not |
5893
|
75 tmp = octave_sparse_params::get_key ("autoamd"); |
5164
|
76 if (!xisnan (tmp)) |
|
77 Control (UMFPACK_FIXQ) = tmp; |
|
78 |
|
79 // Turn-off UMFPACK scaling for LU |
|
80 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
81 |
5322
|
82 UMFPACK_ZNAME (report_control) (control); |
5164
|
83 |
5275
|
84 const octave_idx_type *Ap = a.cidx (); |
|
85 const octave_idx_type *Ai = a.ridx (); |
5164
|
86 const Complex *Ax = a.data (); |
|
87 |
5322
|
88 UMFPACK_ZNAME (report_matrix) (nr, nc, Ap, Ai, |
5760
|
89 reinterpret_cast<const double *> (Ax), |
|
90 NULL, 1, control); |
5164
|
91 |
|
92 void *Symbolic; |
|
93 Matrix Info (1, UMFPACK_INFO); |
|
94 double *info = Info.fortran_vec (); |
5322
|
95 int status = UMFPACK_ZNAME (qsymbolic) (nr, nc, Ap, Ai, |
5760
|
96 reinterpret_cast<const double *> (Ax), |
|
97 NULL, NULL, |
|
98 &Symbolic, control, info); |
5164
|
99 |
|
100 if (status < 0) |
|
101 { |
|
102 (*current_liboctave_error_handler) |
|
103 ("SparseComplexLU::SparseComplexLU symbolic factorization failed"); |
|
104 |
5322
|
105 UMFPACK_ZNAME (report_status) (control, status); |
|
106 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
107 |
5322
|
108 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
109 } |
|
110 else |
|
111 { |
5322
|
112 UMFPACK_ZNAME (report_symbolic) (Symbolic, control); |
5164
|
113 |
|
114 void *Numeric; |
5322
|
115 status = UMFPACK_ZNAME (numeric) (Ap, Ai, |
5760
|
116 reinterpret_cast<const double *> (Ax), |
|
117 NULL, Symbolic, &Numeric, control, |
|
118 info); |
5322
|
119 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
120 |
|
121 cond = Info (UMFPACK_RCOND); |
|
122 |
|
123 if (status < 0) |
|
124 { |
|
125 (*current_liboctave_error_handler) |
|
126 ("SparseComplexLU::SparseComplexLU numeric factorization failed"); |
|
127 |
5322
|
128 UMFPACK_ZNAME (report_status) (control, status); |
|
129 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
130 |
5322
|
131 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
132 } |
|
133 else |
|
134 { |
5322
|
135 UMFPACK_ZNAME (report_numeric) (Numeric, control); |
5164
|
136 |
5322
|
137 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
138 status = UMFPACK_ZNAME (get_lunz) (&lnz, &unz, &ignore1, |
|
139 &ignore2, &ignore3, Numeric) ; |
5164
|
140 |
|
141 if (status < 0) |
|
142 { |
|
143 (*current_liboctave_error_handler) |
|
144 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
145 |
5322
|
146 UMFPACK_ZNAME (report_status) (control, status); |
|
147 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
148 |
5322
|
149 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
150 } |
|
151 else |
|
152 { |
5322
|
153 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5164
|
154 |
|
155 if (lnz < 1) |
5322
|
156 Lfact = SparseComplexMatrix (n_inner, nr, |
5275
|
157 static_cast<octave_idx_type> (1)); |
5164
|
158 else |
5322
|
159 Lfact = SparseComplexMatrix (n_inner, nr, lnz); |
5164
|
160 |
5275
|
161 octave_idx_type *Ltp = Lfact.cidx (); |
|
162 octave_idx_type *Ltj = Lfact.ridx (); |
5164
|
163 Complex *Ltx = Lfact.data (); |
|
164 |
|
165 if (unz < 1) |
5322
|
166 Ufact = SparseComplexMatrix (n_inner, nc, |
5275
|
167 static_cast<octave_idx_type> (1)); |
5164
|
168 else |
5322
|
169 Ufact = SparseComplexMatrix (n_inner, nc, unz); |
5164
|
170 |
5275
|
171 octave_idx_type *Up = Ufact.cidx (); |
|
172 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
173 Complex *Ux = Ufact.data (); |
|
174 |
|
175 P.resize (nr); |
5322
|
176 octave_idx_type *p = P.fortran_vec (); |
5164
|
177 |
|
178 Q.resize (nc); |
5322
|
179 octave_idx_type *q = Q.fortran_vec (); |
5164
|
180 |
5322
|
181 octave_idx_type do_recip; |
|
182 status = UMFPACK_ZNAME (get_numeric) (Ltp, Ltj, |
5760
|
183 reinterpret_cast<double *> (Ltx), |
|
184 NULL, Up, Uj, |
|
185 reinterpret_cast <double *> (Ux), |
|
186 NULL, p, q, NULL, NULL, |
|
187 &do_recip, NULL, Numeric); |
5164
|
188 |
5322
|
189 UMFPACK_ZNAME (free_numeric) (&Numeric) ; |
5164
|
190 |
|
191 if (status < 0 || do_recip) |
|
192 { |
|
193 (*current_liboctave_error_handler) |
|
194 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
195 |
5322
|
196 UMFPACK_ZNAME (report_status) (control, status); |
5164
|
197 } |
|
198 else |
|
199 { |
|
200 Lfact = Lfact.transpose (); |
|
201 |
5322
|
202 UMFPACK_ZNAME (report_matrix) (nr, n_inner, |
|
203 Lfact.cidx (), Lfact.ridx (), |
5760
|
204 reinterpret_cast<double *> (Lfact.data()), |
5164
|
205 NULL, 1, control); |
|
206 |
5322
|
207 UMFPACK_ZNAME (report_matrix) (n_inner, nc, |
|
208 Ufact.cidx (), Ufact.ridx (), |
5760
|
209 reinterpret_cast<double *> (Ufact.data()), |
5164
|
210 NULL, 1, control); |
5322
|
211 UMFPACK_ZNAME (report_perm) (nr, p, control); |
|
212 UMFPACK_ZNAME (report_perm) (nc, q, control); |
5164
|
213 } |
|
214 |
5322
|
215 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
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 (); |
5322
|
241 UMFPACK_ZNAME (defaults) (control); |
5164
|
242 |
5893
|
243 double tmp = octave_sparse_params::get_key ("spumoni"); |
5282
|
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 { |
5893
|
254 tmp = octave_sparse_params::get_key ("piv_tol"); |
5282
|
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 { |
5893
|
270 tmp = octave_sparse_params::get_key ("autoamd"); |
5282
|
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 |
5322
|
278 UMFPACK_ZNAME (report_control) (control); |
5282
|
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 |
5322
|
284 UMFPACK_ZNAME (report_matrix) (nr, nc, Ap, Ai, |
5760
|
285 reinterpret_cast<const double *> (Ax), NULL, |
5282
|
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 { |
5322
|
296 OCTAVE_LOCAL_BUFFER (octave_idx_type, qinit, nc); |
5164
|
297 |
5322
|
298 for (octave_idx_type i = 0; i < nc; i++) |
|
299 qinit [i] = static_cast<octave_idx_type> (Qinit (i)); |
5164
|
300 |
5322
|
301 status = UMFPACK_ZNAME (qsymbolic) (nr, nc, Ap, Ai, |
5760
|
302 reinterpret_cast<const double *> (Ax), |
5282
|
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 |
5322
|
312 UMFPACK_ZNAME (report_status) (control, status); |
|
313 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
314 |
5322
|
315 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
316 } |
|
317 else |
|
318 { |
5322
|
319 UMFPACK_ZNAME (report_symbolic) (Symbolic, control); |
5164
|
320 |
5282
|
321 void *Numeric; |
5322
|
322 status = UMFPACK_ZNAME (numeric) (Ap, Ai, |
5760
|
323 reinterpret_cast<const double *> (Ax), NULL, |
5282
|
324 Symbolic, &Numeric, control, info) ; |
5322
|
325 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5282
|
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 |
5322
|
334 UMFPACK_ZNAME (report_status) (control, status); |
|
335 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
336 |
5322
|
337 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
338 } |
|
339 else |
|
340 { |
5322
|
341 UMFPACK_ZNAME (report_numeric) (Numeric, control); |
5164
|
342 |
5322
|
343 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
344 status = UMFPACK_ZNAME (get_lunz) (&lnz, &unz, |
|
345 &ignore1, &ignore2, &ignore3, Numeric); |
5282
|
346 |
|
347 if (status < 0) |
5164
|
348 { |
|
349 (*current_liboctave_error_handler) |
|
350 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
351 |
5322
|
352 UMFPACK_ZNAME (report_status) (control, status); |
|
353 UMFPACK_ZNAME (report_info) (control, info); |
5282
|
354 |
5322
|
355 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
356 } |
|
357 else |
|
358 { |
5322
|
359 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5282
|
360 |
|
361 if (lnz < 1) |
5322
|
362 Lfact = SparseComplexMatrix (n_inner, nr, |
5282
|
363 static_cast<octave_idx_type> (1)); |
|
364 else |
5322
|
365 Lfact = SparseComplexMatrix (n_inner, nr, lnz); |
5282
|
366 |
|
367 octave_idx_type *Ltp = Lfact.cidx (); |
|
368 octave_idx_type *Ltj = Lfact.ridx (); |
|
369 Complex *Ltx = Lfact.data (); |
5164
|
370 |
5282
|
371 if (unz < 1) |
5322
|
372 Ufact = SparseComplexMatrix (n_inner, nc, |
5282
|
373 static_cast<octave_idx_type> (1)); |
|
374 else |
5322
|
375 Ufact = SparseComplexMatrix (n_inner, nc, unz); |
5282
|
376 |
|
377 octave_idx_type *Up = Ufact.cidx (); |
|
378 octave_idx_type *Uj = Ufact.ridx (); |
|
379 Complex *Ux = Ufact.data (); |
|
380 |
|
381 P.resize (nr); |
5322
|
382 octave_idx_type *p = P.fortran_vec (); |
5282
|
383 |
|
384 Q.resize (nc); |
5322
|
385 octave_idx_type *q = Q.fortran_vec (); |
5164
|
386 |
5322
|
387 octave_idx_type do_recip; |
5282
|
388 status = |
5322
|
389 UMFPACK_ZNAME (get_numeric) (Ltp, Ltj, |
5760
|
390 reinterpret_cast<double *> (Ltx), |
5282
|
391 NULL, Up, Uj, |
5760
|
392 reinterpret_cast<double *> (Ux), |
5282
|
393 NULL, p, q, NULL, NULL, |
|
394 &do_recip, NULL, Numeric) ; |
|
395 |
5322
|
396 UMFPACK_ZNAME (free_numeric) (&Numeric) ; |
5282
|
397 |
|
398 if (status < 0 || do_recip) |
|
399 { |
|
400 (*current_liboctave_error_handler) |
|
401 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
402 |
5322
|
403 UMFPACK_ZNAME (report_status) (control, |
|
404 status); |
5282
|
405 } |
|
406 else |
|
407 { |
|
408 Lfact = Lfact.transpose (); |
|
409 |
5322
|
410 UMFPACK_ZNAME (report_matrix) (nr, n_inner, |
5282
|
411 Lfact.cidx (), |
|
412 Lfact.ridx (), |
5760
|
413 reinterpret_cast<double *> (Lfact.data()), |
5282
|
414 NULL, 1, control); |
|
415 |
5322
|
416 UMFPACK_ZNAME (report_matrix) (n_inner, nc, |
5282
|
417 Ufact.cidx (), |
|
418 Ufact.ridx (), |
5760
|
419 reinterpret_cast<double *> (Ufact.data()), |
5282
|
420 NULL, 1, control); |
5322
|
421 UMFPACK_ZNAME (report_perm) (nr, p, control); |
|
422 UMFPACK_ZNAME (report_perm) (nc, q, control); |
5282
|
423 } |
|
424 |
5322
|
425 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
426 } |
|
427 } |
|
428 } |
5282
|
429 |
|
430 if (udiag) |
|
431 (*current_liboctave_error_handler) |
|
432 ("Option udiag of incomplete LU not implemented"); |
5164
|
433 } |
5203
|
434 #else |
|
435 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
436 #endif |
5164
|
437 } |
|
438 |
|
439 /* |
|
440 ;;; Local Variables: *** |
|
441 ;;; mode: C++ *** |
|
442 ;;; End: *** |
|
443 */ |
|
444 |