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