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