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