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