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