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