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