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