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