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