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