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