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