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