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