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