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 "SparsedbleLU.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 <SparseMatrix, double, SparseMatrix, double>; |
|
40 |
5451
|
41 #include "oct-sparse.h" |
5164
|
42 |
|
43 SparseLU::SparseLU (const SparseMatrix& a, double piv_thres) |
|
44 { |
5203
|
45 #ifdef HAVE_UMFPACK |
5275
|
46 octave_idx_type nr = a.rows (); |
|
47 octave_idx_type nc = a.cols (); |
5164
|
48 |
|
49 // Setup the control parameters |
|
50 Matrix Control (UMFPACK_CONTROL, 1); |
|
51 double *control = Control.fortran_vec (); |
5322
|
52 UMFPACK_DNAME (defaults) (control); |
5164
|
53 |
|
54 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
55 if (!xisnan (tmp)) |
|
56 Control (UMFPACK_PRL) = tmp; |
|
57 |
|
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 { |
|
66 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
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 |
|
75 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
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_DNAME (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 double *Ax = a.data (); |
|
87 |
5322
|
88 UMFPACK_DNAME (report_matrix) (nr, nc, Ap, Ai, Ax, 1, control); |
5164
|
89 |
|
90 void *Symbolic; |
|
91 Matrix Info (1, UMFPACK_INFO); |
|
92 double *info = Info.fortran_vec (); |
5322
|
93 int status = UMFPACK_DNAME (qsymbolic) (nr, nc, Ap, Ai, Ax, NULL, |
5164
|
94 &Symbolic, control, info); |
|
95 |
|
96 if (status < 0) |
|
97 { |
|
98 (*current_liboctave_error_handler) |
|
99 ("SparseLU::SparseLU symbolic factorization failed"); |
|
100 |
5322
|
101 UMFPACK_DNAME (report_status) (control, status); |
|
102 UMFPACK_DNAME (report_info) (control, info); |
5164
|
103 |
5322
|
104 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
105 } |
|
106 else |
|
107 { |
5322
|
108 UMFPACK_DNAME (report_symbolic) (Symbolic, control); |
5164
|
109 |
|
110 void *Numeric; |
5322
|
111 status = UMFPACK_DNAME (numeric) (Ap, Ai, Ax, Symbolic, |
|
112 &Numeric, control, info) ; |
|
113 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
114 |
|
115 cond = Info (UMFPACK_RCOND); |
|
116 |
|
117 if (status < 0) |
|
118 { |
|
119 (*current_liboctave_error_handler) |
|
120 ("SparseLU::SparseLU numeric factorization failed"); |
|
121 |
5322
|
122 UMFPACK_DNAME (report_status) (control, status); |
|
123 UMFPACK_DNAME (report_info) (control, info); |
5164
|
124 |
5322
|
125 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
126 } |
|
127 else |
|
128 { |
5322
|
129 UMFPACK_DNAME (report_numeric) (Numeric, control); |
5164
|
130 |
5322
|
131 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
132 status = UMFPACK_DNAME (get_lunz) (&lnz, &unz, &ignore1, |
|
133 &ignore2, &ignore3, Numeric) ; |
5164
|
134 |
|
135 if (status < 0) |
|
136 { |
|
137 (*current_liboctave_error_handler) |
|
138 ("SparseLU::SparseLU extracting LU factors failed"); |
|
139 |
5322
|
140 UMFPACK_DNAME (report_status) (control, status); |
|
141 UMFPACK_DNAME (report_info) (control, info); |
5164
|
142 |
5322
|
143 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
144 } |
|
145 else |
|
146 { |
5322
|
147 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5164
|
148 |
|
149 if (lnz < 1) |
5322
|
150 Lfact = SparseMatrix (n_inner, nr, |
5275
|
151 static_cast<octave_idx_type> (1)); |
5164
|
152 else |
5322
|
153 Lfact = SparseMatrix (n_inner, nr, lnz); |
5164
|
154 |
5275
|
155 octave_idx_type *Ltp = Lfact.cidx (); |
|
156 octave_idx_type *Ltj = Lfact.ridx (); |
5164
|
157 double *Ltx = Lfact.data (); |
|
158 |
|
159 if (unz < 1) |
5322
|
160 Ufact = SparseMatrix (n_inner, nc, |
5275
|
161 static_cast<octave_idx_type> (1)); |
5164
|
162 else |
5322
|
163 Ufact = SparseMatrix (n_inner, nc, unz); |
5164
|
164 |
5275
|
165 octave_idx_type *Up = Ufact.cidx (); |
|
166 octave_idx_type *Uj = Ufact.ridx (); |
5164
|
167 double *Ux = Ufact.data (); |
|
168 |
|
169 P.resize (nr); |
5322
|
170 octave_idx_type *p = P.fortran_vec (); |
5164
|
171 |
|
172 Q.resize (nc); |
5322
|
173 octave_idx_type *q = Q.fortran_vec (); |
5164
|
174 |
5322
|
175 octave_idx_type do_recip; |
|
176 status = UMFPACK_DNAME (get_numeric) (Ltp, Ltj, Ltx, |
5760
|
177 Up, Uj, Ux, p, q, NULL, |
|
178 &do_recip, NULL, |
5164
|
179 Numeric) ; |
|
180 |
5322
|
181 UMFPACK_DNAME (free_numeric) (&Numeric) ; |
5164
|
182 |
|
183 if (status < 0 || do_recip) |
|
184 { |
|
185 (*current_liboctave_error_handler) |
|
186 ("SparseLU::SparseLU extracting LU factors failed"); |
|
187 |
5322
|
188 UMFPACK_DNAME (report_status) (control, status); |
5164
|
189 } |
|
190 else |
|
191 { |
|
192 Lfact = Lfact.transpose (); |
|
193 |
5322
|
194 UMFPACK_DNAME (report_matrix) (nr, n_inner, |
|
195 Lfact.cidx (), Lfact.ridx (), |
|
196 Lfact.data (), 1, control); |
|
197 UMFPACK_DNAME (report_matrix) (n_inner, nc, |
|
198 Ufact.cidx (), Ufact.ridx (), |
|
199 Ufact.data (), 1, control); |
|
200 UMFPACK_DNAME (report_perm) (nr, p, control); |
|
201 UMFPACK_DNAME (report_perm) (nc, q, control); |
5164
|
202 } |
|
203 |
5322
|
204 UMFPACK_DNAME (report_info) (control, info); |
5164
|
205 } |
|
206 } |
|
207 } |
5203
|
208 #else |
|
209 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
210 #endif |
5164
|
211 } |
|
212 |
|
213 SparseLU::SparseLU (const SparseMatrix& a, const ColumnVector& Qinit, |
5282
|
214 double piv_thres, bool FixedQ, double droptol, |
|
215 bool milu, bool udiag) |
5164
|
216 { |
5203
|
217 #ifdef HAVE_UMFPACK |
5282
|
218 if (milu) |
|
219 (*current_liboctave_error_handler) |
|
220 ("Modified incomplete LU not implemented"); |
5164
|
221 else |
|
222 { |
5282
|
223 octave_idx_type nr = a.rows (); |
|
224 octave_idx_type nc = a.cols (); |
5164
|
225 |
5282
|
226 // Setup the control parameters |
|
227 Matrix Control (UMFPACK_CONTROL, 1); |
|
228 double *control = Control.fortran_vec (); |
5322
|
229 UMFPACK_DNAME (defaults) (control); |
5164
|
230 |
5282
|
231 double tmp = Voctave_sparse_controls.get_key ("spumoni"); |
|
232 if (!xisnan (tmp)) |
|
233 Control (UMFPACK_PRL) = tmp; |
|
234 if (piv_thres >= 0.) |
|
235 { |
|
236 piv_thres = (piv_thres > 1. ? 1. : piv_thres); |
|
237 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = piv_thres; |
|
238 Control (UMFPACK_PIVOT_TOLERANCE) = piv_thres; |
|
239 } |
|
240 else |
|
241 { |
|
242 tmp = Voctave_sparse_controls.get_key ("piv_tol"); |
|
243 if (!xisnan (tmp)) |
|
244 { |
|
245 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
246 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
247 } |
|
248 } |
5164
|
249 |
5282
|
250 if (droptol >= 0.) |
|
251 Control (UMFPACK_DROPTOL) = droptol; |
5164
|
252 |
|
253 |
5282
|
254 // Set whether we are allowed to modify Q or not |
|
255 if (FixedQ) |
|
256 Control (UMFPACK_FIXQ) = 1.0; |
|
257 else |
|
258 { |
|
259 tmp = Voctave_sparse_controls.get_key ("autoamd"); |
|
260 if (!xisnan (tmp)) |
|
261 Control (UMFPACK_FIXQ) = tmp; |
|
262 } |
5164
|
263 |
5282
|
264 // Turn-off UMFPACK scaling for LU |
|
265 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
5164
|
266 |
5322
|
267 UMFPACK_DNAME (report_control) (control); |
5164
|
268 |
5282
|
269 const octave_idx_type *Ap = a.cidx (); |
|
270 const octave_idx_type *Ai = a.ridx (); |
|
271 const double *Ax = a.data (); |
|
272 |
5322
|
273 UMFPACK_DNAME (report_matrix) (nr, nc, Ap, Ai, Ax, 1, |
|
274 control); |
5282
|
275 |
|
276 void *Symbolic; |
|
277 Matrix Info (1, UMFPACK_INFO); |
|
278 double *info = Info.fortran_vec (); |
|
279 int status; |
5164
|
280 |
5282
|
281 // Null loop so that qinit is imediately deallocated when not needed |
|
282 do { |
5322
|
283 OCTAVE_LOCAL_BUFFER (octave_idx_type, qinit, nc); |
5164
|
284 |
5322
|
285 for (octave_idx_type i = 0; i < nc; i++) |
|
286 qinit [i] = static_cast<octave_idx_type> (Qinit (i)); |
5164
|
287 |
5322
|
288 status = UMFPACK_DNAME (qsymbolic) (nr, nc, Ap, Ai, Ax, |
|
289 qinit, &Symbolic, control, info); |
5282
|
290 } while (0); |
5164
|
291 |
|
292 if (status < 0) |
|
293 { |
|
294 (*current_liboctave_error_handler) |
5282
|
295 ("SparseLU::SparseLU symbolic factorization failed"); |
5164
|
296 |
5322
|
297 UMFPACK_DNAME (report_status) (control, status); |
|
298 UMFPACK_DNAME (report_info) (control, info); |
5164
|
299 |
5322
|
300 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5164
|
301 } |
|
302 else |
|
303 { |
5322
|
304 UMFPACK_DNAME (report_symbolic) (Symbolic, control); |
5164
|
305 |
5282
|
306 void *Numeric; |
5322
|
307 status = UMFPACK_DNAME (numeric) (Ap, Ai, Ax, Symbolic, |
|
308 &Numeric, control, info) ; |
|
309 UMFPACK_DNAME (free_symbolic) (&Symbolic) ; |
5282
|
310 |
|
311 cond = Info (UMFPACK_RCOND); |
|
312 |
5164
|
313 if (status < 0) |
|
314 { |
|
315 (*current_liboctave_error_handler) |
5282
|
316 ("SparseLU::SparseLU numeric factorization failed"); |
5164
|
317 |
5322
|
318 UMFPACK_DNAME (report_status) (control, status); |
|
319 UMFPACK_DNAME (report_info) (control, info); |
5164
|
320 |
5322
|
321 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
322 } |
|
323 else |
|
324 { |
5322
|
325 UMFPACK_DNAME (report_numeric) (Numeric, control); |
5164
|
326 |
5322
|
327 octave_idx_type lnz, unz, ignore1, ignore2, ignore3; |
|
328 status = UMFPACK_DNAME (get_lunz) (&lnz, &unz, &ignore1, &ignore2, |
|
329 &ignore3, Numeric) ; |
5282
|
330 |
|
331 if (status < 0) |
5164
|
332 { |
|
333 (*current_liboctave_error_handler) |
|
334 ("SparseLU::SparseLU extracting LU factors failed"); |
|
335 |
5322
|
336 UMFPACK_DNAME (report_status) (control, status); |
|
337 UMFPACK_DNAME (report_info) (control, info); |
5282
|
338 |
5322
|
339 UMFPACK_DNAME (free_numeric) (&Numeric); |
5164
|
340 } |
|
341 else |
|
342 { |
5322
|
343 octave_idx_type n_inner = (nr < nc ? nr : nc); |
5282
|
344 |
|
345 if (lnz < 1) |
5322
|
346 Lfact = SparseMatrix (n_inner, nr, |
|
347 static_cast<octave_idx_type> (1)); |
5282
|
348 else |
5322
|
349 Lfact = SparseMatrix (n_inner, nr, lnz); |
5282
|
350 |
|
351 octave_idx_type *Ltp = Lfact.cidx (); |
|
352 octave_idx_type *Ltj = Lfact.ridx (); |
|
353 double *Ltx = Lfact.data (); |
|
354 |
|
355 if (unz < 1) |
5322
|
356 Ufact = SparseMatrix (n_inner, nc, |
|
357 static_cast<octave_idx_type> (1)); |
5282
|
358 else |
5322
|
359 Ufact = SparseMatrix (n_inner, nc, unz); |
5282
|
360 |
|
361 octave_idx_type *Up = Ufact.cidx (); |
|
362 octave_idx_type *Uj = Ufact.ridx (); |
|
363 double *Ux = Ufact.data (); |
|
364 |
|
365 P.resize (nr); |
5322
|
366 octave_idx_type *p = P.fortran_vec (); |
5282
|
367 |
|
368 Q.resize (nc); |
5322
|
369 octave_idx_type *q = Q.fortran_vec (); |
5282
|
370 |
5322
|
371 octave_idx_type do_recip; |
|
372 status = UMFPACK_DNAME (get_numeric) (Ltp, Ltj, |
|
373 Ltx, Up, Uj, Ux, p, q, |
5760
|
374 NULL, &do_recip, |
|
375 NULL, Numeric) ; |
5282
|
376 |
5322
|
377 UMFPACK_DNAME (free_numeric) (&Numeric) ; |
5282
|
378 |
|
379 if (status < 0 || do_recip) |
|
380 { |
|
381 (*current_liboctave_error_handler) |
|
382 ("SparseLU::SparseLU extracting LU factors failed"); |
|
383 |
5322
|
384 UMFPACK_DNAME (report_status) (control, status); |
5282
|
385 } |
|
386 else |
|
387 { |
|
388 Lfact = Lfact.transpose (); |
5322
|
389 UMFPACK_DNAME (report_matrix) (nr, n_inner, |
5282
|
390 Lfact.cidx (), |
|
391 Lfact.ridx (), |
|
392 Lfact.data (), |
|
393 1, control); |
5322
|
394 UMFPACK_DNAME (report_matrix) (n_inner, nc, |
5282
|
395 Ufact.cidx (), |
|
396 Ufact.ridx (), |
|
397 Ufact.data (), |
|
398 1, control); |
5322
|
399 UMFPACK_DNAME (report_perm) (nr, p, control); |
|
400 UMFPACK_DNAME (report_perm) (nc, q, control); |
5282
|
401 } |
|
402 |
5322
|
403 UMFPACK_DNAME (report_info) (control, info); |
5164
|
404 } |
|
405 } |
|
406 } |
5282
|
407 |
|
408 if (udiag) |
|
409 (*current_liboctave_error_handler) |
|
410 ("Option udiag of incomplete LU not implemented"); |
5164
|
411 } |
5203
|
412 #else |
|
413 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
414 #endif |
5164
|
415 } |
|
416 |
|
417 /* |
|
418 ;;; Local Variables: *** |
|
419 ;;; mode: C++ *** |
|
420 ;;; End: *** |
|
421 */ |
|
422 |