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