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