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