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