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