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