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