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