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