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