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 |
5164
|
51 int nr = a.rows (); |
|
52 int nc = a.cols (); |
|
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 |
|
88 const int *Ap = a.cidx (); |
|
89 const int *Ai = a.ridx (); |
|
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) |
|
156 Lfact = SparseComplexMatrix (n_inner, nr, 1); |
|
157 else |
|
158 Lfact = SparseComplexMatrix (n_inner, nr, lnz); |
|
159 |
|
160 int *Ltp = Lfact.cidx (); |
|
161 int *Ltj = Lfact.ridx (); |
|
162 Complex *Ltx = Lfact.data (); |
|
163 |
|
164 if (unz < 1) |
|
165 Ufact = SparseComplexMatrix (n_inner, nc, 1); |
|
166 else |
|
167 Ufact = SparseComplexMatrix (n_inner, nc, unz); |
|
168 |
|
169 int *Up = Ufact.cidx (); |
|
170 int *Uj = Ufact.ridx (); |
|
171 Complex *Ux = Ufact.data (); |
|
172 |
|
173 P.resize (nr); |
|
174 int *p = P.fortran_vec (); |
|
175 |
|
176 Q.resize (nc); |
|
177 int *q = Q.fortran_vec (); |
|
178 |
|
179 int do_recip; |
|
180 status = umfpack_zi_get_numeric (Ltp, Ltj, X_CAST (double *, Ltx), |
|
181 NULL, Up, Uj, |
|
182 X_CAST (double *, Ux), NULL, p, |
|
183 q, NULL, NULL, &do_recip, |
|
184 NULL, Numeric) ; |
|
185 |
|
186 umfpack_zi_free_numeric (&Numeric) ; |
|
187 |
|
188 if (status < 0 || do_recip) |
|
189 { |
|
190 (*current_liboctave_error_handler) |
|
191 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
192 |
|
193 umfpack_zi_report_status (control, status); |
|
194 } |
|
195 else |
|
196 { |
|
197 Lfact = Lfact.transpose (); |
|
198 |
|
199 umfpack_zi_report_matrix (nr, n_inner, Lfact.cidx (), |
|
200 Lfact.ridx (), |
|
201 X_CAST (double *, Lfact.data()), |
|
202 NULL, 1, control); |
|
203 |
|
204 umfpack_zi_report_matrix (n_inner, nc, Ufact.cidx (), |
|
205 Ufact.ridx (), |
|
206 X_CAST (double *, Ufact.data()), |
|
207 NULL, 1, control); |
|
208 umfpack_zi_report_perm (nr, p, control); |
|
209 umfpack_zi_report_perm (nc, q, control); |
|
210 } |
|
211 |
|
212 umfpack_zi_report_info (control, info); |
|
213 } |
|
214 } |
|
215 } |
5203
|
216 #else |
|
217 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
218 #endif |
5164
|
219 } |
|
220 |
|
221 SparseComplexLU::SparseComplexLU (const SparseComplexMatrix& a, |
|
222 const ColumnVector& Qinit, |
|
223 double piv_thres, bool FixedQ) |
|
224 { |
5203
|
225 #ifdef HAVE_UMFPACK |
5164
|
226 int nr = a.rows (); |
|
227 int nc = a.cols (); |
|
228 |
|
229 // Setup the control parameters |
|
230 Matrix Control (UMFPACK_CONTROL, 1); |
|
231 double *control = Control.fortran_vec (); |
|
232 umfpack_zi_defaults (control); |
|
233 |
|
234 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
235 if (!xisnan (tmp)) |
|
236 Control (UMFPACK_PRL) = tmp; |
|
237 if (piv_thres >= 0.) |
|
238 { |
|
239 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
240 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
241 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
242 } |
|
243 else |
|
244 { |
|
245 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
246 if (!xisnan (tmp)) |
|
247 { |
|
248 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
249 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
250 } |
|
251 } |
|
252 |
|
253 // Set whether we are allowed to modify Q or not |
|
254 if (FixedQ) |
|
255 Control (UMFPACK_FIXQ) = 1.0; |
|
256 else |
|
257 { |
|
258 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
259 if (!xisnan (tmp)) |
|
260 Control (UMFPACK_FIXQ) = tmp; |
|
261 } |
|
262 |
|
263 // Turn-off UMFPACK scaling for LU |
|
264 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
265 |
|
266 umfpack_zi_report_control (control); |
|
267 |
|
268 const int *Ap = a.cidx (); |
|
269 const int *Ai = a.ridx (); |
|
270 const Complex *Ax = a.data (); |
|
271 |
|
272 umfpack_zi_report_matrix (nr, nc, Ap, Ai, X_CAST (const double *, Ax), NULL, |
|
273 1, control); |
|
274 |
|
275 void *Symbolic; |
|
276 Matrix Info (1, UMFPACK_INFO); |
|
277 double *info = Info.fortran_vec (); |
|
278 int status; |
|
279 |
|
280 // Null loop so that qinit is imediately deallocated when not needed |
|
281 do { |
|
282 OCTAVE_LOCAL_BUFFER (int, qinit, nc); |
|
283 |
|
284 for (int i = 0; i < nc; i++) |
|
285 qinit [i] = static_cast<int> (Qinit (i)); |
|
286 |
|
287 status = umfpack_zi_qsymbolic (nr, nc, Ap, Ai, X_CAST (const double *, Ax), |
|
288 NULL, qinit, &Symbolic, control, info); |
|
289 } while (0); |
|
290 |
|
291 if (status < 0) |
|
292 { |
|
293 (*current_liboctave_error_handler) |
|
294 ("SparseComplexLU::SparseComplexLU symbolic factorization failed"); |
|
295 |
|
296 umfpack_zi_report_status (control, status); |
|
297 umfpack_zi_report_info (control, info); |
|
298 |
|
299 umfpack_zi_free_symbolic (&Symbolic) ; |
|
300 } |
|
301 else |
|
302 { |
|
303 umfpack_zi_report_symbolic (Symbolic, control); |
|
304 |
|
305 void *Numeric; |
|
306 status = umfpack_zi_numeric (Ap, Ai, X_CAST (const double *, Ax), NULL, |
|
307 Symbolic, &Numeric, control, info) ; |
|
308 umfpack_zi_free_symbolic (&Symbolic) ; |
|
309 |
|
310 cond = Info (UMFPACK_RCOND); |
|
311 |
|
312 if (status < 0) |
|
313 { |
|
314 (*current_liboctave_error_handler) |
|
315 ("SparseComplexLU::SparseComplexLU numeric factorization failed"); |
|
316 |
|
317 umfpack_zi_report_status (control, status); |
|
318 umfpack_zi_report_info (control, info); |
|
319 |
|
320 umfpack_zi_free_numeric (&Numeric); |
|
321 } |
|
322 else |
|
323 { |
|
324 umfpack_zi_report_numeric (Numeric, control); |
|
325 |
|
326 int lnz, unz, ignore1, ignore2, ignore3; |
|
327 status = umfpack_zi_get_lunz (&lnz, &unz, &ignore1, &ignore2, |
|
328 &ignore3, Numeric) ; |
|
329 |
|
330 if (status < 0) |
|
331 { |
|
332 (*current_liboctave_error_handler) |
|
333 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
334 |
|
335 umfpack_zi_report_status (control, status); |
|
336 umfpack_zi_report_info (control, info); |
|
337 |
|
338 umfpack_zi_free_numeric (&Numeric); |
|
339 } |
|
340 else |
|
341 { |
|
342 int n_inner = (nr < nc ? nr : nc); |
|
343 |
|
344 if (lnz < 1) |
|
345 Lfact = SparseComplexMatrix (n_inner, nr, 1); |
|
346 else |
|
347 Lfact = SparseComplexMatrix (n_inner, nr, lnz); |
|
348 |
|
349 int *Ltp = Lfact.cidx (); |
|
350 int *Ltj = Lfact.ridx (); |
|
351 Complex *Ltx = Lfact.data (); |
|
352 |
|
353 if (unz < 1) |
|
354 Ufact = SparseComplexMatrix (n_inner, nc, 1); |
|
355 else |
|
356 Ufact = SparseComplexMatrix (n_inner, nc, unz); |
|
357 |
|
358 int *Up = Ufact.cidx (); |
|
359 int *Uj = Ufact.ridx (); |
|
360 Complex *Ux = Ufact.data (); |
|
361 |
|
362 P.resize (nr); |
|
363 int *p = P.fortran_vec (); |
|
364 |
|
365 Q.resize (nc); |
|
366 int *q = Q.fortran_vec (); |
|
367 |
|
368 int do_recip; |
|
369 status = umfpack_zi_get_numeric (Ltp, Ltj, X_CAST (double *, Ltx), |
|
370 NULL, Up, Uj, |
|
371 X_CAST (double *, Ux), NULL, p, |
|
372 q, NULL, NULL, &do_recip, |
|
373 NULL, Numeric) ; |
|
374 |
|
375 umfpack_zi_free_numeric (&Numeric) ; |
|
376 |
|
377 if (status < 0 || do_recip) |
|
378 { |
|
379 (*current_liboctave_error_handler) |
|
380 ("SparseComplexLU::SparseComplexLU extracting LU factors failed"); |
|
381 |
|
382 umfpack_zi_report_status (control, status); |
|
383 } |
|
384 else |
|
385 { |
|
386 Lfact = Lfact.transpose (); |
|
387 |
|
388 umfpack_zi_report_matrix (nr, n_inner, Lfact.cidx (), |
|
389 Lfact.ridx (), |
|
390 X_CAST (double *, Lfact.data()), |
|
391 NULL, 1, control); |
|
392 |
|
393 umfpack_zi_report_matrix (n_inner, nc, Ufact.cidx (), |
|
394 Ufact.ridx (), |
|
395 X_CAST (double *, Ufact.data()), |
|
396 NULL, 1, control); |
|
397 umfpack_zi_report_perm (nr, p, control); |
|
398 umfpack_zi_report_perm (nc, q, control); |
|
399 } |
|
400 |
|
401 umfpack_zi_report_info (control, info); |
|
402 } |
|
403 } |
|
404 } |
5203
|
405 #else |
|
406 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
407 #endif |
5164
|
408 } |
|
409 |
|
410 /* |
|
411 ;;; Local Variables: *** |
|
412 ;;; mode: C++ *** |
|
413 ;;; End: *** |
|
414 */ |
|
415 |