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