5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cfloat> |
|
29 |
|
30 #include <iostream> |
|
31 #include <vector> |
|
32 |
|
33 #include "quit.h" |
|
34 #include "lo-ieee.h" |
|
35 #include "lo-mappers.h" |
|
36 #include "f77-fcn.h" |
|
37 #include "dRowVector.h" |
|
38 |
|
39 #include "CSparse.h" |
|
40 #include "boolSparse.h" |
|
41 #include "dSparse.h" |
|
42 #include "oct-spparms.h" |
|
43 #include "SparseCmplxLU.h" |
5451
|
44 #include "oct-sparse.h" |
5506
|
45 #include "sparse-util.h" |
|
46 #include "SparseCmplxCHOL.h" |
5610
|
47 #include "SparseCmplxQR.h" |
5164
|
48 |
5587
|
49 #include "oct-sort.h" |
|
50 |
5681
|
51 // Define whether to use a basic QR solver or one that uses a Dulmange |
|
52 // Mendelsohn factorization to seperate the problem into under-determined, |
|
53 // well-determined and over-determined parts and solves them seperately |
|
54 #ifndef USE_QRSOLVE |
|
55 #include "sparse-dmsolve.cc" |
|
56 #endif |
|
57 |
5164
|
58 // Fortran functions we call. |
|
59 extern "C" |
|
60 { |
|
61 F77_RET_T |
5275
|
62 F77_FUNC (zgbtrf, ZGBTRF) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
63 const octave_idx_type&, Complex*, const octave_idx_type&, octave_idx_type*, octave_idx_type&); |
5164
|
64 |
|
65 F77_RET_T |
5275
|
66 F77_FUNC (zgbtrs, ZGBTRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
67 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
68 const Complex*, const octave_idx_type&, |
|
69 const octave_idx_type*, Complex*, const octave_idx_type&, octave_idx_type& |
5164
|
70 F77_CHAR_ARG_LEN_DECL); |
|
71 |
|
72 F77_RET_T |
5275
|
73 F77_FUNC (zgbcon, ZGBCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
74 const octave_idx_type&, const octave_idx_type&, Complex*, |
|
75 const octave_idx_type&, const octave_idx_type*, const double&, |
|
76 double&, Complex*, double*, octave_idx_type& |
5164
|
77 F77_CHAR_ARG_LEN_DECL); |
|
78 |
|
79 F77_RET_T |
5275
|
80 F77_FUNC (zpbtrf, ZPBTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
81 const octave_idx_type&, Complex*, const octave_idx_type&, octave_idx_type& |
5164
|
82 F77_CHAR_ARG_LEN_DECL); |
|
83 |
|
84 F77_RET_T |
5275
|
85 F77_FUNC (zpbtrs, ZPBTRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
86 const octave_idx_type&, const octave_idx_type&, Complex*, const octave_idx_type&, |
|
87 Complex*, const octave_idx_type&, octave_idx_type& |
5164
|
88 F77_CHAR_ARG_LEN_DECL); |
|
89 |
|
90 F77_RET_T |
5275
|
91 F77_FUNC (zpbcon, ZPBCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
92 const octave_idx_type&, Complex*, const octave_idx_type&, |
5681
|
93 const double&, double&, Complex*, double*, octave_idx_type& |
5164
|
94 F77_CHAR_ARG_LEN_DECL); |
|
95 |
|
96 F77_RET_T |
5275
|
97 F77_FUNC (zgttrf, ZGTTRF) (const octave_idx_type&, Complex*, Complex*, Complex*, |
|
98 Complex*, octave_idx_type*, octave_idx_type&); |
5164
|
99 |
|
100 F77_RET_T |
5275
|
101 F77_FUNC (zgttrs, ZGTTRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
102 const octave_idx_type&, const Complex*, const Complex*, |
|
103 const Complex*, const Complex*, const octave_idx_type*, |
|
104 Complex *, const octave_idx_type&, octave_idx_type& |
5164
|
105 F77_CHAR_ARG_LEN_DECL); |
|
106 |
|
107 F77_RET_T |
5322
|
108 F77_FUNC (zptsv, ZPTSV) (const octave_idx_type&, const octave_idx_type&, double*, Complex*, |
5275
|
109 Complex*, const octave_idx_type&, octave_idx_type&); |
5164
|
110 |
|
111 F77_RET_T |
5275
|
112 F77_FUNC (zgtsv, ZGTSV) (const octave_idx_type&, const octave_idx_type&, Complex*, Complex*, |
|
113 Complex*, Complex*, const octave_idx_type&, octave_idx_type&); |
5164
|
114 } |
|
115 |
|
116 SparseComplexMatrix::SparseComplexMatrix (const SparseMatrix& a) |
5681
|
117 : MSparse<Complex> (a.rows (), a.cols (), a.nnz ()) |
5164
|
118 { |
5275
|
119 octave_idx_type nc = cols (); |
5681
|
120 octave_idx_type nz = a.nnz (); |
5275
|
121 |
|
122 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
123 cidx (i) = a.cidx (i); |
|
124 |
5275
|
125 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
126 { |
5681
|
127 data (i) = Complex (a.data (i)); |
5164
|
128 ridx (i) = a.ridx (i); |
|
129 } |
|
130 } |
|
131 |
|
132 SparseComplexMatrix::SparseComplexMatrix (const SparseBoolMatrix& a) |
5681
|
133 : MSparse<Complex> (a.rows (), a.cols (), a.nnz ()) |
5164
|
134 { |
5275
|
135 octave_idx_type nc = cols (); |
5681
|
136 octave_idx_type nz = a.nnz (); |
5275
|
137 |
|
138 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
139 cidx (i) = a.cidx (i); |
|
140 |
5275
|
141 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
142 { |
5681
|
143 data (i) = Complex (a.data (i)); |
5164
|
144 ridx (i) = a.ridx (i); |
|
145 } |
|
146 } |
|
147 |
|
148 bool |
|
149 SparseComplexMatrix::operator == (const SparseComplexMatrix& a) const |
|
150 { |
5275
|
151 octave_idx_type nr = rows (); |
|
152 octave_idx_type nc = cols (); |
5681
|
153 octave_idx_type nz = nnz (); |
5275
|
154 octave_idx_type nr_a = a.rows (); |
|
155 octave_idx_type nc_a = a.cols (); |
5681
|
156 octave_idx_type nz_a = a.nnz (); |
5164
|
157 |
|
158 if (nr != nr_a || nc != nc_a || nz != nz_a) |
|
159 return false; |
|
160 |
5275
|
161 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
162 if (cidx(i) != a.cidx(i)) |
|
163 return false; |
|
164 |
5275
|
165 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
166 if (data(i) != a.data(i) || ridx(i) != a.ridx(i)) |
|
167 return false; |
|
168 |
|
169 return true; |
|
170 } |
|
171 |
|
172 bool |
|
173 SparseComplexMatrix::operator != (const SparseComplexMatrix& a) const |
|
174 { |
|
175 return !(*this == a); |
|
176 } |
|
177 |
|
178 bool |
|
179 SparseComplexMatrix::is_hermitian (void) const |
|
180 { |
5275
|
181 octave_idx_type nr = rows (); |
|
182 octave_idx_type nc = cols (); |
5164
|
183 |
6207
|
184 if (nr == nc && nr > 0) |
5164
|
185 { |
6207
|
186 for (octave_idx_type j = 0; j < nc; j++) |
|
187 { |
|
188 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
189 { |
|
190 octave_idx_type ri = ridx(i); |
|
191 |
|
192 if (ri != j) |
|
193 { |
|
194 bool found = false; |
|
195 |
|
196 for (octave_idx_type k = cidx(ri); k < cidx(ri+1); k++) |
|
197 { |
|
198 if (ridx(k) == j) |
|
199 { |
|
200 if (data(i) == conj(data(k))) |
|
201 found = true; |
|
202 break; |
|
203 } |
|
204 } |
|
205 |
|
206 if (! found) |
|
207 return false; |
|
208 } |
|
209 } |
|
210 } |
5164
|
211 |
|
212 return true; |
|
213 } |
|
214 |
|
215 return false; |
|
216 } |
|
217 |
|
218 static const Complex Complex_NaN_result (octave_NaN, octave_NaN); |
|
219 |
|
220 SparseComplexMatrix |
|
221 SparseComplexMatrix::max (int dim) const |
|
222 { |
5275
|
223 Array2<octave_idx_type> dummy_idx; |
5164
|
224 return max (dummy_idx, dim); |
|
225 } |
|
226 |
|
227 SparseComplexMatrix |
5275
|
228 SparseComplexMatrix::max (Array2<octave_idx_type>& idx_arg, int dim) const |
5164
|
229 { |
|
230 SparseComplexMatrix result; |
|
231 dim_vector dv = dims (); |
|
232 |
|
233 if (dv.numel () == 0 || dim > dv.length () || dim < 0) |
|
234 return result; |
|
235 |
5275
|
236 octave_idx_type nr = dv(0); |
|
237 octave_idx_type nc = dv(1); |
5164
|
238 |
|
239 if (dim == 0) |
|
240 { |
|
241 idx_arg.resize (1, nc); |
5275
|
242 octave_idx_type nel = 0; |
|
243 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
244 { |
|
245 Complex tmp_max; |
|
246 double abs_max = octave_NaN; |
5275
|
247 octave_idx_type idx_j = 0; |
|
248 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
249 { |
|
250 if (ridx(i) != idx_j) |
|
251 break; |
|
252 else |
|
253 idx_j++; |
|
254 } |
|
255 |
|
256 if (idx_j != nr) |
|
257 { |
|
258 tmp_max = 0.; |
|
259 abs_max = 0.; |
|
260 } |
|
261 |
5275
|
262 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
263 { |
|
264 Complex tmp = data (i); |
|
265 |
5389
|
266 if (xisnan (tmp)) |
5164
|
267 continue; |
|
268 |
5261
|
269 double abs_tmp = std::abs (tmp); |
5164
|
270 |
5389
|
271 if (xisnan (abs_max) || abs_tmp > abs_max) |
5164
|
272 { |
|
273 idx_j = ridx (i); |
|
274 tmp_max = tmp; |
|
275 abs_max = abs_tmp; |
|
276 } |
|
277 } |
|
278 |
5389
|
279 idx_arg.elem (j) = xisnan (tmp_max) ? 0 : idx_j; |
5164
|
280 if (abs_max != 0.) |
|
281 nel++; |
|
282 } |
|
283 |
|
284 result = SparseComplexMatrix (1, nc, nel); |
|
285 |
5275
|
286 octave_idx_type ii = 0; |
5164
|
287 result.xcidx (0) = 0; |
5275
|
288 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
289 { |
|
290 Complex tmp = elem (idx_arg(j), j); |
|
291 if (tmp != 0.) |
|
292 { |
|
293 result.xdata (ii) = tmp; |
|
294 result.xridx (ii++) = 0; |
|
295 } |
|
296 result.xcidx (j+1) = ii; |
|
297 } |
|
298 } |
|
299 else |
|
300 { |
|
301 idx_arg.resize (nr, 1, 0); |
|
302 |
5275
|
303 for (octave_idx_type i = cidx(0); i < cidx(1); i++) |
5164
|
304 idx_arg.elem(ridx(i)) = -1; |
|
305 |
5275
|
306 for (octave_idx_type j = 0; j < nc; j++) |
|
307 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
308 { |
|
309 if (idx_arg.elem(i) != -1) |
|
310 continue; |
|
311 bool found = false; |
5275
|
312 for (octave_idx_type k = cidx(j); k < cidx(j+1); k++) |
5164
|
313 if (ridx(k) == i) |
|
314 { |
|
315 found = true; |
|
316 break; |
|
317 } |
|
318 |
|
319 if (!found) |
|
320 idx_arg.elem(i) = j; |
|
321 |
|
322 } |
|
323 |
5275
|
324 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
325 { |
5275
|
326 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
327 { |
5275
|
328 octave_idx_type ir = ridx (i); |
|
329 octave_idx_type ix = idx_arg.elem (ir); |
5164
|
330 Complex tmp = data (i); |
|
331 |
5389
|
332 if (xisnan (tmp)) |
5164
|
333 continue; |
5261
|
334 else if (ix == -1 || std::abs(tmp) > std::abs(elem (ir, ix))) |
5164
|
335 idx_arg.elem (ir) = j; |
|
336 } |
|
337 } |
|
338 |
5275
|
339 octave_idx_type nel = 0; |
|
340 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
341 if (idx_arg.elem(j) == -1 || elem (j, idx_arg.elem (j)) != 0.) |
|
342 nel++; |
|
343 |
|
344 result = SparseComplexMatrix (nr, 1, nel); |
|
345 |
5275
|
346 octave_idx_type ii = 0; |
5164
|
347 result.xcidx (0) = 0; |
|
348 result.xcidx (1) = nel; |
5275
|
349 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
350 { |
|
351 if (idx_arg(j) == -1) |
|
352 { |
|
353 idx_arg(j) = 0; |
|
354 result.xdata (ii) = Complex_NaN_result; |
|
355 result.xridx (ii++) = j; |
|
356 } |
|
357 else |
|
358 { |
|
359 Complex tmp = elem (j, idx_arg(j)); |
|
360 if (tmp != 0.) |
|
361 { |
|
362 result.xdata (ii) = tmp; |
|
363 result.xridx (ii++) = j; |
|
364 } |
|
365 } |
|
366 } |
|
367 } |
|
368 |
|
369 return result; |
|
370 } |
|
371 |
|
372 SparseComplexMatrix |
|
373 SparseComplexMatrix::min (int dim) const |
|
374 { |
5275
|
375 Array2<octave_idx_type> dummy_idx; |
5164
|
376 return min (dummy_idx, dim); |
|
377 } |
|
378 |
|
379 SparseComplexMatrix |
5275
|
380 SparseComplexMatrix::min (Array2<octave_idx_type>& idx_arg, int dim) const |
5164
|
381 { |
|
382 SparseComplexMatrix result; |
|
383 dim_vector dv = dims (); |
|
384 |
|
385 if (dv.numel () == 0 || dim > dv.length () || dim < 0) |
|
386 return result; |
|
387 |
5275
|
388 octave_idx_type nr = dv(0); |
|
389 octave_idx_type nc = dv(1); |
5164
|
390 |
|
391 if (dim == 0) |
|
392 { |
|
393 idx_arg.resize (1, nc); |
5275
|
394 octave_idx_type nel = 0; |
|
395 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
396 { |
|
397 Complex tmp_min; |
|
398 double abs_min = octave_NaN; |
5275
|
399 octave_idx_type idx_j = 0; |
|
400 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
401 { |
|
402 if (ridx(i) != idx_j) |
|
403 break; |
|
404 else |
|
405 idx_j++; |
|
406 } |
|
407 |
|
408 if (idx_j != nr) |
|
409 { |
|
410 tmp_min = 0.; |
|
411 abs_min = 0.; |
|
412 } |
|
413 |
5275
|
414 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
415 { |
|
416 Complex tmp = data (i); |
|
417 |
5389
|
418 if (xisnan (tmp)) |
5164
|
419 continue; |
|
420 |
5261
|
421 double abs_tmp = std::abs (tmp); |
5164
|
422 |
5389
|
423 if (xisnan (abs_min) || abs_tmp < abs_min) |
5164
|
424 { |
|
425 idx_j = ridx (i); |
|
426 tmp_min = tmp; |
|
427 abs_min = abs_tmp; |
|
428 } |
|
429 } |
|
430 |
5389
|
431 idx_arg.elem (j) = xisnan (tmp_min) ? 0 : idx_j; |
5164
|
432 if (abs_min != 0.) |
|
433 nel++; |
|
434 } |
|
435 |
|
436 result = SparseComplexMatrix (1, nc, nel); |
|
437 |
5275
|
438 octave_idx_type ii = 0; |
5164
|
439 result.xcidx (0) = 0; |
5275
|
440 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
441 { |
|
442 Complex tmp = elem (idx_arg(j), j); |
|
443 if (tmp != 0.) |
|
444 { |
|
445 result.xdata (ii) = tmp; |
|
446 result.xridx (ii++) = 0; |
|
447 } |
|
448 result.xcidx (j+1) = ii; |
|
449 } |
|
450 } |
|
451 else |
|
452 { |
|
453 idx_arg.resize (nr, 1, 0); |
|
454 |
5275
|
455 for (octave_idx_type i = cidx(0); i < cidx(1); i++) |
5164
|
456 idx_arg.elem(ridx(i)) = -1; |
|
457 |
5275
|
458 for (octave_idx_type j = 0; j < nc; j++) |
|
459 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
460 { |
|
461 if (idx_arg.elem(i) != -1) |
|
462 continue; |
|
463 bool found = false; |
5275
|
464 for (octave_idx_type k = cidx(j); k < cidx(j+1); k++) |
5164
|
465 if (ridx(k) == i) |
|
466 { |
|
467 found = true; |
|
468 break; |
|
469 } |
|
470 |
|
471 if (!found) |
|
472 idx_arg.elem(i) = j; |
|
473 |
|
474 } |
|
475 |
5275
|
476 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
477 { |
5275
|
478 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
479 { |
5275
|
480 octave_idx_type ir = ridx (i); |
|
481 octave_idx_type ix = idx_arg.elem (ir); |
5164
|
482 Complex tmp = data (i); |
|
483 |
5389
|
484 if (xisnan (tmp)) |
5164
|
485 continue; |
5261
|
486 else if (ix == -1 || std::abs(tmp) < std::abs(elem (ir, ix))) |
5164
|
487 idx_arg.elem (ir) = j; |
|
488 } |
|
489 } |
|
490 |
5275
|
491 octave_idx_type nel = 0; |
|
492 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
493 if (idx_arg.elem(j) == -1 || elem (j, idx_arg.elem (j)) != 0.) |
|
494 nel++; |
|
495 |
|
496 result = SparseComplexMatrix (nr, 1, nel); |
|
497 |
5275
|
498 octave_idx_type ii = 0; |
5164
|
499 result.xcidx (0) = 0; |
|
500 result.xcidx (1) = nel; |
5275
|
501 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
502 { |
|
503 if (idx_arg(j) == -1) |
|
504 { |
|
505 idx_arg(j) = 0; |
|
506 result.xdata (ii) = Complex_NaN_result; |
|
507 result.xridx (ii++) = j; |
|
508 } |
|
509 else |
|
510 { |
|
511 Complex tmp = elem (j, idx_arg(j)); |
|
512 if (tmp != 0.) |
|
513 { |
|
514 result.xdata (ii) = tmp; |
|
515 result.xridx (ii++) = j; |
|
516 } |
|
517 } |
|
518 } |
|
519 } |
|
520 |
|
521 return result; |
|
522 } |
|
523 |
|
524 // destructive insert/delete/reorder operations |
|
525 |
|
526 SparseComplexMatrix& |
5275
|
527 SparseComplexMatrix::insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c) |
5164
|
528 { |
|
529 SparseComplexMatrix tmp (a); |
6060
|
530 return insert (tmp /*a*/, r, c); |
5164
|
531 } |
|
532 |
|
533 SparseComplexMatrix& |
5275
|
534 SparseComplexMatrix::insert (const SparseComplexMatrix& a, octave_idx_type r, octave_idx_type c) |
5164
|
535 { |
|
536 MSparse<Complex>::insert (a, r, c); |
|
537 return *this; |
|
538 } |
|
539 |
6823
|
540 SparseComplexMatrix& |
|
541 SparseComplexMatrix::insert (const SparseMatrix& a, const Array<octave_idx_type>& indx) |
|
542 { |
|
543 SparseComplexMatrix tmp (a); |
|
544 return insert (tmp /*a*/, indx); |
|
545 } |
|
546 |
|
547 SparseComplexMatrix& |
|
548 SparseComplexMatrix::insert (const SparseComplexMatrix& a, const Array<octave_idx_type>& indx) |
|
549 { |
|
550 MSparse<Complex>::insert (a, indx); |
|
551 return *this; |
|
552 } |
|
553 |
5164
|
554 SparseComplexMatrix |
|
555 SparseComplexMatrix::concat (const SparseComplexMatrix& rb, |
5275
|
556 const Array<octave_idx_type>& ra_idx) |
5164
|
557 { |
|
558 // Don't use numel to avoid all possiblity of an overflow |
|
559 if (rb.rows () > 0 && rb.cols () > 0) |
|
560 insert (rb, ra_idx(0), ra_idx(1)); |
|
561 return *this; |
|
562 } |
|
563 |
|
564 SparseComplexMatrix |
5275
|
565 SparseComplexMatrix::concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164
|
566 { |
|
567 SparseComplexMatrix tmp (rb); |
|
568 if (rb.rows () > 0 && rb.cols () > 0) |
|
569 insert (tmp, ra_idx(0), ra_idx(1)); |
|
570 return *this; |
|
571 } |
|
572 |
|
573 ComplexMatrix |
|
574 SparseComplexMatrix::matrix_value (void) const |
|
575 { |
5275
|
576 octave_idx_type nr = rows (); |
|
577 octave_idx_type nc = cols (); |
5164
|
578 ComplexMatrix retval (nr, nc, Complex (0.0, 0.0)); |
|
579 |
5275
|
580 for (octave_idx_type j = 0; j < nc; j++) |
|
581 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
582 retval.elem (ridx(i), j) = data (i); |
|
583 |
|
584 return retval; |
|
585 } |
|
586 |
|
587 SparseComplexMatrix |
|
588 SparseComplexMatrix::hermitian (void) const |
|
589 { |
5275
|
590 octave_idx_type nr = rows (); |
|
591 octave_idx_type nc = cols (); |
5681
|
592 octave_idx_type nz = nnz (); |
5164
|
593 SparseComplexMatrix retval (nc, nr, nz); |
|
594 |
5648
|
595 OCTAVE_LOCAL_BUFFER (octave_idx_type, w, nr + 1); |
|
596 for (octave_idx_type i = 0; i < nr; i++) |
|
597 w[i] = 0; |
|
598 for (octave_idx_type i = 0; i < nz; i++) |
|
599 w[ridx(i)]++; |
|
600 nz = 0; |
|
601 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
602 { |
5648
|
603 retval.xcidx(i) = nz; |
|
604 nz += w[i]; |
|
605 w[i] = retval.xcidx(i); |
5164
|
606 } |
5648
|
607 retval.xcidx(nr) = nz; |
|
608 w[nr] = nz; |
|
609 |
|
610 for (octave_idx_type j = 0; j < nc; j++) |
|
611 for (octave_idx_type k = cidx(j); k < cidx(j+1); k++) |
|
612 { |
|
613 octave_idx_type q = w [ridx(k)]++; |
|
614 retval.xridx (q) = j; |
|
615 retval.xdata (q) = conj (data (k)); |
|
616 } |
5164
|
617 |
|
618 return retval; |
|
619 } |
|
620 |
|
621 SparseComplexMatrix |
|
622 conj (const SparseComplexMatrix& a) |
|
623 { |
5275
|
624 octave_idx_type nr = a.rows (); |
|
625 octave_idx_type nc = a.cols (); |
5681
|
626 octave_idx_type nz = a.nnz (); |
5164
|
627 SparseComplexMatrix retval (nc, nr, nz); |
|
628 |
5275
|
629 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
630 retval.cidx (i) = a.cidx (i); |
|
631 |
5275
|
632 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
633 { |
|
634 retval.data (i) = conj (a.data (i)); |
|
635 retval.ridx (i) = a.ridx (i); |
|
636 } |
|
637 |
|
638 return retval; |
|
639 } |
|
640 |
|
641 SparseComplexMatrix |
|
642 SparseComplexMatrix::inverse (void) const |
|
643 { |
5275
|
644 octave_idx_type info; |
5164
|
645 double rcond; |
5785
|
646 MatrixType mattype (*this); |
5506
|
647 return inverse (mattype, info, rcond, 0, 0); |
|
648 } |
|
649 |
|
650 SparseComplexMatrix |
5785
|
651 SparseComplexMatrix::inverse (MatrixType& mattype) const |
5506
|
652 { |
|
653 octave_idx_type info; |
|
654 double rcond; |
|
655 return inverse (mattype, info, rcond, 0, 0); |
5164
|
656 } |
|
657 |
|
658 SparseComplexMatrix |
5785
|
659 SparseComplexMatrix::inverse (MatrixType& mattype, octave_idx_type& info) const |
5164
|
660 { |
|
661 double rcond; |
5506
|
662 return inverse (mattype, info, rcond, 0, 0); |
|
663 } |
|
664 |
|
665 SparseComplexMatrix |
5785
|
666 SparseComplexMatrix::dinverse (MatrixType &mattyp, octave_idx_type& info, |
5610
|
667 double& rcond, const bool, |
5506
|
668 const bool calccond) const |
|
669 { |
|
670 SparseComplexMatrix retval; |
|
671 |
|
672 octave_idx_type nr = rows (); |
|
673 octave_idx_type nc = cols (); |
|
674 info = 0; |
|
675 |
|
676 if (nr == 0 || nc == 0 || nr != nc) |
|
677 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
678 else |
|
679 { |
|
680 // Print spparms("spumoni") info if requested |
|
681 int typ = mattyp.type (); |
|
682 mattyp.info (); |
|
683 |
5785
|
684 if (typ == MatrixType::Diagonal || |
|
685 typ == MatrixType::Permuted_Diagonal) |
5506
|
686 { |
5785
|
687 if (typ == MatrixType::Permuted_Diagonal) |
5506
|
688 retval = transpose(); |
|
689 else |
|
690 retval = *this; |
|
691 |
|
692 // Force make_unique to be called |
|
693 Complex *v = retval.data(); |
|
694 |
|
695 if (calccond) |
|
696 { |
|
697 double dmax = 0., dmin = octave_Inf; |
|
698 for (octave_idx_type i = 0; i < nr; i++) |
|
699 { |
|
700 double tmp = std::abs(v[i]); |
|
701 if (tmp > dmax) |
|
702 dmax = tmp; |
|
703 if (tmp < dmin) |
|
704 dmin = tmp; |
|
705 } |
|
706 rcond = dmin / dmax; |
|
707 } |
|
708 |
|
709 for (octave_idx_type i = 0; i < nr; i++) |
|
710 v[i] = 1.0 / v[i]; |
|
711 } |
|
712 else |
|
713 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
714 } |
|
715 |
|
716 return retval; |
|
717 } |
|
718 |
|
719 SparseComplexMatrix |
5785
|
720 SparseComplexMatrix::tinverse (MatrixType &mattyp, octave_idx_type& info, |
5610
|
721 double& rcond, const bool, |
5506
|
722 const bool calccond) const |
|
723 { |
|
724 SparseComplexMatrix retval; |
|
725 |
|
726 octave_idx_type nr = rows (); |
|
727 octave_idx_type nc = cols (); |
|
728 info = 0; |
|
729 |
|
730 if (nr == 0 || nc == 0 || nr != nc) |
|
731 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
732 else |
|
733 { |
|
734 // Print spparms("spumoni") info if requested |
|
735 int typ = mattyp.type (); |
|
736 mattyp.info (); |
|
737 |
5785
|
738 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper || |
|
739 typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
5506
|
740 { |
|
741 double anorm = 0.; |
|
742 double ainvnorm = 0.; |
|
743 |
|
744 if (calccond) |
|
745 { |
|
746 // Calculate the 1-norm of matrix for rcond calculation |
|
747 for (octave_idx_type j = 0; j < nr; j++) |
|
748 { |
|
749 double atmp = 0.; |
|
750 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
751 atmp += std::abs(data(i)); |
|
752 if (atmp > anorm) |
|
753 anorm = atmp; |
|
754 } |
|
755 } |
|
756 |
5785
|
757 if (typ == MatrixType::Upper || typ == MatrixType::Lower) |
5506
|
758 { |
5681
|
759 octave_idx_type nz = nnz (); |
5506
|
760 octave_idx_type cx = 0; |
|
761 octave_idx_type nz2 = nz; |
|
762 retval = SparseComplexMatrix (nr, nc, nz2); |
|
763 |
|
764 for (octave_idx_type i = 0; i < nr; i++) |
|
765 { |
|
766 OCTAVE_QUIT; |
|
767 // place the 1 in the identity position |
|
768 octave_idx_type cx_colstart = cx; |
|
769 |
|
770 if (cx == nz2) |
|
771 { |
|
772 nz2 *= 2; |
|
773 retval.change_capacity (nz2); |
|
774 } |
|
775 |
|
776 retval.xcidx(i) = cx; |
|
777 retval.xridx(cx) = i; |
|
778 retval.xdata(cx) = 1.0; |
|
779 cx++; |
|
780 |
|
781 // iterate accross columns of input matrix |
|
782 for (octave_idx_type j = i+1; j < nr; j++) |
|
783 { |
|
784 Complex v = 0.; |
|
785 // iterate to calculate sum |
|
786 octave_idx_type colXp = retval.xcidx(i); |
|
787 octave_idx_type colUp = cidx(j); |
|
788 octave_idx_type rpX, rpU; |
5876
|
789 |
|
790 if (cidx(j) == cidx(j+1)) |
|
791 { |
|
792 (*current_liboctave_error_handler) |
|
793 ("division by zero"); |
|
794 goto inverse_singular; |
|
795 } |
|
796 |
5506
|
797 do |
|
798 { |
|
799 OCTAVE_QUIT; |
|
800 rpX = retval.xridx(colXp); |
|
801 rpU = ridx(colUp); |
|
802 |
|
803 if (rpX < rpU) |
|
804 colXp++; |
|
805 else if (rpX > rpU) |
|
806 colUp++; |
|
807 else |
|
808 { |
|
809 v -= retval.xdata(colXp) * data(colUp); |
|
810 colXp++; |
|
811 colUp++; |
|
812 } |
|
813 } while ((rpX<j) && (rpU<j) && |
|
814 (colXp<cx) && (colUp<nz)); |
|
815 |
5876
|
816 |
5506
|
817 // get A(m,m) |
5876
|
818 if (typ == MatrixType::Upper) |
|
819 colUp = cidx(j+1) - 1; |
|
820 else |
5877
|
821 colUp = cidx(j); |
5506
|
822 Complex pivot = data(colUp); |
5877
|
823 if (pivot == 0. || ridx(colUp) != j) |
5876
|
824 { |
|
825 (*current_liboctave_error_handler) |
|
826 ("division by zero"); |
|
827 goto inverse_singular; |
|
828 } |
5506
|
829 |
|
830 if (v != 0.) |
|
831 { |
|
832 if (cx == nz2) |
|
833 { |
|
834 nz2 *= 2; |
|
835 retval.change_capacity (nz2); |
|
836 } |
|
837 |
|
838 retval.xridx(cx) = j; |
|
839 retval.xdata(cx) = v / pivot; |
|
840 cx++; |
|
841 } |
|
842 } |
|
843 |
|
844 // get A(m,m) |
5876
|
845 octave_idx_type colUp; |
|
846 if (typ == MatrixType::Upper) |
|
847 colUp = cidx(i+1) - 1; |
|
848 else |
5877
|
849 colUp = cidx(i); |
5506
|
850 Complex pivot = data(colUp); |
5877
|
851 if (pivot == 0. || ridx(colUp) != i) |
5876
|
852 { |
|
853 (*current_liboctave_error_handler) ("division by zero"); |
|
854 goto inverse_singular; |
|
855 } |
5506
|
856 |
|
857 if (pivot != 1.0) |
|
858 for (octave_idx_type j = cx_colstart; j < cx; j++) |
|
859 retval.xdata(j) /= pivot; |
|
860 } |
|
861 retval.xcidx(nr) = cx; |
|
862 retval.maybe_compress (); |
|
863 } |
|
864 else |
|
865 { |
5681
|
866 octave_idx_type nz = nnz (); |
5506
|
867 octave_idx_type cx = 0; |
|
868 octave_idx_type nz2 = nz; |
|
869 retval = SparseComplexMatrix (nr, nc, nz2); |
|
870 |
|
871 OCTAVE_LOCAL_BUFFER (Complex, work, nr); |
|
872 OCTAVE_LOCAL_BUFFER (octave_idx_type, rperm, nr); |
|
873 |
|
874 octave_idx_type *perm = mattyp.triangular_perm(); |
5785
|
875 if (typ == MatrixType::Permuted_Upper) |
5506
|
876 { |
|
877 for (octave_idx_type i = 0; i < nr; i++) |
|
878 rperm[perm[i]] = i; |
|
879 } |
|
880 else |
|
881 { |
|
882 for (octave_idx_type i = 0; i < nr; i++) |
|
883 rperm[i] = perm[i]; |
|
884 for (octave_idx_type i = 0; i < nr; i++) |
|
885 perm[rperm[i]] = i; |
|
886 } |
|
887 |
|
888 for (octave_idx_type i = 0; i < nr; i++) |
|
889 { |
|
890 OCTAVE_QUIT; |
|
891 octave_idx_type iidx = rperm[i]; |
|
892 |
|
893 for (octave_idx_type j = 0; j < nr; j++) |
|
894 work[j] = 0.; |
|
895 |
|
896 // place the 1 in the identity position |
|
897 work[iidx] = 1.0; |
|
898 |
|
899 // iterate accross columns of input matrix |
|
900 for (octave_idx_type j = iidx+1; j < nr; j++) |
|
901 { |
|
902 Complex v = 0.; |
|
903 octave_idx_type jidx = perm[j]; |
|
904 // iterate to calculate sum |
|
905 for (octave_idx_type k = cidx(jidx); |
|
906 k < cidx(jidx+1); k++) |
|
907 { |
|
908 OCTAVE_QUIT; |
|
909 v -= work[ridx(k)] * data(k); |
|
910 } |
|
911 |
|
912 // get A(m,m) |
5876
|
913 Complex pivot; |
|
914 if (typ == MatrixType::Permuted_Upper) |
|
915 pivot = data(cidx(jidx+1) - 1); |
|
916 else |
5877
|
917 pivot = data(cidx(jidx)); |
5506
|
918 if (pivot == 0.) |
5876
|
919 { |
|
920 (*current_liboctave_error_handler) |
|
921 ("division by zero"); |
|
922 goto inverse_singular; |
|
923 } |
5506
|
924 |
|
925 work[j] = v / pivot; |
|
926 } |
|
927 |
|
928 // get A(m,m) |
5876
|
929 octave_idx_type colUp; |
|
930 if (typ == MatrixType::Permuted_Upper) |
|
931 colUp = cidx(perm[iidx]+1) - 1; |
|
932 else |
5877
|
933 colUp = cidx(perm[iidx]); |
5876
|
934 |
|
935 Complex pivot = data(colUp); |
|
936 if (pivot == 0.) |
|
937 { |
|
938 (*current_liboctave_error_handler) |
|
939 ("division by zero"); |
|
940 goto inverse_singular; |
|
941 } |
5506
|
942 |
|
943 octave_idx_type new_cx = cx; |
|
944 for (octave_idx_type j = iidx; j < nr; j++) |
|
945 if (work[j] != 0.0) |
|
946 { |
|
947 new_cx++; |
|
948 if (pivot != 1.0) |
|
949 work[j] /= pivot; |
|
950 } |
|
951 |
|
952 if (cx < new_cx) |
|
953 { |
|
954 nz2 = (2*nz2 < new_cx ? new_cx : 2*nz2); |
|
955 retval.change_capacity (nz2); |
|
956 } |
|
957 |
|
958 retval.xcidx(i) = cx; |
|
959 for (octave_idx_type j = iidx; j < nr; j++) |
|
960 if (work[j] != 0.) |
|
961 { |
|
962 retval.xridx(cx) = j; |
|
963 retval.xdata(cx++) = work[j]; |
|
964 } |
|
965 } |
|
966 |
|
967 retval.xcidx(nr) = cx; |
|
968 retval.maybe_compress (); |
|
969 } |
|
970 |
|
971 if (calccond) |
|
972 { |
|
973 // Calculate the 1-norm of inverse matrix for rcond calculation |
|
974 for (octave_idx_type j = 0; j < nr; j++) |
|
975 { |
|
976 double atmp = 0.; |
|
977 for (octave_idx_type i = retval.cidx(j); |
|
978 i < retval.cidx(j+1); i++) |
|
979 atmp += std::abs(retval.data(i)); |
|
980 if (atmp > ainvnorm) |
|
981 ainvnorm = atmp; |
|
982 } |
|
983 |
|
984 rcond = 1. / ainvnorm / anorm; |
|
985 } |
|
986 } |
|
987 else |
|
988 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
989 } |
|
990 |
|
991 return retval; |
5876
|
992 |
|
993 inverse_singular: |
|
994 return SparseComplexMatrix(); |
5164
|
995 } |
|
996 |
|
997 SparseComplexMatrix |
5785
|
998 SparseComplexMatrix::inverse (MatrixType& mattype, octave_idx_type& info, |
5610
|
999 double& rcond, int, int calc_cond) const |
5506
|
1000 { |
|
1001 int typ = mattype.type (false); |
|
1002 SparseComplexMatrix ret; |
|
1003 |
5785
|
1004 if (typ == MatrixType::Unknown) |
5506
|
1005 typ = mattype.type (*this); |
|
1006 |
5785
|
1007 if (typ == MatrixType::Diagonal || typ == MatrixType::Permuted_Diagonal) |
5506
|
1008 ret = dinverse (mattype, info, rcond, true, calc_cond); |
5785
|
1009 else if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
5506
|
1010 ret = tinverse (mattype, info, rcond, true, calc_cond).transpose(); |
5785
|
1011 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
6185
|
1012 { |
|
1013 MatrixType newtype = mattype.transpose(); |
|
1014 ret = transpose().tinverse (newtype, info, rcond, true, calc_cond); |
|
1015 } |
6840
|
1016 else |
5506
|
1017 { |
|
1018 if (mattype.is_hermitian()) |
|
1019 { |
5785
|
1020 MatrixType tmp_typ (MatrixType::Upper); |
5506
|
1021 SparseComplexCHOL fact (*this, info, false); |
|
1022 rcond = fact.rcond(); |
|
1023 if (info == 0) |
|
1024 { |
|
1025 double rcond2; |
|
1026 SparseMatrix Q = fact.Q(); |
|
1027 SparseComplexMatrix InvL = fact.L().transpose(). |
|
1028 tinverse(tmp_typ, info, rcond2, true, false); |
|
1029 ret = Q * InvL.hermitian() * InvL * Q.transpose(); |
|
1030 } |
|
1031 else |
|
1032 { |
|
1033 // Matrix is either singular or not positive definite |
|
1034 mattype.mark_as_unsymmetric (); |
5785
|
1035 typ = MatrixType::Full; |
5506
|
1036 } |
|
1037 } |
|
1038 |
|
1039 if (!mattype.is_hermitian()) |
|
1040 { |
|
1041 octave_idx_type n = rows(); |
|
1042 ColumnVector Qinit(n); |
|
1043 for (octave_idx_type i = 0; i < n; i++) |
|
1044 Qinit(i) = i; |
|
1045 |
5785
|
1046 MatrixType tmp_typ (MatrixType::Upper); |
5506
|
1047 SparseComplexLU fact (*this, Qinit, -1.0, false); |
|
1048 rcond = fact.rcond(); |
|
1049 double rcond2; |
|
1050 SparseComplexMatrix InvL = fact.L().transpose(). |
|
1051 tinverse(tmp_typ, info, rcond2, true, false); |
|
1052 SparseComplexMatrix InvU = fact.U(). |
|
1053 tinverse(tmp_typ, info, rcond2, true, false).transpose(); |
|
1054 ret = fact.Pc().transpose() * InvU * InvL * fact.Pr(); |
|
1055 } |
|
1056 } |
|
1057 |
|
1058 return ret; |
5164
|
1059 } |
|
1060 |
|
1061 ComplexDET |
|
1062 SparseComplexMatrix::determinant (void) const |
|
1063 { |
5275
|
1064 octave_idx_type info; |
5164
|
1065 double rcond; |
|
1066 return determinant (info, rcond, 0); |
|
1067 } |
|
1068 |
|
1069 ComplexDET |
5275
|
1070 SparseComplexMatrix::determinant (octave_idx_type& info) const |
5164
|
1071 { |
|
1072 double rcond; |
|
1073 return determinant (info, rcond, 0); |
|
1074 } |
|
1075 |
|
1076 ComplexDET |
5610
|
1077 SparseComplexMatrix::determinant (octave_idx_type& err, double& rcond, int) const |
5164
|
1078 { |
|
1079 ComplexDET retval; |
5203
|
1080 #ifdef HAVE_UMFPACK |
5164
|
1081 |
5275
|
1082 octave_idx_type nr = rows (); |
|
1083 octave_idx_type nc = cols (); |
5164
|
1084 |
|
1085 if (nr == 0 || nc == 0 || nr != nc) |
|
1086 { |
|
1087 Complex d[2]; |
|
1088 d[0] = 1.0; |
|
1089 d[1] = 0.0; |
|
1090 retval = ComplexDET (d); |
|
1091 } |
|
1092 else |
|
1093 { |
|
1094 err = 0; |
|
1095 |
|
1096 // Setup the control parameters |
|
1097 Matrix Control (UMFPACK_CONTROL, 1); |
|
1098 double *control = Control.fortran_vec (); |
5322
|
1099 UMFPACK_ZNAME (defaults) (control); |
5164
|
1100 |
5893
|
1101 double tmp = octave_sparse_params::get_key ("spumoni"); |
5164
|
1102 if (!xisnan (tmp)) |
|
1103 Control (UMFPACK_PRL) = tmp; |
|
1104 |
5893
|
1105 tmp = octave_sparse_params::get_key ("piv_tol"); |
5164
|
1106 if (!xisnan (tmp)) |
|
1107 { |
|
1108 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
1109 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
1110 } |
|
1111 |
|
1112 // Set whether we are allowed to modify Q or not |
5893
|
1113 tmp = octave_sparse_params::get_key ("autoamd"); |
5164
|
1114 if (!xisnan (tmp)) |
|
1115 Control (UMFPACK_FIXQ) = tmp; |
|
1116 |
|
1117 // Turn-off UMFPACK scaling for LU |
|
1118 Control (UMFPACK_SCALE) = UMFPACK_SCALE_NONE; |
|
1119 |
5322
|
1120 UMFPACK_ZNAME (report_control) (control); |
5164
|
1121 |
5275
|
1122 const octave_idx_type *Ap = cidx (); |
|
1123 const octave_idx_type *Ai = ridx (); |
5164
|
1124 const Complex *Ax = data (); |
|
1125 |
5322
|
1126 UMFPACK_ZNAME (report_matrix) (nr, nc, Ap, Ai, |
5760
|
1127 reinterpret_cast<const double *> (Ax), |
|
1128 NULL, 1, control); |
5164
|
1129 |
|
1130 void *Symbolic; |
|
1131 Matrix Info (1, UMFPACK_INFO); |
|
1132 double *info = Info.fortran_vec (); |
5322
|
1133 int status = UMFPACK_ZNAME (qsymbolic) |
5760
|
1134 (nr, nc, Ap, Ai, reinterpret_cast<const double *> (Ax), NULL, |
5164
|
1135 NULL, &Symbolic, control, info); |
|
1136 |
|
1137 if (status < 0) |
|
1138 { |
|
1139 (*current_liboctave_error_handler) |
|
1140 ("SparseComplexMatrix::determinant symbolic factorization failed"); |
|
1141 |
5322
|
1142 UMFPACK_ZNAME (report_status) (control, status); |
|
1143 UMFPACK_ZNAME (report_info) (control, info); |
|
1144 |
|
1145 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
1146 } |
|
1147 else |
|
1148 { |
5322
|
1149 UMFPACK_ZNAME (report_symbolic) (Symbolic, control); |
5164
|
1150 |
|
1151 void *Numeric; |
5760
|
1152 status |
|
1153 = UMFPACK_ZNAME (numeric) (Ap, Ai, |
|
1154 reinterpret_cast<const double *> (Ax), |
|
1155 NULL, Symbolic, &Numeric, control, info) ; |
5322
|
1156 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
1157 |
|
1158 rcond = Info (UMFPACK_RCOND); |
|
1159 |
|
1160 if (status < 0) |
|
1161 { |
|
1162 (*current_liboctave_error_handler) |
|
1163 ("SparseComplexMatrix::determinant numeric factorization failed"); |
|
1164 |
5322
|
1165 UMFPACK_ZNAME (report_status) (control, status); |
|
1166 UMFPACK_ZNAME (report_info) (control, info); |
|
1167 |
|
1168 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
1169 } |
|
1170 else |
|
1171 { |
5322
|
1172 UMFPACK_ZNAME (report_numeric) (Numeric, control); |
5164
|
1173 |
|
1174 Complex d[2]; |
|
1175 double d_exponent; |
|
1176 |
5322
|
1177 status = UMFPACK_ZNAME (get_determinant) |
5760
|
1178 (reinterpret_cast<double *> (&d[0]), NULL, &d_exponent, |
5164
|
1179 Numeric, info); |
|
1180 d[1] = d_exponent; |
|
1181 |
|
1182 if (status < 0) |
|
1183 { |
|
1184 (*current_liboctave_error_handler) |
|
1185 ("SparseComplexMatrix::determinant error calculating determinant"); |
|
1186 |
5322
|
1187 UMFPACK_ZNAME (report_status) (control, status); |
|
1188 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
1189 } |
|
1190 else |
|
1191 retval = ComplexDET (d); |
5346
|
1192 |
|
1193 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
1194 } |
|
1195 } |
|
1196 } |
5203
|
1197 #else |
|
1198 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
1199 #endif |
5164
|
1200 |
|
1201 return retval; |
|
1202 } |
|
1203 |
|
1204 ComplexMatrix |
5785
|
1205 SparseComplexMatrix::dsolve (MatrixType &mattype, const Matrix& b, |
5681
|
1206 octave_idx_type& err, double& rcond, |
|
1207 solve_singularity_handler, bool calc_cond) const |
5164
|
1208 { |
|
1209 ComplexMatrix retval; |
|
1210 |
5275
|
1211 octave_idx_type nr = rows (); |
|
1212 octave_idx_type nc = cols (); |
5630
|
1213 octave_idx_type nm = (nc < nr ? nc : nr); |
5164
|
1214 err = 0; |
|
1215 |
6924
|
1216 if (nr != b.rows ()) |
5164
|
1217 (*current_liboctave_error_handler) |
|
1218 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1219 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1220 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
1221 else |
|
1222 { |
|
1223 // Print spparms("spumoni") info if requested |
|
1224 int typ = mattype.type (); |
|
1225 mattype.info (); |
|
1226 |
5785
|
1227 if (typ == MatrixType::Diagonal || |
|
1228 typ == MatrixType::Permuted_Diagonal) |
5164
|
1229 { |
5630
|
1230 retval.resize (nc, b.cols(), Complex(0.,0.)); |
5785
|
1231 if (typ == MatrixType::Diagonal) |
5275
|
1232 for (octave_idx_type j = 0; j < b.cols(); j++) |
5630
|
1233 for (octave_idx_type i = 0; i < nm; i++) |
|
1234 retval(i,j) = b(i,j) / data (i); |
5164
|
1235 else |
5275
|
1236 for (octave_idx_type j = 0; j < b.cols(); j++) |
5630
|
1237 for (octave_idx_type k = 0; k < nc; k++) |
|
1238 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
1239 retval(k,j) = b(ridx(i),j) / data (i); |
5164
|
1240 |
5681
|
1241 if (calc_cond) |
|
1242 { |
|
1243 double dmax = 0., dmin = octave_Inf; |
|
1244 for (octave_idx_type i = 0; i < nm; i++) |
|
1245 { |
|
1246 double tmp = std::abs(data(i)); |
|
1247 if (tmp > dmax) |
|
1248 dmax = tmp; |
|
1249 if (tmp < dmin) |
|
1250 dmin = tmp; |
|
1251 } |
|
1252 rcond = dmin / dmax; |
|
1253 } |
|
1254 else |
|
1255 rcond = 1.0; |
5164
|
1256 } |
|
1257 else |
|
1258 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1259 } |
|
1260 |
|
1261 return retval; |
|
1262 } |
|
1263 |
|
1264 SparseComplexMatrix |
5785
|
1265 SparseComplexMatrix::dsolve (MatrixType &mattype, const SparseMatrix& b, |
5630
|
1266 octave_idx_type& err, double& rcond, |
5681
|
1267 solve_singularity_handler, |
|
1268 bool calc_cond) const |
5164
|
1269 { |
|
1270 SparseComplexMatrix retval; |
|
1271 |
5275
|
1272 octave_idx_type nr = rows (); |
|
1273 octave_idx_type nc = cols (); |
5630
|
1274 octave_idx_type nm = (nc < nr ? nc : nr); |
5164
|
1275 err = 0; |
|
1276 |
6924
|
1277 if (nr != b.rows ()) |
5164
|
1278 (*current_liboctave_error_handler) |
|
1279 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1280 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1281 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
1282 else |
|
1283 { |
|
1284 // Print spparms("spumoni") info if requested |
|
1285 int typ = mattype.type (); |
|
1286 mattype.info (); |
|
1287 |
5785
|
1288 if (typ == MatrixType::Diagonal || |
|
1289 typ == MatrixType::Permuted_Diagonal) |
5164
|
1290 { |
5275
|
1291 octave_idx_type b_nc = b.cols (); |
5681
|
1292 octave_idx_type b_nz = b.nnz (); |
5630
|
1293 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
1294 |
|
1295 retval.xcidx(0) = 0; |
5275
|
1296 octave_idx_type ii = 0; |
5785
|
1297 if (typ == MatrixType::Diagonal) |
5275
|
1298 for (octave_idx_type j = 0; j < b.cols(); j++) |
5164
|
1299 { |
5275
|
1300 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
1301 { |
5681
|
1302 if (b.ridx(i) >= nm) |
|
1303 break; |
5164
|
1304 retval.xridx (ii) = b.ridx(i); |
|
1305 retval.xdata (ii++) = b.data(i) / data (b.ridx (i)); |
|
1306 } |
|
1307 retval.xcidx(j+1) = ii; |
|
1308 } |
|
1309 else |
5275
|
1310 for (octave_idx_type j = 0; j < b.cols(); j++) |
5164
|
1311 { |
5630
|
1312 for (octave_idx_type l = 0; l < nc; l++) |
|
1313 for (octave_idx_type i = cidx(l); i < cidx(l+1); i++) |
|
1314 { |
|
1315 bool found = false; |
|
1316 octave_idx_type k; |
|
1317 for (k = b.cidx(j); k < b.cidx(j+1); k++) |
|
1318 if (ridx(i) == b.ridx(k)) |
|
1319 { |
|
1320 found = true; |
|
1321 break; |
|
1322 } |
|
1323 if (found) |
5164
|
1324 { |
5630
|
1325 retval.xridx (ii) = l; |
|
1326 retval.xdata (ii++) = b.data(k) / data (i); |
5164
|
1327 } |
5630
|
1328 } |
5164
|
1329 retval.xcidx(j+1) = ii; |
|
1330 } |
|
1331 |
5681
|
1332 if (calc_cond) |
|
1333 { |
|
1334 double dmax = 0., dmin = octave_Inf; |
|
1335 for (octave_idx_type i = 0; i < nm; i++) |
|
1336 { |
|
1337 double tmp = std::abs(data(i)); |
|
1338 if (tmp > dmax) |
|
1339 dmax = tmp; |
|
1340 if (tmp < dmin) |
|
1341 dmin = tmp; |
|
1342 } |
|
1343 rcond = dmin / dmax; |
|
1344 } |
|
1345 else |
|
1346 rcond = 1.0; |
5164
|
1347 } |
|
1348 else |
|
1349 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1350 } |
|
1351 |
|
1352 return retval; |
|
1353 } |
|
1354 |
|
1355 ComplexMatrix |
5785
|
1356 SparseComplexMatrix::dsolve (MatrixType &mattype, const ComplexMatrix& b, |
5630
|
1357 octave_idx_type& err, double& rcond, |
5681
|
1358 solve_singularity_handler, |
|
1359 bool calc_cond) const |
5164
|
1360 { |
|
1361 ComplexMatrix retval; |
|
1362 |
5275
|
1363 octave_idx_type nr = rows (); |
|
1364 octave_idx_type nc = cols (); |
5630
|
1365 octave_idx_type nm = (nc < nr ? nc : nr); |
5164
|
1366 err = 0; |
|
1367 |
6924
|
1368 if (nr != b.rows ()) |
5164
|
1369 (*current_liboctave_error_handler) |
|
1370 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1371 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1372 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
1373 else |
|
1374 { |
|
1375 // Print spparms("spumoni") info if requested |
|
1376 int typ = mattype.type (); |
|
1377 mattype.info (); |
|
1378 |
5785
|
1379 if (typ == MatrixType::Diagonal || |
|
1380 typ == MatrixType::Permuted_Diagonal) |
5164
|
1381 { |
5630
|
1382 retval.resize (nc, b.cols(), Complex(0.,0.)); |
5785
|
1383 if (typ == MatrixType::Diagonal) |
5275
|
1384 for (octave_idx_type j = 0; j < b.cols(); j++) |
5630
|
1385 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
1386 retval(i,j) = b(i,j) / data (i); |
|
1387 else |
5275
|
1388 for (octave_idx_type j = 0; j < b.cols(); j++) |
5630
|
1389 for (octave_idx_type k = 0; k < nc; k++) |
|
1390 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
1391 retval(k,j) = b(ridx(i),j) / data (i); |
5164
|
1392 |
5681
|
1393 if (calc_cond) |
|
1394 { |
|
1395 double dmax = 0., dmin = octave_Inf; |
|
1396 for (octave_idx_type i = 0; i < nr; i++) |
|
1397 { |
|
1398 double tmp = std::abs(data(i)); |
|
1399 if (tmp > dmax) |
|
1400 dmax = tmp; |
|
1401 if (tmp < dmin) |
|
1402 dmin = tmp; |
|
1403 } |
|
1404 rcond = dmin / dmax; |
|
1405 } |
|
1406 else |
|
1407 rcond = 1.0; |
5164
|
1408 } |
|
1409 else |
|
1410 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1411 } |
|
1412 |
|
1413 return retval; |
|
1414 } |
|
1415 |
|
1416 SparseComplexMatrix |
5785
|
1417 SparseComplexMatrix::dsolve (MatrixType &mattype, const SparseComplexMatrix& b, |
5630
|
1418 octave_idx_type& err, double& rcond, |
5681
|
1419 solve_singularity_handler, |
|
1420 bool calc_cond) const |
5164
|
1421 { |
|
1422 SparseComplexMatrix retval; |
|
1423 |
5275
|
1424 octave_idx_type nr = rows (); |
|
1425 octave_idx_type nc = cols (); |
5630
|
1426 octave_idx_type nm = (nc < nr ? nc : nr); |
5164
|
1427 err = 0; |
|
1428 |
6924
|
1429 if (nr != b.rows ()) |
5164
|
1430 (*current_liboctave_error_handler) |
|
1431 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1432 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1433 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
1434 else |
|
1435 { |
|
1436 // Print spparms("spumoni") info if requested |
|
1437 int typ = mattype.type (); |
|
1438 mattype.info (); |
|
1439 |
5785
|
1440 if (typ == MatrixType::Diagonal || |
|
1441 typ == MatrixType::Permuted_Diagonal) |
5164
|
1442 { |
5275
|
1443 octave_idx_type b_nc = b.cols (); |
5681
|
1444 octave_idx_type b_nz = b.nnz (); |
5630
|
1445 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
1446 |
|
1447 retval.xcidx(0) = 0; |
5275
|
1448 octave_idx_type ii = 0; |
5785
|
1449 if (typ == MatrixType::Diagonal) |
5275
|
1450 for (octave_idx_type j = 0; j < b.cols(); j++) |
5164
|
1451 { |
5275
|
1452 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
1453 { |
5681
|
1454 if (b.ridx(i) >= nm) |
|
1455 break; |
5164
|
1456 retval.xridx (ii) = b.ridx(i); |
|
1457 retval.xdata (ii++) = b.data(i) / data (b.ridx (i)); |
|
1458 } |
|
1459 retval.xcidx(j+1) = ii; |
|
1460 } |
|
1461 else |
5275
|
1462 for (octave_idx_type j = 0; j < b.cols(); j++) |
5164
|
1463 { |
5630
|
1464 for (octave_idx_type l = 0; l < nc; l++) |
|
1465 for (octave_idx_type i = cidx(l); i < cidx(l+1); i++) |
|
1466 { |
|
1467 bool found = false; |
|
1468 octave_idx_type k; |
|
1469 for (k = b.cidx(j); k < b.cidx(j+1); k++) |
|
1470 if (ridx(i) == b.ridx(k)) |
|
1471 { |
|
1472 found = true; |
|
1473 break; |
|
1474 } |
|
1475 if (found) |
5164
|
1476 { |
5630
|
1477 retval.xridx (ii) = l; |
|
1478 retval.xdata (ii++) = b.data(k) / data (i); |
5164
|
1479 } |
5630
|
1480 } |
5164
|
1481 retval.xcidx(j+1) = ii; |
|
1482 } |
|
1483 |
5681
|
1484 if (calc_cond) |
|
1485 { |
|
1486 double dmax = 0., dmin = octave_Inf; |
|
1487 for (octave_idx_type i = 0; i < nm; i++) |
|
1488 { |
|
1489 double tmp = std::abs(data(i)); |
|
1490 if (tmp > dmax) |
|
1491 dmax = tmp; |
|
1492 if (tmp < dmin) |
|
1493 dmin = tmp; |
|
1494 } |
|
1495 rcond = dmin / dmax; |
|
1496 } |
|
1497 else |
|
1498 rcond = 1.0; |
5164
|
1499 } |
|
1500 else |
|
1501 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1502 } |
|
1503 |
|
1504 return retval; |
|
1505 } |
|
1506 |
|
1507 ComplexMatrix |
5785
|
1508 SparseComplexMatrix::utsolve (MatrixType &mattype, const Matrix& b, |
5630
|
1509 octave_idx_type& err, double& rcond, |
5681
|
1510 solve_singularity_handler sing_handler, |
|
1511 bool calc_cond) const |
5164
|
1512 { |
|
1513 ComplexMatrix retval; |
|
1514 |
5275
|
1515 octave_idx_type nr = rows (); |
|
1516 octave_idx_type nc = cols (); |
5630
|
1517 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
1518 err = 0; |
|
1519 |
6924
|
1520 if (nr != b.rows ()) |
5164
|
1521 (*current_liboctave_error_handler) |
|
1522 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1523 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1524 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
1525 else |
|
1526 { |
|
1527 // Print spparms("spumoni") info if requested |
|
1528 int typ = mattype.type (); |
|
1529 mattype.info (); |
|
1530 |
5785
|
1531 if (typ == MatrixType::Permuted_Upper || |
|
1532 typ == MatrixType::Upper) |
5164
|
1533 { |
|
1534 double anorm = 0.; |
|
1535 double ainvnorm = 0.; |
5630
|
1536 octave_idx_type b_nc = b.cols (); |
5681
|
1537 rcond = 1.; |
|
1538 |
|
1539 if (calc_cond) |
|
1540 { |
|
1541 // Calculate the 1-norm of matrix for rcond calculation |
|
1542 for (octave_idx_type j = 0; j < nc; j++) |
|
1543 { |
|
1544 double atmp = 0.; |
|
1545 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
1546 atmp += std::abs(data(i)); |
|
1547 if (atmp > anorm) |
|
1548 anorm = atmp; |
|
1549 } |
5164
|
1550 } |
|
1551 |
5785
|
1552 if (typ == MatrixType::Permuted_Upper) |
5164
|
1553 { |
5630
|
1554 retval.resize (nc, b_nc); |
5322
|
1555 octave_idx_type *perm = mattype.triangular_perm (); |
5681
|
1556 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
1557 |
5630
|
1558 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
1559 { |
5275
|
1560 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1561 work[i] = b(i,j); |
5630
|
1562 for (octave_idx_type i = nr; i < nc; i++) |
|
1563 work[i] = 0.; |
|
1564 |
|
1565 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
1566 { |
5322
|
1567 octave_idx_type kidx = perm[k]; |
|
1568 |
|
1569 if (work[k] != 0.) |
5164
|
1570 { |
5681
|
1571 if (ridx(cidx(kidx+1)-1) != k || |
|
1572 data(cidx(kidx+1)-1) == 0.) |
5164
|
1573 { |
|
1574 err = -2; |
|
1575 goto triangular_error; |
|
1576 } |
|
1577 |
5322
|
1578 Complex tmp = work[k] / data(cidx(kidx+1)-1); |
|
1579 work[k] = tmp; |
|
1580 for (octave_idx_type i = cidx(kidx); |
|
1581 i < cidx(kidx+1)-1; i++) |
5164
|
1582 { |
5322
|
1583 octave_idx_type iidx = ridx(i); |
|
1584 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
1585 } |
|
1586 } |
|
1587 } |
|
1588 |
5630
|
1589 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
1590 retval (perm[i], j) = work[i]; |
5164
|
1591 } |
|
1592 |
5681
|
1593 if (calc_cond) |
|
1594 { |
|
1595 // Calculation of 1-norm of inv(*this) |
|
1596 for (octave_idx_type i = 0; i < nm; i++) |
|
1597 work[i] = 0.; |
|
1598 |
|
1599 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
1600 { |
5681
|
1601 work[j] = 1.; |
|
1602 |
|
1603 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
1604 { |
5681
|
1605 octave_idx_type iidx = perm[k]; |
|
1606 |
|
1607 if (work[k] != 0.) |
5164
|
1608 { |
5681
|
1609 Complex tmp = work[k] / data(cidx(iidx+1)-1); |
|
1610 work[k] = tmp; |
|
1611 for (octave_idx_type i = cidx(iidx); |
|
1612 i < cidx(iidx+1)-1; i++) |
|
1613 { |
|
1614 octave_idx_type idx2 = ridx(i); |
|
1615 work[idx2] = work[idx2] - tmp * data(i); |
|
1616 } |
5164
|
1617 } |
|
1618 } |
5681
|
1619 double atmp = 0; |
|
1620 for (octave_idx_type i = 0; i < j+1; i++) |
|
1621 { |
|
1622 atmp += std::abs(work[i]); |
|
1623 work[i] = 0.; |
|
1624 } |
|
1625 if (atmp > ainvnorm) |
|
1626 ainvnorm = atmp; |
5164
|
1627 } |
5681
|
1628 rcond = 1. / ainvnorm / anorm; |
5164
|
1629 } |
|
1630 } |
|
1631 else |
|
1632 { |
5630
|
1633 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
1634 retval.resize (nc, b_nc); |
|
1635 |
|
1636 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
1637 { |
5630
|
1638 for (octave_idx_type i = 0; i < nr; i++) |
|
1639 work[i] = b(i,j); |
|
1640 for (octave_idx_type i = nr; i < nc; i++) |
|
1641 work[i] = 0.; |
|
1642 |
|
1643 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
1644 { |
5630
|
1645 if (work[k] != 0.) |
5164
|
1646 { |
5681
|
1647 if (ridx(cidx(k+1)-1) != k || |
|
1648 data(cidx(k+1)-1) == 0.) |
5164
|
1649 { |
|
1650 err = -2; |
|
1651 goto triangular_error; |
|
1652 } |
|
1653 |
5630
|
1654 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
1655 work[k] = tmp; |
5275
|
1656 for (octave_idx_type i = cidx(k); i < cidx(k+1)-1; i++) |
5164
|
1657 { |
5275
|
1658 octave_idx_type iidx = ridx(i); |
5630
|
1659 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
1660 } |
|
1661 } |
|
1662 } |
5630
|
1663 |
|
1664 for (octave_idx_type i = 0; i < nc; i++) |
|
1665 retval.xelem (i, j) = work[i]; |
5164
|
1666 } |
|
1667 |
5681
|
1668 if (calc_cond) |
|
1669 { |
|
1670 // Calculation of 1-norm of inv(*this) |
|
1671 for (octave_idx_type i = 0; i < nm; i++) |
|
1672 work[i] = 0.; |
|
1673 |
|
1674 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
1675 { |
5681
|
1676 work[j] = 1.; |
|
1677 |
|
1678 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
1679 { |
5681
|
1680 if (work[k] != 0.) |
5164
|
1681 { |
5681
|
1682 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
1683 work[k] = tmp; |
|
1684 for (octave_idx_type i = cidx(k); |
|
1685 i < cidx(k+1)-1; i++) |
|
1686 { |
|
1687 octave_idx_type iidx = ridx(i); |
|
1688 work[iidx] = work[iidx] - tmp * data(i); |
|
1689 } |
5164
|
1690 } |
|
1691 } |
5681
|
1692 double atmp = 0; |
|
1693 for (octave_idx_type i = 0; i < j+1; i++) |
|
1694 { |
|
1695 atmp += std::abs(work[i]); |
|
1696 work[i] = 0.; |
|
1697 } |
|
1698 if (atmp > ainvnorm) |
|
1699 ainvnorm = atmp; |
5164
|
1700 } |
5681
|
1701 rcond = 1. / ainvnorm / anorm; |
|
1702 } |
|
1703 } |
5164
|
1704 |
|
1705 triangular_error: |
|
1706 if (err != 0) |
|
1707 { |
|
1708 if (sing_handler) |
5681
|
1709 { |
|
1710 sing_handler (rcond); |
|
1711 mattype.mark_as_rectangular (); |
|
1712 } |
5164
|
1713 else |
|
1714 (*current_liboctave_error_handler) |
|
1715 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
1716 rcond); |
|
1717 } |
|
1718 |
|
1719 volatile double rcond_plus_one = rcond + 1.0; |
|
1720 |
|
1721 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1722 { |
|
1723 err = -2; |
|
1724 |
|
1725 if (sing_handler) |
5681
|
1726 { |
|
1727 sing_handler (rcond); |
|
1728 mattype.mark_as_rectangular (); |
|
1729 } |
5164
|
1730 else |
|
1731 (*current_liboctave_error_handler) |
|
1732 ("matrix singular to machine precision, rcond = %g", |
|
1733 rcond); |
|
1734 } |
|
1735 } |
|
1736 else |
|
1737 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1738 } |
|
1739 |
|
1740 return retval; |
|
1741 } |
|
1742 |
|
1743 SparseComplexMatrix |
5785
|
1744 SparseComplexMatrix::utsolve (MatrixType &mattype, const SparseMatrix& b, |
5630
|
1745 octave_idx_type& err, double& rcond, |
5681
|
1746 solve_singularity_handler sing_handler, |
|
1747 bool calc_cond) const |
5164
|
1748 { |
|
1749 SparseComplexMatrix retval; |
|
1750 |
5275
|
1751 octave_idx_type nr = rows (); |
|
1752 octave_idx_type nc = cols (); |
5630
|
1753 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
1754 err = 0; |
|
1755 |
6924
|
1756 if (nr != b.rows ()) |
5164
|
1757 (*current_liboctave_error_handler) |
|
1758 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1759 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1760 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
1761 else |
|
1762 { |
|
1763 // Print spparms("spumoni") info if requested |
|
1764 int typ = mattype.type (); |
|
1765 mattype.info (); |
|
1766 |
5785
|
1767 if (typ == MatrixType::Permuted_Upper || |
|
1768 typ == MatrixType::Upper) |
5164
|
1769 { |
|
1770 double anorm = 0.; |
|
1771 double ainvnorm = 0.; |
5681
|
1772 rcond = 1.; |
|
1773 |
|
1774 if (calc_cond) |
|
1775 { |
|
1776 // Calculate the 1-norm of matrix for rcond calculation |
|
1777 for (octave_idx_type j = 0; j < nc; j++) |
|
1778 { |
|
1779 double atmp = 0.; |
|
1780 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
1781 atmp += std::abs(data(i)); |
|
1782 if (atmp > anorm) |
|
1783 anorm = atmp; |
|
1784 } |
5164
|
1785 } |
|
1786 |
5275
|
1787 octave_idx_type b_nc = b.cols (); |
5681
|
1788 octave_idx_type b_nz = b.nnz (); |
5630
|
1789 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
1790 retval.xcidx(0) = 0; |
5275
|
1791 octave_idx_type ii = 0; |
|
1792 octave_idx_type x_nz = b_nz; |
5164
|
1793 |
5785
|
1794 if (typ == MatrixType::Permuted_Upper) |
5164
|
1795 { |
5322
|
1796 octave_idx_type *perm = mattype.triangular_perm (); |
5630
|
1797 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
1798 |
|
1799 OCTAVE_LOCAL_BUFFER (octave_idx_type, rperm, nc); |
|
1800 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
1801 rperm[perm[i]] = i; |
5164
|
1802 |
5275
|
1803 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
1804 { |
5630
|
1805 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
1806 work[i] = 0.; |
5275
|
1807 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
1808 work[b.ridx(i)] = b.data(i); |
|
1809 |
5630
|
1810 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
1811 { |
5322
|
1812 octave_idx_type kidx = perm[k]; |
|
1813 |
|
1814 if (work[k] != 0.) |
5164
|
1815 { |
5681
|
1816 if (ridx(cidx(kidx+1)-1) != k || |
|
1817 data(cidx(kidx+1)-1) == 0.) |
5164
|
1818 { |
|
1819 err = -2; |
|
1820 goto triangular_error; |
|
1821 } |
|
1822 |
5322
|
1823 Complex tmp = work[k] / data(cidx(kidx+1)-1); |
|
1824 work[k] = tmp; |
|
1825 for (octave_idx_type i = cidx(kidx); |
|
1826 i < cidx(kidx+1)-1; i++) |
5164
|
1827 { |
5322
|
1828 octave_idx_type iidx = ridx(i); |
|
1829 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
1830 } |
|
1831 } |
|
1832 } |
|
1833 |
|
1834 // Count non-zeros in work vector and adjust space in |
|
1835 // retval if needed |
5275
|
1836 octave_idx_type new_nnz = 0; |
5630
|
1837 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
1838 if (work[i] != 0.) |
|
1839 new_nnz++; |
|
1840 |
|
1841 if (ii + new_nnz > x_nz) |
|
1842 { |
|
1843 // Resize the sparse matrix |
5275
|
1844 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
1845 retval.change_capacity (sz); |
|
1846 x_nz = sz; |
|
1847 } |
|
1848 |
5630
|
1849 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
1850 if (work[rperm[i]] != 0.) |
5164
|
1851 { |
|
1852 retval.xridx(ii) = i; |
5322
|
1853 retval.xdata(ii++) = work[rperm[i]]; |
5164
|
1854 } |
|
1855 retval.xcidx(j+1) = ii; |
|
1856 } |
|
1857 |
|
1858 retval.maybe_compress (); |
|
1859 |
5681
|
1860 if (calc_cond) |
|
1861 { |
|
1862 // Calculation of 1-norm of inv(*this) |
|
1863 for (octave_idx_type i = 0; i < nm; i++) |
|
1864 work[i] = 0.; |
|
1865 |
|
1866 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
1867 { |
5681
|
1868 work[j] = 1.; |
|
1869 |
|
1870 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
1871 { |
5681
|
1872 octave_idx_type iidx = perm[k]; |
|
1873 |
|
1874 if (work[k] != 0.) |
5164
|
1875 { |
5681
|
1876 Complex tmp = work[k] / data(cidx(iidx+1)-1); |
|
1877 work[k] = tmp; |
|
1878 for (octave_idx_type i = cidx(iidx); |
|
1879 i < cidx(iidx+1)-1; i++) |
|
1880 { |
|
1881 octave_idx_type idx2 = ridx(i); |
|
1882 work[idx2] = work[idx2] - tmp * data(i); |
|
1883 } |
5164
|
1884 } |
|
1885 } |
5681
|
1886 double atmp = 0; |
|
1887 for (octave_idx_type i = 0; i < j+1; i++) |
|
1888 { |
|
1889 atmp += std::abs(work[i]); |
|
1890 work[i] = 0.; |
|
1891 } |
|
1892 if (atmp > ainvnorm) |
|
1893 ainvnorm = atmp; |
5164
|
1894 } |
5681
|
1895 rcond = 1. / ainvnorm / anorm; |
5164
|
1896 } |
|
1897 } |
|
1898 else |
|
1899 { |
5630
|
1900 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
1901 |
5275
|
1902 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
1903 { |
5630
|
1904 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
1905 work[i] = 0.; |
5275
|
1906 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
1907 work[b.ridx(i)] = b.data(i); |
|
1908 |
5630
|
1909 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
1910 { |
|
1911 if (work[k] != 0.) |
|
1912 { |
5681
|
1913 if (ridx(cidx(k+1)-1) != k || |
|
1914 data(cidx(k+1)-1) == 0.) |
5164
|
1915 { |
|
1916 err = -2; |
|
1917 goto triangular_error; |
|
1918 } |
|
1919 |
|
1920 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
1921 work[k] = tmp; |
5275
|
1922 for (octave_idx_type i = cidx(k); i < cidx(k+1)-1; i++) |
5164
|
1923 { |
5275
|
1924 octave_idx_type iidx = ridx(i); |
5164
|
1925 work[iidx] = work[iidx] - tmp * data(i); |
|
1926 } |
|
1927 } |
|
1928 } |
|
1929 |
|
1930 // Count non-zeros in work vector and adjust space in |
|
1931 // retval if needed |
5275
|
1932 octave_idx_type new_nnz = 0; |
5630
|
1933 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
1934 if (work[i] != 0.) |
|
1935 new_nnz++; |
|
1936 |
|
1937 if (ii + new_nnz > x_nz) |
|
1938 { |
|
1939 // Resize the sparse matrix |
5275
|
1940 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
1941 retval.change_capacity (sz); |
|
1942 x_nz = sz; |
|
1943 } |
|
1944 |
5630
|
1945 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
1946 if (work[i] != 0.) |
|
1947 { |
|
1948 retval.xridx(ii) = i; |
|
1949 retval.xdata(ii++) = work[i]; |
|
1950 } |
|
1951 retval.xcidx(j+1) = ii; |
|
1952 } |
|
1953 |
|
1954 retval.maybe_compress (); |
|
1955 |
5681
|
1956 if (calc_cond) |
|
1957 { |
|
1958 // Calculation of 1-norm of inv(*this) |
|
1959 for (octave_idx_type i = 0; i < nm; i++) |
|
1960 work[i] = 0.; |
|
1961 |
|
1962 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
1963 { |
5681
|
1964 work[j] = 1.; |
|
1965 |
|
1966 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
1967 { |
5681
|
1968 if (work[k] != 0.) |
5164
|
1969 { |
5681
|
1970 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
1971 work[k] = tmp; |
|
1972 for (octave_idx_type i = cidx(k); |
|
1973 i < cidx(k+1)-1; i++) |
|
1974 { |
|
1975 octave_idx_type iidx = ridx(i); |
|
1976 work[iidx] = work[iidx] - tmp * data(i); |
|
1977 } |
5164
|
1978 } |
|
1979 } |
5681
|
1980 double atmp = 0; |
|
1981 for (octave_idx_type i = 0; i < j+1; i++) |
|
1982 { |
|
1983 atmp += std::abs(work[i]); |
|
1984 work[i] = 0.; |
|
1985 } |
|
1986 if (atmp > ainvnorm) |
|
1987 ainvnorm = atmp; |
5164
|
1988 } |
5681
|
1989 rcond = 1. / ainvnorm / anorm; |
|
1990 } |
|
1991 } |
5164
|
1992 |
|
1993 triangular_error: |
|
1994 if (err != 0) |
|
1995 { |
|
1996 if (sing_handler) |
5681
|
1997 { |
|
1998 sing_handler (rcond); |
|
1999 mattype.mark_as_rectangular (); |
|
2000 } |
5164
|
2001 else |
|
2002 (*current_liboctave_error_handler) |
|
2003 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
2004 rcond); |
|
2005 } |
|
2006 |
|
2007 volatile double rcond_plus_one = rcond + 1.0; |
|
2008 |
|
2009 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
2010 { |
|
2011 err = -2; |
|
2012 |
|
2013 if (sing_handler) |
5681
|
2014 { |
|
2015 sing_handler (rcond); |
|
2016 mattype.mark_as_rectangular (); |
|
2017 } |
5164
|
2018 else |
|
2019 (*current_liboctave_error_handler) |
|
2020 ("matrix singular to machine precision, rcond = %g", |
|
2021 rcond); |
|
2022 } |
|
2023 } |
|
2024 else |
|
2025 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
2026 } |
|
2027 return retval; |
|
2028 } |
|
2029 |
|
2030 ComplexMatrix |
5785
|
2031 SparseComplexMatrix::utsolve (MatrixType &mattype, const ComplexMatrix& b, |
5630
|
2032 octave_idx_type& err, double& rcond, |
5681
|
2033 solve_singularity_handler sing_handler, |
|
2034 bool calc_cond) const |
5164
|
2035 { |
|
2036 ComplexMatrix retval; |
|
2037 |
5275
|
2038 octave_idx_type nr = rows (); |
|
2039 octave_idx_type nc = cols (); |
5630
|
2040 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
2041 err = 0; |
|
2042 |
6924
|
2043 if (nr != b.rows ()) |
5164
|
2044 (*current_liboctave_error_handler) |
|
2045 ("matrix dimension mismatch solution of linear equations"); |
6924
|
2046 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
2047 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
2048 else |
|
2049 { |
|
2050 // Print spparms("spumoni") info if requested |
|
2051 int typ = mattype.type (); |
|
2052 mattype.info (); |
|
2053 |
5785
|
2054 if (typ == MatrixType::Permuted_Upper || |
|
2055 typ == MatrixType::Upper) |
5164
|
2056 { |
|
2057 double anorm = 0.; |
|
2058 double ainvnorm = 0.; |
5275
|
2059 octave_idx_type b_nc = b.cols (); |
5681
|
2060 rcond = 1.; |
|
2061 |
|
2062 if (calc_cond) |
|
2063 { |
|
2064 // Calculate the 1-norm of matrix for rcond calculation |
|
2065 for (octave_idx_type j = 0; j < nc; j++) |
|
2066 { |
|
2067 double atmp = 0.; |
|
2068 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
2069 atmp += std::abs(data(i)); |
|
2070 if (atmp > anorm) |
|
2071 anorm = atmp; |
|
2072 } |
5164
|
2073 } |
|
2074 |
5785
|
2075 if (typ == MatrixType::Permuted_Upper) |
5164
|
2076 { |
5630
|
2077 retval.resize (nc, b_nc); |
5322
|
2078 octave_idx_type *perm = mattype.triangular_perm (); |
5630
|
2079 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
2080 |
5275
|
2081 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2082 { |
5275
|
2083 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
2084 work[i] = b(i,j); |
5630
|
2085 for (octave_idx_type i = nr; i < nc; i++) |
|
2086 work[i] = 0.; |
|
2087 |
|
2088 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
2089 { |
5322
|
2090 octave_idx_type kidx = perm[k]; |
|
2091 |
|
2092 if (work[k] != 0.) |
5164
|
2093 { |
5681
|
2094 if (ridx(cidx(kidx+1)-1) != k || |
|
2095 data(cidx(kidx+1)-1) == 0.) |
5164
|
2096 { |
|
2097 err = -2; |
|
2098 goto triangular_error; |
|
2099 } |
|
2100 |
5322
|
2101 Complex tmp = work[k] / data(cidx(kidx+1)-1); |
|
2102 work[k] = tmp; |
|
2103 for (octave_idx_type i = cidx(kidx); |
|
2104 i < cidx(kidx+1)-1; i++) |
5164
|
2105 { |
5322
|
2106 octave_idx_type iidx = ridx(i); |
|
2107 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2108 } |
|
2109 } |
|
2110 } |
|
2111 |
5630
|
2112 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
2113 retval (perm[i], j) = work[i]; |
5164
|
2114 } |
|
2115 |
5681
|
2116 if (calc_cond) |
|
2117 { |
|
2118 // Calculation of 1-norm of inv(*this) |
|
2119 for (octave_idx_type i = 0; i < nm; i++) |
|
2120 work[i] = 0.; |
|
2121 |
|
2122 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2123 { |
5681
|
2124 work[j] = 1.; |
|
2125 |
|
2126 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
2127 { |
5681
|
2128 octave_idx_type iidx = perm[k]; |
|
2129 |
|
2130 if (work[k] != 0.) |
5164
|
2131 { |
5681
|
2132 Complex tmp = work[k] / data(cidx(iidx+1)-1); |
|
2133 work[k] = tmp; |
|
2134 for (octave_idx_type i = cidx(iidx); |
|
2135 i < cidx(iidx+1)-1; i++) |
|
2136 { |
|
2137 octave_idx_type idx2 = ridx(i); |
|
2138 work[idx2] = work[idx2] - tmp * data(i); |
|
2139 } |
5164
|
2140 } |
|
2141 } |
5681
|
2142 double atmp = 0; |
|
2143 for (octave_idx_type i = 0; i < j+1; i++) |
|
2144 { |
|
2145 atmp += std::abs(work[i]); |
|
2146 work[i] = 0.; |
|
2147 } |
|
2148 if (atmp > ainvnorm) |
|
2149 ainvnorm = atmp; |
5164
|
2150 } |
5681
|
2151 rcond = 1. / ainvnorm / anorm; |
5164
|
2152 } |
|
2153 } |
|
2154 else |
|
2155 { |
5630
|
2156 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
2157 retval.resize (nc, b_nc); |
5164
|
2158 |
5275
|
2159 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2160 { |
5630
|
2161 for (octave_idx_type i = 0; i < nr; i++) |
|
2162 work[i] = b(i,j); |
|
2163 for (octave_idx_type i = nr; i < nc; i++) |
|
2164 work[i] = 0.; |
|
2165 |
|
2166 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
2167 { |
5630
|
2168 if (work[k] != 0.) |
5164
|
2169 { |
5681
|
2170 if (ridx(cidx(k+1)-1) != k || |
|
2171 data(cidx(k+1)-1) == 0.) |
5164
|
2172 { |
|
2173 err = -2; |
|
2174 goto triangular_error; |
|
2175 } |
|
2176 |
5630
|
2177 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
2178 work[k] = tmp; |
5275
|
2179 for (octave_idx_type i = cidx(k); i < cidx(k+1)-1; i++) |
5164
|
2180 { |
5275
|
2181 octave_idx_type iidx = ridx(i); |
5630
|
2182 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2183 } |
|
2184 } |
|
2185 } |
5630
|
2186 |
|
2187 for (octave_idx_type i = 0; i < nc; i++) |
|
2188 retval.xelem (i, j) = work[i]; |
5164
|
2189 } |
|
2190 |
5681
|
2191 if (calc_cond) |
|
2192 { |
|
2193 // Calculation of 1-norm of inv(*this) |
|
2194 for (octave_idx_type i = 0; i < nm; i++) |
|
2195 work[i] = 0.; |
|
2196 |
|
2197 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2198 { |
5681
|
2199 work[j] = 1.; |
|
2200 |
|
2201 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
2202 { |
5681
|
2203 if (work[k] != 0.) |
5164
|
2204 { |
5681
|
2205 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
2206 work[k] = tmp; |
|
2207 for (octave_idx_type i = cidx(k); |
|
2208 i < cidx(k+1)-1; i++) |
|
2209 { |
|
2210 octave_idx_type iidx = ridx(i); |
|
2211 work[iidx] = work[iidx] - tmp * data(i); |
|
2212 } |
5164
|
2213 } |
|
2214 } |
5681
|
2215 double atmp = 0; |
|
2216 for (octave_idx_type i = 0; i < j+1; i++) |
|
2217 { |
|
2218 atmp += std::abs(work[i]); |
|
2219 work[i] = 0.; |
|
2220 } |
|
2221 if (atmp > ainvnorm) |
|
2222 ainvnorm = atmp; |
5164
|
2223 } |
5681
|
2224 rcond = 1. / ainvnorm / anorm; |
|
2225 } |
|
2226 } |
5164
|
2227 |
|
2228 triangular_error: |
|
2229 if (err != 0) |
|
2230 { |
|
2231 if (sing_handler) |
5681
|
2232 { |
|
2233 sing_handler (rcond); |
|
2234 mattype.mark_as_rectangular (); |
|
2235 } |
5164
|
2236 else |
|
2237 (*current_liboctave_error_handler) |
|
2238 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
2239 rcond); |
|
2240 } |
|
2241 |
|
2242 volatile double rcond_plus_one = rcond + 1.0; |
|
2243 |
|
2244 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
2245 { |
|
2246 err = -2; |
|
2247 |
|
2248 if (sing_handler) |
5681
|
2249 { |
|
2250 sing_handler (rcond); |
|
2251 mattype.mark_as_rectangular (); |
|
2252 } |
5164
|
2253 else |
|
2254 (*current_liboctave_error_handler) |
|
2255 ("matrix singular to machine precision, rcond = %g", |
|
2256 rcond); |
|
2257 } |
|
2258 } |
|
2259 else |
|
2260 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
2261 } |
|
2262 |
|
2263 return retval; |
|
2264 } |
|
2265 |
|
2266 SparseComplexMatrix |
5785
|
2267 SparseComplexMatrix::utsolve (MatrixType &mattype, const SparseComplexMatrix& b, |
5630
|
2268 octave_idx_type& err, double& rcond, |
5681
|
2269 solve_singularity_handler sing_handler, |
|
2270 bool calc_cond) const |
5164
|
2271 { |
|
2272 SparseComplexMatrix retval; |
|
2273 |
5275
|
2274 octave_idx_type nr = rows (); |
|
2275 octave_idx_type nc = cols (); |
5630
|
2276 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
2277 err = 0; |
|
2278 |
6924
|
2279 if (nr != b.rows ()) |
5164
|
2280 (*current_liboctave_error_handler) |
|
2281 ("matrix dimension mismatch solution of linear equations"); |
6924
|
2282 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
2283 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
2284 else |
|
2285 { |
|
2286 // Print spparms("spumoni") info if requested |
|
2287 int typ = mattype.type (); |
|
2288 mattype.info (); |
|
2289 |
5785
|
2290 if (typ == MatrixType::Permuted_Upper || |
|
2291 typ == MatrixType::Upper) |
5164
|
2292 { |
|
2293 double anorm = 0.; |
|
2294 double ainvnorm = 0.; |
5681
|
2295 rcond = 1.; |
|
2296 |
|
2297 if (calc_cond) |
|
2298 { |
|
2299 // Calculate the 1-norm of matrix for rcond calculation |
|
2300 for (octave_idx_type j = 0; j < nc; j++) |
|
2301 { |
|
2302 double atmp = 0.; |
|
2303 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
2304 atmp += std::abs(data(i)); |
|
2305 if (atmp > anorm) |
|
2306 anorm = atmp; |
|
2307 } |
5164
|
2308 } |
|
2309 |
5275
|
2310 octave_idx_type b_nc = b.cols (); |
5681
|
2311 octave_idx_type b_nz = b.nnz (); |
5630
|
2312 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
2313 retval.xcidx(0) = 0; |
5275
|
2314 octave_idx_type ii = 0; |
|
2315 octave_idx_type x_nz = b_nz; |
5164
|
2316 |
5785
|
2317 if (typ == MatrixType::Permuted_Upper) |
5164
|
2318 { |
5322
|
2319 octave_idx_type *perm = mattype.triangular_perm (); |
5630
|
2320 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
2321 |
|
2322 OCTAVE_LOCAL_BUFFER (octave_idx_type, rperm, nc); |
|
2323 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
2324 rperm[perm[i]] = i; |
5164
|
2325 |
5275
|
2326 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2327 { |
5630
|
2328 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
2329 work[i] = 0.; |
5275
|
2330 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
2331 work[b.ridx(i)] = b.data(i); |
|
2332 |
5630
|
2333 for (octave_idx_type k = nc-1; k >= 0; k--) |
5164
|
2334 { |
5322
|
2335 octave_idx_type kidx = perm[k]; |
|
2336 |
|
2337 if (work[k] != 0.) |
5164
|
2338 { |
5681
|
2339 if (ridx(cidx(kidx+1)-1) != k || |
|
2340 data(cidx(kidx+1)-1) == 0.) |
5164
|
2341 { |
|
2342 err = -2; |
|
2343 goto triangular_error; |
|
2344 } |
|
2345 |
5322
|
2346 Complex tmp = work[k] / data(cidx(kidx+1)-1); |
|
2347 work[k] = tmp; |
|
2348 for (octave_idx_type i = cidx(kidx); |
|
2349 i < cidx(kidx+1)-1; i++) |
5164
|
2350 { |
5322
|
2351 octave_idx_type iidx = ridx(i); |
|
2352 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2353 } |
|
2354 } |
|
2355 } |
|
2356 |
|
2357 // Count non-zeros in work vector and adjust space in |
|
2358 // retval if needed |
5275
|
2359 octave_idx_type new_nnz = 0; |
5630
|
2360 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
2361 if (work[i] != 0.) |
|
2362 new_nnz++; |
|
2363 |
|
2364 if (ii + new_nnz > x_nz) |
|
2365 { |
|
2366 // Resize the sparse matrix |
5275
|
2367 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
2368 retval.change_capacity (sz); |
|
2369 x_nz = sz; |
|
2370 } |
|
2371 |
5630
|
2372 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
2373 if (work[rperm[i]] != 0.) |
5164
|
2374 { |
|
2375 retval.xridx(ii) = i; |
5322
|
2376 retval.xdata(ii++) = work[rperm[i]]; |
5164
|
2377 } |
|
2378 retval.xcidx(j+1) = ii; |
|
2379 } |
|
2380 |
|
2381 retval.maybe_compress (); |
|
2382 |
5681
|
2383 if (calc_cond) |
|
2384 { |
|
2385 // Calculation of 1-norm of inv(*this) |
|
2386 for (octave_idx_type i = 0; i < nm; i++) |
|
2387 work[i] = 0.; |
|
2388 |
|
2389 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2390 { |
5681
|
2391 work[j] = 1.; |
|
2392 |
|
2393 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
2394 { |
5681
|
2395 octave_idx_type iidx = perm[k]; |
|
2396 |
|
2397 if (work[k] != 0.) |
5164
|
2398 { |
5681
|
2399 Complex tmp = work[k] / data(cidx(iidx+1)-1); |
|
2400 work[k] = tmp; |
|
2401 for (octave_idx_type i = cidx(iidx); |
|
2402 i < cidx(iidx+1)-1; i++) |
|
2403 { |
|
2404 octave_idx_type idx2 = ridx(i); |
|
2405 work[idx2] = work[idx2] - tmp * data(i); |
|
2406 } |
5164
|
2407 } |
|
2408 } |
5681
|
2409 double atmp = 0; |
|
2410 for (octave_idx_type i = 0; i < j+1; i++) |
|
2411 { |
|
2412 atmp += std::abs(work[i]); |
|
2413 work[i] = 0.; |
|
2414 } |
|
2415 if (atmp > ainvnorm) |
|
2416 ainvnorm = atmp; |
5164
|
2417 } |
5681
|
2418 rcond = 1. / ainvnorm / anorm; |
5164
|
2419 } |
|
2420 } |
|
2421 else |
|
2422 { |
5630
|
2423 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
2424 |
5275
|
2425 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2426 { |
5630
|
2427 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
2428 work[i] = 0.; |
5275
|
2429 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
2430 work[b.ridx(i)] = b.data(i); |
|
2431 |
5275
|
2432 for (octave_idx_type k = nr-1; k >= 0; k--) |
5164
|
2433 { |
|
2434 if (work[k] != 0.) |
|
2435 { |
5681
|
2436 if (ridx(cidx(k+1)-1) != k || |
|
2437 data(cidx(k+1)-1) == 0.) |
5164
|
2438 { |
|
2439 err = -2; |
|
2440 goto triangular_error; |
|
2441 } |
|
2442 |
|
2443 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
2444 work[k] = tmp; |
5275
|
2445 for (octave_idx_type i = cidx(k); i < cidx(k+1)-1; i++) |
5164
|
2446 { |
5275
|
2447 octave_idx_type iidx = ridx(i); |
5164
|
2448 work[iidx] = work[iidx] - tmp * data(i); |
|
2449 } |
|
2450 } |
|
2451 } |
|
2452 |
|
2453 // Count non-zeros in work vector and adjust space in |
|
2454 // retval if needed |
5275
|
2455 octave_idx_type new_nnz = 0; |
5630
|
2456 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
2457 if (work[i] != 0.) |
|
2458 new_nnz++; |
|
2459 |
|
2460 if (ii + new_nnz > x_nz) |
|
2461 { |
|
2462 // Resize the sparse matrix |
5275
|
2463 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
2464 retval.change_capacity (sz); |
|
2465 x_nz = sz; |
|
2466 } |
|
2467 |
5630
|
2468 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
2469 if (work[i] != 0.) |
|
2470 { |
|
2471 retval.xridx(ii) = i; |
|
2472 retval.xdata(ii++) = work[i]; |
|
2473 } |
|
2474 retval.xcidx(j+1) = ii; |
|
2475 } |
|
2476 |
|
2477 retval.maybe_compress (); |
|
2478 |
5681
|
2479 if (calc_cond) |
|
2480 { |
|
2481 // Calculation of 1-norm of inv(*this) |
|
2482 for (octave_idx_type i = 0; i < nm; i++) |
|
2483 work[i] = 0.; |
|
2484 |
|
2485 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2486 { |
5681
|
2487 work[j] = 1.; |
|
2488 |
|
2489 for (octave_idx_type k = j; k >= 0; k--) |
5164
|
2490 { |
5681
|
2491 if (work[k] != 0.) |
5164
|
2492 { |
5681
|
2493 Complex tmp = work[k] / data(cidx(k+1)-1); |
|
2494 work[k] = tmp; |
|
2495 for (octave_idx_type i = cidx(k); |
|
2496 i < cidx(k+1)-1; i++) |
|
2497 { |
|
2498 octave_idx_type iidx = ridx(i); |
|
2499 work[iidx] = work[iidx] - tmp * data(i); |
|
2500 } |
5164
|
2501 } |
|
2502 } |
5681
|
2503 double atmp = 0; |
|
2504 for (octave_idx_type i = 0; i < j+1; i++) |
|
2505 { |
|
2506 atmp += std::abs(work[i]); |
|
2507 work[i] = 0.; |
|
2508 } |
|
2509 if (atmp > ainvnorm) |
|
2510 ainvnorm = atmp; |
5164
|
2511 } |
5681
|
2512 rcond = 1. / ainvnorm / anorm; |
|
2513 } |
|
2514 } |
5164
|
2515 |
|
2516 triangular_error: |
|
2517 if (err != 0) |
|
2518 { |
|
2519 if (sing_handler) |
5681
|
2520 { |
|
2521 sing_handler (rcond); |
|
2522 mattype.mark_as_rectangular (); |
|
2523 } |
5164
|
2524 else |
|
2525 (*current_liboctave_error_handler) |
|
2526 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
2527 rcond); |
|
2528 } |
|
2529 |
|
2530 volatile double rcond_plus_one = rcond + 1.0; |
|
2531 |
|
2532 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
2533 { |
|
2534 err = -2; |
|
2535 |
|
2536 if (sing_handler) |
5681
|
2537 { |
|
2538 sing_handler (rcond); |
|
2539 mattype.mark_as_rectangular (); |
|
2540 } |
5164
|
2541 else |
|
2542 (*current_liboctave_error_handler) |
|
2543 ("matrix singular to machine precision, rcond = %g", |
|
2544 rcond); |
|
2545 } |
|
2546 } |
|
2547 else |
|
2548 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
2549 } |
|
2550 |
|
2551 return retval; |
|
2552 } |
|
2553 |
|
2554 ComplexMatrix |
5785
|
2555 SparseComplexMatrix::ltsolve (MatrixType &mattype, const Matrix& b, |
5630
|
2556 octave_idx_type& err, double& rcond, |
5681
|
2557 solve_singularity_handler sing_handler, |
|
2558 bool calc_cond) const |
5164
|
2559 { |
|
2560 ComplexMatrix retval; |
|
2561 |
5275
|
2562 octave_idx_type nr = rows (); |
|
2563 octave_idx_type nc = cols (); |
5630
|
2564 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
2565 err = 0; |
|
2566 |
6924
|
2567 if (nr != b.rows ()) |
5164
|
2568 (*current_liboctave_error_handler) |
|
2569 ("matrix dimension mismatch solution of linear equations"); |
6924
|
2570 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
2571 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
2572 else |
|
2573 { |
|
2574 // Print spparms("spumoni") info if requested |
|
2575 int typ = mattype.type (); |
|
2576 mattype.info (); |
|
2577 |
5785
|
2578 if (typ == MatrixType::Permuted_Lower || |
|
2579 typ == MatrixType::Lower) |
5164
|
2580 { |
|
2581 double anorm = 0.; |
|
2582 double ainvnorm = 0.; |
5630
|
2583 octave_idx_type b_nc = b.cols (); |
5681
|
2584 rcond = 1.; |
|
2585 |
|
2586 if (calc_cond) |
|
2587 { |
|
2588 // Calculate the 1-norm of matrix for rcond calculation |
|
2589 for (octave_idx_type j = 0; j < nc; j++) |
|
2590 { |
|
2591 double atmp = 0.; |
|
2592 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
2593 atmp += std::abs(data(i)); |
|
2594 if (atmp > anorm) |
|
2595 anorm = atmp; |
|
2596 } |
5164
|
2597 } |
|
2598 |
5785
|
2599 if (typ == MatrixType::Permuted_Lower) |
5164
|
2600 { |
5630
|
2601 retval.resize (nc, b_nc); |
|
2602 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5322
|
2603 octave_idx_type *perm = mattype.triangular_perm (); |
5164
|
2604 |
5630
|
2605 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2606 { |
5630
|
2607 for (octave_idx_type i = 0; i < nm; i++) |
|
2608 work[i] = 0.; |
5275
|
2609 for (octave_idx_type i = 0; i < nr; i++) |
5322
|
2610 work[perm[i]] = b(i,j); |
5164
|
2611 |
5630
|
2612 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2613 { |
5322
|
2614 if (work[k] != 0.) |
5164
|
2615 { |
5322
|
2616 octave_idx_type minr = nr; |
|
2617 octave_idx_type mini = 0; |
|
2618 |
|
2619 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
2620 if (perm[ridx(i)] < minr) |
|
2621 { |
|
2622 minr = perm[ridx(i)]; |
|
2623 mini = i; |
|
2624 } |
|
2625 |
5681
|
2626 if (minr != k || data (mini) == 0.) |
5164
|
2627 { |
|
2628 err = -2; |
|
2629 goto triangular_error; |
|
2630 } |
|
2631 |
5322
|
2632 Complex tmp = work[k] / data(mini); |
|
2633 work[k] = tmp; |
|
2634 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
5164
|
2635 { |
5322
|
2636 if (i == mini) |
|
2637 continue; |
|
2638 |
|
2639 octave_idx_type iidx = perm[ridx(i)]; |
|
2640 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2641 } |
|
2642 } |
|
2643 } |
|
2644 |
5630
|
2645 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
2646 retval (i, j) = work[i]; |
5164
|
2647 } |
|
2648 |
5681
|
2649 if (calc_cond) |
|
2650 { |
|
2651 // Calculation of 1-norm of inv(*this) |
|
2652 for (octave_idx_type i = 0; i < nm; i++) |
|
2653 work[i] = 0.; |
|
2654 |
|
2655 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2656 { |
5681
|
2657 work[j] = 1.; |
|
2658 |
|
2659 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2660 { |
5681
|
2661 if (work[k] != 0.) |
5164
|
2662 { |
5681
|
2663 octave_idx_type minr = nr; |
|
2664 octave_idx_type mini = 0; |
|
2665 |
|
2666 for (octave_idx_type i = cidx(k); |
|
2667 i < cidx(k+1); i++) |
|
2668 if (perm[ridx(i)] < minr) |
|
2669 { |
|
2670 minr = perm[ridx(i)]; |
|
2671 mini = i; |
|
2672 } |
|
2673 |
|
2674 Complex tmp = work[k] / data(mini); |
|
2675 work[k] = tmp; |
|
2676 for (octave_idx_type i = cidx(k); |
|
2677 i < cidx(k+1); i++) |
|
2678 { |
|
2679 if (i == mini) |
|
2680 continue; |
|
2681 |
|
2682 octave_idx_type iidx = perm[ridx(i)]; |
|
2683 work[iidx] = work[iidx] - tmp * data(i); |
|
2684 } |
5164
|
2685 } |
|
2686 } |
5681
|
2687 |
|
2688 double atmp = 0; |
|
2689 for (octave_idx_type i = j; i < nc; i++) |
|
2690 { |
|
2691 atmp += std::abs(work[i]); |
|
2692 work[i] = 0.; |
|
2693 } |
|
2694 if (atmp > ainvnorm) |
|
2695 ainvnorm = atmp; |
5164
|
2696 } |
5681
|
2697 rcond = 1. / ainvnorm / anorm; |
5164
|
2698 } |
|
2699 } |
|
2700 else |
|
2701 { |
5630
|
2702 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
2703 retval.resize (nc, b_nc, 0.); |
|
2704 |
|
2705 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2706 { |
5630
|
2707 for (octave_idx_type i = 0; i < nr; i++) |
|
2708 work[i] = b(i,j); |
|
2709 for (octave_idx_type i = nr; i < nc; i++) |
|
2710 work[i] = 0.; |
|
2711 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2712 { |
5630
|
2713 if (work[k] != 0.) |
5164
|
2714 { |
5681
|
2715 if (ridx(cidx(k)) != k || |
|
2716 data(cidx(k)) == 0.) |
5164
|
2717 { |
|
2718 err = -2; |
|
2719 goto triangular_error; |
|
2720 } |
|
2721 |
5630
|
2722 Complex tmp = work[k] / data(cidx(k)); |
|
2723 work[k] = tmp; |
5275
|
2724 for (octave_idx_type i = cidx(k)+1; i < cidx(k+1); i++) |
5164
|
2725 { |
5275
|
2726 octave_idx_type iidx = ridx(i); |
5630
|
2727 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2728 } |
|
2729 } |
|
2730 } |
5630
|
2731 for (octave_idx_type i = 0; i < nc; i++) |
|
2732 retval.xelem (i, j) = work[i]; |
5164
|
2733 } |
|
2734 |
5681
|
2735 if (calc_cond) |
|
2736 { |
|
2737 // Calculation of 1-norm of inv(*this) |
|
2738 for (octave_idx_type i = 0; i < nm; i++) |
|
2739 work[i] = 0.; |
|
2740 |
|
2741 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2742 { |
5681
|
2743 work[j] = 1.; |
|
2744 |
|
2745 for (octave_idx_type k = j; k < nc; k++) |
5164
|
2746 { |
5681
|
2747 |
|
2748 if (work[k] != 0.) |
5164
|
2749 { |
5681
|
2750 Complex tmp = work[k] / data(cidx(k)); |
|
2751 work[k] = tmp; |
|
2752 for (octave_idx_type i = cidx(k)+1; |
|
2753 i < cidx(k+1); i++) |
|
2754 { |
|
2755 octave_idx_type iidx = ridx(i); |
|
2756 work[iidx] = work[iidx] - tmp * data(i); |
|
2757 } |
5164
|
2758 } |
|
2759 } |
5681
|
2760 double atmp = 0; |
|
2761 for (octave_idx_type i = j; i < nc; i++) |
|
2762 { |
|
2763 atmp += std::abs(work[i]); |
|
2764 work[i] = 0.; |
|
2765 } |
|
2766 if (atmp > ainvnorm) |
|
2767 ainvnorm = atmp; |
5164
|
2768 } |
5681
|
2769 rcond = 1. / ainvnorm / anorm; |
|
2770 } |
|
2771 } |
5164
|
2772 triangular_error: |
|
2773 if (err != 0) |
|
2774 { |
|
2775 if (sing_handler) |
5681
|
2776 { |
|
2777 sing_handler (rcond); |
|
2778 mattype.mark_as_rectangular (); |
|
2779 } |
5164
|
2780 else |
|
2781 (*current_liboctave_error_handler) |
|
2782 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
2783 rcond); |
|
2784 } |
|
2785 |
|
2786 volatile double rcond_plus_one = rcond + 1.0; |
|
2787 |
|
2788 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
2789 { |
|
2790 err = -2; |
|
2791 |
|
2792 if (sing_handler) |
5681
|
2793 { |
|
2794 sing_handler (rcond); |
|
2795 mattype.mark_as_rectangular (); |
|
2796 } |
5164
|
2797 else |
|
2798 (*current_liboctave_error_handler) |
|
2799 ("matrix singular to machine precision, rcond = %g", |
|
2800 rcond); |
|
2801 } |
|
2802 } |
|
2803 else |
|
2804 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
2805 } |
|
2806 |
|
2807 return retval; |
|
2808 } |
|
2809 |
|
2810 SparseComplexMatrix |
5785
|
2811 SparseComplexMatrix::ltsolve (MatrixType &mattype, const SparseMatrix& b, |
5630
|
2812 octave_idx_type& err, double& rcond, |
5681
|
2813 solve_singularity_handler sing_handler, |
|
2814 bool calc_cond) const |
5164
|
2815 { |
|
2816 SparseComplexMatrix retval; |
|
2817 |
5275
|
2818 octave_idx_type nr = rows (); |
|
2819 octave_idx_type nc = cols (); |
5630
|
2820 octave_idx_type nm = (nc > nr ? nc : nr); |
|
2821 |
5164
|
2822 err = 0; |
|
2823 |
6924
|
2824 if (nr != b.rows ()) |
5164
|
2825 (*current_liboctave_error_handler) |
|
2826 ("matrix dimension mismatch solution of linear equations"); |
6924
|
2827 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
2828 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
2829 else |
|
2830 { |
|
2831 // Print spparms("spumoni") info if requested |
|
2832 int typ = mattype.type (); |
|
2833 mattype.info (); |
|
2834 |
5785
|
2835 if (typ == MatrixType::Permuted_Lower || |
|
2836 typ == MatrixType::Lower) |
5164
|
2837 { |
|
2838 double anorm = 0.; |
|
2839 double ainvnorm = 0.; |
5681
|
2840 rcond = 1.; |
|
2841 |
|
2842 if (calc_cond) |
|
2843 { |
|
2844 // Calculate the 1-norm of matrix for rcond calculation |
|
2845 for (octave_idx_type j = 0; j < nc; j++) |
|
2846 { |
|
2847 double atmp = 0.; |
|
2848 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
2849 atmp += std::abs(data(i)); |
|
2850 if (atmp > anorm) |
|
2851 anorm = atmp; |
|
2852 } |
5164
|
2853 } |
|
2854 |
5275
|
2855 octave_idx_type b_nc = b.cols (); |
5681
|
2856 octave_idx_type b_nz = b.nnz (); |
5630
|
2857 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
2858 retval.xcidx(0) = 0; |
5275
|
2859 octave_idx_type ii = 0; |
|
2860 octave_idx_type x_nz = b_nz; |
5164
|
2861 |
5785
|
2862 if (typ == MatrixType::Permuted_Lower) |
5164
|
2863 { |
5630
|
2864 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5322
|
2865 octave_idx_type *perm = mattype.triangular_perm (); |
5164
|
2866 |
5275
|
2867 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2868 { |
5630
|
2869 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
2870 work[i] = 0.; |
5275
|
2871 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5322
|
2872 work[perm[b.ridx(i)]] = b.data(i); |
5164
|
2873 |
5630
|
2874 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2875 { |
5322
|
2876 if (work[k] != 0.) |
5164
|
2877 { |
5322
|
2878 octave_idx_type minr = nr; |
|
2879 octave_idx_type mini = 0; |
|
2880 |
|
2881 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
2882 if (perm[ridx(i)] < minr) |
|
2883 { |
|
2884 minr = perm[ridx(i)]; |
|
2885 mini = i; |
|
2886 } |
|
2887 |
5681
|
2888 if (minr != k || data (mini) == 0.) |
5164
|
2889 { |
|
2890 err = -2; |
|
2891 goto triangular_error; |
|
2892 } |
|
2893 |
5322
|
2894 Complex tmp = work[k] / data(mini); |
|
2895 work[k] = tmp; |
|
2896 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
5164
|
2897 { |
5322
|
2898 if (i == mini) |
|
2899 continue; |
|
2900 |
|
2901 octave_idx_type iidx = perm[ridx(i)]; |
|
2902 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
2903 } |
|
2904 } |
|
2905 } |
|
2906 |
|
2907 // Count non-zeros in work vector and adjust space in |
|
2908 // retval if needed |
5275
|
2909 octave_idx_type new_nnz = 0; |
5630
|
2910 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
2911 if (work[i] != 0.) |
|
2912 new_nnz++; |
|
2913 |
|
2914 if (ii + new_nnz > x_nz) |
|
2915 { |
|
2916 // Resize the sparse matrix |
5275
|
2917 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
2918 retval.change_capacity (sz); |
|
2919 x_nz = sz; |
|
2920 } |
|
2921 |
5630
|
2922 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
2923 if (work[i] != 0.) |
5164
|
2924 { |
|
2925 retval.xridx(ii) = i; |
5322
|
2926 retval.xdata(ii++) = work[i]; |
5164
|
2927 } |
|
2928 retval.xcidx(j+1) = ii; |
|
2929 } |
|
2930 |
|
2931 retval.maybe_compress (); |
|
2932 |
5681
|
2933 if (calc_cond) |
|
2934 { |
|
2935 // Calculation of 1-norm of inv(*this) |
|
2936 for (octave_idx_type i = 0; i < nm; i++) |
|
2937 work[i] = 0.; |
|
2938 |
|
2939 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
2940 { |
5681
|
2941 work[j] = 1.; |
|
2942 |
|
2943 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2944 { |
5681
|
2945 if (work[k] != 0.) |
5164
|
2946 { |
5681
|
2947 octave_idx_type minr = nr; |
|
2948 octave_idx_type mini = 0; |
|
2949 |
|
2950 for (octave_idx_type i = cidx(k); |
|
2951 i < cidx(k+1); i++) |
|
2952 if (perm[ridx(i)] < minr) |
|
2953 { |
|
2954 minr = perm[ridx(i)]; |
|
2955 mini = i; |
|
2956 } |
|
2957 |
|
2958 Complex tmp = work[k] / data(mini); |
|
2959 work[k] = tmp; |
|
2960 for (octave_idx_type i = cidx(k); |
|
2961 i < cidx(k+1); i++) |
|
2962 { |
|
2963 if (i == mini) |
|
2964 continue; |
|
2965 |
|
2966 octave_idx_type iidx = perm[ridx(i)]; |
|
2967 work[iidx] = work[iidx] - tmp * data(i); |
|
2968 } |
5164
|
2969 } |
|
2970 } |
5681
|
2971 |
|
2972 double atmp = 0; |
|
2973 for (octave_idx_type i = j; i < nc; i++) |
|
2974 { |
|
2975 atmp += std::abs(work[i]); |
|
2976 work[i] = 0.; |
|
2977 } |
|
2978 if (atmp > ainvnorm) |
|
2979 ainvnorm = atmp; |
5164
|
2980 } |
5681
|
2981 rcond = 1. / ainvnorm / anorm; |
5164
|
2982 } |
|
2983 } |
|
2984 else |
|
2985 { |
5630
|
2986 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
2987 |
5275
|
2988 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
2989 { |
5630
|
2990 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
2991 work[i] = 0.; |
5275
|
2992 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
2993 work[b.ridx(i)] = b.data(i); |
|
2994 |
5630
|
2995 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
2996 { |
|
2997 if (work[k] != 0.) |
|
2998 { |
5681
|
2999 if (ridx(cidx(k)) != k || |
|
3000 data(cidx(k)) == 0.) |
5164
|
3001 { |
|
3002 err = -2; |
|
3003 goto triangular_error; |
|
3004 } |
|
3005 |
|
3006 Complex tmp = work[k] / data(cidx(k)); |
|
3007 work[k] = tmp; |
5275
|
3008 for (octave_idx_type i = cidx(k)+1; i < cidx(k+1); i++) |
5164
|
3009 { |
5275
|
3010 octave_idx_type iidx = ridx(i); |
5164
|
3011 work[iidx] = work[iidx] - tmp * data(i); |
|
3012 } |
|
3013 } |
|
3014 } |
|
3015 |
|
3016 // Count non-zeros in work vector and adjust space in |
|
3017 // retval if needed |
5275
|
3018 octave_idx_type new_nnz = 0; |
5630
|
3019 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
3020 if (work[i] != 0.) |
|
3021 new_nnz++; |
|
3022 |
|
3023 if (ii + new_nnz > x_nz) |
|
3024 { |
|
3025 // Resize the sparse matrix |
5275
|
3026 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
3027 retval.change_capacity (sz); |
|
3028 x_nz = sz; |
|
3029 } |
|
3030 |
5630
|
3031 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
3032 if (work[i] != 0.) |
|
3033 { |
|
3034 retval.xridx(ii) = i; |
|
3035 retval.xdata(ii++) = work[i]; |
|
3036 } |
|
3037 retval.xcidx(j+1) = ii; |
|
3038 } |
|
3039 |
|
3040 retval.maybe_compress (); |
|
3041 |
5681
|
3042 if (calc_cond) |
|
3043 { |
|
3044 // Calculation of 1-norm of inv(*this) |
|
3045 for (octave_idx_type i = 0; i < nm; i++) |
|
3046 work[i] = 0.; |
|
3047 |
|
3048 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
3049 { |
5681
|
3050 work[j] = 1.; |
|
3051 |
|
3052 for (octave_idx_type k = j; k < nc; k++) |
5164
|
3053 { |
5681
|
3054 |
|
3055 if (work[k] != 0.) |
5164
|
3056 { |
5681
|
3057 Complex tmp = work[k] / data(cidx(k)); |
|
3058 work[k] = tmp; |
|
3059 for (octave_idx_type i = cidx(k)+1; |
|
3060 i < cidx(k+1); i++) |
|
3061 { |
|
3062 octave_idx_type iidx = ridx(i); |
|
3063 work[iidx] = work[iidx] - tmp * data(i); |
|
3064 } |
5164
|
3065 } |
|
3066 } |
5681
|
3067 double atmp = 0; |
|
3068 for (octave_idx_type i = j; i < nc; i++) |
|
3069 { |
|
3070 atmp += std::abs(work[i]); |
|
3071 work[i] = 0.; |
|
3072 } |
|
3073 if (atmp > ainvnorm) |
|
3074 ainvnorm = atmp; |
5164
|
3075 } |
5681
|
3076 rcond = 1. / ainvnorm / anorm; |
|
3077 } |
|
3078 } |
5164
|
3079 |
|
3080 triangular_error: |
|
3081 if (err != 0) |
|
3082 { |
|
3083 if (sing_handler) |
5681
|
3084 { |
|
3085 sing_handler (rcond); |
|
3086 mattype.mark_as_rectangular (); |
|
3087 } |
5164
|
3088 else |
|
3089 (*current_liboctave_error_handler) |
|
3090 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
3091 rcond); |
|
3092 } |
|
3093 |
|
3094 volatile double rcond_plus_one = rcond + 1.0; |
|
3095 |
|
3096 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
3097 { |
|
3098 err = -2; |
|
3099 |
|
3100 if (sing_handler) |
5681
|
3101 { |
|
3102 sing_handler (rcond); |
|
3103 mattype.mark_as_rectangular (); |
|
3104 } |
5164
|
3105 else |
|
3106 (*current_liboctave_error_handler) |
|
3107 ("matrix singular to machine precision, rcond = %g", |
|
3108 rcond); |
|
3109 } |
|
3110 } |
|
3111 else |
|
3112 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
3113 } |
|
3114 |
|
3115 return retval; |
|
3116 } |
|
3117 |
|
3118 ComplexMatrix |
5785
|
3119 SparseComplexMatrix::ltsolve (MatrixType &mattype, const ComplexMatrix& b, |
5630
|
3120 octave_idx_type& err, double& rcond, |
5681
|
3121 solve_singularity_handler sing_handler, |
|
3122 bool calc_cond) const |
5164
|
3123 { |
|
3124 ComplexMatrix retval; |
|
3125 |
5275
|
3126 octave_idx_type nr = rows (); |
|
3127 octave_idx_type nc = cols (); |
5630
|
3128 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
3129 err = 0; |
|
3130 |
6924
|
3131 if (nr != b.rows ()) |
5164
|
3132 (*current_liboctave_error_handler) |
|
3133 ("matrix dimension mismatch solution of linear equations"); |
6924
|
3134 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
3135 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
3136 else |
|
3137 { |
|
3138 // Print spparms("spumoni") info if requested |
|
3139 int typ = mattype.type (); |
|
3140 mattype.info (); |
|
3141 |
5785
|
3142 if (typ == MatrixType::Permuted_Lower || |
|
3143 typ == MatrixType::Lower) |
5164
|
3144 { |
|
3145 double anorm = 0.; |
|
3146 double ainvnorm = 0.; |
5275
|
3147 octave_idx_type b_nc = b.cols (); |
5681
|
3148 rcond = 1.; |
|
3149 |
|
3150 if (calc_cond) |
|
3151 { |
|
3152 // Calculate the 1-norm of matrix for rcond calculation |
|
3153 for (octave_idx_type j = 0; j < nc; j++) |
|
3154 { |
|
3155 double atmp = 0.; |
|
3156 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
3157 atmp += std::abs(data(i)); |
|
3158 if (atmp > anorm) |
|
3159 anorm = atmp; |
|
3160 } |
5164
|
3161 } |
|
3162 |
5785
|
3163 if (typ == MatrixType::Permuted_Lower) |
5164
|
3164 { |
5630
|
3165 retval.resize (nc, b_nc); |
|
3166 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5322
|
3167 octave_idx_type *perm = mattype.triangular_perm (); |
5164
|
3168 |
5275
|
3169 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
3170 { |
5630
|
3171 for (octave_idx_type i = 0; i < nm; i++) |
|
3172 work[i] = 0.; |
5275
|
3173 for (octave_idx_type i = 0; i < nr; i++) |
5322
|
3174 work[perm[i]] = b(i,j); |
5164
|
3175 |
5630
|
3176 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3177 { |
5322
|
3178 if (work[k] != 0.) |
5164
|
3179 { |
5322
|
3180 octave_idx_type minr = nr; |
|
3181 octave_idx_type mini = 0; |
|
3182 |
|
3183 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
3184 if (perm[ridx(i)] < minr) |
|
3185 { |
|
3186 minr = perm[ridx(i)]; |
|
3187 mini = i; |
|
3188 } |
|
3189 |
5681
|
3190 if (minr != k || data (mini) == 0.) |
5164
|
3191 { |
|
3192 err = -2; |
|
3193 goto triangular_error; |
|
3194 } |
|
3195 |
5322
|
3196 Complex tmp = work[k] / data(mini); |
|
3197 work[k] = tmp; |
|
3198 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
5164
|
3199 { |
5322
|
3200 if (i == mini) |
|
3201 continue; |
|
3202 |
|
3203 octave_idx_type iidx = perm[ridx(i)]; |
|
3204 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
3205 } |
|
3206 } |
|
3207 } |
|
3208 |
5630
|
3209 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
3210 retval (i, j) = work[i]; |
5164
|
3211 } |
|
3212 |
5681
|
3213 if (calc_cond) |
|
3214 { |
|
3215 // Calculation of 1-norm of inv(*this) |
|
3216 for (octave_idx_type i = 0; i < nm; i++) |
|
3217 work[i] = 0.; |
|
3218 |
|
3219 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
3220 { |
5681
|
3221 work[j] = 1.; |
|
3222 |
|
3223 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3224 { |
5681
|
3225 if (work[k] != 0.) |
5164
|
3226 { |
5681
|
3227 octave_idx_type minr = nr; |
|
3228 octave_idx_type mini = 0; |
|
3229 |
|
3230 for (octave_idx_type i = cidx(k); |
|
3231 i < cidx(k+1); i++) |
|
3232 if (perm[ridx(i)] < minr) |
|
3233 { |
|
3234 minr = perm[ridx(i)]; |
|
3235 mini = i; |
|
3236 } |
|
3237 |
|
3238 Complex tmp = work[k] / data(mini); |
|
3239 work[k] = tmp; |
|
3240 for (octave_idx_type i = cidx(k); |
|
3241 i < cidx(k+1); i++) |
|
3242 { |
|
3243 if (i == mini) |
|
3244 continue; |
|
3245 |
|
3246 octave_idx_type iidx = perm[ridx(i)]; |
|
3247 work[iidx] = work[iidx] - tmp * data(i); |
|
3248 } |
5164
|
3249 } |
|
3250 } |
5681
|
3251 |
|
3252 double atmp = 0; |
|
3253 for (octave_idx_type i = j; i < nc; i++) |
|
3254 { |
|
3255 atmp += std::abs(work[i]); |
|
3256 work[i] = 0.; |
|
3257 } |
|
3258 if (atmp > ainvnorm) |
|
3259 ainvnorm = atmp; |
5164
|
3260 } |
5681
|
3261 rcond = 1. / ainvnorm / anorm; |
5164
|
3262 } |
|
3263 } |
|
3264 else |
|
3265 { |
5630
|
3266 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
|
3267 retval.resize (nc, b_nc, 0.); |
|
3268 |
5164
|
3269 |
5275
|
3270 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
3271 { |
5630
|
3272 for (octave_idx_type i = 0; i < nr; i++) |
|
3273 work[i] = b(i,j); |
|
3274 for (octave_idx_type i = nr; i < nc; i++) |
|
3275 work[i] = 0.; |
|
3276 |
|
3277 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3278 { |
5630
|
3279 if (work[k] != 0.) |
5164
|
3280 { |
5681
|
3281 if (ridx(cidx(k)) != k || |
|
3282 data(cidx(k)) == 0.) |
5164
|
3283 { |
|
3284 err = -2; |
|
3285 goto triangular_error; |
|
3286 } |
|
3287 |
5630
|
3288 Complex tmp = work[k] / data(cidx(k)); |
|
3289 work[k] = tmp; |
5275
|
3290 for (octave_idx_type i = cidx(k)+1; i < cidx(k+1); i++) |
5164
|
3291 { |
5275
|
3292 octave_idx_type iidx = ridx(i); |
5630
|
3293 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
3294 } |
|
3295 } |
|
3296 } |
5630
|
3297 |
|
3298 for (octave_idx_type i = 0; i < nc; i++) |
|
3299 retval.xelem (i, j) = work[i]; |
5164
|
3300 } |
|
3301 |
5681
|
3302 if (calc_cond) |
|
3303 { |
|
3304 // Calculation of 1-norm of inv(*this) |
|
3305 for (octave_idx_type i = 0; i < nm; i++) |
|
3306 work[i] = 0.; |
|
3307 |
|
3308 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
3309 { |
5681
|
3310 work[j] = 1.; |
|
3311 |
|
3312 for (octave_idx_type k = j; k < nc; k++) |
5164
|
3313 { |
5681
|
3314 |
|
3315 if (work[k] != 0.) |
5164
|
3316 { |
5681
|
3317 Complex tmp = work[k] / data(cidx(k)); |
|
3318 work[k] = tmp; |
|
3319 for (octave_idx_type i = cidx(k)+1; |
|
3320 i < cidx(k+1); i++) |
|
3321 { |
|
3322 octave_idx_type iidx = ridx(i); |
|
3323 work[iidx] = work[iidx] - tmp * data(i); |
|
3324 } |
5164
|
3325 } |
|
3326 } |
5681
|
3327 double atmp = 0; |
|
3328 for (octave_idx_type i = j; i < nc; i++) |
|
3329 { |
|
3330 atmp += std::abs(work[i]); |
|
3331 work[i] = 0.; |
|
3332 } |
|
3333 if (atmp > ainvnorm) |
|
3334 ainvnorm = atmp; |
5164
|
3335 } |
5681
|
3336 rcond = 1. / ainvnorm / anorm; |
|
3337 } |
|
3338 } |
5164
|
3339 |
|
3340 triangular_error: |
|
3341 if (err != 0) |
|
3342 { |
|
3343 if (sing_handler) |
5681
|
3344 { |
|
3345 sing_handler (rcond); |
|
3346 mattype.mark_as_rectangular (); |
|
3347 } |
5164
|
3348 else |
|
3349 (*current_liboctave_error_handler) |
|
3350 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
3351 rcond); |
|
3352 } |
|
3353 |
|
3354 volatile double rcond_plus_one = rcond + 1.0; |
|
3355 |
|
3356 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
3357 { |
|
3358 err = -2; |
|
3359 |
|
3360 if (sing_handler) |
5681
|
3361 { |
|
3362 sing_handler (rcond); |
|
3363 mattype.mark_as_rectangular (); |
|
3364 } |
5164
|
3365 else |
|
3366 (*current_liboctave_error_handler) |
|
3367 ("matrix singular to machine precision, rcond = %g", |
|
3368 rcond); |
|
3369 } |
|
3370 } |
|
3371 else |
|
3372 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
3373 } |
|
3374 |
|
3375 return retval; |
|
3376 } |
|
3377 |
|
3378 SparseComplexMatrix |
5785
|
3379 SparseComplexMatrix::ltsolve (MatrixType &mattype, const SparseComplexMatrix& b, |
5630
|
3380 octave_idx_type& err, double& rcond, |
5681
|
3381 solve_singularity_handler sing_handler, |
|
3382 bool calc_cond) const |
5164
|
3383 { |
|
3384 SparseComplexMatrix retval; |
|
3385 |
5275
|
3386 octave_idx_type nr = rows (); |
|
3387 octave_idx_type nc = cols (); |
5630
|
3388 octave_idx_type nm = (nc > nr ? nc : nr); |
5164
|
3389 err = 0; |
|
3390 |
6924
|
3391 if (nr != b.rows ()) |
5164
|
3392 (*current_liboctave_error_handler) |
|
3393 ("matrix dimension mismatch solution of linear equations"); |
6924
|
3394 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
3395 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
3396 else |
|
3397 { |
|
3398 // Print spparms("spumoni") info if requested |
|
3399 int typ = mattype.type (); |
|
3400 mattype.info (); |
|
3401 |
5785
|
3402 if (typ == MatrixType::Permuted_Lower || |
|
3403 typ == MatrixType::Lower) |
5164
|
3404 { |
|
3405 double anorm = 0.; |
|
3406 double ainvnorm = 0.; |
5681
|
3407 rcond = 1.; |
|
3408 |
|
3409 if (calc_cond) |
|
3410 { |
|
3411 // Calculate the 1-norm of matrix for rcond calculation |
|
3412 for (octave_idx_type j = 0; j < nc; j++) |
|
3413 { |
|
3414 double atmp = 0.; |
|
3415 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
3416 atmp += std::abs(data(i)); |
|
3417 if (atmp > anorm) |
|
3418 anorm = atmp; |
|
3419 } |
5164
|
3420 } |
|
3421 |
5275
|
3422 octave_idx_type b_nc = b.cols (); |
5681
|
3423 octave_idx_type b_nz = b.nnz (); |
5630
|
3424 retval = SparseComplexMatrix (nc, b_nc, b_nz); |
5164
|
3425 retval.xcidx(0) = 0; |
5275
|
3426 octave_idx_type ii = 0; |
|
3427 octave_idx_type x_nz = b_nz; |
5164
|
3428 |
5785
|
3429 if (typ == MatrixType::Permuted_Lower) |
5164
|
3430 { |
5630
|
3431 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5322
|
3432 octave_idx_type *perm = mattype.triangular_perm (); |
5164
|
3433 |
5275
|
3434 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
3435 { |
5630
|
3436 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
3437 work[i] = 0.; |
5275
|
3438 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5322
|
3439 work[perm[b.ridx(i)]] = b.data(i); |
5164
|
3440 |
5630
|
3441 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3442 { |
5322
|
3443 if (work[k] != 0.) |
5164
|
3444 { |
5322
|
3445 octave_idx_type minr = nr; |
|
3446 octave_idx_type mini = 0; |
|
3447 |
|
3448 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
|
3449 if (perm[ridx(i)] < minr) |
|
3450 { |
|
3451 minr = perm[ridx(i)]; |
|
3452 mini = i; |
|
3453 } |
|
3454 |
5681
|
3455 if (minr != k || data (mini) == 0.) |
5164
|
3456 { |
|
3457 err = -2; |
|
3458 goto triangular_error; |
|
3459 } |
|
3460 |
5322
|
3461 Complex tmp = work[k] / data(mini); |
|
3462 work[k] = tmp; |
|
3463 for (octave_idx_type i = cidx(k); i < cidx(k+1); i++) |
5164
|
3464 { |
5322
|
3465 if (i == mini) |
|
3466 continue; |
|
3467 |
|
3468 octave_idx_type iidx = perm[ridx(i)]; |
|
3469 work[iidx] = work[iidx] - tmp * data(i); |
5164
|
3470 } |
|
3471 } |
|
3472 } |
|
3473 |
|
3474 // Count non-zeros in work vector and adjust space in |
|
3475 // retval if needed |
5275
|
3476 octave_idx_type new_nnz = 0; |
5630
|
3477 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
3478 if (work[i] != 0.) |
|
3479 new_nnz++; |
|
3480 |
|
3481 if (ii + new_nnz > x_nz) |
|
3482 { |
|
3483 // Resize the sparse matrix |
5275
|
3484 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
3485 retval.change_capacity (sz); |
|
3486 x_nz = sz; |
|
3487 } |
|
3488 |
5630
|
3489 for (octave_idx_type i = 0; i < nc; i++) |
5322
|
3490 if (work[i] != 0.) |
5164
|
3491 { |
|
3492 retval.xridx(ii) = i; |
5322
|
3493 retval.xdata(ii++) = work[i]; |
5164
|
3494 } |
|
3495 retval.xcidx(j+1) = ii; |
|
3496 } |
|
3497 |
|
3498 retval.maybe_compress (); |
|
3499 |
5681
|
3500 if (calc_cond) |
|
3501 { |
|
3502 // Calculation of 1-norm of inv(*this) |
|
3503 for (octave_idx_type i = 0; i < nm; i++) |
|
3504 work[i] = 0.; |
|
3505 |
|
3506 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
3507 { |
5681
|
3508 work[j] = 1.; |
|
3509 |
|
3510 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3511 { |
5681
|
3512 if (work[k] != 0.) |
5164
|
3513 { |
5681
|
3514 octave_idx_type minr = nr; |
|
3515 octave_idx_type mini = 0; |
|
3516 |
|
3517 for (octave_idx_type i = cidx(k); |
|
3518 i < cidx(k+1); i++) |
|
3519 if (perm[ridx(i)] < minr) |
|
3520 { |
|
3521 minr = perm[ridx(i)]; |
|
3522 mini = i; |
|
3523 } |
|
3524 |
|
3525 Complex tmp = work[k] / data(mini); |
|
3526 work[k] = tmp; |
|
3527 for (octave_idx_type i = cidx(k); |
|
3528 i < cidx(k+1); i++) |
|
3529 { |
|
3530 if (i == mini) |
|
3531 continue; |
|
3532 |
|
3533 octave_idx_type iidx = perm[ridx(i)]; |
|
3534 work[iidx] = work[iidx] - tmp * data(i); |
|
3535 } |
5164
|
3536 } |
|
3537 } |
5681
|
3538 |
|
3539 double atmp = 0; |
|
3540 for (octave_idx_type i = j; i < nc; i++) |
|
3541 { |
|
3542 atmp += std::abs(work[i]); |
|
3543 work[i] = 0.; |
|
3544 } |
|
3545 if (atmp > ainvnorm) |
|
3546 ainvnorm = atmp; |
5164
|
3547 } |
5681
|
3548 rcond = 1. / ainvnorm / anorm; |
5164
|
3549 } |
|
3550 } |
|
3551 else |
|
3552 { |
5630
|
3553 OCTAVE_LOCAL_BUFFER (Complex, work, nm); |
5164
|
3554 |
5275
|
3555 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
3556 { |
5630
|
3557 for (octave_idx_type i = 0; i < nm; i++) |
5164
|
3558 work[i] = 0.; |
5275
|
3559 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
3560 work[b.ridx(i)] = b.data(i); |
|
3561 |
5630
|
3562 for (octave_idx_type k = 0; k < nc; k++) |
5164
|
3563 { |
|
3564 if (work[k] != 0.) |
|
3565 { |
5681
|
3566 if (ridx(cidx(k)) != k || |
|
3567 data(cidx(k)) == 0.) |
5164
|
3568 { |
|
3569 err = -2; |
|
3570 goto triangular_error; |
|
3571 } |
|
3572 |
|
3573 Complex tmp = work[k] / data(cidx(k)); |
|
3574 work[k] = tmp; |
5275
|
3575 for (octave_idx_type i = cidx(k)+1; i < cidx(k+1); i++) |
5164
|
3576 { |
5275
|
3577 octave_idx_type iidx = ridx(i); |
5164
|
3578 work[iidx] = work[iidx] - tmp * data(i); |
|
3579 } |
|
3580 } |
|
3581 } |
|
3582 |
|
3583 // Count non-zeros in work vector and adjust space in |
|
3584 // retval if needed |
5275
|
3585 octave_idx_type new_nnz = 0; |
5630
|
3586 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
3587 if (work[i] != 0.) |
|
3588 new_nnz++; |
|
3589 |
|
3590 if (ii + new_nnz > x_nz) |
|
3591 { |
|
3592 // Resize the sparse matrix |
5275
|
3593 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
3594 retval.change_capacity (sz); |
|
3595 x_nz = sz; |
|
3596 } |
|
3597 |
5630
|
3598 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
3599 if (work[i] != 0.) |
|
3600 { |
|
3601 retval.xridx(ii) = i; |
|
3602 retval.xdata(ii++) = work[i]; |
|
3603 } |
|
3604 retval.xcidx(j+1) = ii; |
|
3605 } |
|
3606 |
|
3607 retval.maybe_compress (); |
|
3608 |
5681
|
3609 if (calc_cond) |
|
3610 { |
|
3611 // Calculation of 1-norm of inv(*this) |
|
3612 for (octave_idx_type i = 0; i < nm; i++) |
|
3613 work[i] = 0.; |
|
3614 |
|
3615 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
3616 { |
5681
|
3617 work[j] = 1.; |
|
3618 |
|
3619 for (octave_idx_type k = j; k < nc; k++) |
5164
|
3620 { |
5681
|
3621 |
|
3622 if (work[k] != 0.) |
5164
|
3623 { |
5681
|
3624 Complex tmp = work[k] / data(cidx(k)); |
|
3625 work[k] = tmp; |
|
3626 for (octave_idx_type i = cidx(k)+1; |
|
3627 i < cidx(k+1); i++) |
|
3628 { |
|
3629 octave_idx_type iidx = ridx(i); |
|
3630 work[iidx] = work[iidx] - tmp * data(i); |
|
3631 } |
5164
|
3632 } |
|
3633 } |
5681
|
3634 double atmp = 0; |
|
3635 for (octave_idx_type i = j; i < nc; i++) |
|
3636 { |
|
3637 atmp += std::abs(work[i]); |
|
3638 work[i] = 0.; |
|
3639 } |
|
3640 if (atmp > ainvnorm) |
|
3641 ainvnorm = atmp; |
5164
|
3642 } |
5681
|
3643 rcond = 1. / ainvnorm / anorm; |
|
3644 } |
|
3645 } |
5164
|
3646 |
|
3647 triangular_error: |
|
3648 if (err != 0) |
|
3649 { |
|
3650 if (sing_handler) |
5681
|
3651 { |
|
3652 sing_handler (rcond); |
|
3653 mattype.mark_as_rectangular (); |
|
3654 } |
5164
|
3655 else |
|
3656 (*current_liboctave_error_handler) |
|
3657 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
3658 rcond); |
|
3659 } |
|
3660 |
|
3661 volatile double rcond_plus_one = rcond + 1.0; |
|
3662 |
|
3663 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
3664 { |
|
3665 err = -2; |
|
3666 |
|
3667 if (sing_handler) |
5681
|
3668 { |
|
3669 sing_handler (rcond); |
|
3670 mattype.mark_as_rectangular (); |
|
3671 } |
5164
|
3672 else |
|
3673 (*current_liboctave_error_handler) |
|
3674 ("matrix singular to machine precision, rcond = %g", |
|
3675 rcond); |
|
3676 } |
|
3677 } |
|
3678 else |
|
3679 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
3680 } |
|
3681 |
|
3682 return retval; |
|
3683 } |
|
3684 |
|
3685 ComplexMatrix |
5785
|
3686 SparseComplexMatrix::trisolve (MatrixType &mattype, const Matrix& b, |
5681
|
3687 octave_idx_type& err, double& rcond, |
|
3688 solve_singularity_handler sing_handler, |
|
3689 bool calc_cond) const |
5164
|
3690 { |
|
3691 ComplexMatrix retval; |
|
3692 |
5275
|
3693 octave_idx_type nr = rows (); |
|
3694 octave_idx_type nc = cols (); |
5164
|
3695 err = 0; |
|
3696 |
6924
|
3697 if (nr != nc || nr != b.rows ()) |
5164
|
3698 (*current_liboctave_error_handler) |
|
3699 ("matrix dimension mismatch solution of linear equations"); |
6924
|
3700 else if (nr == 0 || b.cols () == 0) |
|
3701 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5681
|
3702 else if (calc_cond) |
|
3703 (*current_liboctave_error_handler) |
|
3704 ("calculation of condition number not implemented"); |
5164
|
3705 else |
|
3706 { |
|
3707 // Print spparms("spumoni") info if requested |
|
3708 volatile int typ = mattype.type (); |
|
3709 mattype.info (); |
|
3710 |
5785
|
3711 if (typ == MatrixType::Tridiagonal_Hermitian) |
5164
|
3712 { |
5322
|
3713 OCTAVE_LOCAL_BUFFER (double, D, nr); |
5164
|
3714 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
|
3715 |
|
3716 if (mattype.is_dense ()) |
|
3717 { |
5275
|
3718 octave_idx_type ii = 0; |
|
3719 |
|
3720 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
3721 { |
5322
|
3722 D[j] = std::real(data(ii++)); |
5164
|
3723 DL[j] = data(ii); |
|
3724 ii += 2; |
|
3725 } |
5322
|
3726 D[nc-1] = std::real(data(ii)); |
5164
|
3727 } |
|
3728 else |
|
3729 { |
|
3730 D[0] = 0.; |
5275
|
3731 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
3732 { |
|
3733 D[i+1] = 0.; |
|
3734 DL[i] = 0.; |
|
3735 } |
|
3736 |
5275
|
3737 for (octave_idx_type j = 0; j < nc; j++) |
|
3738 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
3739 { |
|
3740 if (ridx(i) == j) |
5322
|
3741 D[j] = std::real(data(i)); |
5164
|
3742 else if (ridx(i) == j + 1) |
|
3743 DL[j] = data(i); |
|
3744 } |
|
3745 } |
|
3746 |
5275
|
3747 octave_idx_type b_nc = b.cols(); |
5164
|
3748 retval = ComplexMatrix (b); |
|
3749 Complex *result = retval.fortran_vec (); |
|
3750 |
|
3751 F77_XFCN (zptsv, ZPTSV, (nr, b_nc, D, DL, result, |
|
3752 b.rows(), err)); |
|
3753 |
|
3754 if (f77_exception_encountered) |
|
3755 (*current_liboctave_error_handler) |
|
3756 ("unrecoverable error in zptsv"); |
|
3757 else if (err != 0) |
|
3758 { |
|
3759 err = 0; |
|
3760 mattype.mark_as_unsymmetric (); |
5785
|
3761 typ = MatrixType::Tridiagonal; |
5164
|
3762 } |
|
3763 else |
|
3764 rcond = 1.; |
|
3765 } |
|
3766 |
5785
|
3767 if (typ == MatrixType::Tridiagonal) |
5164
|
3768 { |
|
3769 OCTAVE_LOCAL_BUFFER (Complex, DU, nr - 1); |
|
3770 OCTAVE_LOCAL_BUFFER (Complex, D, nr); |
|
3771 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
|
3772 |
|
3773 if (mattype.is_dense ()) |
|
3774 { |
5275
|
3775 octave_idx_type ii = 0; |
|
3776 |
|
3777 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
3778 { |
|
3779 D[j] = data(ii++); |
|
3780 DL[j] = data(ii++); |
|
3781 DU[j] = data(ii++); |
|
3782 } |
|
3783 D[nc-1] = data(ii); |
|
3784 } |
|
3785 else |
|
3786 { |
|
3787 D[0] = 0.; |
5275
|
3788 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
3789 { |
|
3790 D[i+1] = 0.; |
|
3791 DL[i] = 0.; |
|
3792 DU[i] = 0.; |
|
3793 } |
|
3794 |
5275
|
3795 for (octave_idx_type j = 0; j < nc; j++) |
|
3796 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
3797 { |
|
3798 if (ridx(i) == j) |
|
3799 D[j] = data(i); |
|
3800 else if (ridx(i) == j + 1) |
|
3801 DL[j] = data(i); |
|
3802 else if (ridx(i) == j - 1) |
5322
|
3803 DU[j-1] = data(i); |
5164
|
3804 } |
|
3805 } |
|
3806 |
5275
|
3807 octave_idx_type b_nc = b.cols(); |
5164
|
3808 retval = ComplexMatrix (b); |
|
3809 Complex *result = retval.fortran_vec (); |
|
3810 |
|
3811 F77_XFCN (zgtsv, ZGTSV, (nr, b_nc, DL, D, DU, result, |
|
3812 b.rows(), err)); |
|
3813 |
|
3814 if (f77_exception_encountered) |
|
3815 (*current_liboctave_error_handler) |
|
3816 ("unrecoverable error in zgtsv"); |
|
3817 else if (err != 0) |
|
3818 { |
|
3819 rcond = 0.; |
|
3820 err = -2; |
|
3821 |
|
3822 if (sing_handler) |
5681
|
3823 { |
|
3824 sing_handler (rcond); |
|
3825 mattype.mark_as_rectangular (); |
|
3826 } |
5164
|
3827 else |
|
3828 (*current_liboctave_error_handler) |
|
3829 ("matrix singular to machine precision"); |
|
3830 |
|
3831 } |
|
3832 else |
|
3833 rcond = 1.; |
|
3834 } |
5785
|
3835 else if (typ != MatrixType::Tridiagonal_Hermitian) |
5164
|
3836 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
3837 } |
|
3838 |
|
3839 return retval; |
|
3840 } |
|
3841 |
|
3842 SparseComplexMatrix |
5785
|
3843 SparseComplexMatrix::trisolve (MatrixType &mattype, const SparseMatrix& b, |
5681
|
3844 octave_idx_type& err, double& rcond, |
|
3845 solve_singularity_handler sing_handler, |
|
3846 bool calc_cond) const |
5164
|
3847 { |
|
3848 SparseComplexMatrix retval; |
|
3849 |
5275
|
3850 octave_idx_type nr = rows (); |
|
3851 octave_idx_type nc = cols (); |
5164
|
3852 err = 0; |
|
3853 |
6924
|
3854 if (nr != nc || nr != b.rows ()) |
5164
|
3855 (*current_liboctave_error_handler) |
|
3856 ("matrix dimension mismatch solution of linear equations"); |
6924
|
3857 else if (nr == 0 || b.cols () == 0) |
|
3858 retval = SparseComplexMatrix (nc, b.cols ()); |
5681
|
3859 else if (calc_cond) |
|
3860 (*current_liboctave_error_handler) |
|
3861 ("calculation of condition number not implemented"); |
5164
|
3862 else |
|
3863 { |
|
3864 // Print spparms("spumoni") info if requested |
|
3865 int typ = mattype.type (); |
|
3866 mattype.info (); |
|
3867 |
|
3868 // Note can't treat symmetric case as there is no dpttrf function |
5785
|
3869 if (typ == MatrixType::Tridiagonal || |
|
3870 typ == MatrixType::Tridiagonal_Hermitian) |
5164
|
3871 { |
|
3872 OCTAVE_LOCAL_BUFFER (Complex, DU2, nr - 2); |
|
3873 OCTAVE_LOCAL_BUFFER (Complex, DU, nr - 1); |
|
3874 OCTAVE_LOCAL_BUFFER (Complex, D, nr); |
|
3875 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
5275
|
3876 Array<octave_idx_type> ipvt (nr); |
|
3877 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
3878 |
|
3879 if (mattype.is_dense ()) |
|
3880 { |
5275
|
3881 octave_idx_type ii = 0; |
|
3882 |
|
3883 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
3884 { |
|
3885 D[j] = data(ii++); |
|
3886 DL[j] = data(ii++); |
|
3887 DU[j] = data(ii++); |
|
3888 } |
|
3889 D[nc-1] = data(ii); |
|
3890 } |
|
3891 else |
|
3892 { |
|
3893 D[0] = 0.; |
5275
|
3894 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
3895 { |
|
3896 D[i+1] = 0.; |
|
3897 DL[i] = 0.; |
|
3898 DU[i] = 0.; |
|
3899 } |
|
3900 |
5275
|
3901 for (octave_idx_type j = 0; j < nc; j++) |
|
3902 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
3903 { |
|
3904 if (ridx(i) == j) |
|
3905 D[j] = data(i); |
|
3906 else if (ridx(i) == j + 1) |
|
3907 DL[j] = data(i); |
|
3908 else if (ridx(i) == j - 1) |
5322
|
3909 DU[j-1] = data(i); |
5164
|
3910 } |
|
3911 } |
|
3912 |
|
3913 F77_XFCN (zgttrf, ZGTTRF, (nr, DL, D, DU, DU2, pipvt, err)); |
|
3914 |
|
3915 if (f77_exception_encountered) |
|
3916 (*current_liboctave_error_handler) |
|
3917 ("unrecoverable error in zgttrf"); |
|
3918 else |
|
3919 { |
|
3920 if (err != 0) |
|
3921 { |
|
3922 err = -2; |
5681
|
3923 rcond = 0.0; |
5164
|
3924 |
|
3925 if (sing_handler) |
5681
|
3926 { |
|
3927 sing_handler (rcond); |
|
3928 mattype.mark_as_rectangular (); |
|
3929 } |
5164
|
3930 else |
|
3931 (*current_liboctave_error_handler) |
|
3932 ("matrix singular to machine precision"); |
|
3933 |
|
3934 } |
|
3935 else |
|
3936 { |
|
3937 char job = 'N'; |
5681
|
3938 volatile octave_idx_type x_nz = b.nnz (); |
5275
|
3939 octave_idx_type b_nc = b.cols (); |
5164
|
3940 retval = SparseComplexMatrix (nr, b_nc, x_nz); |
|
3941 retval.xcidx(0) = 0; |
5275
|
3942 volatile octave_idx_type ii = 0; |
5681
|
3943 rcond = 1.0; |
5164
|
3944 |
|
3945 OCTAVE_LOCAL_BUFFER (Complex, work, nr); |
|
3946 |
5275
|
3947 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
5164
|
3948 { |
5275
|
3949 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
3950 work[i] = 0.; |
5275
|
3951 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) |
5164
|
3952 work[b.ridx(i)] = b.data(i); |
|
3953 |
|
3954 F77_XFCN (zgttrs, ZGTTRS, |
|
3955 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
3956 nr, 1, DL, D, DU, DU2, pipvt, |
|
3957 work, b.rows (), err |
|
3958 F77_CHAR_ARG_LEN (1))); |
|
3959 |
|
3960 if (f77_exception_encountered) |
|
3961 { |
|
3962 (*current_liboctave_error_handler) |
|
3963 ("unrecoverable error in zgttrs"); |
|
3964 break; |
|
3965 } |
|
3966 |
|
3967 // Count non-zeros in work vector and adjust |
|
3968 // space in retval if needed |
5275
|
3969 octave_idx_type new_nnz = 0; |
|
3970 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
3971 if (work[i] != 0.) |
|
3972 new_nnz++; |
|
3973 |
|
3974 if (ii + new_nnz > x_nz) |
|
3975 { |
|
3976 // Resize the sparse matrix |
5275
|
3977 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
3978 retval.change_capacity (sz); |
|
3979 x_nz = sz; |
|
3980 } |
|
3981 |
5275
|
3982 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
3983 if (work[i] != 0.) |
|
3984 { |
|
3985 retval.xridx(ii) = i; |
|
3986 retval.xdata(ii++) = work[i]; |
|
3987 } |
|
3988 retval.xcidx(j+1) = ii; |
|
3989 } |
|
3990 |
|
3991 retval.maybe_compress (); |
|
3992 } |
|
3993 } |
|
3994 } |
5785
|
3995 else if (typ != MatrixType::Tridiagonal_Hermitian) |
5164
|
3996 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
3997 } |
|
3998 |
|
3999 return retval; |
|
4000 } |
|
4001 |
|
4002 ComplexMatrix |
5785
|
4003 SparseComplexMatrix::trisolve (MatrixType &mattype, const ComplexMatrix& b, |
5275
|
4004 octave_idx_type& err, double& rcond, |
5681
|
4005 solve_singularity_handler sing_handler, |
|
4006 bool calc_cond) const |
5164
|
4007 { |
|
4008 ComplexMatrix retval; |
|
4009 |
5275
|
4010 octave_idx_type nr = rows (); |
|
4011 octave_idx_type nc = cols (); |
5164
|
4012 err = 0; |
|
4013 |
6924
|
4014 if (nr != nc || nr != b.rows ()) |
5164
|
4015 (*current_liboctave_error_handler) |
|
4016 ("matrix dimension mismatch solution of linear equations"); |
6924
|
4017 else if (nr == 0 || b.cols () == 0) |
|
4018 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5681
|
4019 else if (calc_cond) |
|
4020 (*current_liboctave_error_handler) |
|
4021 ("calculation of condition number not implemented"); |
5164
|
4022 else |
|
4023 { |
|
4024 // Print spparms("spumoni") info if requested |
|
4025 volatile int typ = mattype.type (); |
|
4026 mattype.info (); |
|
4027 |
5785
|
4028 if (typ == MatrixType::Tridiagonal_Hermitian) |
5164
|
4029 { |
5322
|
4030 OCTAVE_LOCAL_BUFFER (double, D, nr); |
5164
|
4031 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
|
4032 |
|
4033 if (mattype.is_dense ()) |
|
4034 { |
5275
|
4035 octave_idx_type ii = 0; |
|
4036 |
|
4037 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
4038 { |
5322
|
4039 D[j] = std::real(data(ii++)); |
5164
|
4040 DL[j] = data(ii); |
|
4041 ii += 2; |
|
4042 } |
5322
|
4043 D[nc-1] = std::real(data(ii)); |
5164
|
4044 } |
|
4045 else |
|
4046 { |
|
4047 D[0] = 0.; |
5275
|
4048 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
4049 { |
|
4050 D[i+1] = 0.; |
|
4051 DL[i] = 0.; |
|
4052 } |
|
4053 |
5275
|
4054 for (octave_idx_type j = 0; j < nc; j++) |
|
4055 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4056 { |
|
4057 if (ridx(i) == j) |
5322
|
4058 D[j] = std::real (data(i)); |
5164
|
4059 else if (ridx(i) == j + 1) |
|
4060 DL[j] = data(i); |
|
4061 } |
|
4062 } |
|
4063 |
5275
|
4064 octave_idx_type b_nr = b.rows (); |
|
4065 octave_idx_type b_nc = b.cols(); |
5164
|
4066 rcond = 1.; |
|
4067 |
|
4068 retval = ComplexMatrix (b); |
|
4069 Complex *result = retval.fortran_vec (); |
|
4070 |
|
4071 F77_XFCN (zptsv, ZPTSV, (nr, b_nc, D, DL, result, |
|
4072 b_nr, err)); |
|
4073 |
|
4074 if (f77_exception_encountered) |
|
4075 { |
|
4076 (*current_liboctave_error_handler) |
|
4077 ("unrecoverable error in zptsv"); |
|
4078 err = -1; |
|
4079 } |
|
4080 else if (err != 0) |
|
4081 { |
|
4082 err = 0; |
|
4083 mattype.mark_as_unsymmetric (); |
5785
|
4084 typ = MatrixType::Tridiagonal; |
5164
|
4085 } |
|
4086 } |
|
4087 |
5785
|
4088 if (typ == MatrixType::Tridiagonal) |
5164
|
4089 { |
|
4090 OCTAVE_LOCAL_BUFFER (Complex, DU, nr - 1); |
|
4091 OCTAVE_LOCAL_BUFFER (Complex, D, nr); |
|
4092 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
|
4093 |
|
4094 if (mattype.is_dense ()) |
|
4095 { |
5275
|
4096 octave_idx_type ii = 0; |
|
4097 |
|
4098 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
4099 { |
|
4100 D[j] = data(ii++); |
|
4101 DL[j] = data(ii++); |
|
4102 DU[j] = data(ii++); |
|
4103 } |
|
4104 D[nc-1] = data(ii); |
|
4105 } |
|
4106 else |
|
4107 { |
|
4108 D[0] = 0.; |
5275
|
4109 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
4110 { |
|
4111 D[i+1] = 0.; |
|
4112 DL[i] = 0.; |
|
4113 DU[i] = 0.; |
|
4114 } |
|
4115 |
5275
|
4116 for (octave_idx_type j = 0; j < nc; j++) |
|
4117 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4118 { |
|
4119 if (ridx(i) == j) |
|
4120 D[j] = data(i); |
|
4121 else if (ridx(i) == j + 1) |
|
4122 DL[j] = data(i); |
|
4123 else if (ridx(i) == j - 1) |
5322
|
4124 DU[j-1] = data(i); |
5164
|
4125 } |
|
4126 } |
|
4127 |
5275
|
4128 octave_idx_type b_nr = b.rows(); |
|
4129 octave_idx_type b_nc = b.cols(); |
5164
|
4130 rcond = 1.; |
|
4131 |
|
4132 retval = ComplexMatrix (b); |
|
4133 Complex *result = retval.fortran_vec (); |
|
4134 |
|
4135 F77_XFCN (zgtsv, ZGTSV, (nr, b_nc, DL, D, DU, result, |
|
4136 b_nr, err)); |
|
4137 |
|
4138 if (f77_exception_encountered) |
|
4139 { |
|
4140 (*current_liboctave_error_handler) |
|
4141 ("unrecoverable error in zgtsv"); |
|
4142 err = -1; |
|
4143 } |
|
4144 else if (err != 0) |
|
4145 { |
|
4146 rcond = 0.; |
|
4147 err = -2; |
|
4148 |
|
4149 if (sing_handler) |
5681
|
4150 { |
|
4151 sing_handler (rcond); |
|
4152 mattype.mark_as_rectangular (); |
|
4153 } |
5164
|
4154 else |
|
4155 (*current_liboctave_error_handler) |
|
4156 ("matrix singular to machine precision"); |
|
4157 } |
|
4158 } |
5785
|
4159 else if (typ != MatrixType::Tridiagonal_Hermitian) |
5164
|
4160 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
4161 } |
|
4162 |
|
4163 return retval; |
|
4164 } |
|
4165 |
|
4166 SparseComplexMatrix |
5785
|
4167 SparseComplexMatrix::trisolve (MatrixType &mattype, |
5681
|
4168 const SparseComplexMatrix& b, |
|
4169 octave_idx_type& err, double& rcond, |
|
4170 solve_singularity_handler sing_handler, |
|
4171 bool calc_cond) const |
5164
|
4172 { |
|
4173 SparseComplexMatrix retval; |
|
4174 |
5275
|
4175 octave_idx_type nr = rows (); |
|
4176 octave_idx_type nc = cols (); |
5164
|
4177 err = 0; |
|
4178 |
6924
|
4179 if (nr != nc || nr != b.rows ()) |
5164
|
4180 (*current_liboctave_error_handler) |
|
4181 ("matrix dimension mismatch solution of linear equations"); |
6924
|
4182 else if (nr == 0 || b.cols () == 0) |
|
4183 retval = SparseComplexMatrix (nc, b.cols ()); |
5681
|
4184 else if (calc_cond) |
|
4185 (*current_liboctave_error_handler) |
|
4186 ("calculation of condition number not implemented"); |
5164
|
4187 else |
|
4188 { |
|
4189 // Print spparms("spumoni") info if requested |
|
4190 int typ = mattype.type (); |
|
4191 mattype.info (); |
|
4192 |
|
4193 // Note can't treat symmetric case as there is no dpttrf function |
5785
|
4194 if (typ == MatrixType::Tridiagonal || |
|
4195 typ == MatrixType::Tridiagonal_Hermitian) |
5164
|
4196 { |
|
4197 OCTAVE_LOCAL_BUFFER (Complex, DU2, nr - 2); |
|
4198 OCTAVE_LOCAL_BUFFER (Complex, DU, nr - 1); |
|
4199 OCTAVE_LOCAL_BUFFER (Complex, D, nr); |
|
4200 OCTAVE_LOCAL_BUFFER (Complex, DL, nr - 1); |
5275
|
4201 Array<octave_idx_type> ipvt (nr); |
|
4202 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
4203 |
|
4204 if (mattype.is_dense ()) |
|
4205 { |
5275
|
4206 octave_idx_type ii = 0; |
|
4207 |
|
4208 for (octave_idx_type j = 0; j < nc-1; j++) |
5164
|
4209 { |
|
4210 D[j] = data(ii++); |
|
4211 DL[j] = data(ii++); |
|
4212 DU[j] = data(ii++); |
|
4213 } |
|
4214 D[nc-1] = data(ii); |
|
4215 } |
|
4216 else |
|
4217 { |
|
4218 D[0] = 0.; |
5275
|
4219 for (octave_idx_type i = 0; i < nr - 1; i++) |
5164
|
4220 { |
|
4221 D[i+1] = 0.; |
|
4222 DL[i] = 0.; |
|
4223 DU[i] = 0.; |
|
4224 } |
|
4225 |
5275
|
4226 for (octave_idx_type j = 0; j < nc; j++) |
|
4227 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4228 { |
|
4229 if (ridx(i) == j) |
|
4230 D[j] = data(i); |
|
4231 else if (ridx(i) == j + 1) |
|
4232 DL[j] = data(i); |
|
4233 else if (ridx(i) == j - 1) |
5322
|
4234 DU[j-1] = data(i); |
5164
|
4235 } |
|
4236 } |
|
4237 |
|
4238 F77_XFCN (zgttrf, ZGTTRF, (nr, DL, D, DU, DU2, pipvt, err)); |
|
4239 |
|
4240 if (f77_exception_encountered) |
|
4241 (*current_liboctave_error_handler) |
|
4242 ("unrecoverable error in zgttrf"); |
|
4243 else |
|
4244 { |
|
4245 if (err != 0) |
|
4246 { |
5681
|
4247 rcond = 0.0; |
5164
|
4248 err = -2; |
|
4249 |
|
4250 if (sing_handler) |
5681
|
4251 { |
|
4252 sing_handler (rcond); |
|
4253 mattype.mark_as_rectangular (); |
|
4254 } |
5164
|
4255 else |
|
4256 (*current_liboctave_error_handler) |
|
4257 ("matrix singular to machine precision"); |
|
4258 } |
|
4259 else |
|
4260 { |
|
4261 rcond = 1.; |
|
4262 char job = 'N'; |
5275
|
4263 octave_idx_type b_nr = b.rows (); |
|
4264 octave_idx_type b_nc = b.cols (); |
5164
|
4265 OCTAVE_LOCAL_BUFFER (Complex, Bx, b_nr); |
|
4266 |
|
4267 // Take a first guess that the number of non-zero terms |
|
4268 // will be as many as in b |
5681
|
4269 volatile octave_idx_type x_nz = b.nnz (); |
5275
|
4270 volatile octave_idx_type ii = 0; |
5164
|
4271 retval = SparseComplexMatrix (b_nr, b_nc, x_nz); |
|
4272 |
|
4273 retval.xcidx(0) = 0; |
5275
|
4274 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
5164
|
4275 { |
|
4276 |
5275
|
4277 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
4278 Bx[i] = b (i,j); |
|
4279 |
|
4280 F77_XFCN (zgttrs, ZGTTRS, |
|
4281 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4282 nr, 1, DL, D, DU, DU2, pipvt, |
|
4283 Bx, b_nr, err |
|
4284 F77_CHAR_ARG_LEN (1))); |
|
4285 |
|
4286 if (f77_exception_encountered) |
|
4287 { |
|
4288 (*current_liboctave_error_handler) |
|
4289 ("unrecoverable error in zgttrs"); |
|
4290 break; |
|
4291 } |
|
4292 |
|
4293 if (err != 0) |
|
4294 { |
|
4295 (*current_liboctave_error_handler) |
|
4296 ("SparseComplexMatrix::solve solve failed"); |
|
4297 |
|
4298 err = -1; |
|
4299 break; |
|
4300 } |
|
4301 |
|
4302 // Count non-zeros in work vector and adjust |
|
4303 // space in retval if needed |
5275
|
4304 octave_idx_type new_nnz = 0; |
|
4305 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
4306 if (Bx[i] != 0.) |
|
4307 new_nnz++; |
|
4308 |
|
4309 if (ii + new_nnz > x_nz) |
|
4310 { |
|
4311 // Resize the sparse matrix |
5275
|
4312 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
5164
|
4313 retval.change_capacity (sz); |
|
4314 x_nz = sz; |
|
4315 } |
|
4316 |
5275
|
4317 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
4318 if (Bx[i] != 0.) |
|
4319 { |
|
4320 retval.xridx(ii) = i; |
|
4321 retval.xdata(ii++) = Bx[i]; |
|
4322 } |
|
4323 |
|
4324 retval.xcidx(j+1) = ii; |
|
4325 } |
|
4326 |
|
4327 retval.maybe_compress (); |
|
4328 } |
|
4329 } |
|
4330 } |
5785
|
4331 else if (typ != MatrixType::Tridiagonal_Hermitian) |
5164
|
4332 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
4333 } |
|
4334 |
|
4335 return retval; |
|
4336 } |
|
4337 |
|
4338 ComplexMatrix |
5785
|
4339 SparseComplexMatrix::bsolve (MatrixType &mattype, const Matrix& b, |
5681
|
4340 octave_idx_type& err, double& rcond, |
|
4341 solve_singularity_handler sing_handler, |
|
4342 bool calc_cond) const |
5164
|
4343 { |
|
4344 ComplexMatrix retval; |
|
4345 |
5275
|
4346 octave_idx_type nr = rows (); |
|
4347 octave_idx_type nc = cols (); |
5164
|
4348 err = 0; |
|
4349 |
6924
|
4350 if (nr != nc || nr != b.rows ()) |
5164
|
4351 (*current_liboctave_error_handler) |
|
4352 ("matrix dimension mismatch solution of linear equations"); |
6924
|
4353 else if (nr == 0 || b.cols () == 0) |
|
4354 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
4355 else |
|
4356 { |
|
4357 // Print spparms("spumoni") info if requested |
|
4358 volatile int typ = mattype.type (); |
|
4359 mattype.info (); |
|
4360 |
5785
|
4361 if (typ == MatrixType::Banded_Hermitian) |
5164
|
4362 { |
5275
|
4363 octave_idx_type n_lower = mattype.nlower (); |
|
4364 octave_idx_type ldm = n_lower + 1; |
5164
|
4365 ComplexMatrix m_band (ldm, nc); |
|
4366 Complex *tmp_data = m_band.fortran_vec (); |
|
4367 |
|
4368 if (! mattype.is_dense ()) |
|
4369 { |
5275
|
4370 octave_idx_type ii = 0; |
|
4371 |
|
4372 for (octave_idx_type j = 0; j < ldm; j++) |
|
4373 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
4374 tmp_data[ii++] = 0.; |
|
4375 } |
|
4376 |
5275
|
4377 for (octave_idx_type j = 0; j < nc; j++) |
|
4378 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4379 { |
5275
|
4380 octave_idx_type ri = ridx (i); |
5164
|
4381 if (ri >= j) |
|
4382 m_band(ri - j, j) = data(i); |
|
4383 } |
|
4384 |
|
4385 // Calculate the norm of the matrix, for later use. |
5681
|
4386 double anorm; |
|
4387 if (calc_cond) |
|
4388 anorm = m_band.abs().sum().row(0).max(); |
5164
|
4389 |
|
4390 char job = 'L'; |
|
4391 F77_XFCN (zpbtrf, ZPBTRF, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4392 nr, n_lower, tmp_data, ldm, err |
|
4393 F77_CHAR_ARG_LEN (1))); |
|
4394 |
|
4395 if (f77_exception_encountered) |
|
4396 (*current_liboctave_error_handler) |
|
4397 ("unrecoverable error in zpbtrf"); |
|
4398 else |
|
4399 { |
|
4400 if (err != 0) |
|
4401 { |
5681
|
4402 rcond = 0.0; |
5164
|
4403 // Matrix is not positive definite!! Fall through to |
|
4404 // unsymmetric banded solver. |
|
4405 mattype.mark_as_unsymmetric (); |
5785
|
4406 typ = MatrixType::Banded; |
5164
|
4407 err = 0; |
|
4408 } |
|
4409 else |
|
4410 { |
5681
|
4411 if (calc_cond) |
|
4412 { |
|
4413 Array<Complex> z (2 * nr); |
|
4414 Complex *pz = z.fortran_vec (); |
|
4415 Array<double> iz (nr); |
|
4416 double *piz = iz.fortran_vec (); |
|
4417 |
|
4418 F77_XFCN (zpbcon, ZPBCON, |
|
4419 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4420 nr, n_lower, tmp_data, ldm, |
|
4421 anorm, rcond, pz, piz, err |
|
4422 F77_CHAR_ARG_LEN (1))); |
|
4423 |
|
4424 if (f77_exception_encountered) |
|
4425 (*current_liboctave_error_handler) |
|
4426 ("unrecoverable error in zpbcon"); |
|
4427 |
|
4428 if (err != 0) |
|
4429 err = -2; |
|
4430 |
|
4431 volatile double rcond_plus_one = rcond + 1.0; |
|
4432 |
|
4433 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
4434 { |
|
4435 err = -2; |
|
4436 |
|
4437 if (sing_handler) |
|
4438 { |
|
4439 sing_handler (rcond); |
|
4440 mattype.mark_as_rectangular (); |
|
4441 } |
|
4442 else |
|
4443 (*current_liboctave_error_handler) |
|
4444 ("matrix singular to machine precision, rcond = %g", |
|
4445 rcond); |
|
4446 } |
|
4447 } |
|
4448 else |
|
4449 rcond = 1.0; |
|
4450 |
|
4451 if (err == 0) |
|
4452 { |
|
4453 retval = ComplexMatrix (b); |
|
4454 Complex *result = retval.fortran_vec (); |
|
4455 |
|
4456 octave_idx_type b_nc = b.cols (); |
|
4457 |
|
4458 F77_XFCN (zpbtrs, ZPBTRS, |
|
4459 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4460 nr, n_lower, b_nc, tmp_data, |
|
4461 ldm, result, b.rows(), err |
|
4462 F77_CHAR_ARG_LEN (1))); |
5164
|
4463 |
5681
|
4464 if (f77_exception_encountered) |
|
4465 (*current_liboctave_error_handler) |
|
4466 ("unrecoverable error in zpbtrs"); |
|
4467 |
|
4468 if (err != 0) |
|
4469 { |
|
4470 (*current_liboctave_error_handler) |
|
4471 ("SparseMatrix::solve solve failed"); |
|
4472 err = -1; |
|
4473 } |
5164
|
4474 } |
|
4475 } |
|
4476 } |
|
4477 } |
|
4478 |
5785
|
4479 if (typ == MatrixType::Banded) |
5164
|
4480 { |
|
4481 // Create the storage for the banded form of the sparse matrix |
5275
|
4482 octave_idx_type n_upper = mattype.nupper (); |
|
4483 octave_idx_type n_lower = mattype.nlower (); |
|
4484 octave_idx_type ldm = n_upper + 2 * n_lower + 1; |
5164
|
4485 |
|
4486 ComplexMatrix m_band (ldm, nc); |
|
4487 Complex *tmp_data = m_band.fortran_vec (); |
|
4488 |
|
4489 if (! mattype.is_dense ()) |
|
4490 { |
5275
|
4491 octave_idx_type ii = 0; |
|
4492 |
|
4493 for (octave_idx_type j = 0; j < ldm; j++) |
|
4494 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
4495 tmp_data[ii++] = 0.; |
|
4496 } |
|
4497 |
5275
|
4498 for (octave_idx_type j = 0; j < nc; j++) |
|
4499 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4500 m_band(ridx(i) - j + n_lower + n_upper, j) = data(i); |
|
4501 |
5681
|
4502 // Calculate the norm of the matrix, for later use. |
|
4503 double anorm; |
|
4504 if (calc_cond) |
|
4505 { |
|
4506 for (octave_idx_type j = 0; j < nr; j++) |
|
4507 { |
|
4508 double atmp = 0.; |
|
4509 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
4510 atmp += std::abs(data(i)); |
|
4511 if (atmp > anorm) |
|
4512 anorm = atmp; |
|
4513 } |
|
4514 } |
|
4515 |
5275
|
4516 Array<octave_idx_type> ipvt (nr); |
|
4517 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
4518 |
5630
|
4519 F77_XFCN (zgbtrf, ZGBTRF, (nr, nc, n_lower, n_upper, tmp_data, |
5164
|
4520 ldm, pipvt, err)); |
|
4521 |
|
4522 if (f77_exception_encountered) |
|
4523 (*current_liboctave_error_handler) |
|
4524 ("unrecoverable error in zgbtrf"); |
|
4525 else |
|
4526 { |
|
4527 // Throw-away extra info LAPACK gives so as to not |
|
4528 // change output. |
|
4529 if (err != 0) |
|
4530 { |
5681
|
4531 rcond = 0.0; |
5164
|
4532 err = -2; |
|
4533 |
|
4534 if (sing_handler) |
5681
|
4535 { |
|
4536 sing_handler (rcond); |
|
4537 mattype.mark_as_rectangular (); |
|
4538 } |
5164
|
4539 else |
|
4540 (*current_liboctave_error_handler) |
|
4541 ("matrix singular to machine precision"); |
|
4542 } |
|
4543 else |
|
4544 { |
5681
|
4545 if (calc_cond) |
|
4546 { |
|
4547 char job = '1'; |
|
4548 Array<Complex> z (2 * nr); |
|
4549 Complex *pz = z.fortran_vec (); |
|
4550 Array<double> iz (nr); |
|
4551 double *piz = iz.fortran_vec (); |
|
4552 |
|
4553 F77_XFCN (zgbcon, ZGBCON, |
|
4554 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4555 nc, n_lower, n_upper, tmp_data, ldm, pipvt, |
|
4556 anorm, rcond, pz, piz, err |
|
4557 F77_CHAR_ARG_LEN (1))); |
|
4558 |
|
4559 if (f77_exception_encountered) |
|
4560 (*current_liboctave_error_handler) |
|
4561 ("unrecoverable error in zgbcon"); |
|
4562 |
|
4563 if (err != 0) |
|
4564 err = -2; |
|
4565 |
|
4566 volatile double rcond_plus_one = rcond + 1.0; |
|
4567 |
|
4568 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
4569 { |
|
4570 err = -2; |
|
4571 |
|
4572 if (sing_handler) |
|
4573 { |
|
4574 sing_handler (rcond); |
|
4575 mattype.mark_as_rectangular (); |
|
4576 } |
|
4577 else |
|
4578 (*current_liboctave_error_handler) |
|
4579 ("matrix singular to machine precision, rcond = %g", |
|
4580 rcond); |
|
4581 } |
|
4582 } |
|
4583 else |
|
4584 rcond = 1.; |
|
4585 |
|
4586 if (err == 0) |
|
4587 { |
|
4588 retval = ComplexMatrix (b); |
|
4589 Complex *result = retval.fortran_vec (); |
|
4590 |
|
4591 octave_idx_type b_nc = b.cols (); |
|
4592 |
|
4593 char job = 'N'; |
|
4594 F77_XFCN (zgbtrs, ZGBTRS, |
|
4595 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4596 nr, n_lower, n_upper, b_nc, tmp_data, |
|
4597 ldm, pipvt, result, b.rows(), err |
|
4598 F77_CHAR_ARG_LEN (1))); |
5164
|
4599 |
5681
|
4600 if (f77_exception_encountered) |
|
4601 (*current_liboctave_error_handler) |
|
4602 ("unrecoverable error in zgbtrs"); |
|
4603 } |
5164
|
4604 } |
|
4605 } |
|
4606 } |
5785
|
4607 else if (typ != MatrixType::Banded_Hermitian) |
5164
|
4608 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
4609 } |
|
4610 |
|
4611 return retval; |
|
4612 } |
|
4613 |
|
4614 SparseComplexMatrix |
5785
|
4615 SparseComplexMatrix::bsolve (MatrixType &mattype, const SparseMatrix& b, |
5275
|
4616 octave_idx_type& err, double& rcond, |
5681
|
4617 solve_singularity_handler sing_handler, |
|
4618 bool calc_cond) const |
5164
|
4619 { |
|
4620 SparseComplexMatrix retval; |
|
4621 |
5275
|
4622 octave_idx_type nr = rows (); |
|
4623 octave_idx_type nc = cols (); |
5164
|
4624 err = 0; |
|
4625 |
6924
|
4626 if (nr != nc || nr != b.rows ()) |
5164
|
4627 (*current_liboctave_error_handler) |
|
4628 ("matrix dimension mismatch solution of linear equations"); |
6924
|
4629 else if (nr == 0 || b.cols () == 0) |
|
4630 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
4631 else |
|
4632 { |
|
4633 // Print spparms("spumoni") info if requested |
|
4634 volatile int typ = mattype.type (); |
|
4635 mattype.info (); |
|
4636 |
5785
|
4637 if (typ == MatrixType::Banded_Hermitian) |
5164
|
4638 { |
5275
|
4639 octave_idx_type n_lower = mattype.nlower (); |
|
4640 octave_idx_type ldm = n_lower + 1; |
5164
|
4641 |
|
4642 ComplexMatrix m_band (ldm, nc); |
|
4643 Complex *tmp_data = m_band.fortran_vec (); |
|
4644 |
|
4645 if (! mattype.is_dense ()) |
|
4646 { |
5275
|
4647 octave_idx_type ii = 0; |
|
4648 |
|
4649 for (octave_idx_type j = 0; j < ldm; j++) |
|
4650 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
4651 tmp_data[ii++] = 0.; |
|
4652 } |
|
4653 |
5275
|
4654 for (octave_idx_type j = 0; j < nc; j++) |
|
4655 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4656 { |
5275
|
4657 octave_idx_type ri = ridx (i); |
5164
|
4658 if (ri >= j) |
|
4659 m_band(ri - j, j) = data(i); |
|
4660 } |
|
4661 |
5681
|
4662 // Calculate the norm of the matrix, for later use. |
|
4663 double anorm; |
|
4664 if (calc_cond) |
|
4665 anorm = m_band.abs().sum().row(0).max(); |
|
4666 |
5164
|
4667 char job = 'L'; |
|
4668 F77_XFCN (zpbtrf, ZPBTRF, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4669 nr, n_lower, tmp_data, ldm, err |
|
4670 F77_CHAR_ARG_LEN (1))); |
|
4671 |
|
4672 if (f77_exception_encountered) |
|
4673 (*current_liboctave_error_handler) |
|
4674 ("unrecoverable error in zpbtrf"); |
|
4675 else |
|
4676 { |
|
4677 if (err != 0) |
|
4678 { |
5681
|
4679 rcond = 0.0; |
5164
|
4680 mattype.mark_as_unsymmetric (); |
5785
|
4681 typ = MatrixType::Banded; |
5164
|
4682 err = 0; |
|
4683 } |
|
4684 else |
|
4685 { |
5681
|
4686 if (calc_cond) |
|
4687 { |
|
4688 Array<Complex> z (2 * nr); |
|
4689 Complex *pz = z.fortran_vec (); |
|
4690 Array<double> iz (nr); |
|
4691 double *piz = iz.fortran_vec (); |
|
4692 |
|
4693 F77_XFCN (zpbcon, ZPBCON, |
|
4694 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4695 nr, n_lower, tmp_data, ldm, |
|
4696 anorm, rcond, pz, piz, err |
|
4697 F77_CHAR_ARG_LEN (1))); |
|
4698 |
|
4699 if (f77_exception_encountered) |
|
4700 (*current_liboctave_error_handler) |
|
4701 ("unrecoverable error in zpbcon"); |
|
4702 |
|
4703 if (err != 0) |
|
4704 err = -2; |
|
4705 |
|
4706 volatile double rcond_plus_one = rcond + 1.0; |
|
4707 |
|
4708 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
4709 { |
|
4710 err = -2; |
|
4711 |
|
4712 if (sing_handler) |
|
4713 { |
|
4714 sing_handler (rcond); |
|
4715 mattype.mark_as_rectangular (); |
|
4716 } |
|
4717 else |
|
4718 (*current_liboctave_error_handler) |
|
4719 ("matrix singular to machine precision, rcond = %g", |
|
4720 rcond); |
|
4721 } |
|
4722 } |
|
4723 else |
|
4724 rcond = 1.0; |
|
4725 |
|
4726 if (err == 0) |
5164
|
4727 { |
5681
|
4728 octave_idx_type b_nr = b.rows (); |
|
4729 octave_idx_type b_nc = b.cols (); |
|
4730 OCTAVE_LOCAL_BUFFER (Complex, Bx, b_nr); |
|
4731 |
|
4732 // Take a first guess that the number of non-zero terms |
|
4733 // will be as many as in b |
|
4734 volatile octave_idx_type x_nz = b.nnz (); |
|
4735 volatile octave_idx_type ii = 0; |
|
4736 retval = SparseComplexMatrix (b_nr, b_nc, x_nz); |
|
4737 |
|
4738 retval.xcidx(0) = 0; |
|
4739 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
|
4740 { |
|
4741 for (octave_idx_type i = 0; i < b_nr; i++) |
|
4742 Bx[i] = b.elem (i, j); |
|
4743 |
|
4744 F77_XFCN (zpbtrs, ZPBTRS, |
|
4745 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4746 nr, n_lower, 1, tmp_data, |
|
4747 ldm, Bx, b_nr, err |
|
4748 F77_CHAR_ARG_LEN (1))); |
5164
|
4749 |
5681
|
4750 if (f77_exception_encountered) |
|
4751 { |
|
4752 (*current_liboctave_error_handler) |
|
4753 ("unrecoverable error in dpbtrs"); |
|
4754 err = -1; |
|
4755 break; |
|
4756 } |
|
4757 |
|
4758 if (err != 0) |
|
4759 { |
|
4760 (*current_liboctave_error_handler) |
|
4761 ("SparseComplexMatrix::solve solve failed"); |
|
4762 err = -1; |
|
4763 break; |
|
4764 } |
|
4765 |
|
4766 for (octave_idx_type i = 0; i < b_nr; i++) |
|
4767 { |
|
4768 Complex tmp = Bx[i]; |
|
4769 if (tmp != 0.0) |
|
4770 { |
|
4771 if (ii == x_nz) |
|
4772 { |
|
4773 // Resize the sparse matrix |
|
4774 octave_idx_type sz = x_nz * |
|
4775 (b_nc - j) / b_nc; |
|
4776 sz = (sz > 10 ? sz : 10) + x_nz; |
|
4777 retval.change_capacity (sz); |
|
4778 x_nz = sz; |
|
4779 } |
|
4780 retval.xdata(ii) = tmp; |
|
4781 retval.xridx(ii++) = i; |
|
4782 } |
|
4783 } |
|
4784 retval.xcidx(j+1) = ii; |
5164
|
4785 } |
|
4786 |
5681
|
4787 retval.maybe_compress (); |
5164
|
4788 } |
|
4789 } |
|
4790 } |
|
4791 } |
|
4792 |
5785
|
4793 if (typ == MatrixType::Banded) |
5164
|
4794 { |
|
4795 // Create the storage for the banded form of the sparse matrix |
5275
|
4796 octave_idx_type n_upper = mattype.nupper (); |
|
4797 octave_idx_type n_lower = mattype.nlower (); |
|
4798 octave_idx_type ldm = n_upper + 2 * n_lower + 1; |
5164
|
4799 |
|
4800 ComplexMatrix m_band (ldm, nc); |
|
4801 Complex *tmp_data = m_band.fortran_vec (); |
|
4802 |
|
4803 if (! mattype.is_dense ()) |
|
4804 { |
5275
|
4805 octave_idx_type ii = 0; |
|
4806 |
|
4807 for (octave_idx_type j = 0; j < ldm; j++) |
|
4808 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
4809 tmp_data[ii++] = 0.; |
|
4810 } |
|
4811 |
5275
|
4812 for (octave_idx_type j = 0; j < nc; j++) |
|
4813 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
4814 m_band(ridx(i) - j + n_lower + n_upper, j) = data(i); |
|
4815 |
5681
|
4816 // Calculate the norm of the matrix, for later use. |
|
4817 double anorm; |
|
4818 if (calc_cond) |
|
4819 { |
|
4820 for (octave_idx_type j = 0; j < nr; j++) |
|
4821 { |
|
4822 double atmp = 0.; |
|
4823 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
4824 atmp += std::abs(data(i)); |
|
4825 if (atmp > anorm) |
|
4826 anorm = atmp; |
|
4827 } |
|
4828 } |
|
4829 |
5275
|
4830 Array<octave_idx_type> ipvt (nr); |
|
4831 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
4832 |
|
4833 F77_XFCN (zgbtrf, ZGBTRF, (nr, nr, n_lower, n_upper, tmp_data, |
|
4834 ldm, pipvt, err)); |
|
4835 |
|
4836 if (f77_exception_encountered) |
|
4837 (*current_liboctave_error_handler) |
|
4838 ("unrecoverable error in zgbtrf"); |
|
4839 else |
|
4840 { |
|
4841 if (err != 0) |
|
4842 { |
5681
|
4843 rcond = 0.0; |
5164
|
4844 err = -2; |
|
4845 |
|
4846 if (sing_handler) |
5681
|
4847 { |
5164
|
4848 sing_handler (rcond); |
5681
|
4849 mattype.mark_as_rectangular (); |
|
4850 } |
5164
|
4851 else |
|
4852 (*current_liboctave_error_handler) |
|
4853 ("matrix singular to machine precision"); |
|
4854 |
|
4855 } |
|
4856 else |
|
4857 { |
5681
|
4858 if (calc_cond) |
5164
|
4859 { |
5681
|
4860 char job = '1'; |
|
4861 Array<Complex> z (2 * nr); |
|
4862 Complex *pz = z.fortran_vec (); |
|
4863 Array<double> iz (nr); |
|
4864 double *piz = iz.fortran_vec (); |
|
4865 |
|
4866 F77_XFCN (zgbcon, ZGBCON, |
|
4867 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4868 nc, n_lower, n_upper, tmp_data, ldm, pipvt, |
|
4869 anorm, rcond, pz, piz, err |
|
4870 F77_CHAR_ARG_LEN (1))); |
|
4871 |
5164
|
4872 if (f77_exception_encountered) |
5681
|
4873 (*current_liboctave_error_handler) |
|
4874 ("unrecoverable error in zgbcon"); |
|
4875 |
|
4876 if (err != 0) |
|
4877 err = -2; |
|
4878 |
|
4879 volatile double rcond_plus_one = rcond + 1.0; |
|
4880 |
|
4881 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
4882 { |
|
4883 err = -2; |
|
4884 |
|
4885 if (sing_handler) |
|
4886 { |
|
4887 sing_handler (rcond); |
|
4888 mattype.mark_as_rectangular (); |
|
4889 } |
|
4890 else |
|
4891 (*current_liboctave_error_handler) |
|
4892 ("matrix singular to machine precision, rcond = %g", |
|
4893 rcond); |
|
4894 } |
|
4895 } |
|
4896 else |
|
4897 rcond = 1.; |
|
4898 |
|
4899 if (err == 0) |
|
4900 { |
|
4901 char job = 'N'; |
|
4902 volatile octave_idx_type x_nz = b.nnz (); |
|
4903 octave_idx_type b_nc = b.cols (); |
|
4904 retval = SparseComplexMatrix (nr, b_nc, x_nz); |
|
4905 retval.xcidx(0) = 0; |
|
4906 volatile octave_idx_type ii = 0; |
|
4907 |
|
4908 OCTAVE_LOCAL_BUFFER (Complex, work, nr); |
|
4909 |
|
4910 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
5164
|
4911 { |
5681
|
4912 for (octave_idx_type i = 0; i < nr; i++) |
|
4913 work[i] = 0.; |
|
4914 for (octave_idx_type i = b.cidx(j); |
|
4915 i < b.cidx(j+1); i++) |
|
4916 work[b.ridx(i)] = b.data(i); |
|
4917 |
|
4918 F77_XFCN (zgbtrs, ZGBTRS, |
|
4919 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
4920 nr, n_lower, n_upper, 1, tmp_data, |
|
4921 ldm, pipvt, work, b.rows (), err |
|
4922 F77_CHAR_ARG_LEN (1))); |
|
4923 |
|
4924 if (f77_exception_encountered) |
|
4925 { |
|
4926 (*current_liboctave_error_handler) |
|
4927 ("unrecoverable error in zgbtrs"); |
|
4928 break; |
|
4929 } |
|
4930 |
|
4931 // Count non-zeros in work vector and adjust |
|
4932 // space in retval if needed |
|
4933 octave_idx_type new_nnz = 0; |
|
4934 for (octave_idx_type i = 0; i < nr; i++) |
|
4935 if (work[i] != 0.) |
|
4936 new_nnz++; |
|
4937 |
|
4938 if (ii + new_nnz > x_nz) |
|
4939 { |
|
4940 // Resize the sparse matrix |
|
4941 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
|
4942 retval.change_capacity (sz); |
|
4943 x_nz = sz; |
|
4944 } |
|
4945 |
|
4946 for (octave_idx_type i = 0; i < nr; i++) |
|
4947 if (work[i] != 0.) |
|
4948 { |
|
4949 retval.xridx(ii) = i; |
|
4950 retval.xdata(ii++) = work[i]; |
|
4951 } |
|
4952 retval.xcidx(j+1) = ii; |
5164
|
4953 } |
|
4954 |
5681
|
4955 retval.maybe_compress (); |
5164
|
4956 } |
|
4957 } |
|
4958 } |
|
4959 } |
5785
|
4960 else if (typ != MatrixType::Banded_Hermitian) |
5164
|
4961 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
4962 } |
|
4963 |
|
4964 return retval; |
|
4965 } |
|
4966 |
|
4967 ComplexMatrix |
5785
|
4968 SparseComplexMatrix::bsolve (MatrixType &mattype, const ComplexMatrix& b, |
5275
|
4969 octave_idx_type& err, double& rcond, |
5681
|
4970 solve_singularity_handler sing_handler, |
|
4971 bool calc_cond) const |
5164
|
4972 { |
|
4973 ComplexMatrix retval; |
|
4974 |
5275
|
4975 octave_idx_type nr = rows (); |
|
4976 octave_idx_type nc = cols (); |
5164
|
4977 err = 0; |
|
4978 |
6924
|
4979 if (nr != nc || nr != b.rows ()) |
5164
|
4980 (*current_liboctave_error_handler) |
|
4981 ("matrix dimension mismatch solution of linear equations"); |
6924
|
4982 else if (nr == 0 || b.cols () == 0) |
|
4983 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
4984 else |
|
4985 { |
|
4986 // Print spparms("spumoni") info if requested |
|
4987 volatile int typ = mattype.type (); |
|
4988 mattype.info (); |
|
4989 |
5785
|
4990 if (typ == MatrixType::Banded_Hermitian) |
5164
|
4991 { |
5275
|
4992 octave_idx_type n_lower = mattype.nlower (); |
|
4993 octave_idx_type ldm = n_lower + 1; |
5164
|
4994 |
|
4995 ComplexMatrix m_band (ldm, nc); |
|
4996 Complex *tmp_data = m_band.fortran_vec (); |
|
4997 |
|
4998 if (! mattype.is_dense ()) |
|
4999 { |
5275
|
5000 octave_idx_type ii = 0; |
|
5001 |
|
5002 for (octave_idx_type j = 0; j < ldm; j++) |
|
5003 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
5004 tmp_data[ii++] = 0.; |
|
5005 } |
|
5006 |
5275
|
5007 for (octave_idx_type j = 0; j < nc; j++) |
|
5008 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
5009 { |
5275
|
5010 octave_idx_type ri = ridx (i); |
5164
|
5011 if (ri >= j) |
|
5012 m_band(ri - j, j) = data(i); |
|
5013 } |
|
5014 |
5681
|
5015 // Calculate the norm of the matrix, for later use. |
|
5016 double anorm; |
|
5017 if (calc_cond) |
|
5018 anorm = m_band.abs().sum().row(0).max(); |
|
5019 |
5164
|
5020 char job = 'L'; |
|
5021 F77_XFCN (zpbtrf, ZPBTRF, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5022 nr, n_lower, tmp_data, ldm, err |
|
5023 F77_CHAR_ARG_LEN (1))); |
|
5024 |
|
5025 if (f77_exception_encountered) |
|
5026 (*current_liboctave_error_handler) |
|
5027 ("unrecoverable error in zpbtrf"); |
|
5028 else |
|
5029 { |
|
5030 if (err != 0) |
|
5031 { |
|
5032 // Matrix is not positive definite!! Fall through to |
|
5033 // unsymmetric banded solver. |
5681
|
5034 rcond = 0.0; |
5164
|
5035 mattype.mark_as_unsymmetric (); |
5785
|
5036 typ = MatrixType::Banded; |
5164
|
5037 err = 0; |
|
5038 } |
|
5039 else |
|
5040 { |
5681
|
5041 if (calc_cond) |
|
5042 { |
|
5043 Array<Complex> z (2 * nr); |
|
5044 Complex *pz = z.fortran_vec (); |
|
5045 Array<double> iz (nr); |
|
5046 double *piz = iz.fortran_vec (); |
|
5047 |
|
5048 F77_XFCN (zpbcon, ZPBCON, |
|
5049 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5050 nr, n_lower, tmp_data, ldm, |
|
5051 anorm, rcond, pz, piz, err |
|
5052 F77_CHAR_ARG_LEN (1))); |
|
5053 |
|
5054 if (f77_exception_encountered) |
|
5055 (*current_liboctave_error_handler) |
|
5056 ("unrecoverable error in zpbcon"); |
|
5057 |
|
5058 if (err != 0) |
|
5059 err = -2; |
|
5060 |
|
5061 volatile double rcond_plus_one = rcond + 1.0; |
|
5062 |
|
5063 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5064 { |
|
5065 err = -2; |
|
5066 |
|
5067 if (sing_handler) |
|
5068 { |
|
5069 sing_handler (rcond); |
|
5070 mattype.mark_as_rectangular (); |
|
5071 } |
|
5072 else |
|
5073 (*current_liboctave_error_handler) |
|
5074 ("matrix singular to machine precision, rcond = %g", |
|
5075 rcond); |
|
5076 } |
|
5077 } |
|
5078 else |
|
5079 rcond = 1.0; |
|
5080 |
|
5081 if (err == 0) |
|
5082 { |
|
5083 octave_idx_type b_nr = b.rows (); |
|
5084 octave_idx_type b_nc = b.cols (); |
|
5085 retval = ComplexMatrix (b); |
|
5086 Complex *result = retval.fortran_vec (); |
|
5087 |
|
5088 F77_XFCN (zpbtrs, ZPBTRS, |
|
5089 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5090 nr, n_lower, b_nc, tmp_data, |
|
5091 ldm, result, b_nr, err |
|
5092 F77_CHAR_ARG_LEN (1))); |
5164
|
5093 |
5681
|
5094 if (f77_exception_encountered) |
|
5095 { |
|
5096 (*current_liboctave_error_handler) |
|
5097 ("unrecoverable error in zpbtrs"); |
|
5098 err = -1; |
|
5099 } |
|
5100 |
|
5101 if (err != 0) |
|
5102 { |
|
5103 (*current_liboctave_error_handler) |
|
5104 ("SparseComplexMatrix::solve solve failed"); |
|
5105 err = -1; |
|
5106 } |
5164
|
5107 } |
|
5108 } |
|
5109 } |
|
5110 } |
|
5111 |
5785
|
5112 if (typ == MatrixType::Banded) |
5164
|
5113 { |
|
5114 // Create the storage for the banded form of the sparse matrix |
5275
|
5115 octave_idx_type n_upper = mattype.nupper (); |
|
5116 octave_idx_type n_lower = mattype.nlower (); |
|
5117 octave_idx_type ldm = n_upper + 2 * n_lower + 1; |
5164
|
5118 |
|
5119 ComplexMatrix m_band (ldm, nc); |
|
5120 Complex *tmp_data = m_band.fortran_vec (); |
|
5121 |
|
5122 if (! mattype.is_dense ()) |
|
5123 { |
5275
|
5124 octave_idx_type ii = 0; |
|
5125 |
|
5126 for (octave_idx_type j = 0; j < ldm; j++) |
|
5127 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
5128 tmp_data[ii++] = 0.; |
|
5129 } |
|
5130 |
5275
|
5131 for (octave_idx_type j = 0; j < nc; j++) |
|
5132 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
5133 m_band(ridx(i) - j + n_lower + n_upper, j) = data(i); |
|
5134 |
5681
|
5135 // Calculate the norm of the matrix, for later use. |
|
5136 double anorm; |
|
5137 if (calc_cond) |
|
5138 { |
|
5139 for (octave_idx_type j = 0; j < nr; j++) |
|
5140 { |
|
5141 double atmp = 0.; |
|
5142 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
5143 atmp += std::abs(data(i)); |
|
5144 if (atmp > anorm) |
|
5145 anorm = atmp; |
|
5146 } |
|
5147 } |
|
5148 |
5275
|
5149 Array<octave_idx_type> ipvt (nr); |
|
5150 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
5151 |
|
5152 F77_XFCN (zgbtrf, ZGBTRF, (nr, nr, n_lower, n_upper, tmp_data, |
|
5153 ldm, pipvt, err)); |
|
5154 |
|
5155 if (f77_exception_encountered) |
|
5156 (*current_liboctave_error_handler) |
|
5157 ("unrecoverable error in zgbtrf"); |
|
5158 else |
|
5159 { |
|
5160 if (err != 0) |
|
5161 { |
|
5162 err = -2; |
5681
|
5163 rcond = 0.0; |
5164
|
5164 |
|
5165 if (sing_handler) |
5681
|
5166 { |
|
5167 sing_handler (rcond); |
|
5168 mattype.mark_as_rectangular (); |
|
5169 } |
5164
|
5170 else |
|
5171 (*current_liboctave_error_handler) |
|
5172 ("matrix singular to machine precision"); |
|
5173 } |
|
5174 else |
|
5175 { |
5681
|
5176 if (calc_cond) |
|
5177 { |
|
5178 char job = '1'; |
|
5179 Array<Complex> z (2 * nr); |
|
5180 Complex *pz = z.fortran_vec (); |
|
5181 Array<double> iz (nr); |
|
5182 double *piz = iz.fortran_vec (); |
|
5183 |
|
5184 F77_XFCN (zgbcon, ZGBCON, |
|
5185 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5186 nc, n_lower, n_upper, tmp_data, ldm, pipvt, |
|
5187 anorm, rcond, pz, piz, err |
|
5188 F77_CHAR_ARG_LEN (1))); |
|
5189 |
|
5190 if (f77_exception_encountered) |
|
5191 (*current_liboctave_error_handler) |
|
5192 ("unrecoverable error in zgbcon"); |
|
5193 |
|
5194 if (err != 0) |
|
5195 err = -2; |
|
5196 |
|
5197 volatile double rcond_plus_one = rcond + 1.0; |
|
5198 |
|
5199 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5200 { |
|
5201 err = -2; |
|
5202 |
|
5203 if (sing_handler) |
|
5204 { |
|
5205 sing_handler (rcond); |
|
5206 mattype.mark_as_rectangular (); |
|
5207 } |
|
5208 else |
|
5209 (*current_liboctave_error_handler) |
|
5210 ("matrix singular to machine precision, rcond = %g", |
|
5211 rcond); |
|
5212 } |
|
5213 } |
|
5214 else |
|
5215 rcond = 1.; |
|
5216 |
|
5217 if (err == 0) |
|
5218 { |
|
5219 char job = 'N'; |
|
5220 octave_idx_type b_nc = b.cols (); |
|
5221 retval = ComplexMatrix (b); |
|
5222 Complex *result = retval.fortran_vec (); |
|
5223 |
|
5224 F77_XFCN (zgbtrs, ZGBTRS, |
|
5225 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5226 nr, n_lower, n_upper, b_nc, tmp_data, |
|
5227 ldm, pipvt, result, b.rows (), err |
|
5228 F77_CHAR_ARG_LEN (1))); |
5164
|
5229 |
5681
|
5230 if (f77_exception_encountered) |
|
5231 { |
|
5232 (*current_liboctave_error_handler) |
|
5233 ("unrecoverable error in dgbtrs"); |
|
5234 } |
5164
|
5235 } |
|
5236 } |
|
5237 } |
|
5238 } |
5785
|
5239 else if (typ != MatrixType::Banded_Hermitian) |
5164
|
5240 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
5241 } |
|
5242 |
|
5243 return retval; |
|
5244 } |
|
5245 |
|
5246 SparseComplexMatrix |
5785
|
5247 SparseComplexMatrix::bsolve (MatrixType &mattype, const SparseComplexMatrix& b, |
5681
|
5248 octave_idx_type& err, double& rcond, |
|
5249 solve_singularity_handler sing_handler, |
|
5250 bool calc_cond) const |
5164
|
5251 { |
|
5252 SparseComplexMatrix retval; |
|
5253 |
5275
|
5254 octave_idx_type nr = rows (); |
|
5255 octave_idx_type nc = cols (); |
5164
|
5256 err = 0; |
|
5257 |
6924
|
5258 if (nr != nc || nr != b.rows ()) |
5164
|
5259 (*current_liboctave_error_handler) |
|
5260 ("matrix dimension mismatch solution of linear equations"); |
6924
|
5261 else if (nr == 0 || b.cols () == 0) |
|
5262 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
5263 else |
|
5264 { |
|
5265 // Print spparms("spumoni") info if requested |
|
5266 volatile int typ = mattype.type (); |
|
5267 mattype.info (); |
|
5268 |
5785
|
5269 if (typ == MatrixType::Banded_Hermitian) |
5164
|
5270 { |
5275
|
5271 octave_idx_type n_lower = mattype.nlower (); |
|
5272 octave_idx_type ldm = n_lower + 1; |
5164
|
5273 |
|
5274 ComplexMatrix m_band (ldm, nc); |
|
5275 Complex *tmp_data = m_band.fortran_vec (); |
|
5276 |
|
5277 if (! mattype.is_dense ()) |
|
5278 { |
5275
|
5279 octave_idx_type ii = 0; |
|
5280 |
|
5281 for (octave_idx_type j = 0; j < ldm; j++) |
|
5282 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
5283 tmp_data[ii++] = 0.; |
|
5284 } |
|
5285 |
5275
|
5286 for (octave_idx_type j = 0; j < nc; j++) |
|
5287 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
5288 { |
5275
|
5289 octave_idx_type ri = ridx (i); |
5164
|
5290 if (ri >= j) |
|
5291 m_band(ri - j, j) = data(i); |
|
5292 } |
|
5293 |
5681
|
5294 // Calculate the norm of the matrix, for later use. |
|
5295 double anorm; |
|
5296 if (calc_cond) |
|
5297 anorm = m_band.abs().sum().row(0).max(); |
|
5298 |
5164
|
5299 char job = 'L'; |
|
5300 F77_XFCN (zpbtrf, ZPBTRF, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5301 nr, n_lower, tmp_data, ldm, err |
|
5302 F77_CHAR_ARG_LEN (1))); |
|
5303 |
|
5304 if (f77_exception_encountered) |
|
5305 (*current_liboctave_error_handler) |
|
5306 ("unrecoverable error in zpbtrf"); |
|
5307 else |
|
5308 { |
|
5309 if (err != 0) |
|
5310 { |
|
5311 // Matrix is not positive definite!! Fall through to |
|
5312 // unsymmetric banded solver. |
|
5313 mattype.mark_as_unsymmetric (); |
5785
|
5314 typ = MatrixType::Banded; |
5164
|
5315 |
5681
|
5316 rcond = 0.0; |
5164
|
5317 err = 0; |
|
5318 } |
|
5319 else |
|
5320 { |
5681
|
5321 if (calc_cond) |
5164
|
5322 { |
5681
|
5323 Array<Complex> z (2 * nr); |
|
5324 Complex *pz = z.fortran_vec (); |
|
5325 Array<double> iz (nr); |
|
5326 double *piz = iz.fortran_vec (); |
|
5327 |
|
5328 F77_XFCN (zpbcon, ZPBCON, |
|
5329 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5330 nr, n_lower, tmp_data, ldm, |
|
5331 anorm, rcond, pz, piz, err |
|
5332 F77_CHAR_ARG_LEN (1))); |
|
5333 |
5164
|
5334 if (f77_exception_encountered) |
5681
|
5335 (*current_liboctave_error_handler) |
|
5336 ("unrecoverable error in zpbcon"); |
|
5337 |
|
5338 if (err != 0) |
|
5339 err = -2; |
|
5340 |
|
5341 volatile double rcond_plus_one = rcond + 1.0; |
|
5342 |
|
5343 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5344 { |
|
5345 err = -2; |
|
5346 |
|
5347 if (sing_handler) |
|
5348 { |
|
5349 sing_handler (rcond); |
|
5350 mattype.mark_as_rectangular (); |
|
5351 } |
|
5352 else |
|
5353 (*current_liboctave_error_handler) |
|
5354 ("matrix singular to machine precision, rcond = %g", |
|
5355 rcond); |
|
5356 } |
|
5357 } |
|
5358 else |
|
5359 rcond = 1.0; |
|
5360 |
|
5361 if (err == 0) |
|
5362 { |
|
5363 octave_idx_type b_nr = b.rows (); |
|
5364 octave_idx_type b_nc = b.cols (); |
|
5365 OCTAVE_LOCAL_BUFFER (Complex, Bx, b_nr); |
|
5366 |
|
5367 // Take a first guess that the number of non-zero terms |
|
5368 // will be as many as in b |
|
5369 volatile octave_idx_type x_nz = b.nnz (); |
|
5370 volatile octave_idx_type ii = 0; |
|
5371 retval = SparseComplexMatrix (b_nr, b_nc, x_nz); |
|
5372 |
|
5373 retval.xcidx(0) = 0; |
|
5374 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
5164
|
5375 { |
5681
|
5376 |
|
5377 for (octave_idx_type i = 0; i < b_nr; i++) |
|
5378 Bx[i] = b (i,j); |
|
5379 |
|
5380 F77_XFCN (zpbtrs, ZPBTRS, |
|
5381 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5382 nr, n_lower, 1, tmp_data, |
|
5383 ldm, Bx, b_nr, err |
|
5384 F77_CHAR_ARG_LEN (1))); |
|
5385 |
|
5386 if (f77_exception_encountered) |
|
5387 { |
|
5388 (*current_liboctave_error_handler) |
|
5389 ("unrecoverable error in zpbtrs"); |
|
5390 err = -1; |
|
5391 break; |
|
5392 } |
|
5393 |
|
5394 if (err != 0) |
|
5395 { |
|
5396 (*current_liboctave_error_handler) |
|
5397 ("SparseMatrix::solve solve failed"); |
|
5398 err = -1; |
|
5399 break; |
|
5400 } |
|
5401 |
|
5402 // Count non-zeros in work vector and adjust |
|
5403 // space in retval if needed |
|
5404 octave_idx_type new_nnz = 0; |
|
5405 for (octave_idx_type i = 0; i < nr; i++) |
|
5406 if (Bx[i] != 0.) |
|
5407 new_nnz++; |
5164
|
5408 |
5681
|
5409 if (ii + new_nnz > x_nz) |
|
5410 { |
|
5411 // Resize the sparse matrix |
|
5412 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
|
5413 retval.change_capacity (sz); |
|
5414 x_nz = sz; |
|
5415 } |
5164
|
5416 |
5681
|
5417 for (octave_idx_type i = 0; i < nr; i++) |
|
5418 if (Bx[i] != 0.) |
|
5419 { |
|
5420 retval.xridx(ii) = i; |
|
5421 retval.xdata(ii++) = Bx[i]; |
|
5422 } |
|
5423 |
|
5424 retval.xcidx(j+1) = ii; |
|
5425 } |
|
5426 |
|
5427 retval.maybe_compress (); |
5164
|
5428 } |
|
5429 } |
|
5430 } |
|
5431 } |
|
5432 |
5785
|
5433 if (typ == MatrixType::Banded) |
5164
|
5434 { |
|
5435 // Create the storage for the banded form of the sparse matrix |
5275
|
5436 octave_idx_type n_upper = mattype.nupper (); |
|
5437 octave_idx_type n_lower = mattype.nlower (); |
|
5438 octave_idx_type ldm = n_upper + 2 * n_lower + 1; |
5164
|
5439 |
|
5440 ComplexMatrix m_band (ldm, nc); |
|
5441 Complex *tmp_data = m_band.fortran_vec (); |
|
5442 |
|
5443 if (! mattype.is_dense ()) |
|
5444 { |
5275
|
5445 octave_idx_type ii = 0; |
|
5446 |
|
5447 for (octave_idx_type j = 0; j < ldm; j++) |
|
5448 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
5449 tmp_data[ii++] = 0.; |
|
5450 } |
|
5451 |
5275
|
5452 for (octave_idx_type j = 0; j < nc; j++) |
|
5453 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
5454 m_band(ridx(i) - j + n_lower + n_upper, j) = data(i); |
|
5455 |
5681
|
5456 // Calculate the norm of the matrix, for later use. |
|
5457 double anorm; |
|
5458 if (calc_cond) |
|
5459 { |
|
5460 for (octave_idx_type j = 0; j < nr; j++) |
|
5461 { |
|
5462 double atmp = 0.; |
|
5463 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
|
5464 atmp += std::abs(data(i)); |
|
5465 if (atmp > anorm) |
|
5466 anorm = atmp; |
|
5467 } |
|
5468 } |
|
5469 |
5275
|
5470 Array<octave_idx_type> ipvt (nr); |
|
5471 octave_idx_type *pipvt = ipvt.fortran_vec (); |
5164
|
5472 |
|
5473 F77_XFCN (zgbtrf, ZGBTRF, (nr, nr, n_lower, n_upper, tmp_data, |
|
5474 ldm, pipvt, err)); |
|
5475 |
|
5476 if (f77_exception_encountered) |
|
5477 (*current_liboctave_error_handler) |
|
5478 ("unrecoverable error in xgbtrf"); |
|
5479 else |
|
5480 { |
|
5481 if (err != 0) |
|
5482 { |
|
5483 err = -2; |
5681
|
5484 rcond = 0.0; |
5164
|
5485 |
|
5486 if (sing_handler) |
5681
|
5487 { |
|
5488 sing_handler (rcond); |
|
5489 mattype.mark_as_rectangular (); |
|
5490 } |
5164
|
5491 else |
|
5492 (*current_liboctave_error_handler) |
|
5493 ("matrix singular to machine precision"); |
|
5494 |
|
5495 } |
|
5496 else |
|
5497 { |
5681
|
5498 if (calc_cond) |
5164
|
5499 { |
5681
|
5500 char job = '1'; |
|
5501 Array<Complex> z (2 * nr); |
|
5502 Complex *pz = z.fortran_vec (); |
|
5503 Array<double> iz (nr); |
|
5504 double *piz = iz.fortran_vec (); |
|
5505 |
|
5506 F77_XFCN (zgbcon, ZGBCON, |
|
5507 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5508 nc, n_lower, n_upper, tmp_data, ldm, pipvt, |
|
5509 anorm, rcond, pz, piz, err |
|
5510 F77_CHAR_ARG_LEN (1))); |
|
5511 |
5164
|
5512 if (f77_exception_encountered) |
5681
|
5513 (*current_liboctave_error_handler) |
|
5514 ("unrecoverable error in zgbcon"); |
|
5515 |
|
5516 if (err != 0) |
|
5517 err = -2; |
|
5518 |
|
5519 volatile double rcond_plus_one = rcond + 1.0; |
|
5520 |
|
5521 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5522 { |
|
5523 err = -2; |
|
5524 |
|
5525 if (sing_handler) |
|
5526 { |
|
5527 sing_handler (rcond); |
|
5528 mattype.mark_as_rectangular (); |
|
5529 } |
|
5530 else |
|
5531 (*current_liboctave_error_handler) |
|
5532 ("matrix singular to machine precision, rcond = %g", |
|
5533 rcond); |
|
5534 } |
|
5535 } |
|
5536 else |
|
5537 rcond = 1.; |
|
5538 |
|
5539 if (err == 0) |
|
5540 { |
|
5541 char job = 'N'; |
|
5542 volatile octave_idx_type x_nz = b.nnz (); |
|
5543 octave_idx_type b_nc = b.cols (); |
|
5544 retval = SparseComplexMatrix (nr, b_nc, x_nz); |
|
5545 retval.xcidx(0) = 0; |
|
5546 volatile octave_idx_type ii = 0; |
|
5547 |
|
5548 OCTAVE_LOCAL_BUFFER (Complex, Bx, nr); |
|
5549 |
|
5550 for (volatile octave_idx_type j = 0; j < b_nc; j++) |
5164
|
5551 { |
5681
|
5552 for (octave_idx_type i = 0; i < nr; i++) |
|
5553 Bx[i] = 0.; |
|
5554 |
|
5555 for (octave_idx_type i = b.cidx(j); |
|
5556 i < b.cidx(j+1); i++) |
|
5557 Bx[b.ridx(i)] = b.data(i); |
|
5558 |
|
5559 F77_XFCN (zgbtrs, ZGBTRS, |
|
5560 (F77_CONST_CHAR_ARG2 (&job, 1), |
|
5561 nr, n_lower, n_upper, 1, tmp_data, |
|
5562 ldm, pipvt, Bx, b.rows (), err |
|
5563 F77_CHAR_ARG_LEN (1))); |
|
5564 |
|
5565 if (f77_exception_encountered) |
|
5566 { |
|
5567 (*current_liboctave_error_handler) |
|
5568 ("unrecoverable error in dgbtrs"); |
|
5569 break; |
|
5570 } |
|
5571 |
|
5572 // Count non-zeros in work vector and adjust |
|
5573 // space in retval if needed |
|
5574 octave_idx_type new_nnz = 0; |
|
5575 for (octave_idx_type i = 0; i < nr; i++) |
|
5576 if (Bx[i] != 0.) |
|
5577 new_nnz++; |
|
5578 |
|
5579 if (ii + new_nnz > x_nz) |
|
5580 { |
|
5581 // Resize the sparse matrix |
|
5582 octave_idx_type sz = new_nnz * (b_nc - j) + x_nz; |
|
5583 retval.change_capacity (sz); |
|
5584 x_nz = sz; |
|
5585 } |
|
5586 |
|
5587 for (octave_idx_type i = 0; i < nr; i++) |
|
5588 if (Bx[i] != 0.) |
|
5589 { |
|
5590 retval.xridx(ii) = i; |
|
5591 retval.xdata(ii++) = Bx[i]; |
|
5592 } |
|
5593 retval.xcidx(j+1) = ii; |
5164
|
5594 } |
|
5595 |
5681
|
5596 retval.maybe_compress (); |
5164
|
5597 } |
|
5598 } |
|
5599 } |
|
5600 } |
5785
|
5601 else if (typ != MatrixType::Banded_Hermitian) |
5164
|
5602 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
5603 } |
|
5604 |
|
5605 return retval; |
|
5606 } |
|
5607 |
|
5608 void * |
5681
|
5609 SparseComplexMatrix::factorize (octave_idx_type& err, double &rcond, |
|
5610 Matrix &Control, Matrix &Info, |
|
5611 solve_singularity_handler sing_handler, |
|
5612 bool calc_cond) const |
5164
|
5613 { |
|
5614 // The return values |
5404
|
5615 void *Numeric = 0; |
5164
|
5616 err = 0; |
|
5617 |
5203
|
5618 #ifdef HAVE_UMFPACK |
5164
|
5619 // Setup the control parameters |
|
5620 Control = Matrix (UMFPACK_CONTROL, 1); |
|
5621 double *control = Control.fortran_vec (); |
5322
|
5622 UMFPACK_ZNAME (defaults) (control); |
5164
|
5623 |
5893
|
5624 double tmp = octave_sparse_params::get_key ("spumoni"); |
5164
|
5625 if (!xisnan (tmp)) |
|
5626 Control (UMFPACK_PRL) = tmp; |
5893
|
5627 tmp = octave_sparse_params::get_key ("piv_tol"); |
5164
|
5628 if (!xisnan (tmp)) |
|
5629 { |
|
5630 Control (UMFPACK_SYM_PIVOT_TOLERANCE) = tmp; |
|
5631 Control (UMFPACK_PIVOT_TOLERANCE) = tmp; |
|
5632 } |
|
5633 |
|
5634 // Set whether we are allowed to modify Q or not |
5893
|
5635 tmp = octave_sparse_params::get_key ("autoamd"); |
5164
|
5636 if (!xisnan (tmp)) |
|
5637 Control (UMFPACK_FIXQ) = tmp; |
|
5638 |
5322
|
5639 UMFPACK_ZNAME (report_control) (control); |
5164
|
5640 |
5275
|
5641 const octave_idx_type *Ap = cidx (); |
|
5642 const octave_idx_type *Ai = ridx (); |
5164
|
5643 const Complex *Ax = data (); |
5275
|
5644 octave_idx_type nr = rows (); |
|
5645 octave_idx_type nc = cols (); |
5164
|
5646 |
5322
|
5647 UMFPACK_ZNAME (report_matrix) (nr, nc, Ap, Ai, |
5760
|
5648 reinterpret_cast<const double *> (Ax), |
|
5649 NULL, 1, control); |
5164
|
5650 |
|
5651 void *Symbolic; |
|
5652 Info = Matrix (1, UMFPACK_INFO); |
|
5653 double *info = Info.fortran_vec (); |
5322
|
5654 int status = UMFPACK_ZNAME (qsymbolic) (nr, nc, Ap, Ai, |
5760
|
5655 reinterpret_cast<const double *> (Ax), |
5164
|
5656 NULL, NULL, &Symbolic, control, info); |
|
5657 |
|
5658 if (status < 0) |
|
5659 { |
|
5660 (*current_liboctave_error_handler) |
|
5661 ("SparseComplexMatrix::solve symbolic factorization failed"); |
|
5662 err = -1; |
|
5663 |
5322
|
5664 UMFPACK_ZNAME (report_status) (control, status); |
|
5665 UMFPACK_ZNAME (report_info) (control, info); |
|
5666 |
|
5667 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
5668 } |
|
5669 else |
|
5670 { |
5322
|
5671 UMFPACK_ZNAME (report_symbolic) (Symbolic, control); |
|
5672 |
|
5673 status = UMFPACK_ZNAME (numeric) (Ap, Ai, |
5760
|
5674 reinterpret_cast<const double *> (Ax), NULL, |
5164
|
5675 Symbolic, &Numeric, control, info) ; |
5322
|
5676 UMFPACK_ZNAME (free_symbolic) (&Symbolic) ; |
5164
|
5677 |
5681
|
5678 if (calc_cond) |
|
5679 rcond = Info (UMFPACK_RCOND); |
|
5680 else |
|
5681 rcond = 1.; |
5164
|
5682 volatile double rcond_plus_one = rcond + 1.0; |
|
5683 |
|
5684 if (status == UMFPACK_WARNING_singular_matrix || |
|
5685 rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5686 { |
5322
|
5687 UMFPACK_ZNAME (report_numeric) (Numeric, control); |
5164
|
5688 |
|
5689 err = -2; |
|
5690 |
|
5691 if (sing_handler) |
|
5692 sing_handler (rcond); |
|
5693 else |
|
5694 (*current_liboctave_error_handler) |
|
5695 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
5696 rcond); |
|
5697 |
|
5698 } |
5610
|
5699 else if (status < 0) |
5164
|
5700 { |
|
5701 (*current_liboctave_error_handler) |
|
5702 ("SparseComplexMatrix::solve numeric factorization failed"); |
|
5703 |
5322
|
5704 UMFPACK_ZNAME (report_status) (control, status); |
|
5705 UMFPACK_ZNAME (report_info) (control, info); |
5164
|
5706 |
|
5707 err = -1; |
|
5708 } |
|
5709 else |
|
5710 { |
5322
|
5711 UMFPACK_ZNAME (report_numeric) (Numeric, control); |
5164
|
5712 } |
|
5713 } |
|
5714 |
|
5715 if (err != 0) |
5322
|
5716 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5203
|
5717 #else |
|
5718 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
5719 #endif |
5164
|
5720 |
|
5721 return Numeric; |
|
5722 } |
|
5723 |
|
5724 ComplexMatrix |
5785
|
5725 SparseComplexMatrix::fsolve (MatrixType &mattype, const Matrix& b, |
5681
|
5726 octave_idx_type& err, double& rcond, |
|
5727 solve_singularity_handler sing_handler, |
|
5728 bool calc_cond) const |
5164
|
5729 { |
|
5730 ComplexMatrix retval; |
|
5731 |
5275
|
5732 octave_idx_type nr = rows (); |
|
5733 octave_idx_type nc = cols (); |
5164
|
5734 err = 0; |
|
5735 |
6924
|
5736 if (nr != nc || nr != b.rows ()) |
5164
|
5737 (*current_liboctave_error_handler) |
|
5738 ("matrix dimension mismatch solution of linear equations"); |
6924
|
5739 else if (nr == 0 || b.cols () == 0) |
|
5740 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
5741 else |
|
5742 { |
|
5743 // Print spparms("spumoni") info if requested |
|
5744 volatile int typ = mattype.type (); |
|
5745 mattype.info (); |
|
5746 |
5785
|
5747 if (typ == MatrixType::Hermitian) |
5164
|
5748 { |
5506
|
5749 #ifdef HAVE_CHOLMOD |
|
5750 cholmod_common Common; |
|
5751 cholmod_common *cm = &Common; |
|
5752 |
|
5753 // Setup initial parameters |
|
5754 CHOLMOD_NAME(start) (cm); |
5526
|
5755 cm->prefer_zomplex = false; |
5506
|
5756 |
5893
|
5757 double spu = octave_sparse_params::get_key ("spumoni"); |
5506
|
5758 if (spu == 0.) |
|
5759 { |
|
5760 cm->print = -1; |
|
5761 cm->print_function = NULL; |
|
5762 } |
|
5763 else |
|
5764 { |
5760
|
5765 cm->print = static_cast<int> (spu) + 2; |
5506
|
5766 cm->print_function =&SparseCholPrint; |
|
5767 } |
|
5768 |
|
5769 cm->error_handler = &SparseCholError; |
|
5770 cm->complex_divide = CHOLMOD_NAME(divcomplex); |
|
5771 cm->hypotenuse = CHOLMOD_NAME(hypot); |
|
5772 |
|
5773 #ifdef HAVE_METIS |
5710
|
5774 // METIS 4.0.1 uses malloc and free, and will terminate if |
|
5775 // it runs out of memory. Use CHOLMOD's memory guard for |
|
5776 // METIS, which allocates a huge block of memory (and then |
|
5777 // immediately frees it) before calling METIS |
5506
|
5778 cm->metis_memory = 2.0; |
|
5779 |
|
5780 #if defined(METIS_VERSION) |
|
5781 #if (METIS_VERSION >= METIS_VER(4,0,2)) |
5710
|
5782 // METIS 4.0.2 uses function pointers for malloc and free. |
5506
|
5783 METIS_malloc = cm->malloc_memory; |
|
5784 METIS_free = cm->free_memory; |
5710
|
5785 // Turn off METIS memory guard. |
5506
|
5786 cm->metis_memory = 0.0; |
|
5787 #endif |
|
5788 #endif |
|
5789 #endif |
5526
|
5790 cm->final_ll = true; |
5506
|
5791 |
|
5792 cholmod_sparse Astore; |
|
5793 cholmod_sparse *A = &Astore; |
|
5794 double dummy; |
|
5795 A->nrow = nr; |
|
5796 A->ncol = nc; |
|
5797 |
|
5798 A->p = cidx(); |
|
5799 A->i = ridx(); |
5604
|
5800 A->nzmax = nnz(); |
5526
|
5801 A->packed = true; |
|
5802 A->sorted = true; |
5506
|
5803 A->nz = NULL; |
|
5804 #ifdef IDX_TYPE_LONG |
|
5805 A->itype = CHOLMOD_LONG; |
|
5806 #else |
|
5807 A->itype = CHOLMOD_INT; |
|
5808 #endif |
|
5809 A->dtype = CHOLMOD_DOUBLE; |
|
5810 A->stype = 1; |
|
5811 A->xtype = CHOLMOD_COMPLEX; |
|
5812 |
|
5813 if (nr < 1) |
|
5814 A->x = &dummy; |
|
5815 else |
|
5816 A->x = data(); |
|
5817 |
|
5818 cholmod_dense Bstore; |
|
5819 cholmod_dense *B = &Bstore; |
|
5820 B->nrow = b.rows(); |
|
5821 B->ncol = b.cols(); |
|
5822 B->d = B->nrow; |
|
5823 B->nzmax = B->nrow * B->ncol; |
|
5824 B->dtype = CHOLMOD_DOUBLE; |
|
5825 B->xtype = CHOLMOD_REAL; |
|
5826 if (nc < 1 || b.cols() < 1) |
|
5827 B->x = &dummy; |
|
5828 else |
|
5829 // We won't alter it, honest :-) |
|
5830 B->x = const_cast<double *>(b.fortran_vec()); |
|
5831 |
|
5832 cholmod_factor *L; |
|
5833 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5834 L = CHOLMOD_NAME(analyze) (A, cm); |
|
5835 CHOLMOD_NAME(factorize) (A, L, cm); |
5681
|
5836 if (calc_cond) |
|
5837 rcond = CHOLMOD_NAME(rcond)(L, cm); |
|
5838 else |
|
5839 rcond = 1.; |
5506
|
5840 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5841 |
|
5842 if (rcond == 0.0) |
|
5843 { |
|
5844 // Either its indefinite or singular. Try UMFPACK |
|
5845 mattype.mark_as_unsymmetric (); |
5785
|
5846 typ = MatrixType::Full; |
5506
|
5847 } |
|
5848 else |
|
5849 { |
|
5850 volatile double rcond_plus_one = rcond + 1.0; |
|
5851 |
|
5852 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
5853 { |
|
5854 err = -2; |
|
5855 |
|
5856 if (sing_handler) |
5681
|
5857 { |
|
5858 sing_handler (rcond); |
|
5859 mattype.mark_as_rectangular (); |
|
5860 } |
5506
|
5861 else |
|
5862 (*current_liboctave_error_handler) |
|
5863 ("SparseMatrix::solve matrix singular to machine precision, rcond = %g", |
|
5864 rcond); |
|
5865 |
|
5866 return retval; |
|
5867 } |
|
5868 |
|
5869 cholmod_dense *X; |
|
5870 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5871 X = CHOLMOD_NAME(solve) (CHOLMOD_A, L, B, cm); |
|
5872 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5873 |
|
5874 retval.resize (b.rows (), b.cols()); |
|
5875 for (octave_idx_type j = 0; j < b.cols(); j++) |
|
5876 { |
|
5877 octave_idx_type jr = j * b.rows(); |
|
5878 for (octave_idx_type i = 0; i < b.rows(); i++) |
|
5879 retval.xelem(i,j) = static_cast<Complex *>(X->x)[jr + i]; |
|
5880 } |
|
5881 |
|
5882 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5883 CHOLMOD_NAME(free_dense) (&X, cm); |
|
5884 CHOLMOD_NAME(free_factor) (&L, cm); |
|
5885 CHOLMOD_NAME(finish) (cm); |
6482
|
5886 static char tmp[] = " "; |
|
5887 CHOLMOD_NAME(print_common) (tmp, cm); |
5506
|
5888 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
5889 } |
|
5890 #else |
5164
|
5891 (*current_liboctave_warning_handler) |
5506
|
5892 ("CHOLMOD not installed"); |
5164
|
5893 |
|
5894 mattype.mark_as_unsymmetric (); |
5785
|
5895 typ = MatrixType::Full; |
5506
|
5896 #endif |
5164
|
5897 } |
|
5898 |
5785
|
5899 if (typ == MatrixType::Full) |
5164
|
5900 { |
5203
|
5901 #ifdef HAVE_UMFPACK |
5164
|
5902 Matrix Control, Info; |
|
5903 void *Numeric = factorize (err, rcond, Control, Info, |
5681
|
5904 sing_handler, calc_cond); |
5164
|
5905 |
|
5906 if (err == 0) |
|
5907 { |
5275
|
5908 octave_idx_type b_nr = b.rows (); |
|
5909 octave_idx_type b_nc = b.cols (); |
5164
|
5910 int status = 0; |
|
5911 double *control = Control.fortran_vec (); |
|
5912 double *info = Info.fortran_vec (); |
5275
|
5913 const octave_idx_type *Ap = cidx (); |
|
5914 const octave_idx_type *Ai = ridx (); |
5164
|
5915 const Complex *Ax = data (); |
5203
|
5916 #ifdef UMFPACK_SEPARATE_SPLIT |
5164
|
5917 const double *Bx = b.fortran_vec (); |
|
5918 OCTAVE_LOCAL_BUFFER (double, Bz, b_nr); |
5275
|
5919 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
5920 Bz[i] = 0.; |
5203
|
5921 #else |
|
5922 OCTAVE_LOCAL_BUFFER (Complex, Bz, b_nr); |
|
5923 #endif |
5164
|
5924 retval.resize (b_nr, b_nc); |
|
5925 Complex *Xx = retval.fortran_vec (); |
|
5926 |
5275
|
5927 for (octave_idx_type j = 0, iidx = 0; j < b_nc; j++, iidx += b_nr) |
5164
|
5928 { |
5203
|
5929 #ifdef UMFPACK_SEPARATE_SPLIT |
5322
|
5930 status = UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, |
5760
|
5931 Ai, |
|
5932 reinterpret_cast<const double *> (Ax), |
5164
|
5933 NULL, |
5760
|
5934 reinterpret_cast<double *> (&Xx[iidx]), |
5164
|
5935 NULL, |
|
5936 &Bx[iidx], Bz, Numeric, |
|
5937 control, info); |
5203
|
5938 #else |
5275
|
5939 for (octave_idx_type i = 0; i < b_nr; i++) |
5203
|
5940 Bz[i] = b.elem (i, j); |
|
5941 |
5322
|
5942 status = UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, |
5760
|
5943 Ai, |
|
5944 reinterpret_cast<const double *> (Ax), |
5203
|
5945 NULL, |
5780
|
5946 reinterpret_cast<double *> (&Xx[iidx]), |
5203
|
5947 NULL, |
5760
|
5948 reinterpret_cast<const double *> (Bz), |
5203
|
5949 NULL, Numeric, |
|
5950 control, info); |
|
5951 #endif |
|
5952 |
5164
|
5953 if (status < 0) |
|
5954 { |
|
5955 (*current_liboctave_error_handler) |
|
5956 ("SparseComplexMatrix::solve solve failed"); |
|
5957 |
5322
|
5958 UMFPACK_ZNAME (report_status) (control, status); |
5164
|
5959 |
|
5960 err = -1; |
|
5961 |
|
5962 break; |
|
5963 } |
|
5964 } |
|
5965 |
5322
|
5966 UMFPACK_ZNAME (report_info) (control, info); |
|
5967 |
|
5968 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
5969 } |
5681
|
5970 else |
|
5971 mattype.mark_as_rectangular (); |
|
5972 |
5203
|
5973 #else |
|
5974 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
5975 #endif |
5164
|
5976 } |
5785
|
5977 else if (typ != MatrixType::Hermitian) |
5164
|
5978 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
5979 } |
|
5980 |
|
5981 return retval; |
|
5982 } |
|
5983 |
|
5984 SparseComplexMatrix |
5785
|
5985 SparseComplexMatrix::fsolve (MatrixType &mattype, const SparseMatrix& b, |
5275
|
5986 octave_idx_type& err, double& rcond, |
5681
|
5987 solve_singularity_handler sing_handler, |
|
5988 bool calc_cond) const |
5164
|
5989 { |
|
5990 SparseComplexMatrix retval; |
|
5991 |
5275
|
5992 octave_idx_type nr = rows (); |
|
5993 octave_idx_type nc = cols (); |
5164
|
5994 err = 0; |
|
5995 |
6924
|
5996 if (nr != nc || nr != b.rows ()) |
5164
|
5997 (*current_liboctave_error_handler) |
|
5998 ("matrix dimension mismatch solution of linear equations"); |
6924
|
5999 else if (nr == 0 || b.cols () == 0) |
|
6000 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
6001 else |
|
6002 { |
|
6003 // Print spparms("spumoni") info if requested |
5506
|
6004 volatile int typ = mattype.type (); |
5164
|
6005 mattype.info (); |
|
6006 |
5785
|
6007 if (typ == MatrixType::Hermitian) |
5164
|
6008 { |
5506
|
6009 #ifdef HAVE_CHOLMOD |
|
6010 cholmod_common Common; |
|
6011 cholmod_common *cm = &Common; |
|
6012 |
|
6013 // Setup initial parameters |
|
6014 CHOLMOD_NAME(start) (cm); |
5526
|
6015 cm->prefer_zomplex = false; |
5506
|
6016 |
5893
|
6017 double spu = octave_sparse_params::get_key ("spumoni"); |
5506
|
6018 if (spu == 0.) |
|
6019 { |
|
6020 cm->print = -1; |
|
6021 cm->print_function = NULL; |
|
6022 } |
|
6023 else |
|
6024 { |
5760
|
6025 cm->print = static_cast<int> (spu) + 2; |
5506
|
6026 cm->print_function =&SparseCholPrint; |
|
6027 } |
|
6028 |
|
6029 cm->error_handler = &SparseCholError; |
|
6030 cm->complex_divide = CHOLMOD_NAME(divcomplex); |
|
6031 cm->hypotenuse = CHOLMOD_NAME(hypot); |
|
6032 |
|
6033 #ifdef HAVE_METIS |
|
6034 // METIS 4.0.1 uses malloc and free, and will terminate MATLAB if |
|
6035 // it runs out of memory. Use CHOLMOD's memory guard for METIS, |
|
6036 // which mxMalloc's a huge block of memory (and then immediately |
|
6037 // mxFree's it) before calling METIS |
|
6038 cm->metis_memory = 2.0; |
|
6039 |
|
6040 #if defined(METIS_VERSION) |
|
6041 #if (METIS_VERSION >= METIS_VER(4,0,2)) |
|
6042 // METIS 4.0.2 uses function pointers for malloc and free |
|
6043 METIS_malloc = cm->malloc_memory; |
|
6044 METIS_free = cm->free_memory; |
|
6045 // Turn off METIS memory guard. It is not needed, because mxMalloc |
|
6046 // will safely terminate the mexFunction and free any workspace |
|
6047 // without killing all of octave. |
|
6048 cm->metis_memory = 0.0; |
|
6049 #endif |
|
6050 #endif |
|
6051 #endif |
|
6052 |
5526
|
6053 cm->final_ll = true; |
5506
|
6054 |
|
6055 cholmod_sparse Astore; |
|
6056 cholmod_sparse *A = &Astore; |
|
6057 double dummy; |
|
6058 A->nrow = nr; |
|
6059 A->ncol = nc; |
|
6060 |
|
6061 A->p = cidx(); |
|
6062 A->i = ridx(); |
5604
|
6063 A->nzmax = nnz(); |
5526
|
6064 A->packed = true; |
|
6065 A->sorted = true; |
5506
|
6066 A->nz = NULL; |
|
6067 #ifdef IDX_TYPE_LONG |
|
6068 A->itype = CHOLMOD_LONG; |
|
6069 #else |
|
6070 A->itype = CHOLMOD_INT; |
|
6071 #endif |
|
6072 A->dtype = CHOLMOD_DOUBLE; |
|
6073 A->stype = 1; |
|
6074 A->xtype = CHOLMOD_COMPLEX; |
|
6075 |
|
6076 if (nr < 1) |
|
6077 A->x = &dummy; |
|
6078 else |
|
6079 A->x = data(); |
|
6080 |
|
6081 cholmod_sparse Bstore; |
|
6082 cholmod_sparse *B = &Bstore; |
|
6083 B->nrow = b.rows(); |
|
6084 B->ncol = b.cols(); |
|
6085 B->p = b.cidx(); |
|
6086 B->i = b.ridx(); |
5604
|
6087 B->nzmax = b.nnz(); |
5526
|
6088 B->packed = true; |
|
6089 B->sorted = true; |
5506
|
6090 B->nz = NULL; |
|
6091 #ifdef IDX_TYPE_LONG |
|
6092 B->itype = CHOLMOD_LONG; |
|
6093 #else |
|
6094 B->itype = CHOLMOD_INT; |
|
6095 #endif |
|
6096 B->dtype = CHOLMOD_DOUBLE; |
|
6097 B->stype = 0; |
|
6098 B->xtype = CHOLMOD_REAL; |
|
6099 |
|
6100 if (b.rows() < 1 || b.cols() < 1) |
|
6101 B->x = &dummy; |
|
6102 else |
|
6103 B->x = b.data(); |
|
6104 |
|
6105 cholmod_factor *L; |
|
6106 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6107 L = CHOLMOD_NAME(analyze) (A, cm); |
|
6108 CHOLMOD_NAME(factorize) (A, L, cm); |
5681
|
6109 if (calc_cond) |
|
6110 rcond = CHOLMOD_NAME(rcond)(L, cm); |
|
6111 else |
|
6112 rcond = 1.; |
5506
|
6113 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6114 |
|
6115 if (rcond == 0.0) |
|
6116 { |
|
6117 // Either its indefinite or singular. Try UMFPACK |
|
6118 mattype.mark_as_unsymmetric (); |
5785
|
6119 typ = MatrixType::Full; |
5506
|
6120 } |
|
6121 else |
|
6122 { |
|
6123 volatile double rcond_plus_one = rcond + 1.0; |
|
6124 |
|
6125 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
6126 { |
|
6127 err = -2; |
|
6128 |
|
6129 if (sing_handler) |
5681
|
6130 { |
|
6131 sing_handler (rcond); |
|
6132 mattype.mark_as_rectangular (); |
|
6133 } |
5506
|
6134 else |
|
6135 (*current_liboctave_error_handler) |
|
6136 ("SparseMatrix::solve matrix singular to machine precision, rcond = %g", |
|
6137 rcond); |
|
6138 |
|
6139 return retval; |
|
6140 } |
|
6141 |
|
6142 cholmod_sparse *X; |
|
6143 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6144 X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm); |
|
6145 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6146 |
|
6147 retval = SparseComplexMatrix |
|
6148 (static_cast<octave_idx_type>(X->nrow), |
|
6149 static_cast<octave_idx_type>(X->ncol), |
|
6150 static_cast<octave_idx_type>(X->nzmax)); |
|
6151 for (octave_idx_type j = 0; |
|
6152 j <= static_cast<octave_idx_type>(X->ncol); j++) |
|
6153 retval.xcidx(j) = static_cast<octave_idx_type *>(X->p)[j]; |
|
6154 for (octave_idx_type j = 0; |
|
6155 j < static_cast<octave_idx_type>(X->nzmax); j++) |
|
6156 { |
|
6157 retval.xridx(j) = static_cast<octave_idx_type *>(X->i)[j]; |
|
6158 retval.xdata(j) = static_cast<Complex *>(X->x)[j]; |
|
6159 } |
|
6160 |
|
6161 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6162 CHOLMOD_NAME(free_sparse) (&X, cm); |
|
6163 CHOLMOD_NAME(free_factor) (&L, cm); |
|
6164 CHOLMOD_NAME(finish) (cm); |
6482
|
6165 static char tmp[] = " "; |
|
6166 CHOLMOD_NAME(print_common) (tmp, cm); |
5506
|
6167 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6168 } |
|
6169 #else |
5164
|
6170 (*current_liboctave_warning_handler) |
5506
|
6171 ("CHOLMOD not installed"); |
5164
|
6172 |
|
6173 mattype.mark_as_unsymmetric (); |
5785
|
6174 typ = MatrixType::Full; |
5506
|
6175 #endif |
5164
|
6176 } |
|
6177 |
5785
|
6178 if (typ == MatrixType::Full) |
5164
|
6179 { |
5203
|
6180 #ifdef HAVE_UMFPACK |
5164
|
6181 Matrix Control, Info; |
5681
|
6182 void *Numeric = factorize (err, rcond, Control, Info, |
|
6183 sing_handler, calc_cond); |
5164
|
6184 |
|
6185 if (err == 0) |
|
6186 { |
5275
|
6187 octave_idx_type b_nr = b.rows (); |
|
6188 octave_idx_type b_nc = b.cols (); |
5164
|
6189 int status = 0; |
|
6190 double *control = Control.fortran_vec (); |
|
6191 double *info = Info.fortran_vec (); |
5275
|
6192 const octave_idx_type *Ap = cidx (); |
|
6193 const octave_idx_type *Ai = ridx (); |
5164
|
6194 const Complex *Ax = data (); |
|
6195 |
5203
|
6196 #ifdef UMFPACK_SEPARATE_SPLIT |
5164
|
6197 OCTAVE_LOCAL_BUFFER (double, Bx, b_nr); |
|
6198 OCTAVE_LOCAL_BUFFER (double, Bz, b_nr); |
5275
|
6199 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
6200 Bz[i] = 0.; |
5203
|
6201 #else |
|
6202 OCTAVE_LOCAL_BUFFER (Complex, Bz, b_nr); |
|
6203 #endif |
5164
|
6204 |
|
6205 // Take a first guess that the number of non-zero terms |
|
6206 // will be as many as in b |
5681
|
6207 octave_idx_type x_nz = b.nnz (); |
5275
|
6208 octave_idx_type ii = 0; |
5164
|
6209 retval = SparseComplexMatrix (b_nr, b_nc, x_nz); |
|
6210 |
|
6211 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
|
6212 |
|
6213 retval.xcidx(0) = 0; |
5275
|
6214 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
6215 { |
|
6216 |
5203
|
6217 #ifdef UMFPACK_SEPARATE_SPLIT |
5275
|
6218 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
6219 Bx[i] = b.elem (i, j); |
|
6220 |
5322
|
6221 status = UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, |
5760
|
6222 Ai, |
|
6223 reinterpret_cast<const double *> (Ax), |
5164
|
6224 NULL, |
5760
|
6225 reinterpret_cast<double *> (Xx), |
|
6226 NULL, |
5164
|
6227 Bx, Bz, Numeric, control, |
|
6228 info); |
5203
|
6229 #else |
5275
|
6230 for (octave_idx_type i = 0; i < b_nr; i++) |
5203
|
6231 Bz[i] = b.elem (i, j); |
|
6232 |
5322
|
6233 status = UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, Ai, |
5760
|
6234 reinterpret_cast<const double *> (Ax), |
5203
|
6235 NULL, |
5760
|
6236 reinterpret_cast<double *> (Xx), |
|
6237 NULL, |
|
6238 reinterpret_cast<double *> (Bz), |
|
6239 NULL, |
5203
|
6240 Numeric, control, |
|
6241 info); |
|
6242 #endif |
5164
|
6243 if (status < 0) |
|
6244 { |
|
6245 (*current_liboctave_error_handler) |
|
6246 ("SparseComplexMatrix::solve solve failed"); |
|
6247 |
5322
|
6248 UMFPACK_ZNAME (report_status) (control, status); |
5164
|
6249 |
|
6250 err = -1; |
|
6251 |
|
6252 break; |
|
6253 } |
|
6254 |
5275
|
6255 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
6256 { |
|
6257 Complex tmp = Xx[i]; |
|
6258 if (tmp != 0.0) |
|
6259 { |
|
6260 if (ii == x_nz) |
|
6261 { |
|
6262 // Resize the sparse matrix |
5275
|
6263 octave_idx_type sz = x_nz * (b_nc - j) / b_nc; |
5164
|
6264 sz = (sz > 10 ? sz : 10) + x_nz; |
|
6265 retval.change_capacity (sz); |
|
6266 x_nz = sz; |
|
6267 } |
|
6268 retval.xdata(ii) = tmp; |
|
6269 retval.xridx(ii++) = i; |
|
6270 } |
|
6271 } |
|
6272 retval.xcidx(j+1) = ii; |
|
6273 } |
|
6274 |
|
6275 retval.maybe_compress (); |
|
6276 |
5322
|
6277 UMFPACK_ZNAME (report_info) (control, info); |
|
6278 |
|
6279 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
6280 } |
5681
|
6281 else |
|
6282 mattype.mark_as_rectangular (); |
|
6283 |
5203
|
6284 #else |
|
6285 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
6286 #endif |
5164
|
6287 } |
5785
|
6288 else if (typ != MatrixType::Hermitian) |
5164
|
6289 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
6290 } |
|
6291 |
|
6292 return retval; |
|
6293 } |
|
6294 |
|
6295 ComplexMatrix |
5785
|
6296 SparseComplexMatrix::fsolve (MatrixType &mattype, const ComplexMatrix& b, |
5275
|
6297 octave_idx_type& err, double& rcond, |
5681
|
6298 solve_singularity_handler sing_handler, |
|
6299 bool calc_cond) const |
5164
|
6300 { |
|
6301 ComplexMatrix retval; |
|
6302 |
5275
|
6303 octave_idx_type nr = rows (); |
|
6304 octave_idx_type nc = cols (); |
5164
|
6305 err = 0; |
|
6306 |
6924
|
6307 if (nr != nc || nr != b.rows ()) |
5164
|
6308 (*current_liboctave_error_handler) |
|
6309 ("matrix dimension mismatch solution of linear equations"); |
6924
|
6310 else if (nr == 0 || b.cols () == 0) |
|
6311 retval = ComplexMatrix (nc, b.cols (), Complex (0.0, 0.0)); |
5164
|
6312 else |
|
6313 { |
|
6314 // Print spparms("spumoni") info if requested |
5506
|
6315 volatile int typ = mattype.type (); |
5164
|
6316 mattype.info (); |
|
6317 |
5785
|
6318 if (typ == MatrixType::Hermitian) |
5164
|
6319 { |
5506
|
6320 #ifdef HAVE_CHOLMOD |
|
6321 cholmod_common Common; |
|
6322 cholmod_common *cm = &Common; |
|
6323 |
|
6324 // Setup initial parameters |
|
6325 CHOLMOD_NAME(start) (cm); |
5526
|
6326 cm->prefer_zomplex = false; |
5506
|
6327 |
5893
|
6328 double spu = octave_sparse_params::get_key ("spumoni"); |
5506
|
6329 if (spu == 0.) |
|
6330 { |
|
6331 cm->print = -1; |
|
6332 cm->print_function = NULL; |
|
6333 } |
|
6334 else |
|
6335 { |
5760
|
6336 cm->print = static_cast<int> (spu) + 2; |
5506
|
6337 cm->print_function =&SparseCholPrint; |
|
6338 } |
|
6339 |
|
6340 cm->error_handler = &SparseCholError; |
|
6341 cm->complex_divide = CHOLMOD_NAME(divcomplex); |
|
6342 cm->hypotenuse = CHOLMOD_NAME(hypot); |
|
6343 |
|
6344 #ifdef HAVE_METIS |
|
6345 // METIS 4.0.1 uses malloc and free, and will terminate MATLAB if |
|
6346 // it runs out of memory. Use CHOLMOD's memory guard for METIS, |
|
6347 // which mxMalloc's a huge block of memory (and then immediately |
|
6348 // mxFree's it) before calling METIS |
|
6349 cm->metis_memory = 2.0; |
|
6350 |
|
6351 #if defined(METIS_VERSION) |
|
6352 #if (METIS_VERSION >= METIS_VER(4,0,2)) |
|
6353 // METIS 4.0.2 uses function pointers for malloc and free |
|
6354 METIS_malloc = cm->malloc_memory; |
|
6355 METIS_free = cm->free_memory; |
|
6356 // Turn off METIS memory guard. It is not needed, because mxMalloc |
|
6357 // will safely terminate the mexFunction and free any workspace |
|
6358 // without killing all of octave. |
|
6359 cm->metis_memory = 0.0; |
|
6360 #endif |
|
6361 #endif |
|
6362 #endif |
|
6363 |
5526
|
6364 cm->final_ll = true; |
5506
|
6365 |
|
6366 cholmod_sparse Astore; |
|
6367 cholmod_sparse *A = &Astore; |
|
6368 double dummy; |
|
6369 A->nrow = nr; |
|
6370 A->ncol = nc; |
|
6371 |
|
6372 A->p = cidx(); |
|
6373 A->i = ridx(); |
5604
|
6374 A->nzmax = nnz(); |
5526
|
6375 A->packed = true; |
|
6376 A->sorted = true; |
5506
|
6377 A->nz = NULL; |
|
6378 #ifdef IDX_TYPE_LONG |
|
6379 A->itype = CHOLMOD_LONG; |
|
6380 #else |
|
6381 A->itype = CHOLMOD_INT; |
|
6382 #endif |
|
6383 A->dtype = CHOLMOD_DOUBLE; |
|
6384 A->stype = 1; |
|
6385 A->xtype = CHOLMOD_COMPLEX; |
|
6386 |
|
6387 if (nr < 1) |
|
6388 A->x = &dummy; |
|
6389 else |
|
6390 A->x = data(); |
|
6391 |
|
6392 cholmod_dense Bstore; |
|
6393 cholmod_dense *B = &Bstore; |
|
6394 B->nrow = b.rows(); |
|
6395 B->ncol = b.cols(); |
|
6396 B->d = B->nrow; |
|
6397 B->nzmax = B->nrow * B->ncol; |
|
6398 B->dtype = CHOLMOD_DOUBLE; |
|
6399 B->xtype = CHOLMOD_COMPLEX; |
|
6400 if (nc < 1 || b.cols() < 1) |
|
6401 B->x = &dummy; |
|
6402 else |
|
6403 // We won't alter it, honest :-) |
|
6404 B->x = const_cast<Complex *>(b.fortran_vec()); |
|
6405 |
|
6406 cholmod_factor *L; |
|
6407 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6408 L = CHOLMOD_NAME(analyze) (A, cm); |
|
6409 CHOLMOD_NAME(factorize) (A, L, cm); |
5681
|
6410 if (calc_cond) |
|
6411 rcond = CHOLMOD_NAME(rcond)(L, cm); |
|
6412 else |
|
6413 rcond = 1.; |
5506
|
6414 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6415 |
|
6416 if (rcond == 0.0) |
|
6417 { |
|
6418 // Either its indefinite or singular. Try UMFPACK |
|
6419 mattype.mark_as_unsymmetric (); |
5785
|
6420 typ = MatrixType::Full; |
5506
|
6421 } |
|
6422 else |
|
6423 { |
|
6424 volatile double rcond_plus_one = rcond + 1.0; |
|
6425 |
|
6426 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
6427 { |
|
6428 err = -2; |
|
6429 |
|
6430 if (sing_handler) |
5681
|
6431 { |
|
6432 sing_handler (rcond); |
|
6433 mattype.mark_as_rectangular (); |
|
6434 } |
5506
|
6435 else |
|
6436 (*current_liboctave_error_handler) |
|
6437 ("SparseMatrix::solve matrix singular to machine precision, rcond = %g", |
|
6438 rcond); |
|
6439 |
|
6440 return retval; |
|
6441 } |
|
6442 |
|
6443 cholmod_dense *X; |
|
6444 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6445 X = CHOLMOD_NAME(solve) (CHOLMOD_A, L, B, cm); |
|
6446 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6447 |
|
6448 retval.resize (b.rows (), b.cols()); |
|
6449 for (octave_idx_type j = 0; j < b.cols(); j++) |
|
6450 { |
|
6451 octave_idx_type jr = j * b.rows(); |
|
6452 for (octave_idx_type i = 0; i < b.rows(); i++) |
|
6453 retval.xelem(i,j) = static_cast<Complex *>(X->x)[jr + i]; |
|
6454 } |
|
6455 |
|
6456 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6457 CHOLMOD_NAME(free_dense) (&X, cm); |
|
6458 CHOLMOD_NAME(free_factor) (&L, cm); |
|
6459 CHOLMOD_NAME(finish) (cm); |
6482
|
6460 static char tmp[] = " "; |
|
6461 CHOLMOD_NAME(print_common) (tmp, cm); |
5506
|
6462 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6463 } |
|
6464 #else |
5164
|
6465 (*current_liboctave_warning_handler) |
5506
|
6466 ("CHOLMOD not installed"); |
5164
|
6467 |
|
6468 mattype.mark_as_unsymmetric (); |
5785
|
6469 typ = MatrixType::Full; |
5506
|
6470 #endif |
5164
|
6471 } |
|
6472 |
5785
|
6473 if (typ == MatrixType::Full) |
5164
|
6474 { |
5203
|
6475 #ifdef HAVE_UMFPACK |
5164
|
6476 Matrix Control, Info; |
5681
|
6477 void *Numeric = factorize (err, rcond, Control, Info, |
|
6478 sing_handler, calc_cond); |
5164
|
6479 |
|
6480 if (err == 0) |
|
6481 { |
5275
|
6482 octave_idx_type b_nr = b.rows (); |
|
6483 octave_idx_type b_nc = b.cols (); |
5164
|
6484 int status = 0; |
|
6485 double *control = Control.fortran_vec (); |
|
6486 double *info = Info.fortran_vec (); |
5275
|
6487 const octave_idx_type *Ap = cidx (); |
|
6488 const octave_idx_type *Ai = ridx (); |
5164
|
6489 const Complex *Ax = data (); |
|
6490 const Complex *Bx = b.fortran_vec (); |
|
6491 |
|
6492 retval.resize (b_nr, b_nc); |
|
6493 Complex *Xx = retval.fortran_vec (); |
|
6494 |
5275
|
6495 for (octave_idx_type j = 0, iidx = 0; j < b_nc; j++, iidx += b_nr) |
5164
|
6496 { |
|
6497 status = |
5322
|
6498 UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, Ai, |
5760
|
6499 reinterpret_cast<const double *> (Ax), |
|
6500 NULL, |
|
6501 reinterpret_cast<double *> (&Xx[iidx]), |
|
6502 NULL, |
|
6503 reinterpret_cast<const double *> (&Bx[iidx]), |
5164
|
6504 NULL, Numeric, control, info); |
|
6505 |
|
6506 if (status < 0) |
|
6507 { |
|
6508 (*current_liboctave_error_handler) |
|
6509 ("SparseComplexMatrix::solve solve failed"); |
|
6510 |
5322
|
6511 UMFPACK_ZNAME (report_status) (control, status); |
5164
|
6512 |
|
6513 err = -1; |
|
6514 |
|
6515 break; |
|
6516 } |
|
6517 } |
|
6518 |
5322
|
6519 UMFPACK_ZNAME (report_info) (control, info); |
|
6520 |
|
6521 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
6522 } |
5681
|
6523 else |
|
6524 mattype.mark_as_rectangular (); |
|
6525 |
5203
|
6526 #else |
|
6527 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
6528 #endif |
5164
|
6529 } |
5785
|
6530 else if (typ != MatrixType::Hermitian) |
5164
|
6531 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
6532 } |
|
6533 |
|
6534 return retval; |
|
6535 } |
|
6536 |
|
6537 SparseComplexMatrix |
5785
|
6538 SparseComplexMatrix::fsolve (MatrixType &mattype, const SparseComplexMatrix& b, |
5275
|
6539 octave_idx_type& err, double& rcond, |
5681
|
6540 solve_singularity_handler sing_handler, |
|
6541 bool calc_cond) const |
5164
|
6542 { |
|
6543 SparseComplexMatrix retval; |
|
6544 |
5275
|
6545 octave_idx_type nr = rows (); |
|
6546 octave_idx_type nc = cols (); |
5164
|
6547 err = 0; |
|
6548 |
6924
|
6549 if (nr != nc || nr != b.rows ()) |
5164
|
6550 (*current_liboctave_error_handler) |
|
6551 ("matrix dimension mismatch solution of linear equations"); |
6924
|
6552 else if (nr == 0 || b.cols () == 0) |
|
6553 retval = SparseComplexMatrix (nc, b.cols ()); |
5164
|
6554 else |
|
6555 { |
|
6556 // Print spparms("spumoni") info if requested |
5506
|
6557 volatile int typ = mattype.type (); |
5164
|
6558 mattype.info (); |
|
6559 |
5785
|
6560 if (typ == MatrixType::Hermitian) |
5164
|
6561 { |
5506
|
6562 #ifdef HAVE_CHOLMOD |
|
6563 cholmod_common Common; |
|
6564 cholmod_common *cm = &Common; |
|
6565 |
|
6566 // Setup initial parameters |
|
6567 CHOLMOD_NAME(start) (cm); |
5526
|
6568 cm->prefer_zomplex = false; |
5506
|
6569 |
5893
|
6570 double spu = octave_sparse_params::get_key ("spumoni"); |
5506
|
6571 if (spu == 0.) |
|
6572 { |
|
6573 cm->print = -1; |
|
6574 cm->print_function = NULL; |
|
6575 } |
|
6576 else |
|
6577 { |
5760
|
6578 cm->print = static_cast<int> (spu) + 2; |
5506
|
6579 cm->print_function =&SparseCholPrint; |
|
6580 } |
|
6581 |
|
6582 cm->error_handler = &SparseCholError; |
|
6583 cm->complex_divide = CHOLMOD_NAME(divcomplex); |
|
6584 cm->hypotenuse = CHOLMOD_NAME(hypot); |
|
6585 |
|
6586 #ifdef HAVE_METIS |
|
6587 // METIS 4.0.1 uses malloc and free, and will terminate MATLAB if |
|
6588 // it runs out of memory. Use CHOLMOD's memory guard for METIS, |
|
6589 // which mxMalloc's a huge block of memory (and then immediately |
|
6590 // mxFree's it) before calling METIS |
|
6591 cm->metis_memory = 2.0; |
|
6592 |
|
6593 #if defined(METIS_VERSION) |
|
6594 #if (METIS_VERSION >= METIS_VER(4,0,2)) |
|
6595 // METIS 4.0.2 uses function pointers for malloc and free |
|
6596 METIS_malloc = cm->malloc_memory; |
|
6597 METIS_free = cm->free_memory; |
|
6598 // Turn off METIS memory guard. It is not needed, because mxMalloc |
|
6599 // will safely terminate the mexFunction and free any workspace |
|
6600 // without killing all of octave. |
|
6601 cm->metis_memory = 0.0; |
|
6602 #endif |
|
6603 #endif |
|
6604 #endif |
|
6605 |
5526
|
6606 cm->final_ll = true; |
5506
|
6607 |
|
6608 cholmod_sparse Astore; |
|
6609 cholmod_sparse *A = &Astore; |
|
6610 double dummy; |
|
6611 A->nrow = nr; |
|
6612 A->ncol = nc; |
|
6613 |
|
6614 A->p = cidx(); |
|
6615 A->i = ridx(); |
5604
|
6616 A->nzmax = nnz(); |
5526
|
6617 A->packed = true; |
|
6618 A->sorted = true; |
5506
|
6619 A->nz = NULL; |
|
6620 #ifdef IDX_TYPE_LONG |
|
6621 A->itype = CHOLMOD_LONG; |
|
6622 #else |
|
6623 A->itype = CHOLMOD_INT; |
|
6624 #endif |
|
6625 A->dtype = CHOLMOD_DOUBLE; |
|
6626 A->stype = 1; |
|
6627 A->xtype = CHOLMOD_COMPLEX; |
|
6628 |
|
6629 if (nr < 1) |
|
6630 A->x = &dummy; |
|
6631 else |
|
6632 A->x = data(); |
|
6633 |
|
6634 cholmod_sparse Bstore; |
|
6635 cholmod_sparse *B = &Bstore; |
|
6636 B->nrow = b.rows(); |
|
6637 B->ncol = b.cols(); |
|
6638 B->p = b.cidx(); |
|
6639 B->i = b.ridx(); |
5604
|
6640 B->nzmax = b.nnz(); |
5526
|
6641 B->packed = true; |
|
6642 B->sorted = true; |
5506
|
6643 B->nz = NULL; |
|
6644 #ifdef IDX_TYPE_LONG |
|
6645 B->itype = CHOLMOD_LONG; |
|
6646 #else |
|
6647 B->itype = CHOLMOD_INT; |
|
6648 #endif |
|
6649 B->dtype = CHOLMOD_DOUBLE; |
|
6650 B->stype = 0; |
|
6651 B->xtype = CHOLMOD_COMPLEX; |
|
6652 |
|
6653 if (b.rows() < 1 || b.cols() < 1) |
|
6654 B->x = &dummy; |
|
6655 else |
|
6656 B->x = b.data(); |
|
6657 |
|
6658 cholmod_factor *L; |
|
6659 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6660 L = CHOLMOD_NAME(analyze) (A, cm); |
|
6661 CHOLMOD_NAME(factorize) (A, L, cm); |
5681
|
6662 if (calc_cond) |
|
6663 rcond = CHOLMOD_NAME(rcond)(L, cm); |
|
6664 else |
|
6665 rcond = 1.; |
5506
|
6666 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6667 |
|
6668 if (rcond == 0.0) |
|
6669 { |
|
6670 // Either its indefinite or singular. Try UMFPACK |
|
6671 mattype.mark_as_unsymmetric (); |
5785
|
6672 typ = MatrixType::Full; |
5506
|
6673 } |
|
6674 else |
|
6675 { |
|
6676 volatile double rcond_plus_one = rcond + 1.0; |
|
6677 |
|
6678 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
6679 { |
|
6680 err = -2; |
|
6681 |
|
6682 if (sing_handler) |
5681
|
6683 { |
|
6684 sing_handler (rcond); |
|
6685 mattype.mark_as_rectangular (); |
|
6686 } |
5506
|
6687 else |
|
6688 (*current_liboctave_error_handler) |
|
6689 ("SparseMatrix::solve matrix singular to machine precision, rcond = %g", |
|
6690 rcond); |
|
6691 |
|
6692 return retval; |
|
6693 } |
|
6694 |
|
6695 cholmod_sparse *X; |
|
6696 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6697 X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm); |
|
6698 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6699 |
|
6700 retval = SparseComplexMatrix |
|
6701 (static_cast<octave_idx_type>(X->nrow), |
|
6702 static_cast<octave_idx_type>(X->ncol), |
|
6703 static_cast<octave_idx_type>(X->nzmax)); |
|
6704 for (octave_idx_type j = 0; |
|
6705 j <= static_cast<octave_idx_type>(X->ncol); j++) |
|
6706 retval.xcidx(j) = static_cast<octave_idx_type *>(X->p)[j]; |
|
6707 for (octave_idx_type j = 0; |
|
6708 j < static_cast<octave_idx_type>(X->nzmax); j++) |
|
6709 { |
|
6710 retval.xridx(j) = static_cast<octave_idx_type *>(X->i)[j]; |
|
6711 retval.xdata(j) = static_cast<Complex *>(X->x)[j]; |
|
6712 } |
|
6713 |
|
6714 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6715 CHOLMOD_NAME(free_sparse) (&X, cm); |
|
6716 CHOLMOD_NAME(free_factor) (&L, cm); |
|
6717 CHOLMOD_NAME(finish) (cm); |
6482
|
6718 static char tmp[] = " "; |
|
6719 CHOLMOD_NAME(print_common) (tmp, cm); |
5506
|
6720 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
6721 } |
|
6722 #else |
5164
|
6723 (*current_liboctave_warning_handler) |
5506
|
6724 ("CHOLMOD not installed"); |
5164
|
6725 |
|
6726 mattype.mark_as_unsymmetric (); |
5785
|
6727 typ = MatrixType::Full; |
5506
|
6728 #endif |
5164
|
6729 } |
|
6730 |
5785
|
6731 if (typ == MatrixType::Full) |
5164
|
6732 { |
5203
|
6733 #ifdef HAVE_UMFPACK |
5164
|
6734 Matrix Control, Info; |
5681
|
6735 void *Numeric = factorize (err, rcond, Control, Info, |
|
6736 sing_handler, calc_cond); |
5164
|
6737 |
|
6738 if (err == 0) |
|
6739 { |
5275
|
6740 octave_idx_type b_nr = b.rows (); |
|
6741 octave_idx_type b_nc = b.cols (); |
5164
|
6742 int status = 0; |
|
6743 double *control = Control.fortran_vec (); |
|
6744 double *info = Info.fortran_vec (); |
5275
|
6745 const octave_idx_type *Ap = cidx (); |
|
6746 const octave_idx_type *Ai = ridx (); |
5164
|
6747 const Complex *Ax = data (); |
|
6748 |
|
6749 OCTAVE_LOCAL_BUFFER (Complex, Bx, b_nr); |
|
6750 |
|
6751 // Take a first guess that the number of non-zero terms |
|
6752 // will be as many as in b |
5681
|
6753 octave_idx_type x_nz = b.nnz (); |
5275
|
6754 octave_idx_type ii = 0; |
5164
|
6755 retval = SparseComplexMatrix (b_nr, b_nc, x_nz); |
|
6756 |
|
6757 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
|
6758 |
|
6759 retval.xcidx(0) = 0; |
5275
|
6760 for (octave_idx_type j = 0; j < b_nc; j++) |
5164
|
6761 { |
5275
|
6762 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
6763 Bx[i] = b (i,j); |
|
6764 |
5322
|
6765 status = UMFPACK_ZNAME (solve) (UMFPACK_A, Ap, |
5760
|
6766 Ai, |
|
6767 reinterpret_cast<const double *> (Ax), |
|
6768 NULL, |
|
6769 reinterpret_cast<double *> (Xx), |
|
6770 NULL, |
|
6771 reinterpret_cast<double *> (Bx), |
5164
|
6772 NULL, Numeric, control, info); |
|
6773 |
|
6774 if (status < 0) |
|
6775 { |
|
6776 (*current_liboctave_error_handler) |
|
6777 ("SparseComplexMatrix::solve solve failed"); |
|
6778 |
5322
|
6779 UMFPACK_ZNAME (report_status) (control, status); |
5164
|
6780 |
|
6781 err = -1; |
|
6782 |
|
6783 break; |
|
6784 } |
|
6785 |
5275
|
6786 for (octave_idx_type i = 0; i < b_nr; i++) |
5164
|
6787 { |
|
6788 Complex tmp = Xx[i]; |
|
6789 if (tmp != 0.0) |
|
6790 { |
|
6791 if (ii == x_nz) |
|
6792 { |
|
6793 // Resize the sparse matrix |
5275
|
6794 octave_idx_type sz = x_nz * (b_nc - j) / b_nc; |
5164
|
6795 sz = (sz > 10 ? sz : 10) + x_nz; |
|
6796 retval.change_capacity (sz); |
|
6797 x_nz = sz; |
|
6798 } |
|
6799 retval.xdata(ii) = tmp; |
|
6800 retval.xridx(ii++) = i; |
|
6801 } |
|
6802 } |
|
6803 retval.xcidx(j+1) = ii; |
|
6804 } |
|
6805 |
|
6806 retval.maybe_compress (); |
|
6807 |
|
6808 rcond = Info (UMFPACK_RCOND); |
|
6809 volatile double rcond_plus_one = rcond + 1.0; |
|
6810 |
|
6811 if (status == UMFPACK_WARNING_singular_matrix || |
|
6812 rcond_plus_one == 1.0 || xisnan (rcond)) |
|
6813 { |
|
6814 err = -2; |
|
6815 |
|
6816 if (sing_handler) |
|
6817 sing_handler (rcond); |
|
6818 else |
|
6819 (*current_liboctave_error_handler) |
|
6820 ("SparseComplexMatrix::solve matrix singular to machine precision, rcond = %g", |
|
6821 rcond); |
|
6822 |
|
6823 } |
|
6824 |
5322
|
6825 UMFPACK_ZNAME (report_info) (control, info); |
|
6826 |
|
6827 UMFPACK_ZNAME (free_numeric) (&Numeric); |
5164
|
6828 } |
5681
|
6829 else |
|
6830 mattype.mark_as_rectangular (); |
|
6831 |
5203
|
6832 #else |
|
6833 (*current_liboctave_error_handler) ("UMFPACK not installed"); |
|
6834 #endif |
5164
|
6835 } |
5785
|
6836 else if (typ != MatrixType::Hermitian) |
5164
|
6837 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
6838 } |
|
6839 |
|
6840 return retval; |
|
6841 } |
|
6842 |
|
6843 ComplexMatrix |
5785
|
6844 SparseComplexMatrix::solve (MatrixType &mattype, const Matrix& b) const |
5164
|
6845 { |
5275
|
6846 octave_idx_type info; |
5164
|
6847 double rcond; |
|
6848 return solve (mattype, b, info, rcond, 0); |
|
6849 } |
|
6850 |
|
6851 ComplexMatrix |
5785
|
6852 SparseComplexMatrix::solve (MatrixType &mattype, const Matrix& b, |
5275
|
6853 octave_idx_type& info) const |
5164
|
6854 { |
|
6855 double rcond; |
|
6856 return solve (mattype, b, info, rcond, 0); |
|
6857 } |
|
6858 |
|
6859 ComplexMatrix |
5785
|
6860 SparseComplexMatrix::solve (MatrixType &mattype, const Matrix& b, |
5697
|
6861 octave_idx_type& info, double& rcond) const |
5164
|
6862 { |
|
6863 return solve (mattype, b, info, rcond, 0); |
|
6864 } |
|
6865 |
|
6866 ComplexMatrix |
5785
|
6867 SparseComplexMatrix::solve (MatrixType &mattype, const Matrix& b, |
5697
|
6868 octave_idx_type& err, double& rcond, |
|
6869 solve_singularity_handler sing_handler, |
|
6870 bool singular_fallback) const |
5164
|
6871 { |
5681
|
6872 ComplexMatrix retval; |
5322
|
6873 int typ = mattype.type (false); |
5164
|
6874 |
5785
|
6875 if (typ == MatrixType::Unknown) |
5164
|
6876 typ = mattype.type (*this); |
|
6877 |
5785
|
6878 if (typ == MatrixType::Diagonal || typ == MatrixType::Permuted_Diagonal) |
5681
|
6879 retval = dsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6880 else if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
5681
|
6881 retval = utsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6882 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
5681
|
6883 retval = ltsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6884 else if (typ == MatrixType::Banded || typ == MatrixType::Banded_Hermitian) |
5681
|
6885 retval = bsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6886 else if (typ == MatrixType::Tridiagonal || |
|
6887 typ == MatrixType::Tridiagonal_Hermitian) |
5681
|
6888 retval = trisolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6889 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
5681
|
6890 retval = fsolve (mattype, b, err, rcond, sing_handler, true); |
5785
|
6891 else if (typ != MatrixType::Rectangular) |
5164
|
6892 { |
5681
|
6893 (*current_liboctave_error_handler) ("unknown matrix type"); |
5164
|
6894 return ComplexMatrix (); |
|
6895 } |
5681
|
6896 |
5785
|
6897 if (singular_fallback && mattype.type(false) == MatrixType::Rectangular) |
5681
|
6898 { |
|
6899 rcond = 1.; |
|
6900 #ifdef USE_QRSOLVE |
|
6901 retval = qrsolve (*this, b, err); |
|
6902 #else |
|
6903 retval = dmsolve<ComplexMatrix, SparseComplexMatrix, |
|
6904 Matrix> (*this, b, err); |
|
6905 #endif |
|
6906 } |
|
6907 |
|
6908 return retval; |
5164
|
6909 } |
|
6910 |
|
6911 SparseComplexMatrix |
5785
|
6912 SparseComplexMatrix::solve (MatrixType &mattype, const SparseMatrix& b) const |
5164
|
6913 { |
5275
|
6914 octave_idx_type info; |
5164
|
6915 double rcond; |
|
6916 return solve (mattype, b, info, rcond, 0); |
|
6917 } |
|
6918 |
|
6919 SparseComplexMatrix |
5785
|
6920 SparseComplexMatrix::solve (MatrixType &mattype, const SparseMatrix& b, |
5275
|
6921 octave_idx_type& info) const |
5164
|
6922 { |
|
6923 double rcond; |
|
6924 return solve (mattype, b, info, rcond, 0); |
|
6925 } |
|
6926 |
|
6927 SparseComplexMatrix |
5785
|
6928 SparseComplexMatrix::solve (MatrixType &mattype, const SparseMatrix& b, |
5275
|
6929 octave_idx_type& info, double& rcond) const |
5164
|
6930 { |
|
6931 return solve (mattype, b, info, rcond, 0); |
|
6932 } |
|
6933 |
|
6934 SparseComplexMatrix |
5785
|
6935 SparseComplexMatrix::solve (MatrixType &mattype, const SparseMatrix& b, |
5275
|
6936 octave_idx_type& err, double& rcond, |
5697
|
6937 solve_singularity_handler sing_handler, |
|
6938 bool singular_fallback) const |
5164
|
6939 { |
5681
|
6940 SparseComplexMatrix retval; |
5322
|
6941 int typ = mattype.type (false); |
5164
|
6942 |
5785
|
6943 if (typ == MatrixType::Unknown) |
5164
|
6944 typ = mattype.type (*this); |
|
6945 |
5785
|
6946 if (typ == MatrixType::Diagonal || typ == MatrixType::Permuted_Diagonal) |
5681
|
6947 retval = dsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6948 else if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
5681
|
6949 retval = utsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6950 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
5681
|
6951 retval = ltsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6952 else if (typ == MatrixType::Banded || typ == MatrixType::Banded_Hermitian) |
5681
|
6953 retval = bsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6954 else if (typ == MatrixType::Tridiagonal || |
|
6955 typ == MatrixType::Tridiagonal_Hermitian) |
5681
|
6956 retval = trisolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
6957 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
5681
|
6958 retval = fsolve (mattype, b, err, rcond, sing_handler, true); |
5785
|
6959 else if (typ != MatrixType::Rectangular) |
5164
|
6960 { |
5681
|
6961 (*current_liboctave_error_handler) ("unknown matrix type"); |
5164
|
6962 return SparseComplexMatrix (); |
|
6963 } |
5681
|
6964 |
5785
|
6965 if (singular_fallback && mattype.type(false) == MatrixType::Rectangular) |
5681
|
6966 { |
|
6967 rcond = 1.; |
|
6968 #ifdef USE_QRSOLVE |
|
6969 retval = qrsolve (*this, b, err); |
|
6970 #else |
|
6971 retval = dmsolve<SparseComplexMatrix, SparseComplexMatrix, |
|
6972 SparseMatrix> (*this, b, err); |
|
6973 #endif |
|
6974 } |
|
6975 |
|
6976 return retval; |
5164
|
6977 } |
|
6978 |
|
6979 ComplexMatrix |
5785
|
6980 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b) const |
5164
|
6981 { |
5275
|
6982 octave_idx_type info; |
5164
|
6983 double rcond; |
|
6984 return solve (mattype, b, info, rcond, 0); |
|
6985 } |
|
6986 |
|
6987 ComplexMatrix |
5785
|
6988 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, |
5275
|
6989 octave_idx_type& info) const |
5164
|
6990 { |
|
6991 double rcond; |
|
6992 return solve (mattype, b, info, rcond, 0); |
|
6993 } |
|
6994 |
|
6995 ComplexMatrix |
5785
|
6996 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, |
5697
|
6997 octave_idx_type& info, double& rcond) const |
5164
|
6998 { |
|
6999 return solve (mattype, b, info, rcond, 0); |
|
7000 } |
|
7001 |
|
7002 ComplexMatrix |
5785
|
7003 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexMatrix& b, |
5697
|
7004 octave_idx_type& err, double& rcond, |
|
7005 solve_singularity_handler sing_handler, |
|
7006 bool singular_fallback) const |
5164
|
7007 { |
5681
|
7008 ComplexMatrix retval; |
5322
|
7009 int typ = mattype.type (false); |
5164
|
7010 |
5785
|
7011 if (typ == MatrixType::Unknown) |
5164
|
7012 typ = mattype.type (*this); |
|
7013 |
5785
|
7014 if (typ == MatrixType::Diagonal || typ == MatrixType::Permuted_Diagonal) |
5681
|
7015 retval = dsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7016 else if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
5681
|
7017 retval = utsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7018 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
5681
|
7019 retval = ltsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7020 else if (typ == MatrixType::Banded || typ == MatrixType::Banded_Hermitian) |
5681
|
7021 retval = bsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7022 else if (typ == MatrixType::Tridiagonal || |
|
7023 typ == MatrixType::Tridiagonal_Hermitian) |
5681
|
7024 retval = trisolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7025 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
5681
|
7026 retval = fsolve (mattype, b, err, rcond, sing_handler, true); |
5785
|
7027 else if (typ != MatrixType::Rectangular) |
5164
|
7028 { |
5681
|
7029 (*current_liboctave_error_handler) ("unknown matrix type"); |
5164
|
7030 return ComplexMatrix (); |
|
7031 } |
5681
|
7032 |
5785
|
7033 if (singular_fallback && mattype.type(false) == MatrixType::Rectangular) |
5681
|
7034 { |
|
7035 rcond = 1.; |
|
7036 #ifdef USE_QRSOLVE |
|
7037 retval = qrsolve (*this, b, err); |
|
7038 #else |
|
7039 retval = dmsolve<ComplexMatrix, SparseComplexMatrix, |
|
7040 ComplexMatrix> (*this, b, err); |
|
7041 #endif |
|
7042 } |
|
7043 |
|
7044 return retval; |
5164
|
7045 } |
|
7046 |
|
7047 SparseComplexMatrix |
5785
|
7048 SparseComplexMatrix::solve (MatrixType &mattype, |
5164
|
7049 const SparseComplexMatrix& b) const |
|
7050 { |
5275
|
7051 octave_idx_type info; |
5164
|
7052 double rcond; |
|
7053 return solve (mattype, b, info, rcond, 0); |
|
7054 } |
|
7055 |
|
7056 SparseComplexMatrix |
5785
|
7057 SparseComplexMatrix::solve (MatrixType &mattype, const SparseComplexMatrix& b, |
5697
|
7058 octave_idx_type& info) const |
5164
|
7059 { |
|
7060 double rcond; |
|
7061 return solve (mattype, b, info, rcond, 0); |
|
7062 } |
|
7063 |
|
7064 SparseComplexMatrix |
5785
|
7065 SparseComplexMatrix::solve (MatrixType &mattype, const SparseComplexMatrix& b, |
5697
|
7066 octave_idx_type& info, double& rcond) const |
5164
|
7067 { |
|
7068 return solve (mattype, b, info, rcond, 0); |
|
7069 } |
|
7070 |
|
7071 SparseComplexMatrix |
5785
|
7072 SparseComplexMatrix::solve (MatrixType &mattype, const SparseComplexMatrix& b, |
5275
|
7073 octave_idx_type& err, double& rcond, |
5697
|
7074 solve_singularity_handler sing_handler, |
|
7075 bool singular_fallback) const |
5164
|
7076 { |
5681
|
7077 SparseComplexMatrix retval; |
5322
|
7078 int typ = mattype.type (false); |
5164
|
7079 |
5785
|
7080 if (typ == MatrixType::Unknown) |
5164
|
7081 typ = mattype.type (*this); |
|
7082 |
5785
|
7083 if (typ == MatrixType::Diagonal || typ == MatrixType::Permuted_Diagonal) |
5681
|
7084 retval = dsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7085 else if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
5681
|
7086 retval = utsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7087 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
5681
|
7088 retval = ltsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7089 else if (typ == MatrixType::Banded || typ == MatrixType::Banded_Hermitian) |
5681
|
7090 retval = bsolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7091 else if (typ == MatrixType::Tridiagonal || |
|
7092 typ == MatrixType::Tridiagonal_Hermitian) |
5681
|
7093 retval = trisolve (mattype, b, err, rcond, sing_handler, false); |
5785
|
7094 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
5681
|
7095 retval = fsolve (mattype, b, err, rcond, sing_handler, true); |
5785
|
7096 else if (typ != MatrixType::Rectangular) |
5164
|
7097 { |
5681
|
7098 (*current_liboctave_error_handler) ("unknown matrix type"); |
5164
|
7099 return SparseComplexMatrix (); |
|
7100 } |
5681
|
7101 |
5785
|
7102 if (singular_fallback && mattype.type(false) == MatrixType::Rectangular) |
5681
|
7103 { |
|
7104 rcond = 1.; |
|
7105 #ifdef USE_QRSOLVE |
|
7106 retval = qrsolve (*this, b, err); |
|
7107 #else |
|
7108 retval = dmsolve<SparseComplexMatrix, SparseComplexMatrix, |
|
7109 SparseComplexMatrix> (*this, b, err); |
|
7110 #endif |
|
7111 } |
|
7112 |
|
7113 return retval; |
5164
|
7114 } |
|
7115 |
|
7116 ComplexColumnVector |
5785
|
7117 SparseComplexMatrix::solve (MatrixType &mattype, const ColumnVector& b) const |
5164
|
7118 { |
5275
|
7119 octave_idx_type info; double rcond; |
5164
|
7120 return solve (mattype, b, info, rcond); |
|
7121 } |
|
7122 |
|
7123 ComplexColumnVector |
5785
|
7124 SparseComplexMatrix::solve (MatrixType &mattype, const ColumnVector& b, |
5275
|
7125 octave_idx_type& info) const |
5164
|
7126 { |
|
7127 double rcond; |
|
7128 return solve (mattype, b, info, rcond); |
|
7129 } |
|
7130 |
|
7131 ComplexColumnVector |
5785
|
7132 SparseComplexMatrix::solve (MatrixType &mattype, const ColumnVector& b, |
5275
|
7133 octave_idx_type& info, double& rcond) const |
5164
|
7134 { |
|
7135 return solve (mattype, b, info, rcond, 0); |
|
7136 } |
|
7137 |
|
7138 ComplexColumnVector |
5785
|
7139 SparseComplexMatrix::solve (MatrixType &mattype, const ColumnVector& b, |
5275
|
7140 octave_idx_type& info, double& rcond, |
5164
|
7141 solve_singularity_handler sing_handler) const |
|
7142 { |
|
7143 Matrix tmp (b); |
5275
|
7144 return solve (mattype, tmp, info, rcond, sing_handler).column (static_cast<octave_idx_type> (0)); |
5164
|
7145 } |
|
7146 |
|
7147 ComplexColumnVector |
5785
|
7148 SparseComplexMatrix::solve (MatrixType &mattype, |
5164
|
7149 const ComplexColumnVector& b) const |
|
7150 { |
5275
|
7151 octave_idx_type info; |
5164
|
7152 double rcond; |
|
7153 return solve (mattype, b, info, rcond, 0); |
|
7154 } |
|
7155 |
|
7156 ComplexColumnVector |
5785
|
7157 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexColumnVector& b, |
5275
|
7158 octave_idx_type& info) const |
5164
|
7159 { |
|
7160 double rcond; |
|
7161 return solve (mattype, b, info, rcond, 0); |
|
7162 } |
|
7163 |
|
7164 ComplexColumnVector |
5785
|
7165 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexColumnVector& b, |
5275
|
7166 octave_idx_type& info, double& rcond) const |
5164
|
7167 { |
|
7168 return solve (mattype, b, info, rcond, 0); |
|
7169 } |
|
7170 |
|
7171 ComplexColumnVector |
5785
|
7172 SparseComplexMatrix::solve (MatrixType &mattype, const ComplexColumnVector& b, |
5275
|
7173 octave_idx_type& info, double& rcond, |
5164
|
7174 solve_singularity_handler sing_handler) const |
|
7175 { |
|
7176 ComplexMatrix tmp (b); |
5275
|
7177 return solve (mattype, tmp, info, rcond, sing_handler).column (static_cast<octave_idx_type> (0)); |
5164
|
7178 } |
|
7179 |
|
7180 ComplexMatrix |
|
7181 SparseComplexMatrix::solve (const Matrix& b) const |
|
7182 { |
5275
|
7183 octave_idx_type info; |
5164
|
7184 double rcond; |
|
7185 return solve (b, info, rcond, 0); |
|
7186 } |
|
7187 |
|
7188 ComplexMatrix |
5275
|
7189 SparseComplexMatrix::solve (const Matrix& b, octave_idx_type& info) const |
5164
|
7190 { |
|
7191 double rcond; |
|
7192 return solve (b, info, rcond, 0); |
|
7193 } |
|
7194 |
|
7195 ComplexMatrix |
5275
|
7196 SparseComplexMatrix::solve (const Matrix& b, octave_idx_type& info, |
5164
|
7197 double& rcond) const |
|
7198 { |
|
7199 return solve (b, info, rcond, 0); |
|
7200 } |
|
7201 |
|
7202 ComplexMatrix |
5275
|
7203 SparseComplexMatrix::solve (const Matrix& b, octave_idx_type& err, |
5164
|
7204 double& rcond, |
|
7205 solve_singularity_handler sing_handler) const |
|
7206 { |
5785
|
7207 MatrixType mattype (*this); |
5164
|
7208 return solve (mattype, b, err, rcond, sing_handler); |
|
7209 } |
|
7210 |
|
7211 SparseComplexMatrix |
|
7212 SparseComplexMatrix::solve (const SparseMatrix& b) const |
|
7213 { |
5275
|
7214 octave_idx_type info; |
5164
|
7215 double rcond; |
|
7216 return solve (b, info, rcond, 0); |
|
7217 } |
|
7218 |
|
7219 SparseComplexMatrix |
|
7220 SparseComplexMatrix::solve (const SparseMatrix& b, |
5275
|
7221 octave_idx_type& info) const |
5164
|
7222 { |
|
7223 double rcond; |
|
7224 return solve (b, info, rcond, 0); |
|
7225 } |
|
7226 |
|
7227 SparseComplexMatrix |
|
7228 SparseComplexMatrix::solve (const SparseMatrix& b, |
5275
|
7229 octave_idx_type& info, double& rcond) const |
5164
|
7230 { |
|
7231 return solve (b, info, rcond, 0); |
|
7232 } |
|
7233 |
|
7234 SparseComplexMatrix |
|
7235 SparseComplexMatrix::solve (const SparseMatrix& b, |
5275
|
7236 octave_idx_type& err, double& rcond, |
5164
|
7237 solve_singularity_handler sing_handler) const |
|
7238 { |
5785
|
7239 MatrixType mattype (*this); |
5164
|
7240 return solve (mattype, b, err, rcond, sing_handler); |
|
7241 } |
|
7242 |
|
7243 ComplexMatrix |
|
7244 SparseComplexMatrix::solve (const ComplexMatrix& b, |
5275
|
7245 octave_idx_type& info) const |
5164
|
7246 { |
|
7247 double rcond; |
|
7248 return solve (b, info, rcond, 0); |
|
7249 } |
|
7250 |
|
7251 ComplexMatrix |
|
7252 SparseComplexMatrix::solve (const ComplexMatrix& b, |
5275
|
7253 octave_idx_type& info, double& rcond) const |
5164
|
7254 { |
|
7255 return solve (b, info, rcond, 0); |
|
7256 } |
|
7257 |
|
7258 ComplexMatrix |
|
7259 SparseComplexMatrix::solve (const ComplexMatrix& b, |
5275
|
7260 octave_idx_type& err, double& rcond, |
5164
|
7261 solve_singularity_handler sing_handler) const |
|
7262 { |
5785
|
7263 MatrixType mattype (*this); |
5164
|
7264 return solve (mattype, b, err, rcond, sing_handler); |
|
7265 } |
|
7266 |
|
7267 SparseComplexMatrix |
|
7268 SparseComplexMatrix::solve (const SparseComplexMatrix& b) const |
|
7269 { |
5275
|
7270 octave_idx_type info; |
5164
|
7271 double rcond; |
|
7272 return solve (b, info, rcond, 0); |
|
7273 } |
|
7274 |
|
7275 SparseComplexMatrix |
|
7276 SparseComplexMatrix::solve (const SparseComplexMatrix& b, |
5275
|
7277 octave_idx_type& info) const |
5164
|
7278 { |
|
7279 double rcond; |
|
7280 return solve (b, info, rcond, 0); |
|
7281 } |
|
7282 |
|
7283 SparseComplexMatrix |
|
7284 SparseComplexMatrix::solve (const SparseComplexMatrix& b, |
5275
|
7285 octave_idx_type& info, double& rcond) const |
5164
|
7286 { |
|
7287 return solve (b, info, rcond, 0); |
|
7288 } |
|
7289 |
|
7290 SparseComplexMatrix |
|
7291 SparseComplexMatrix::solve (const SparseComplexMatrix& b, |
5275
|
7292 octave_idx_type& err, double& rcond, |
5164
|
7293 solve_singularity_handler sing_handler) const |
|
7294 { |
5785
|
7295 MatrixType mattype (*this); |
5164
|
7296 return solve (mattype, b, err, rcond, sing_handler); |
|
7297 } |
|
7298 |
|
7299 ComplexColumnVector |
|
7300 SparseComplexMatrix::solve (const ColumnVector& b) const |
|
7301 { |
5275
|
7302 octave_idx_type info; double rcond; |
5164
|
7303 return solve (b, info, rcond); |
|
7304 } |
|
7305 |
|
7306 ComplexColumnVector |
5275
|
7307 SparseComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info) const |
5164
|
7308 { |
|
7309 double rcond; |
|
7310 return solve (b, info, rcond); |
|
7311 } |
|
7312 |
|
7313 ComplexColumnVector |
5275
|
7314 SparseComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, |
5164
|
7315 double& rcond) const |
|
7316 { |
|
7317 return solve (b, info, rcond, 0); |
|
7318 } |
|
7319 |
|
7320 ComplexColumnVector |
5275
|
7321 SparseComplexMatrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
5164
|
7322 solve_singularity_handler sing_handler) const |
|
7323 { |
|
7324 Matrix tmp (b); |
5275
|
7325 return solve (tmp, info, rcond, sing_handler).column (static_cast<octave_idx_type> (0)); |
5164
|
7326 } |
|
7327 |
|
7328 ComplexColumnVector |
|
7329 SparseComplexMatrix::solve (const ComplexColumnVector& b) const |
|
7330 { |
5275
|
7331 octave_idx_type info; |
5164
|
7332 double rcond; |
|
7333 return solve (b, info, rcond, 0); |
|
7334 } |
|
7335 |
|
7336 ComplexColumnVector |
5275
|
7337 SparseComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
5164
|
7338 { |
|
7339 double rcond; |
|
7340 return solve (b, info, rcond, 0); |
|
7341 } |
|
7342 |
|
7343 ComplexColumnVector |
5275
|
7344 SparseComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
5164
|
7345 double& rcond) const |
|
7346 { |
|
7347 return solve (b, info, rcond, 0); |
|
7348 } |
|
7349 |
|
7350 ComplexColumnVector |
5275
|
7351 SparseComplexMatrix::solve (const ComplexColumnVector& b, octave_idx_type& info, |
5164
|
7352 double& rcond, |
|
7353 solve_singularity_handler sing_handler) const |
|
7354 { |
|
7355 ComplexMatrix tmp (b); |
5275
|
7356 return solve (tmp, info, rcond, sing_handler).column (static_cast<octave_idx_type> (0)); |
5164
|
7357 } |
|
7358 |
|
7359 // unary operations |
|
7360 SparseBoolMatrix |
|
7361 SparseComplexMatrix::operator ! (void) const |
|
7362 { |
5275
|
7363 octave_idx_type nr = rows (); |
|
7364 octave_idx_type nc = cols (); |
5681
|
7365 octave_idx_type nz1 = nnz (); |
5275
|
7366 octave_idx_type nz2 = nr*nc - nz1; |
5164
|
7367 |
|
7368 SparseBoolMatrix r (nr, nc, nz2); |
|
7369 |
5275
|
7370 octave_idx_type ii = 0; |
|
7371 octave_idx_type jj = 0; |
5164
|
7372 r.cidx (0) = 0; |
5275
|
7373 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
7374 { |
5275
|
7375 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
7376 { |
|
7377 if (jj < cidx(i+1) && ridx(jj) == j) |
|
7378 jj++; |
|
7379 else |
|
7380 { |
|
7381 r.data(ii) = true; |
|
7382 r.ridx(ii++) = j; |
|
7383 } |
|
7384 } |
|
7385 r.cidx (i+1) = ii; |
|
7386 } |
|
7387 |
|
7388 return r; |
|
7389 } |
|
7390 |
|
7391 SparseComplexMatrix |
|
7392 SparseComplexMatrix::squeeze (void) const |
|
7393 { |
|
7394 return MSparse<Complex>::squeeze (); |
|
7395 } |
|
7396 |
|
7397 SparseComplexMatrix |
|
7398 SparseComplexMatrix::index (idx_vector& i, int resize_ok) const |
|
7399 { |
|
7400 return MSparse<Complex>::index (i, resize_ok); |
|
7401 } |
|
7402 |
|
7403 SparseComplexMatrix |
|
7404 SparseComplexMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const |
|
7405 { |
|
7406 return MSparse<Complex>::index (i, j, resize_ok); |
|
7407 } |
|
7408 |
|
7409 SparseComplexMatrix |
|
7410 SparseComplexMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
7411 { |
|
7412 return MSparse<Complex>::index (ra_idx, resize_ok); |
|
7413 } |
|
7414 SparseComplexMatrix |
|
7415 SparseComplexMatrix::reshape (const dim_vector& new_dims) const |
|
7416 { |
|
7417 return MSparse<Complex>::reshape (new_dims); |
|
7418 } |
|
7419 |
|
7420 SparseComplexMatrix |
5275
|
7421 SparseComplexMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164
|
7422 { |
|
7423 return MSparse<Complex>::permute (vec, inv); |
|
7424 } |
|
7425 |
|
7426 SparseComplexMatrix |
5275
|
7427 SparseComplexMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164
|
7428 { |
|
7429 return MSparse<Complex>::ipermute (vec); |
|
7430 } |
|
7431 |
|
7432 // other operations |
|
7433 |
|
7434 SparseComplexMatrix |
|
7435 SparseComplexMatrix::map (c_c_Mapper f) const |
|
7436 { |
5275
|
7437 octave_idx_type nr = rows (); |
|
7438 octave_idx_type nc = cols (); |
5681
|
7439 octave_idx_type nz = nnz (); |
5164
|
7440 bool f_zero = (f(0.0) == 0.0); |
|
7441 |
|
7442 // Count number of non-zero elements |
5275
|
7443 octave_idx_type nel = (f_zero ? 0 : nr*nc - nz); |
|
7444 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
7445 if (f (data(i)) != 0.0) |
|
7446 nel++; |
|
7447 |
|
7448 SparseComplexMatrix retval (nr, nc, nel); |
|
7449 |
|
7450 if (f_zero) |
|
7451 { |
5275
|
7452 octave_idx_type ii = 0; |
|
7453 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7454 { |
5275
|
7455 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
7456 { |
|
7457 Complex tmp = f (elem (i, j)); |
|
7458 if (tmp != 0.0) |
|
7459 { |
|
7460 retval.data(ii) = tmp; |
|
7461 retval.ridx(ii++) = i; |
|
7462 } |
|
7463 } |
|
7464 retval.cidx(j+1) = ii; |
|
7465 } |
|
7466 } |
|
7467 else |
|
7468 { |
5275
|
7469 octave_idx_type ii = 0; |
|
7470 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7471 { |
5275
|
7472 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
7473 { |
|
7474 retval.data(ii) = f (elem(i)); |
|
7475 retval.ridx(ii++) = ridx(i); |
|
7476 } |
|
7477 retval.cidx(j+1) = ii; |
|
7478 } |
|
7479 } |
|
7480 |
|
7481 return retval; |
|
7482 } |
|
7483 |
|
7484 SparseMatrix |
|
7485 SparseComplexMatrix::map (d_c_Mapper f) const |
|
7486 { |
5275
|
7487 octave_idx_type nr = rows (); |
|
7488 octave_idx_type nc = cols (); |
5681
|
7489 octave_idx_type nz = nnz (); |
5164
|
7490 bool f_zero = (f(0.0) == 0.0); |
|
7491 |
|
7492 // Count number of non-zero elements |
5275
|
7493 octave_idx_type nel = (f_zero ? 0 : nr*nc - nz); |
|
7494 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
7495 if (f (data(i)) != 0.0) |
|
7496 nel++; |
|
7497 |
|
7498 SparseMatrix retval (nr, nc, nel); |
|
7499 |
|
7500 if (f_zero) |
|
7501 { |
5275
|
7502 octave_idx_type ii = 0; |
|
7503 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7504 { |
5275
|
7505 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
7506 { |
|
7507 double tmp = f (elem (i, j)); |
|
7508 if (tmp != 0.0) |
|
7509 { |
|
7510 retval.data(ii) = tmp; |
|
7511 retval.ridx(ii++) = i; |
|
7512 } |
|
7513 } |
|
7514 retval.cidx(j+1) = ii; |
|
7515 } |
|
7516 } |
|
7517 else |
|
7518 { |
5275
|
7519 octave_idx_type ii = 0; |
|
7520 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7521 { |
5275
|
7522 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
7523 { |
|
7524 retval.data(ii) = f (elem(i)); |
|
7525 retval.ridx(ii++) = ridx(i); |
|
7526 } |
|
7527 retval.cidx(j+1) = ii; |
|
7528 } |
|
7529 } |
|
7530 |
|
7531 return retval; |
|
7532 } |
|
7533 |
|
7534 SparseBoolMatrix |
|
7535 SparseComplexMatrix::map (b_c_Mapper f) const |
|
7536 { |
5275
|
7537 octave_idx_type nr = rows (); |
|
7538 octave_idx_type nc = cols (); |
5681
|
7539 octave_idx_type nz = nnz (); |
5164
|
7540 bool f_zero = f(0.0); |
|
7541 |
|
7542 // Count number of non-zero elements |
5275
|
7543 octave_idx_type nel = (f_zero ? 0 : nr*nc - nz); |
|
7544 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
7545 if (f (data(i)) != 0.0) |
|
7546 nel++; |
|
7547 |
|
7548 SparseBoolMatrix retval (nr, nc, nel); |
|
7549 |
|
7550 if (f_zero) |
|
7551 { |
5275
|
7552 octave_idx_type ii = 0; |
|
7553 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7554 { |
5275
|
7555 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
7556 { |
|
7557 bool tmp = f (elem (i, j)); |
|
7558 if (tmp) |
|
7559 { |
|
7560 retval.data(ii) = tmp; |
|
7561 retval.ridx(ii++) = i; |
|
7562 } |
|
7563 } |
|
7564 retval.cidx(j+1) = ii; |
|
7565 } |
|
7566 } |
|
7567 else |
|
7568 { |
5275
|
7569 octave_idx_type ii = 0; |
|
7570 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
7571 { |
5275
|
7572 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
7573 { |
|
7574 retval.data(ii) = f (elem(i)); |
|
7575 retval.ridx(ii++) = ridx(i); |
|
7576 } |
|
7577 retval.cidx(j+1) = ii; |
|
7578 } |
|
7579 } |
|
7580 |
|
7581 return retval; |
|
7582 } |
|
7583 |
|
7584 SparseComplexMatrix& |
|
7585 SparseComplexMatrix::apply (c_c_Mapper f) |
|
7586 { |
|
7587 *this = map (f); |
|
7588 return *this; |
|
7589 } |
|
7590 |
|
7591 bool |
|
7592 SparseComplexMatrix::any_element_is_inf_or_nan (void) const |
|
7593 { |
5681
|
7594 octave_idx_type nel = nnz (); |
5275
|
7595 |
|
7596 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
7597 { |
|
7598 Complex val = data (i); |
|
7599 if (xisinf (val) || xisnan (val)) |
|
7600 return true; |
|
7601 } |
|
7602 |
|
7603 return false; |
|
7604 } |
|
7605 |
|
7606 // Return true if no elements have imaginary components. |
|
7607 |
|
7608 bool |
|
7609 SparseComplexMatrix::all_elements_are_real (void) const |
|
7610 { |
5681
|
7611 octave_idx_type nel = nnz (); |
5275
|
7612 |
|
7613 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
7614 { |
5261
|
7615 double ip = std::imag (data (i)); |
5164
|
7616 |
|
7617 if (ip != 0.0 || lo_ieee_signbit (ip)) |
|
7618 return false; |
|
7619 } |
|
7620 |
|
7621 return true; |
|
7622 } |
|
7623 |
|
7624 // Return nonzero if any element of CM has a non-integer real or |
|
7625 // imaginary part. Also extract the largest and smallest (real or |
|
7626 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
7627 |
|
7628 bool |
|
7629 SparseComplexMatrix::all_integers (double& max_val, double& min_val) const |
|
7630 { |
5681
|
7631 octave_idx_type nel = nnz (); |
5164
|
7632 |
|
7633 if (nel == 0) |
|
7634 return false; |
|
7635 |
5261
|
7636 max_val = std::real(data (0)); |
|
7637 min_val = std::real(data (0)); |
5164
|
7638 |
5275
|
7639 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
7640 { |
|
7641 Complex val = data (i); |
|
7642 |
5261
|
7643 double r_val = std::real (val); |
|
7644 double i_val = std::imag (val); |
5164
|
7645 |
|
7646 if (r_val > max_val) |
|
7647 max_val = r_val; |
|
7648 |
|
7649 if (i_val > max_val) |
|
7650 max_val = i_val; |
|
7651 |
|
7652 if (r_val < min_val) |
|
7653 min_val = r_val; |
|
7654 |
|
7655 if (i_val < min_val) |
|
7656 min_val = i_val; |
|
7657 |
|
7658 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
|
7659 return false; |
|
7660 } |
|
7661 |
|
7662 return true; |
|
7663 } |
|
7664 |
|
7665 bool |
|
7666 SparseComplexMatrix::too_large_for_float (void) const |
|
7667 { |
5681
|
7668 octave_idx_type nel = nnz (); |
5275
|
7669 |
|
7670 for (octave_idx_type i = 0; i < nel; i++) |
5164
|
7671 { |
|
7672 Complex val = data (i); |
|
7673 |
5261
|
7674 double r_val = std::real (val); |
|
7675 double i_val = std::imag (val); |
5164
|
7676 |
|
7677 if (r_val > FLT_MAX |
|
7678 || i_val > FLT_MAX |
|
7679 || r_val < FLT_MIN |
|
7680 || i_val < FLT_MIN) |
|
7681 return true; |
|
7682 } |
|
7683 |
|
7684 return false; |
|
7685 } |
|
7686 |
5775
|
7687 // FIXME Do these really belong here? Maybe they should be |
5164
|
7688 // in a base class? |
|
7689 |
|
7690 SparseBoolMatrix |
|
7691 SparseComplexMatrix::all (int dim) const |
|
7692 { |
|
7693 SPARSE_ALL_OP (dim); |
|
7694 } |
|
7695 |
|
7696 SparseBoolMatrix |
|
7697 SparseComplexMatrix::any (int dim) const |
|
7698 { |
|
7699 SPARSE_ANY_OP (dim); |
|
7700 } |
|
7701 |
|
7702 SparseComplexMatrix |
|
7703 SparseComplexMatrix::cumprod (int dim) const |
|
7704 { |
|
7705 SPARSE_CUMPROD (SparseComplexMatrix, Complex, cumprod); |
|
7706 } |
|
7707 |
|
7708 SparseComplexMatrix |
|
7709 SparseComplexMatrix::cumsum (int dim) const |
|
7710 { |
|
7711 SPARSE_CUMSUM (SparseComplexMatrix, Complex, cumsum); |
|
7712 } |
|
7713 |
|
7714 SparseComplexMatrix |
|
7715 SparseComplexMatrix::prod (int dim) const |
|
7716 { |
|
7717 SPARSE_REDUCTION_OP (SparseComplexMatrix, Complex, *=, 1.0, 1.0); |
|
7718 } |
|
7719 |
|
7720 SparseComplexMatrix |
|
7721 SparseComplexMatrix::sum (int dim) const |
|
7722 { |
|
7723 SPARSE_REDUCTION_OP (SparseComplexMatrix, Complex, +=, 0.0, 0.0); |
|
7724 } |
|
7725 |
|
7726 SparseComplexMatrix |
|
7727 SparseComplexMatrix::sumsq (int dim) const |
|
7728 { |
|
7729 #define ROW_EXPR \ |
|
7730 Complex d = elem (i, j); \ |
|
7731 tmp [i] += d * conj (d) |
|
7732 |
|
7733 #define COL_EXPR \ |
|
7734 Complex d = elem (i, j); \ |
|
7735 tmp [j] += d * conj (d) |
|
7736 |
|
7737 SPARSE_BASE_REDUCTION_OP (SparseComplexMatrix, Complex, ROW_EXPR, |
|
7738 COL_EXPR, 0.0, 0.0); |
|
7739 |
|
7740 #undef ROW_EXPR |
|
7741 #undef COL_EXPR |
|
7742 } |
|
7743 |
|
7744 SparseMatrix SparseComplexMatrix::abs (void) const |
|
7745 { |
5681
|
7746 octave_idx_type nz = nnz (); |
5275
|
7747 octave_idx_type nc = cols (); |
5164
|
7748 |
|
7749 SparseMatrix retval (rows(), nc, nz); |
|
7750 |
5275
|
7751 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
7752 retval.cidx (i) = cidx (i); |
|
7753 |
5275
|
7754 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
7755 { |
5261
|
7756 retval.data (i) = std::abs (data (i)); |
5164
|
7757 retval.ridx (i) = ridx (i); |
|
7758 } |
|
7759 |
|
7760 return retval; |
|
7761 } |
|
7762 |
|
7763 SparseComplexMatrix |
5275
|
7764 SparseComplexMatrix::diag (octave_idx_type k) const |
5164
|
7765 { |
5275
|
7766 octave_idx_type nnr = rows (); |
|
7767 octave_idx_type nnc = cols (); |
5164
|
7768 |
|
7769 if (k > 0) |
|
7770 nnc -= k; |
|
7771 else if (k < 0) |
|
7772 nnr += k; |
|
7773 |
|
7774 SparseComplexMatrix d; |
|
7775 |
|
7776 if (nnr > 0 && nnc > 0) |
|
7777 { |
5275
|
7778 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
5164
|
7779 |
|
7780 // Count the number of non-zero elements |
5275
|
7781 octave_idx_type nel = 0; |
5164
|
7782 if (k > 0) |
|
7783 { |
5275
|
7784 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7785 if (elem (i, i+k) != 0.) |
|
7786 nel++; |
|
7787 } |
|
7788 else if ( k < 0) |
|
7789 { |
5275
|
7790 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7791 if (elem (i-k, i) != 0.) |
|
7792 nel++; |
|
7793 } |
|
7794 else |
|
7795 { |
5275
|
7796 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7797 if (elem (i, i) != 0.) |
|
7798 nel++; |
|
7799 } |
|
7800 |
|
7801 d = SparseComplexMatrix (ndiag, 1, nel); |
|
7802 d.xcidx (0) = 0; |
|
7803 d.xcidx (1) = nel; |
|
7804 |
5275
|
7805 octave_idx_type ii = 0; |
5164
|
7806 if (k > 0) |
|
7807 { |
5275
|
7808 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7809 { |
|
7810 Complex tmp = elem (i, i+k); |
|
7811 if (tmp != 0.) |
|
7812 { |
|
7813 d.xdata (ii) = tmp; |
|
7814 d.xridx (ii++) = i; |
|
7815 } |
|
7816 } |
|
7817 } |
|
7818 else if ( k < 0) |
|
7819 { |
5275
|
7820 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7821 { |
|
7822 Complex tmp = elem (i-k, i); |
|
7823 if (tmp != 0.) |
|
7824 { |
|
7825 d.xdata (ii) = tmp; |
|
7826 d.xridx (ii++) = i; |
|
7827 } |
|
7828 } |
|
7829 } |
|
7830 else |
|
7831 { |
5275
|
7832 for (octave_idx_type i = 0; i < ndiag; i++) |
5164
|
7833 { |
|
7834 Complex tmp = elem (i, i); |
|
7835 if (tmp != 0.) |
|
7836 { |
|
7837 d.xdata (ii) = tmp; |
|
7838 d.xridx (ii++) = i; |
|
7839 } |
|
7840 } |
|
7841 } |
|
7842 } |
|
7843 else |
|
7844 (*current_liboctave_error_handler) |
|
7845 ("diag: requested diagonal out of range"); |
|
7846 |
|
7847 return d; |
|
7848 } |
|
7849 |
|
7850 std::ostream& |
|
7851 operator << (std::ostream& os, const SparseComplexMatrix& a) |
|
7852 { |
5275
|
7853 octave_idx_type nc = a.cols (); |
5164
|
7854 |
|
7855 // add one to the printed indices to go from |
|
7856 // zero-based to one-based arrays |
5275
|
7857 for (octave_idx_type j = 0; j < nc; j++) { |
5164
|
7858 OCTAVE_QUIT; |
5275
|
7859 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) { |
5164
|
7860 os << a.ridx(i) + 1 << " " << j + 1 << " "; |
|
7861 octave_write_complex (os, a.data(i)); |
|
7862 os << "\n"; |
|
7863 } |
|
7864 } |
|
7865 |
|
7866 return os; |
|
7867 } |
|
7868 |
|
7869 std::istream& |
|
7870 operator >> (std::istream& is, SparseComplexMatrix& a) |
|
7871 { |
5275
|
7872 octave_idx_type nr = a.rows (); |
|
7873 octave_idx_type nc = a.cols (); |
5604
|
7874 octave_idx_type nz = a.nzmax (); |
5164
|
7875 |
|
7876 if (nr < 1 || nc < 1) |
|
7877 is.clear (std::ios::badbit); |
|
7878 else |
|
7879 { |
5275
|
7880 octave_idx_type itmp, jtmp, jold = 0; |
5164
|
7881 Complex tmp; |
5275
|
7882 octave_idx_type ii = 0; |
5164
|
7883 |
|
7884 a.cidx (0) = 0; |
5275
|
7885 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
7886 { |
|
7887 is >> itmp; |
|
7888 itmp--; |
|
7889 is >> jtmp; |
|
7890 jtmp--; |
|
7891 tmp = octave_read_complex (is); |
|
7892 |
|
7893 if (is) |
|
7894 { |
|
7895 if (jold != jtmp) |
|
7896 { |
5275
|
7897 for (octave_idx_type j = jold; j < jtmp; j++) |
5164
|
7898 a.cidx(j+1) = ii; |
|
7899 |
|
7900 jold = jtmp; |
|
7901 } |
|
7902 a.data (ii) = tmp; |
|
7903 a.ridx (ii++) = itmp; |
|
7904 } |
|
7905 else |
|
7906 goto done; |
|
7907 } |
|
7908 |
5275
|
7909 for (octave_idx_type j = jold; j < nc; j++) |
5164
|
7910 a.cidx(j+1) = ii; |
|
7911 } |
|
7912 |
|
7913 done: |
|
7914 |
|
7915 return is; |
|
7916 } |
|
7917 |
|
7918 SparseComplexMatrix |
|
7919 operator * (const SparseComplexMatrix& m, const SparseMatrix& a) |
|
7920 { |
5681
|
7921 SPARSE_SPARSE_MUL (SparseComplexMatrix, Complex, double); |
5164
|
7922 } |
|
7923 |
|
7924 SparseComplexMatrix |
|
7925 operator * (const SparseMatrix& m, const SparseComplexMatrix& a) |
|
7926 { |
5681
|
7927 SPARSE_SPARSE_MUL (SparseComplexMatrix, Complex, Complex); |
5164
|
7928 } |
|
7929 |
|
7930 SparseComplexMatrix |
|
7931 operator * (const SparseComplexMatrix& m, const SparseComplexMatrix& a) |
|
7932 { |
5681
|
7933 SPARSE_SPARSE_MUL (SparseComplexMatrix, Complex, Complex); |
5164
|
7934 } |
|
7935 |
5429
|
7936 ComplexMatrix |
|
7937 operator * (const ComplexMatrix& m, const SparseMatrix& a) |
|
7938 { |
5681
|
7939 FULL_SPARSE_MUL (ComplexMatrix, double, Complex (0.,0.)); |
5429
|
7940 } |
|
7941 |
|
7942 ComplexMatrix |
|
7943 operator * (const Matrix& m, const SparseComplexMatrix& a) |
|
7944 { |
5681
|
7945 FULL_SPARSE_MUL (ComplexMatrix, Complex, Complex (0.,0.)); |
5429
|
7946 } |
|
7947 |
|
7948 ComplexMatrix |
|
7949 operator * (const ComplexMatrix& m, const SparseComplexMatrix& a) |
|
7950 { |
5681
|
7951 FULL_SPARSE_MUL (ComplexMatrix, Complex, Complex (0.,0.)); |
5429
|
7952 } |
|
7953 |
|
7954 ComplexMatrix |
|
7955 operator * (const SparseComplexMatrix& m, const Matrix& a) |
|
7956 { |
5681
|
7957 SPARSE_FULL_MUL (ComplexMatrix, double, Complex (0.,0.)); |
5429
|
7958 } |
|
7959 |
|
7960 ComplexMatrix |
|
7961 operator * (const SparseMatrix& m, const ComplexMatrix& a) |
|
7962 { |
5681
|
7963 SPARSE_FULL_MUL (ComplexMatrix, Complex, Complex (0.,0.)); |
5429
|
7964 } |
|
7965 |
|
7966 ComplexMatrix |
|
7967 operator * (const SparseComplexMatrix& m, const ComplexMatrix& a) |
|
7968 { |
5681
|
7969 SPARSE_FULL_MUL (ComplexMatrix, Complex, Complex (0.,0.)); |
5429
|
7970 } |
|
7971 |
5775
|
7972 // FIXME -- it would be nice to share code among the min/max |
5164
|
7973 // functions below. |
|
7974 |
|
7975 #define EMPTY_RETURN_CHECK(T) \ |
|
7976 if (nr == 0 || nc == 0) \ |
|
7977 return T (nr, nc); |
|
7978 |
|
7979 SparseComplexMatrix |
|
7980 min (const Complex& c, const SparseComplexMatrix& m) |
|
7981 { |
|
7982 SparseComplexMatrix result; |
|
7983 |
5275
|
7984 octave_idx_type nr = m.rows (); |
|
7985 octave_idx_type nc = m.columns (); |
5164
|
7986 |
|
7987 EMPTY_RETURN_CHECK (SparseComplexMatrix); |
|
7988 |
|
7989 if (abs(c) == 0.) |
|
7990 return SparseComplexMatrix (nr, nc); |
|
7991 else |
|
7992 { |
|
7993 result = SparseComplexMatrix (m); |
|
7994 |
5275
|
7995 for (octave_idx_type j = 0; j < nc; j++) |
|
7996 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
7997 result.data(i) = xmin(c, m.data(i)); |
|
7998 } |
|
7999 |
|
8000 return result; |
|
8001 } |
|
8002 |
|
8003 SparseComplexMatrix |
|
8004 min (const SparseComplexMatrix& m, const Complex& c) |
|
8005 { |
|
8006 return min (c, m); |
|
8007 } |
|
8008 |
|
8009 SparseComplexMatrix |
|
8010 min (const SparseComplexMatrix& a, const SparseComplexMatrix& b) |
|
8011 { |
|
8012 SparseComplexMatrix r; |
|
8013 |
|
8014 if ((a.rows() == b.rows()) && (a.cols() == b.cols())) |
|
8015 { |
5275
|
8016 octave_idx_type a_nr = a.rows (); |
|
8017 octave_idx_type a_nc = a.cols (); |
|
8018 |
|
8019 octave_idx_type b_nr = b.rows (); |
|
8020 octave_idx_type b_nc = b.cols (); |
5164
|
8021 |
5681
|
8022 if (a_nr == 0 || b_nc == 0 || a.nnz () == 0 || b.nnz () == 0) |
5164
|
8023 return SparseComplexMatrix (a_nr, a_nc); |
|
8024 |
|
8025 if (a_nr != b_nr || a_nc != b_nc) |
|
8026 gripe_nonconformant ("min", a_nr, a_nc, b_nr, b_nc); |
|
8027 else |
|
8028 { |
5681
|
8029 r = SparseComplexMatrix (a_nr, a_nc, (a.nnz () + b.nnz ())); |
5164
|
8030 |
5275
|
8031 octave_idx_type jx = 0; |
5164
|
8032 r.cidx (0) = 0; |
5275
|
8033 for (octave_idx_type i = 0 ; i < a_nc ; i++) |
5164
|
8034 { |
5275
|
8035 octave_idx_type ja = a.cidx(i); |
|
8036 octave_idx_type ja_max = a.cidx(i+1); |
5164
|
8037 bool ja_lt_max= ja < ja_max; |
|
8038 |
5275
|
8039 octave_idx_type jb = b.cidx(i); |
|
8040 octave_idx_type jb_max = b.cidx(i+1); |
5164
|
8041 bool jb_lt_max = jb < jb_max; |
|
8042 |
|
8043 while (ja_lt_max || jb_lt_max ) |
|
8044 { |
|
8045 OCTAVE_QUIT; |
|
8046 if ((! jb_lt_max) || |
|
8047 (ja_lt_max && (a.ridx(ja) < b.ridx(jb)))) |
|
8048 { |
|
8049 Complex tmp = xmin (a.data(ja), 0.); |
|
8050 if (tmp != 0.) |
|
8051 { |
|
8052 r.ridx(jx) = a.ridx(ja); |
|
8053 r.data(jx) = tmp; |
|
8054 jx++; |
|
8055 } |
|
8056 ja++; |
|
8057 ja_lt_max= ja < ja_max; |
|
8058 } |
|
8059 else if (( !ja_lt_max ) || |
|
8060 (jb_lt_max && (b.ridx(jb) < a.ridx(ja)) ) ) |
|
8061 { |
|
8062 Complex tmp = xmin (0., b.data(jb)); |
|
8063 if (tmp != 0.) |
|
8064 { |
|
8065 r.ridx(jx) = b.ridx(jb); |
|
8066 r.data(jx) = tmp; |
|
8067 jx++; |
|
8068 } |
|
8069 jb++; |
|
8070 jb_lt_max= jb < jb_max; |
|
8071 } |
|
8072 else |
|
8073 { |
|
8074 Complex tmp = xmin (a.data(ja), b.data(jb)); |
|
8075 if (tmp != 0.) |
|
8076 { |
|
8077 r.data(jx) = tmp; |
|
8078 r.ridx(jx) = a.ridx(ja); |
|
8079 jx++; |
|
8080 } |
|
8081 ja++; |
|
8082 ja_lt_max= ja < ja_max; |
|
8083 jb++; |
|
8084 jb_lt_max= jb < jb_max; |
|
8085 } |
|
8086 } |
|
8087 r.cidx(i+1) = jx; |
|
8088 } |
|
8089 |
|
8090 r.maybe_compress (); |
|
8091 } |
|
8092 } |
|
8093 else |
|
8094 (*current_liboctave_error_handler) ("matrix size mismatch"); |
|
8095 |
|
8096 return r; |
|
8097 } |
|
8098 |
|
8099 SparseComplexMatrix |
|
8100 max (const Complex& c, const SparseComplexMatrix& m) |
|
8101 { |
|
8102 SparseComplexMatrix result; |
|
8103 |
5275
|
8104 octave_idx_type nr = m.rows (); |
|
8105 octave_idx_type nc = m.columns (); |
5164
|
8106 |
|
8107 EMPTY_RETURN_CHECK (SparseComplexMatrix); |
|
8108 |
|
8109 // Count the number of non-zero elements |
|
8110 if (xmax(c, 0.) != 0.) |
|
8111 { |
|
8112 result = SparseComplexMatrix (nr, nc, c); |
5275
|
8113 for (octave_idx_type j = 0; j < nc; j++) |
|
8114 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
8115 result.xdata(m.ridx(i) + j * nr) = xmax (c, m.data(i)); |
|
8116 } |
|
8117 else |
|
8118 result = SparseComplexMatrix (m); |
|
8119 |
|
8120 return result; |
|
8121 } |
|
8122 |
|
8123 SparseComplexMatrix |
|
8124 max (const SparseComplexMatrix& m, const Complex& c) |
|
8125 { |
|
8126 return max (c, m); |
|
8127 } |
|
8128 |
|
8129 SparseComplexMatrix |
|
8130 max (const SparseComplexMatrix& a, const SparseComplexMatrix& b) |
|
8131 { |
|
8132 SparseComplexMatrix r; |
|
8133 |
|
8134 if ((a.rows() == b.rows()) && (a.cols() == b.cols())) |
|
8135 { |
5275
|
8136 octave_idx_type a_nr = a.rows (); |
|
8137 octave_idx_type a_nc = a.cols (); |
|
8138 |
|
8139 octave_idx_type b_nr = b.rows (); |
|
8140 octave_idx_type b_nc = b.cols (); |
5164
|
8141 |
|
8142 if (a_nr == 0 || b_nc == 0) |
|
8143 return SparseComplexMatrix (a_nr, a_nc); |
5681
|
8144 if (a.nnz () == 0) |
5164
|
8145 return SparseComplexMatrix (b); |
5681
|
8146 if (b.nnz () == 0) |
5164
|
8147 return SparseComplexMatrix (a); |
|
8148 |
|
8149 if (a_nr != b_nr || a_nc != b_nc) |
|
8150 gripe_nonconformant ("min", a_nr, a_nc, b_nr, b_nc); |
|
8151 else |
|
8152 { |
5681
|
8153 r = SparseComplexMatrix (a_nr, a_nc, (a.nnz () + b.nnz ())); |
5164
|
8154 |
5275
|
8155 octave_idx_type jx = 0; |
5164
|
8156 r.cidx (0) = 0; |
5275
|
8157 for (octave_idx_type i = 0 ; i < a_nc ; i++) |
5164
|
8158 { |
5275
|
8159 octave_idx_type ja = a.cidx(i); |
|
8160 octave_idx_type ja_max = a.cidx(i+1); |
5164
|
8161 bool ja_lt_max= ja < ja_max; |
|
8162 |
5275
|
8163 octave_idx_type jb = b.cidx(i); |
|
8164 octave_idx_type jb_max = b.cidx(i+1); |
5164
|
8165 bool jb_lt_max = jb < jb_max; |
|
8166 |
|
8167 while (ja_lt_max || jb_lt_max ) |
|
8168 { |
|
8169 OCTAVE_QUIT; |
|
8170 if ((! jb_lt_max) || |
|
8171 (ja_lt_max && (a.ridx(ja) < b.ridx(jb)))) |
|
8172 { |
|
8173 Complex tmp = xmax (a.data(ja), 0.); |
|
8174 if (tmp != 0.) |
|
8175 { |
|
8176 r.ridx(jx) = a.ridx(ja); |
|
8177 r.data(jx) = tmp; |
|
8178 jx++; |
|
8179 } |
|
8180 ja++; |
|
8181 ja_lt_max= ja < ja_max; |
|
8182 } |
|
8183 else if (( !ja_lt_max ) || |
|
8184 (jb_lt_max && (b.ridx(jb) < a.ridx(ja)) ) ) |
|
8185 { |
|
8186 Complex tmp = xmax (0., b.data(jb)); |
|
8187 if (tmp != 0.) |
|
8188 { |
|
8189 r.ridx(jx) = b.ridx(jb); |
|
8190 r.data(jx) = tmp; |
|
8191 jx++; |
|
8192 } |
|
8193 jb++; |
|
8194 jb_lt_max= jb < jb_max; |
|
8195 } |
|
8196 else |
|
8197 { |
|
8198 Complex tmp = xmax (a.data(ja), b.data(jb)); |
|
8199 if (tmp != 0.) |
|
8200 { |
|
8201 r.data(jx) = tmp; |
|
8202 r.ridx(jx) = a.ridx(ja); |
|
8203 jx++; |
|
8204 } |
|
8205 ja++; |
|
8206 ja_lt_max= ja < ja_max; |
|
8207 jb++; |
|
8208 jb_lt_max= jb < jb_max; |
|
8209 } |
|
8210 } |
|
8211 r.cidx(i+1) = jx; |
|
8212 } |
|
8213 |
|
8214 r.maybe_compress (); |
|
8215 } |
|
8216 } |
|
8217 else |
|
8218 (*current_liboctave_error_handler) ("matrix size mismatch"); |
|
8219 |
|
8220 return r; |
|
8221 } |
|
8222 |
|
8223 SPARSE_SMS_CMP_OPS (SparseComplexMatrix, 0.0, real, Complex, |
|
8224 0.0, real) |
|
8225 SPARSE_SMS_BOOL_OPS (SparseComplexMatrix, Complex, 0.0) |
|
8226 |
|
8227 SPARSE_SSM_CMP_OPS (Complex, 0.0, real, SparseComplexMatrix, |
|
8228 0.0, real) |
|
8229 SPARSE_SSM_BOOL_OPS (Complex, SparseComplexMatrix, 0.0) |
|
8230 |
|
8231 SPARSE_SMSM_CMP_OPS (SparseComplexMatrix, 0.0, real, SparseComplexMatrix, |
|
8232 0.0, real) |
|
8233 SPARSE_SMSM_BOOL_OPS (SparseComplexMatrix, SparseComplexMatrix, 0.0) |
|
8234 |
|
8235 /* |
|
8236 ;;; Local Variables: *** |
|
8237 ;;; mode: C++ *** |
|
8238 ;;; End: *** |
|
8239 */ |