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