5610
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
4 |
|
5 Octave is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License as published by the |
|
7 Free Software Foundation; either version 2, or (at your option) any |
|
8 later version. |
|
9 |
|
10 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with this program; see the file COPYING. If not, write to the |
|
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
18 Boston, MA 02110-1301, USA. |
|
19 |
|
20 */ |
|
21 |
|
22 #ifdef HAVE_CONFIG_H |
|
23 #include <config.h> |
|
24 #endif |
|
25 #include <vector> |
|
26 |
|
27 #include "lo-error.h" |
|
28 #include "SparseCmplxQR.h" |
|
29 |
5648
|
30 // Why did g++ 4.x stl_vector.h make |
|
31 // OCTAVE_LOCAL_BUFFER (double _Complex, buf, n) |
|
32 // an error ? |
|
33 #define OCTAVE_C99_COMPLEX(buf, n) \ |
|
34 OCTAVE_LOCAL_BUFFER (double, buf ## tmp, (2 * (n))); \ |
|
35 double _Complex *buf = reinterpret_cast<double _Complex *> (buf ## tmp); |
|
36 |
5681
|
37 #define OCTAVE_C99_ZERO (0. + 0.iF); |
|
38 |
5610
|
39 SparseComplexQR::SparseComplexQR_rep::SparseComplexQR_rep |
|
40 (const SparseComplexMatrix& a, int order) |
|
41 { |
|
42 #ifdef HAVE_CXSPARSE |
5648
|
43 CXSPARSE_ZNAME () A; |
5610
|
44 A.nzmax = a.nnz (); |
|
45 A.m = a.rows (); |
|
46 A.n = a.cols (); |
|
47 nrows = A.m; |
|
48 // Cast away const on A, with full knowledge that CSparse won't touch it |
|
49 // Prevents the methods below making a copy of the data. |
|
50 A.p = const_cast<octave_idx_type *>(a.cidx ()); |
|
51 A.i = const_cast<octave_idx_type *>(a.ridx ()); |
|
52 A.x = const_cast<double _Complex *>(reinterpret_cast<const double _Complex *> |
|
53 (a.data ())); |
|
54 A.nz = -1; |
|
55 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
56 S = CXSPARSE_ZNAME (_sqr) (&A, order, 1); |
|
57 N = CXSPARSE_ZNAME (_qr) (&A, S); |
5610
|
58 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
59 if (!N) |
|
60 (*current_liboctave_error_handler) |
|
61 ("SparseComplexQR: sparse matrix QR factorization filled"); |
|
62 count = 1; |
|
63 #else |
|
64 (*current_liboctave_error_handler) |
|
65 ("SparseComplexQR: sparse matrix QR factorization not implemented"); |
|
66 #endif |
|
67 } |
|
68 |
|
69 SparseComplexQR::SparseComplexQR_rep::~SparseComplexQR_rep (void) |
|
70 { |
|
71 #ifdef HAVE_CXSPARSE |
5648
|
72 CXSPARSE_ZNAME (_sfree) (S); |
|
73 CXSPARSE_ZNAME (_nfree) (N); |
5610
|
74 #endif |
|
75 } |
|
76 |
|
77 SparseComplexMatrix |
|
78 SparseComplexQR::SparseComplexQR_rep::V (void) const |
|
79 { |
|
80 #ifdef HAVE_CXSPARSE |
|
81 // Drop zeros from V and sort |
5775
|
82 // FIXME Is the double transpose to sort necessary? |
5610
|
83 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
84 CXSPARSE_ZNAME (_dropzeros) (N->L); |
|
85 CXSPARSE_ZNAME () *D = CXSPARSE_ZNAME (_transpose) (N->L, 1); |
|
86 CXSPARSE_ZNAME (_spfree) (N->L); |
|
87 N->L = CXSPARSE_ZNAME (_transpose) (D, 1); |
|
88 CXSPARSE_ZNAME (_spfree) (D); |
5610
|
89 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
90 |
|
91 octave_idx_type nc = N->L->n; |
|
92 octave_idx_type nz = N->L->nzmax; |
|
93 SparseComplexMatrix ret (N->L->m, nc, nz); |
|
94 for (octave_idx_type j = 0; j < nc+1; j++) |
|
95 ret.xcidx (j) = N->L->p[j]; |
|
96 for (octave_idx_type j = 0; j < nz; j++) |
|
97 { |
|
98 ret.xridx (j) = N->L->i[j]; |
|
99 ret.xdata (j) = reinterpret_cast<Complex *>(N->L->x)[j]; |
|
100 } |
|
101 return ret; |
|
102 #else |
|
103 return SparseComplexMatrix (); |
|
104 #endif |
|
105 } |
|
106 |
|
107 ColumnVector |
|
108 SparseComplexQR::SparseComplexQR_rep::Pinv (void) const |
|
109 { |
|
110 #ifdef HAVE_CXSPARSE |
|
111 ColumnVector ret(N->L->m); |
|
112 for (octave_idx_type i = 0; i < N->L->m; i++) |
|
113 ret.xelem(i) = S->Pinv[i]; |
|
114 return ret; |
|
115 #else |
|
116 return ColumnVector (); |
|
117 #endif |
|
118 } |
|
119 |
|
120 ColumnVector |
|
121 SparseComplexQR::SparseComplexQR_rep::P (void) const |
|
122 { |
|
123 #ifdef HAVE_CXSPARSE |
|
124 ColumnVector ret(N->L->m); |
|
125 for (octave_idx_type i = 0; i < N->L->m; i++) |
|
126 ret.xelem(S->Pinv[i]) = i; |
|
127 return ret; |
|
128 #else |
|
129 return ColumnVector (); |
|
130 #endif |
|
131 } |
|
132 |
|
133 SparseComplexMatrix |
|
134 SparseComplexQR::SparseComplexQR_rep::R (const bool econ) const |
|
135 { |
|
136 #ifdef HAVE_CXSPARSE |
|
137 // Drop zeros from R and sort |
5775
|
138 // FIXME Is the double transpose to sort necessary? |
5610
|
139 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
140 CXSPARSE_ZNAME (_dropzeros) (N->U); |
|
141 CXSPARSE_ZNAME () *D = CXSPARSE_ZNAME (_transpose) (N->U, 1); |
|
142 CXSPARSE_ZNAME (_spfree) (N->U); |
|
143 N->U = CXSPARSE_ZNAME (_transpose) (D, 1); |
|
144 CXSPARSE_ZNAME (_spfree) (D); |
5610
|
145 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
146 |
|
147 octave_idx_type nc = N->U->n; |
|
148 octave_idx_type nz = N->U->nzmax; |
|
149 SparseComplexMatrix ret ((econ ? (nc > nrows ? nrows : nc) : nrows), nc, nz); |
|
150 for (octave_idx_type j = 0; j < nc+1; j++) |
|
151 ret.xcidx (j) = N->U->p[j]; |
|
152 for (octave_idx_type j = 0; j < nz; j++) |
|
153 { |
|
154 ret.xridx (j) = N->U->i[j]; |
|
155 ret.xdata (j) = reinterpret_cast<Complex *>(N->U->x)[j]; |
|
156 } |
|
157 return ret; |
|
158 #else |
|
159 return SparseComplexMatrix (); |
|
160 #endif |
|
161 } |
|
162 |
|
163 ComplexMatrix |
|
164 SparseComplexQR::SparseComplexQR_rep::C (const ComplexMatrix &b) const |
|
165 { |
|
166 #ifdef HAVE_CXSPARSE |
|
167 octave_idx_type b_nr = b.rows(); |
|
168 octave_idx_type b_nc = b.cols(); |
|
169 octave_idx_type nc = N->L->n; |
|
170 octave_idx_type nr = nrows; |
|
171 const double _Complex *bvec = |
|
172 reinterpret_cast<const double _Complex *>(b.fortran_vec()); |
|
173 ComplexMatrix ret(b_nr,b_nc); |
|
174 Complex *vec = ret.fortran_vec(); |
|
175 if (nr < 1 || nc < 1 || nr != b_nr) |
|
176 (*current_liboctave_error_handler) ("matrix dimension mismatch"); |
|
177 else |
|
178 { |
|
179 OCTAVE_LOCAL_BUFFER (Complex, buf, S->m2); |
|
180 for (volatile octave_idx_type j = 0, idx = 0; j < b_nc; j++, idx+=b_nr) |
|
181 { |
|
182 OCTAVE_QUIT; |
|
183 volatile octave_idx_type nm = (nr < nc ? nr : nc); |
|
184 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
185 CXSPARSE_ZNAME (_ipvec) (b_nr, S->Pinv, bvec + idx, |
5610
|
186 reinterpret_cast<double _Complex *>(buf)); |
|
187 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
188 for (volatile octave_idx_type i = 0; i < nm; i++) |
|
189 { |
|
190 OCTAVE_QUIT; |
|
191 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
192 CXSPARSE_ZNAME (_happly) |
5610
|
193 (N->L, i, N->B[i], reinterpret_cast<double _Complex *>(buf)); |
|
194 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
195 } |
|
196 for (octave_idx_type i = 0; i < b_nr; i++) |
|
197 vec[i+idx] = buf[i]; |
|
198 } |
|
199 } |
|
200 return ret; |
|
201 #else |
|
202 return ComplexMatrix (); |
|
203 #endif |
|
204 } |
|
205 |
|
206 ComplexMatrix |
|
207 qrsolve(const SparseComplexMatrix&a, const Matrix &b, octave_idx_type &info) |
|
208 { |
|
209 #ifdef HAVE_CXSPARSE |
|
210 octave_idx_type nr = a.rows(); |
|
211 octave_idx_type nc = a.cols(); |
|
212 octave_idx_type b_nc = b.cols(); |
|
213 octave_idx_type b_nr = b.rows(); |
|
214 ComplexMatrix x; |
|
215 info = 0; |
|
216 |
|
217 if (nr < 1 || nc < 1 || nr != b_nr) |
|
218 (*current_liboctave_error_handler) |
|
219 ("matrix dimension mismatch in solution of minimum norm problem"); |
|
220 else if (nr >= nc) |
|
221 { |
|
222 SparseComplexQR q (a, 2); |
|
223 if (! q.ok ()) |
|
224 { |
|
225 info = -1; |
|
226 return ComplexMatrix(); |
|
227 } |
|
228 x.resize(nc, b_nc); |
|
229 double _Complex *vec = reinterpret_cast<double _Complex *> |
|
230 (x.fortran_vec()); |
5648
|
231 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610
|
232 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
|
233 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
234 { |
|
235 OCTAVE_QUIT; |
|
236 for (octave_idx_type j = 0; j < b_nr; j++) |
|
237 Xx[j] = b.xelem(j,i); |
5681
|
238 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
|
239 buf[j] = OCTAVE_C99_ZERO; |
5610
|
240 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
241 CXSPARSE_ZNAME (_ipvec) |
5610
|
242 (nr, q.S()->Pinv, reinterpret_cast<double _Complex *>(Xx), buf); |
|
243 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
244 for (volatile octave_idx_type j = 0; j < nc; j++) |
|
245 { |
|
246 OCTAVE_QUIT; |
|
247 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
248 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610
|
249 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
250 } |
|
251 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
252 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
|
253 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, vec + idx); |
5610
|
254 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
255 } |
|
256 } |
|
257 else |
|
258 { |
|
259 SparseComplexMatrix at = a.hermitian(); |
|
260 SparseComplexQR q (at, 2); |
|
261 if (! q.ok ()) |
|
262 { |
|
263 info = -1; |
|
264 return ComplexMatrix(); |
|
265 } |
|
266 x.resize(nc, b_nc); |
|
267 double _Complex *vec = reinterpret_cast<double _Complex *> |
|
268 (x.fortran_vec()); |
5681
|
269 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
|
270 OCTAVE_C99_COMPLEX (buf, nbuf); |
5610
|
271 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
|
272 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
|
273 for (octave_idx_type i = 0; i < nr; i++) |
|
274 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); |
|
275 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
276 { |
|
277 OCTAVE_QUIT; |
|
278 for (octave_idx_type j = 0; j < b_nr; j++) |
|
279 Xx[j] = b.xelem(j,i); |
5681
|
280 for (octave_idx_type j = nr; j < nbuf; j++) |
|
281 buf[j] = OCTAVE_C99_ZERO; |
5610
|
282 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
283 CXSPARSE_ZNAME (_pvec) |
5610
|
284 (nr, q.S()->Q, reinterpret_cast<double _Complex *>(Xx), buf); |
5648
|
285 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610
|
286 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
287 for (volatile octave_idx_type j = nr-1; j >= 0; j--) |
|
288 { |
|
289 OCTAVE_QUIT; |
|
290 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
291 |
5648
|
292 CXSPARSE_ZNAME (_happly) |
5610
|
293 (q.N()->L, j, reinterpret_cast<double _Complex *>(B)[j], buf); |
|
294 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
295 } |
|
296 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
297 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, vec + idx); |
5610
|
298 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
299 } |
|
300 } |
|
301 |
|
302 return x; |
|
303 #else |
|
304 return ComplexMatrix (); |
|
305 #endif |
|
306 } |
|
307 |
|
308 SparseComplexMatrix |
|
309 qrsolve(const SparseComplexMatrix&a, const SparseMatrix &b, octave_idx_type &info) |
|
310 { |
|
311 #ifdef HAVE_CXSPARSE |
|
312 octave_idx_type nr = a.rows(); |
|
313 octave_idx_type nc = a.cols(); |
|
314 octave_idx_type b_nc = b.cols(); |
|
315 octave_idx_type b_nr = b.rows(); |
|
316 SparseComplexMatrix x; |
|
317 volatile octave_idx_type ii, x_nz; |
|
318 info = 0; |
|
319 |
|
320 if (nr < 1 || nc < 1 || nr != b_nr) |
|
321 (*current_liboctave_error_handler) |
|
322 ("matrix dimension mismatch in solution of minimum norm problem"); |
|
323 else if (nr >= nc) |
|
324 { |
|
325 SparseComplexQR q (a, 2); |
|
326 if (! q.ok ()) |
|
327 { |
|
328 info = -1; |
|
329 return SparseComplexMatrix(); |
|
330 } |
|
331 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
|
332 x.xcidx(0) = 0; |
|
333 x_nz = b.nzmax(); |
|
334 ii = 0; |
|
335 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5648
|
336 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610
|
337 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
338 { |
|
339 OCTAVE_QUIT; |
|
340 for (octave_idx_type j = 0; j < b_nr; j++) |
|
341 Xx[j] = b.xelem(j,i); |
5681
|
342 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
|
343 buf[j] = OCTAVE_C99_ZERO; |
5610
|
344 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
345 CXSPARSE_ZNAME (_ipvec) |
5610
|
346 (nr, q.S()->Pinv, reinterpret_cast<double _Complex *>(Xx), buf); |
|
347 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
348 for (volatile octave_idx_type j = 0; j < nc; j++) |
|
349 { |
|
350 OCTAVE_QUIT; |
|
351 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
352 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610
|
353 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
354 } |
|
355 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
356 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
|
357 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, |
5610
|
358 reinterpret_cast<double _Complex *>(Xx)); |
|
359 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
360 |
|
361 for (octave_idx_type j = 0; j < nc; j++) |
|
362 { |
|
363 Complex tmp = Xx[j]; |
|
364 if (tmp != 0.0) |
|
365 { |
|
366 if (ii == x_nz) |
|
367 { |
|
368 // Resize the sparse matrix |
|
369 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; |
|
370 sz = (sz > 10 ? sz : 10) + x_nz; |
|
371 x.change_capacity (sz); |
|
372 x_nz = sz; |
|
373 } |
|
374 x.xdata(ii) = tmp; |
|
375 x.xridx(ii++) = j; |
|
376 } |
|
377 } |
|
378 x.xcidx(i+1) = ii; |
|
379 } |
|
380 } |
|
381 else |
|
382 { |
|
383 SparseComplexMatrix at = a.hermitian(); |
|
384 SparseComplexQR q (at, 2); |
|
385 if (! q.ok ()) |
|
386 { |
|
387 info = -1; |
|
388 return SparseComplexMatrix(); |
|
389 } |
|
390 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
|
391 x.xcidx(0) = 0; |
|
392 x_nz = b.nzmax(); |
|
393 ii = 0; |
5681
|
394 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
5610
|
395 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5681
|
396 OCTAVE_C99_COMPLEX (buf, nbuf); |
5610
|
397 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
|
398 for (octave_idx_type i = 0; i < nr; i++) |
|
399 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); |
|
400 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
401 { |
|
402 OCTAVE_QUIT; |
|
403 for (octave_idx_type j = 0; j < b_nr; j++) |
|
404 Xx[j] = b.xelem(j,i); |
5681
|
405 for (octave_idx_type j = nr; j < nbuf; j++) |
|
406 buf[j] = OCTAVE_C99_ZERO; |
5610
|
407 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
408 CXSPARSE_ZNAME (_pvec) |
5610
|
409 (nr, q.S()->Q, reinterpret_cast<double _Complex *>(Xx), buf); |
5648
|
410 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610
|
411 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
412 for (volatile octave_idx_type j = nr-1; j >= 0; j--) |
|
413 { |
|
414 OCTAVE_QUIT; |
|
415 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
416 CXSPARSE_ZNAME (_happly) |
5610
|
417 (q.N()->L, j, reinterpret_cast<double _Complex *>(B)[j], buf); |
|
418 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
419 } |
|
420 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
421 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, |
5610
|
422 reinterpret_cast<double _Complex *>(Xx)); |
|
423 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
424 |
|
425 for (octave_idx_type j = 0; j < nc; j++) |
|
426 { |
|
427 Complex tmp = Xx[j]; |
|
428 if (tmp != 0.0) |
|
429 { |
|
430 if (ii == x_nz) |
|
431 { |
|
432 // Resize the sparse matrix |
|
433 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; |
|
434 sz = (sz > 10 ? sz : 10) + x_nz; |
|
435 x.change_capacity (sz); |
|
436 x_nz = sz; |
|
437 } |
|
438 x.xdata(ii) = tmp; |
|
439 x.xridx(ii++) = j; |
|
440 } |
|
441 } |
|
442 x.xcidx(i+1) = ii; |
|
443 } |
|
444 } |
|
445 |
|
446 x.maybe_compress (); |
|
447 return x; |
|
448 #else |
|
449 return SparseComplexMatrix (); |
|
450 #endif |
|
451 } |
|
452 |
|
453 ComplexMatrix |
|
454 qrsolve(const SparseComplexMatrix&a, const ComplexMatrix &b, octave_idx_type &info) |
|
455 { |
|
456 #ifdef HAVE_CXSPARSE |
|
457 octave_idx_type nr = a.rows(); |
|
458 octave_idx_type nc = a.cols(); |
|
459 octave_idx_type b_nc = b.cols(); |
|
460 octave_idx_type b_nr = b.rows(); |
|
461 const double _Complex *bvec = |
|
462 reinterpret_cast<const double _Complex *>(b.fortran_vec()); |
|
463 ComplexMatrix x; |
|
464 info = 0; |
|
465 |
|
466 if (nr < 1 || nc < 1 || nr != b_nr) |
|
467 (*current_liboctave_error_handler) |
|
468 ("matrix dimension mismatch in solution of minimum norm problem"); |
|
469 else if (nr >= nc) |
|
470 { |
|
471 SparseComplexQR q (a, 2); |
|
472 if (! q.ok ()) |
|
473 { |
|
474 info = -1; |
|
475 return ComplexMatrix(); |
|
476 } |
|
477 x.resize(nc, b_nc); |
|
478 double _Complex *vec = reinterpret_cast<double _Complex *> |
|
479 (x.fortran_vec()); |
5648
|
480 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610
|
481 for (volatile octave_idx_type i = 0, idx = 0, bidx = 0; i < b_nc; |
|
482 i++, idx+=nc, bidx+=b_nr) |
|
483 { |
|
484 OCTAVE_QUIT; |
5681
|
485 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
|
486 buf[j] = OCTAVE_C99_ZERO; |
5610
|
487 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
488 CXSPARSE_ZNAME (_ipvec) (nr, q.S()->Pinv, bvec + bidx, buf); |
5610
|
489 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
490 for (volatile octave_idx_type j = 0; j < nc; j++) |
|
491 { |
|
492 OCTAVE_QUIT; |
|
493 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
494 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610
|
495 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
496 } |
|
497 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
498 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
|
499 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, vec + idx); |
5610
|
500 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
501 } |
|
502 } |
|
503 else |
|
504 { |
|
505 SparseComplexMatrix at = a.hermitian(); |
|
506 SparseComplexQR q (at, 2); |
|
507 if (! q.ok ()) |
|
508 { |
|
509 info = -1; |
|
510 return ComplexMatrix(); |
|
511 } |
|
512 x.resize(nc, b_nc); |
|
513 double _Complex *vec = reinterpret_cast<double _Complex *> |
|
514 (x.fortran_vec()); |
5681
|
515 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
|
516 OCTAVE_C99_COMPLEX (buf, nbuf); |
5610
|
517 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
|
518 for (octave_idx_type i = 0; i < nr; i++) |
|
519 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); |
|
520 for (volatile octave_idx_type i = 0, idx = 0, bidx = 0; i < b_nc; |
|
521 i++, idx+=nc, bidx+=b_nr) |
|
522 { |
|
523 OCTAVE_QUIT; |
5681
|
524 for (octave_idx_type j = nr; j < nbuf; j++) |
|
525 buf[j] = OCTAVE_C99_ZERO; |
5610
|
526 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
527 CXSPARSE_ZNAME (_pvec) (nr, q.S()->Q, bvec + bidx, buf); |
|
528 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610
|
529 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
530 for (volatile octave_idx_type j = nr-1; j >= 0; j--) |
|
531 { |
|
532 OCTAVE_QUIT; |
|
533 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
534 CXSPARSE_ZNAME (_happly) |
5610
|
535 (q.N()->L, j, reinterpret_cast<double _Complex *>(B)[j], buf); |
|
536 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
537 } |
|
538 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
539 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, vec + idx); |
5610
|
540 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
541 } |
|
542 } |
|
543 |
|
544 return x; |
|
545 #else |
|
546 return ComplexMatrix (); |
|
547 #endif |
|
548 } |
|
549 |
|
550 SparseComplexMatrix |
|
551 qrsolve(const SparseComplexMatrix&a, const SparseComplexMatrix &b, octave_idx_type &info) |
|
552 { |
|
553 #ifdef HAVE_CXSPARSE |
|
554 octave_idx_type nr = a.rows(); |
|
555 octave_idx_type nc = a.cols(); |
|
556 octave_idx_type b_nc = b.cols(); |
|
557 octave_idx_type b_nr = b.rows(); |
|
558 SparseComplexMatrix x; |
|
559 volatile octave_idx_type ii, x_nz; |
|
560 info = 0; |
|
561 |
|
562 if (nr < 1 || nc < 1 || nr != b_nr) |
|
563 (*current_liboctave_error_handler) |
|
564 ("matrix dimension mismatch in solution of minimum norm problem"); |
|
565 else if (nr >= nc) |
|
566 { |
|
567 SparseComplexQR q (a, 2); |
|
568 if (! q.ok ()) |
|
569 { |
|
570 info = -1; |
|
571 return SparseComplexMatrix(); |
|
572 } |
|
573 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
|
574 x.xcidx(0) = 0; |
|
575 x_nz = b.nzmax(); |
|
576 ii = 0; |
|
577 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5648
|
578 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610
|
579 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
580 { |
|
581 OCTAVE_QUIT; |
|
582 for (octave_idx_type j = 0; j < b_nr; j++) |
|
583 Xx[j] = b.xelem(j,i); |
5681
|
584 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
|
585 buf[j] = OCTAVE_C99_ZERO; |
5610
|
586 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
587 CXSPARSE_ZNAME (_ipvec) |
5610
|
588 (nr, q.S()->Pinv, reinterpret_cast<double _Complex *>(Xx), buf); |
|
589 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
590 for (volatile octave_idx_type j = 0; j < nc; j++) |
|
591 { |
|
592 OCTAVE_QUIT; |
|
593 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
594 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610
|
595 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
596 } |
|
597 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
598 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
|
599 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, |
5610
|
600 reinterpret_cast<double _Complex *>(Xx)); |
|
601 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
602 |
|
603 for (octave_idx_type j = 0; j < nc; j++) |
|
604 { |
|
605 Complex tmp = Xx[j]; |
|
606 if (tmp != 0.0) |
|
607 { |
|
608 if (ii == x_nz) |
|
609 { |
|
610 // Resize the sparse matrix |
|
611 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; |
|
612 sz = (sz > 10 ? sz : 10) + x_nz; |
|
613 x.change_capacity (sz); |
|
614 x_nz = sz; |
|
615 } |
|
616 x.xdata(ii) = tmp; |
|
617 x.xridx(ii++) = j; |
|
618 } |
|
619 } |
|
620 x.xcidx(i+1) = ii; |
|
621 } |
|
622 } |
|
623 else |
|
624 { |
|
625 SparseComplexMatrix at = a.hermitian(); |
|
626 SparseComplexQR q (at, 2); |
|
627 if (! q.ok ()) |
|
628 { |
|
629 info = -1; |
|
630 return SparseComplexMatrix(); |
|
631 } |
|
632 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
|
633 x.xcidx(0) = 0; |
|
634 x_nz = b.nzmax(); |
|
635 ii = 0; |
5681
|
636 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
5610
|
637 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5681
|
638 OCTAVE_C99_COMPLEX (buf, nbuf); |
5610
|
639 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
|
640 for (octave_idx_type i = 0; i < nr; i++) |
|
641 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); |
|
642 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
|
643 { |
|
644 OCTAVE_QUIT; |
|
645 for (octave_idx_type j = 0; j < b_nr; j++) |
|
646 Xx[j] = b.xelem(j,i); |
5681
|
647 for (octave_idx_type j = nr; j < nbuf; j++) |
|
648 buf[j] = OCTAVE_C99_ZERO; |
5610
|
649 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
650 CXSPARSE_ZNAME (_pvec) |
5610
|
651 (nr, q.S()->Q, reinterpret_cast<double _Complex *>(Xx), buf); |
5648
|
652 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610
|
653 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
654 for (volatile octave_idx_type j = nr-1; j >= 0; j--) |
|
655 { |
|
656 OCTAVE_QUIT; |
|
657 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
658 CXSPARSE_ZNAME (_happly) |
5610
|
659 (q.N()->L, j, reinterpret_cast<double _Complex *>(B)[j], buf); |
|
660 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
661 } |
|
662 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648
|
663 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, |
5610
|
664 reinterpret_cast<double _Complex *>(Xx)); |
|
665 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
666 |
|
667 for (octave_idx_type j = 0; j < nc; j++) |
|
668 { |
|
669 Complex tmp = Xx[j]; |
|
670 if (tmp != 0.0) |
|
671 { |
|
672 if (ii == x_nz) |
|
673 { |
|
674 // Resize the sparse matrix |
|
675 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; |
|
676 sz = (sz > 10 ? sz : 10) + x_nz; |
|
677 x.change_capacity (sz); |
|
678 x_nz = sz; |
|
679 } |
|
680 x.xdata(ii) = tmp; |
|
681 x.xridx(ii++) = j; |
|
682 } |
|
683 } |
|
684 x.xcidx(i+1) = ii; |
|
685 } |
|
686 } |
|
687 |
|
688 x.maybe_compress (); |
|
689 return x; |
|
690 #else |
|
691 return SparseComplexMatrix (); |
|
692 #endif |
|
693 } |
|
694 |
|
695 /* |
|
696 ;;; Local Variables: *** |
|
697 ;;; mode: C++ *** |
|
698 ;;; End: *** |
|
699 */ |