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