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