Mercurial > hg > octave-lyh
annotate liboctave/SparseCmplxQR.cc @ 7505:f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 20 Feb 2008 15:52:11 -0500 |
parents | a1dbe9d80eee |
children | 82be108cc558 |
rev | line source |
---|---|
5610 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2005, 2006, 2007 David Bateman |
5610 | 4 |
7016 | 5 This file is part of Octave. |
6 | |
5610 | 7 Octave is free software; you can redistribute it and/or modify it |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
5610 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
5610 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 #include <vector> | |
27 | |
28 #include "lo-error.h" | |
29 #include "SparseCmplxQR.h" | |
30 | |
6685 | 31 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER < 2)) || (CS_VER < 2)) |
32 typedef double _Complex cs_complex_t; | |
33 | |
5648 | 34 // Why did g++ 4.x stl_vector.h make |
6685 | 35 // OCTAVE_LOCAL_BUFFER (cs_complex_t, buf, n) |
5648 | 36 // an error ? |
37 #define OCTAVE_C99_COMPLEX(buf, n) \ | |
38 OCTAVE_LOCAL_BUFFER (double, buf ## tmp, (2 * (n))); \ | |
6685 | 39 cs_complex_t *buf = reinterpret_cast<cs_complex_t *> (buf ## tmp); |
5648 | 40 |
6719 | 41 #define OCTAVE_C99_ZERO (0. + 0.iF) |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
42 #define OCTAVE_C99_ONE (1. + 0.iF) |
6685 | 43 #else |
44 #define OCTAVE_C99_COMPLEX(buf, n) \ | |
45 OCTAVE_LOCAL_BUFFER (cs_complex_t, buf, (n)); | |
6719 | 46 #define OCTAVE_C99_ZERO cs_complex_t(0., 0.); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
47 #define OCTAVE_C99_ONE cs_complex_t(1., 0.); |
6685 | 48 #endif |
49 | |
5610 | 50 SparseComplexQR::SparseComplexQR_rep::SparseComplexQR_rep |
6513 | 51 (GCC_ATTR_UNUSED const SparseComplexMatrix& a, GCC_ATTR_UNUSED int order) |
5610 | 52 { |
53 #ifdef HAVE_CXSPARSE | |
5648 | 54 CXSPARSE_ZNAME () A; |
5610 | 55 A.nzmax = a.nnz (); |
56 A.m = a.rows (); | |
57 A.n = a.cols (); | |
58 nrows = A.m; | |
59 // Cast away const on A, with full knowledge that CSparse won't touch it | |
60 // Prevents the methods below making a copy of the data. | |
61 A.p = const_cast<octave_idx_type *>(a.cidx ()); | |
62 A.i = const_cast<octave_idx_type *>(a.ridx ()); | |
6685 | 63 A.x = const_cast<cs_complex_t *>(reinterpret_cast<const cs_complex_t *> |
5610 | 64 (a.data ())); |
65 A.nz = -1; | |
66 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 67 #if defined(CS_VER) && (CS_VER >= 2) |
68 S = CXSPARSE_ZNAME (_sqr) (order, &A, 1); | |
69 #else | |
70 S = CXSPARSE_ZNAME (_sqr) (&A, order - 1, 1); | |
71 #endif | |
5648 | 72 N = CXSPARSE_ZNAME (_qr) (&A, S); |
5610 | 73 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
74 if (!N) | |
75 (*current_liboctave_error_handler) | |
76 ("SparseComplexQR: sparse matrix QR factorization filled"); | |
77 count = 1; | |
78 #else | |
79 (*current_liboctave_error_handler) | |
80 ("SparseComplexQR: sparse matrix QR factorization not implemented"); | |
81 #endif | |
82 } | |
83 | |
84 SparseComplexQR::SparseComplexQR_rep::~SparseComplexQR_rep (void) | |
85 { | |
86 #ifdef HAVE_CXSPARSE | |
5648 | 87 CXSPARSE_ZNAME (_sfree) (S); |
88 CXSPARSE_ZNAME (_nfree) (N); | |
5610 | 89 #endif |
90 } | |
91 | |
92 SparseComplexMatrix | |
93 SparseComplexQR::SparseComplexQR_rep::V (void) const | |
94 { | |
95 #ifdef HAVE_CXSPARSE | |
96 // Drop zeros from V and sort | |
5775 | 97 // FIXME Is the double transpose to sort necessary? |
5610 | 98 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648 | 99 CXSPARSE_ZNAME (_dropzeros) (N->L); |
100 CXSPARSE_ZNAME () *D = CXSPARSE_ZNAME (_transpose) (N->L, 1); | |
101 CXSPARSE_ZNAME (_spfree) (N->L); | |
102 N->L = CXSPARSE_ZNAME (_transpose) (D, 1); | |
103 CXSPARSE_ZNAME (_spfree) (D); | |
5610 | 104 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
105 | |
106 octave_idx_type nc = N->L->n; | |
107 octave_idx_type nz = N->L->nzmax; | |
108 SparseComplexMatrix ret (N->L->m, nc, nz); | |
109 for (octave_idx_type j = 0; j < nc+1; j++) | |
110 ret.xcidx (j) = N->L->p[j]; | |
111 for (octave_idx_type j = 0; j < nz; j++) | |
112 { | |
113 ret.xridx (j) = N->L->i[j]; | |
114 ret.xdata (j) = reinterpret_cast<Complex *>(N->L->x)[j]; | |
115 } | |
116 return ret; | |
117 #else | |
118 return SparseComplexMatrix (); | |
119 #endif | |
120 } | |
121 | |
122 ColumnVector | |
123 SparseComplexQR::SparseComplexQR_rep::Pinv (void) const | |
124 { | |
125 #ifdef HAVE_CXSPARSE | |
126 ColumnVector ret(N->L->m); | |
127 for (octave_idx_type i = 0; i < N->L->m; i++) | |
5792 | 128 #if defined(CS_VER) && (CS_VER >= 2) |
129 ret.xelem(i) = S->pinv[i]; | |
130 #else | |
5610 | 131 ret.xelem(i) = S->Pinv[i]; |
5792 | 132 #endif |
5610 | 133 return ret; |
134 #else | |
135 return ColumnVector (); | |
136 #endif | |
137 } | |
138 | |
139 ColumnVector | |
140 SparseComplexQR::SparseComplexQR_rep::P (void) const | |
141 { | |
142 #ifdef HAVE_CXSPARSE | |
143 ColumnVector ret(N->L->m); | |
144 for (octave_idx_type i = 0; i < N->L->m; i++) | |
5792 | 145 #if defined(CS_VER) && (CS_VER >= 2) |
146 ret.xelem(S->pinv[i]) = i; | |
147 #else | |
5610 | 148 ret.xelem(S->Pinv[i]) = i; |
5792 | 149 #endif |
5610 | 150 return ret; |
151 #else | |
152 return ColumnVector (); | |
153 #endif | |
154 } | |
155 | |
156 SparseComplexMatrix | |
157 SparseComplexQR::SparseComplexQR_rep::R (const bool econ) const | |
158 { | |
159 #ifdef HAVE_CXSPARSE | |
160 // Drop zeros from R and sort | |
5775 | 161 // FIXME Is the double transpose to sort necessary? |
5610 | 162 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5648 | 163 CXSPARSE_ZNAME (_dropzeros) (N->U); |
164 CXSPARSE_ZNAME () *D = CXSPARSE_ZNAME (_transpose) (N->U, 1); | |
165 CXSPARSE_ZNAME (_spfree) (N->U); | |
166 N->U = CXSPARSE_ZNAME (_transpose) (D, 1); | |
167 CXSPARSE_ZNAME (_spfree) (D); | |
5610 | 168 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
169 | |
170 octave_idx_type nc = N->U->n; | |
171 octave_idx_type nz = N->U->nzmax; | |
172 SparseComplexMatrix ret ((econ ? (nc > nrows ? nrows : nc) : nrows), nc, nz); | |
173 for (octave_idx_type j = 0; j < nc+1; j++) | |
174 ret.xcidx (j) = N->U->p[j]; | |
175 for (octave_idx_type j = 0; j < nz; j++) | |
176 { | |
177 ret.xridx (j) = N->U->i[j]; | |
178 ret.xdata (j) = reinterpret_cast<Complex *>(N->U->x)[j]; | |
179 } | |
180 return ret; | |
181 #else | |
182 return SparseComplexMatrix (); | |
183 #endif | |
184 } | |
185 | |
186 ComplexMatrix | |
187 SparseComplexQR::SparseComplexQR_rep::C (const ComplexMatrix &b) const | |
188 { | |
189 #ifdef HAVE_CXSPARSE | |
190 octave_idx_type b_nr = b.rows(); | |
191 octave_idx_type b_nc = b.cols(); | |
192 octave_idx_type nc = N->L->n; | |
193 octave_idx_type nr = nrows; | |
6685 | 194 const cs_complex_t *bvec = |
195 reinterpret_cast<const cs_complex_t *>(b.fortran_vec()); | |
6924 | 196 ComplexMatrix ret(b_nr, b_nc); |
5610 | 197 Complex *vec = ret.fortran_vec(); |
6924 | 198 if (nr < 0 || nc < 0 || nr != b_nr) |
5610 | 199 (*current_liboctave_error_handler) ("matrix dimension mismatch"); |
6924 | 200 else if (nr == 0 || nc == 0 || b_nc == 0) |
201 ret = ComplexMatrix (nc, b_nc, Complex (0.0, 0.0)); | |
5610 | 202 else |
203 { | |
204 OCTAVE_LOCAL_BUFFER (Complex, buf, S->m2); | |
205 for (volatile octave_idx_type j = 0, idx = 0; j < b_nc; j++, idx+=b_nr) | |
206 { | |
207 OCTAVE_QUIT; | |
208 volatile octave_idx_type nm = (nr < nc ? nr : nc); | |
209 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 210 #if defined(CS_VER) && (CS_VER >= 2) |
211 CXSPARSE_ZNAME (_ipvec) | |
6685 | 212 (S->pinv, bvec + idx, reinterpret_cast<cs_complex_t *>(buf), b_nr); |
5792 | 213 #else |
214 CXSPARSE_ZNAME (_ipvec) | |
6685 | 215 (b_nr, S->Pinv, bvec + idx, reinterpret_cast<cs_complex_t *>(buf)); |
5792 | 216 #endif |
5610 | 217 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
218 for (volatile octave_idx_type i = 0; i < nm; i++) | |
219 { | |
220 OCTAVE_QUIT; | |
221 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 222 CXSPARSE_ZNAME (_happly) |
6685 | 223 (N->L, i, N->B[i], reinterpret_cast<cs_complex_t *>(buf)); |
5610 | 224 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
225 } | |
226 for (octave_idx_type i = 0; i < b_nr; i++) | |
227 vec[i+idx] = buf[i]; | |
228 } | |
229 } | |
230 return ret; | |
231 #else | |
232 return ComplexMatrix (); | |
233 #endif | |
234 } | |
235 | |
236 ComplexMatrix | |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
237 SparseComplexQR::SparseComplexQR_rep::Q (void) const |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
238 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
239 #ifdef HAVE_CXSPARSE |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
240 octave_idx_type nc = N->L->n; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
241 octave_idx_type nr = nrows; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
242 ComplexMatrix ret(nr, nr); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
243 Complex *vec = ret.fortran_vec(); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
244 if (nr < 0 || nc < 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
245 (*current_liboctave_error_handler) ("matrix dimension mismatch"); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
246 else if (nr == 0 || nc == 0) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
247 ret = ComplexMatrix (nc, nr, Complex (0.0, 0.0)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
248 else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
249 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
250 OCTAVE_C99_COMPLEX (bvec, nr); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
251 for (octave_idx_type i = 0; i < nr; i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
252 bvec[i] = OCTAVE_C99_ZERO; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
253 OCTAVE_LOCAL_BUFFER (Complex, buf, S->m2); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
254 for (volatile octave_idx_type j = 0, idx = 0; j < nr; j++, idx+=nr) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
255 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
256 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
257 bvec[j] = OCTAVE_C99_ONE; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
258 volatile octave_idx_type nm = (nr < nc ? nr : nc); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
259 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
260 #if defined(CS_VER) && (CS_VER >= 2) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
261 CXSPARSE_ZNAME (_ipvec) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
262 (S->pinv, bvec, reinterpret_cast<cs_complex_t *>(buf), nr); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
263 #else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
264 CXSPARSE_ZNAME (_ipvec) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
265 (nr, S->Pinv, bvec, reinterpret_cast<cs_complex_t *>(buf)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
266 #endif |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
267 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
268 for (volatile octave_idx_type i = 0; i < nm; i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
269 { |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
270 OCTAVE_QUIT; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
271 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
272 CXSPARSE_ZNAME (_happly) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
273 (N->L, i, N->B[i], reinterpret_cast<cs_complex_t *>(buf)); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
274 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
275 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
276 for (octave_idx_type i = 0; i < nr; i++) |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
277 vec[i+idx] = buf[i]; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
278 bvec[j] = OCTAVE_C99_ZERO; |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
279 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
280 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
281 return ret.hermitian (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
282 #else |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
283 return ComplexMatrix (); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
284 #endif |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
285 } |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
286 |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
287 ComplexMatrix |
5610 | 288 qrsolve(const SparseComplexMatrix&a, const Matrix &b, octave_idx_type &info) |
289 { | |
5797 | 290 info = -1; |
5610 | 291 #ifdef HAVE_CXSPARSE |
292 octave_idx_type nr = a.rows(); | |
293 octave_idx_type nc = a.cols(); | |
294 octave_idx_type b_nc = b.cols(); | |
295 octave_idx_type b_nr = b.rows(); | |
296 ComplexMatrix x; | |
297 | |
6924 | 298 if (nr < 0 || nc < 0 || nr != b_nr) |
5610 | 299 (*current_liboctave_error_handler) |
300 ("matrix dimension mismatch in solution of minimum norm problem"); | |
6924 | 301 else if (nr == 0 || nc == 0 || b_nc == 0) |
302 x = ComplexMatrix (nc, b_nc, Complex (0.0, 0.0)); | |
5610 | 303 else if (nr >= nc) |
304 { | |
305 SparseComplexQR q (a, 2); | |
306 if (! q.ok ()) | |
5797 | 307 return ComplexMatrix(); |
5610 | 308 x.resize(nc, b_nc); |
6685 | 309 cs_complex_t *vec = reinterpret_cast<cs_complex_t *> |
5610 | 310 (x.fortran_vec()); |
5648 | 311 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610 | 312 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
313 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) | |
314 { | |
315 OCTAVE_QUIT; | |
316 for (octave_idx_type j = 0; j < b_nr; j++) | |
317 Xx[j] = b.xelem(j,i); | |
5681 | 318 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
319 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 320 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 321 #if defined(CS_VER) && (CS_VER >= 2) |
322 CXSPARSE_ZNAME (_ipvec) | |
6685 | 323 (q.S()->pinv, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 324 #else |
5648 | 325 CXSPARSE_ZNAME (_ipvec) |
6685 | 326 (nr, q.S()->Pinv, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 327 #endif |
5610 | 328 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
329 for (volatile octave_idx_type j = 0; j < nc; j++) | |
330 { | |
331 OCTAVE_QUIT; | |
332 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 333 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610 | 334 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
335 } | |
336 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 337 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
5792 | 338 #if defined(CS_VER) && (CS_VER >= 2) |
339 CXSPARSE_ZNAME (_ipvec) (q.S()->q, buf, vec + idx, nc); | |
340 #else | |
5648 | 341 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, vec + idx); |
5792 | 342 #endif |
5610 | 343 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
344 } | |
5797 | 345 info = 0; |
5610 | 346 } |
347 else | |
348 { | |
349 SparseComplexMatrix at = a.hermitian(); | |
350 SparseComplexQR q (at, 2); | |
351 if (! q.ok ()) | |
5797 | 352 return ComplexMatrix(); |
5610 | 353 x.resize(nc, b_nc); |
6685 | 354 cs_complex_t *vec = reinterpret_cast<cs_complex_t *> |
5610 | 355 (x.fortran_vec()); |
5681 | 356 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
357 OCTAVE_C99_COMPLEX (buf, nbuf); | |
5610 | 358 OCTAVE_LOCAL_BUFFER (Complex, Xx, b_nr); |
6685 | 359 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
360 OCTAVE_LOCAL_BUFFER (double, B, nr); | |
361 for (octave_idx_type i = 0; i < nr; i++) | |
362 B[i] = q.N()->B [i]; | |
363 #else | |
5610 | 364 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
365 for (octave_idx_type i = 0; i < nr; i++) | |
366 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); | |
6685 | 367 #endif |
5610 | 368 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
369 { | |
370 OCTAVE_QUIT; | |
371 for (octave_idx_type j = 0; j < b_nr; j++) | |
372 Xx[j] = b.xelem(j,i); | |
5681 | 373 for (octave_idx_type j = nr; j < nbuf; j++) |
374 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 375 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 376 #if defined(CS_VER) && (CS_VER >= 2) |
377 CXSPARSE_ZNAME (_pvec) | |
6685 | 378 (q.S()->q, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 379 #else |
5648 | 380 CXSPARSE_ZNAME (_pvec) |
6685 | 381 (nr, q.S()->Q, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 382 #endif |
5648 | 383 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610 | 384 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
385 for (volatile octave_idx_type j = nr-1; j >= 0; j--) | |
386 { | |
387 OCTAVE_QUIT; | |
388 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
389 | |
6685 | 390 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
391 CXSPARSE_ZNAME (_happly) (q.N()->L, j, B[j], buf); | |
392 #else | |
5648 | 393 CXSPARSE_ZNAME (_happly) |
6685 | 394 (q.N()->L, j, reinterpret_cast<cs_complex_t *>(B)[j], buf); |
395 #endif | |
5610 | 396 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
397 } | |
398 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 399 #if defined(CS_VER) && (CS_VER >= 2) |
400 CXSPARSE_ZNAME (_pvec) (q.S()->pinv, buf, vec + idx, nc); | |
401 #else | |
5648 | 402 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, vec + idx); |
5792 | 403 #endif |
5610 | 404 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
405 } | |
5797 | 406 info = 0; |
5610 | 407 } |
408 | |
409 return x; | |
410 #else | |
411 return ComplexMatrix (); | |
412 #endif | |
413 } | |
414 | |
415 SparseComplexMatrix | |
416 qrsolve(const SparseComplexMatrix&a, const SparseMatrix &b, octave_idx_type &info) | |
417 { | |
5797 | 418 info = -1; |
5610 | 419 #ifdef HAVE_CXSPARSE |
420 octave_idx_type nr = a.rows(); | |
421 octave_idx_type nc = a.cols(); | |
422 octave_idx_type b_nc = b.cols(); | |
423 octave_idx_type b_nr = b.rows(); | |
424 SparseComplexMatrix x; | |
425 volatile octave_idx_type ii, x_nz; | |
426 | |
6924 | 427 if (nr < 0 || nc < 0 || nr != b_nr) |
5610 | 428 (*current_liboctave_error_handler) |
429 ("matrix dimension mismatch in solution of minimum norm problem"); | |
6924 | 430 else if (nr == 0 || nc == 0 || b_nc == 0) |
431 x = SparseComplexMatrix (nc, b_nc); | |
5610 | 432 else if (nr >= nc) |
433 { | |
434 SparseComplexQR q (a, 2); | |
435 if (! q.ok ()) | |
5797 | 436 return SparseComplexMatrix(); |
5610 | 437 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
438 x.xcidx(0) = 0; | |
439 x_nz = b.nzmax(); | |
440 ii = 0; | |
441 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); | |
5648 | 442 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610 | 443 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
444 { | |
445 OCTAVE_QUIT; | |
446 for (octave_idx_type j = 0; j < b_nr; j++) | |
447 Xx[j] = b.xelem(j,i); | |
5681 | 448 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
449 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 450 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 451 #if defined(CS_VER) && (CS_VER >= 2) |
452 CXSPARSE_ZNAME (_ipvec) | |
6685 | 453 (q.S()->pinv, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 454 #else |
5648 | 455 CXSPARSE_ZNAME (_ipvec) |
6685 | 456 (nr, q.S()->Pinv, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 457 #endif |
5610 | 458 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
459 for (volatile octave_idx_type j = 0; j < nc; j++) | |
460 { | |
461 OCTAVE_QUIT; | |
462 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 463 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610 | 464 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
465 } | |
466 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 467 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
5792 | 468 #if defined(CS_VER) && (CS_VER >= 2) |
469 CXSPARSE_ZNAME (_ipvec) | |
6685 | 470 (q.S()->q, buf, reinterpret_cast<cs_complex_t *>(Xx), nc); |
5792 | 471 #else |
472 CXSPARSE_ZNAME (_ipvec) | |
6685 | 473 (nc, q.S()->Q, buf, reinterpret_cast<cs_complex_t *>(Xx)); |
5792 | 474 #endif |
5610 | 475 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
476 | |
477 for (octave_idx_type j = 0; j < nc; j++) | |
478 { | |
479 Complex tmp = Xx[j]; | |
480 if (tmp != 0.0) | |
481 { | |
482 if (ii == x_nz) | |
483 { | |
484 // Resize the sparse matrix | |
485 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; | |
486 sz = (sz > 10 ? sz : 10) + x_nz; | |
487 x.change_capacity (sz); | |
488 x_nz = sz; | |
489 } | |
490 x.xdata(ii) = tmp; | |
491 x.xridx(ii++) = j; | |
492 } | |
493 } | |
494 x.xcidx(i+1) = ii; | |
495 } | |
5797 | 496 info = 0; |
5610 | 497 } |
498 else | |
499 { | |
500 SparseComplexMatrix at = a.hermitian(); | |
501 SparseComplexQR q (at, 2); | |
502 if (! q.ok ()) | |
5797 | 503 return SparseComplexMatrix(); |
5610 | 504 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
505 x.xcidx(0) = 0; | |
506 x_nz = b.nzmax(); | |
507 ii = 0; | |
5681 | 508 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
5610 | 509 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5681 | 510 OCTAVE_C99_COMPLEX (buf, nbuf); |
6685 | 511 |
512 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) | |
513 OCTAVE_LOCAL_BUFFER (double, B, nr); | |
514 for (octave_idx_type i = 0; i < nr; i++) | |
515 B[i] = q.N()->B [i]; | |
516 #else | |
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]); | |
6685 | 520 #endif |
5610 | 521 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
522 { | |
523 OCTAVE_QUIT; | |
524 for (octave_idx_type j = 0; j < b_nr; j++) | |
525 Xx[j] = b.xelem(j,i); | |
5681 | 526 for (octave_idx_type j = nr; j < nbuf; j++) |
527 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 528 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 529 #if defined(CS_VER) && (CS_VER >= 2) |
530 CXSPARSE_ZNAME (_pvec) | |
6685 | 531 (q.S()->q, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 532 #else |
5648 | 533 CXSPARSE_ZNAME (_pvec) |
6685 | 534 (nr, q.S()->Q, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 535 #endif |
5648 | 536 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610 | 537 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
538 for (volatile octave_idx_type j = nr-1; j >= 0; j--) | |
539 { | |
540 OCTAVE_QUIT; | |
541 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
6685 | 542 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
543 CXSPARSE_ZNAME (_happly) (q.N()->L, j, B[j], buf); | |
544 #else | |
5648 | 545 CXSPARSE_ZNAME (_happly) |
6685 | 546 (q.N()->L, j, reinterpret_cast<cs_complex_t *>(B)[j], buf); |
547 #endif | |
5610 | 548 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
549 } | |
550 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 551 #if defined(CS_VER) && (CS_VER >= 2) |
552 CXSPARSE_ZNAME (_pvec) | |
6685 | 553 (q.S()->pinv, buf, reinterpret_cast<cs_complex_t *>(Xx), nc); |
5792 | 554 #else |
555 CXSPARSE_ZNAME (_pvec) | |
6685 | 556 (nc, q.S()->Pinv, buf, reinterpret_cast<cs_complex_t *>(Xx)); |
5792 | 557 #endif |
5610 | 558 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
559 | |
560 for (octave_idx_type j = 0; j < nc; j++) | |
561 { | |
562 Complex tmp = Xx[j]; | |
563 if (tmp != 0.0) | |
564 { | |
565 if (ii == x_nz) | |
566 { | |
567 // Resize the sparse matrix | |
568 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; | |
569 sz = (sz > 10 ? sz : 10) + x_nz; | |
570 x.change_capacity (sz); | |
571 x_nz = sz; | |
572 } | |
573 x.xdata(ii) = tmp; | |
574 x.xridx(ii++) = j; | |
575 } | |
576 } | |
577 x.xcidx(i+1) = ii; | |
578 } | |
5797 | 579 info = 0; |
5610 | 580 } |
581 | |
582 x.maybe_compress (); | |
583 return x; | |
584 #else | |
585 return SparseComplexMatrix (); | |
586 #endif | |
587 } | |
588 | |
589 ComplexMatrix | |
590 qrsolve(const SparseComplexMatrix&a, const ComplexMatrix &b, octave_idx_type &info) | |
591 { | |
5797 | 592 info = -1; |
5610 | 593 #ifdef HAVE_CXSPARSE |
594 octave_idx_type nr = a.rows(); | |
595 octave_idx_type nc = a.cols(); | |
596 octave_idx_type b_nc = b.cols(); | |
597 octave_idx_type b_nr = b.rows(); | |
6685 | 598 const cs_complex_t *bvec = |
599 reinterpret_cast<const cs_complex_t *>(b.fortran_vec()); | |
5610 | 600 ComplexMatrix x; |
601 | |
6924 | 602 if (nr < 0 || nc < 0 || nr != b_nr) |
5610 | 603 (*current_liboctave_error_handler) |
604 ("matrix dimension mismatch in solution of minimum norm problem"); | |
6924 | 605 else if (nr == 0 || nc == 0 || b_nc == 0) |
606 x = ComplexMatrix (nc, b_nc, Complex (0.0, 0.0)); | |
5610 | 607 else if (nr >= nc) |
608 { | |
609 SparseComplexQR q (a, 2); | |
610 if (! q.ok ()) | |
5797 | 611 return ComplexMatrix(); |
5610 | 612 x.resize(nc, b_nc); |
6685 | 613 cs_complex_t *vec = reinterpret_cast<cs_complex_t *> |
5610 | 614 (x.fortran_vec()); |
5648 | 615 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610 | 616 for (volatile octave_idx_type i = 0, idx = 0, bidx = 0; i < b_nc; |
617 i++, idx+=nc, bidx+=b_nr) | |
618 { | |
619 OCTAVE_QUIT; | |
5681 | 620 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
621 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 622 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 623 #if defined(CS_VER) && (CS_VER >= 2) |
624 CXSPARSE_ZNAME (_ipvec) (q.S()->pinv, bvec + bidx, buf, nr); | |
625 #else | |
5648 | 626 CXSPARSE_ZNAME (_ipvec) (nr, q.S()->Pinv, bvec + bidx, buf); |
5792 | 627 #endif |
5610 | 628 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
629 for (volatile octave_idx_type j = 0; j < nc; j++) | |
630 { | |
631 OCTAVE_QUIT; | |
632 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 633 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610 | 634 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
635 } | |
636 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 637 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
5792 | 638 #if defined(CS_VER) && (CS_VER >= 2) |
639 CXSPARSE_ZNAME (_ipvec) (q.S()->q, buf, vec + idx, nc); | |
640 #else | |
5648 | 641 CXSPARSE_ZNAME (_ipvec) (nc, q.S()->Q, buf, vec + idx); |
5792 | 642 #endif |
5610 | 643 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
644 } | |
5797 | 645 info = 0; |
5610 | 646 } |
647 else | |
648 { | |
649 SparseComplexMatrix at = a.hermitian(); | |
650 SparseComplexQR q (at, 2); | |
651 if (! q.ok ()) | |
5797 | 652 return ComplexMatrix(); |
5610 | 653 x.resize(nc, b_nc); |
6685 | 654 cs_complex_t *vec = reinterpret_cast<cs_complex_t *> |
5610 | 655 (x.fortran_vec()); |
5681 | 656 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
657 OCTAVE_C99_COMPLEX (buf, nbuf); | |
6685 | 658 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
659 OCTAVE_LOCAL_BUFFER (double, B, nr); | |
660 for (octave_idx_type i = 0; i < nr; i++) | |
661 B[i] = q.N()->B [i]; | |
662 #else | |
5610 | 663 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
664 for (octave_idx_type i = 0; i < nr; i++) | |
665 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); | |
6685 | 666 #endif |
5610 | 667 for (volatile octave_idx_type i = 0, idx = 0, bidx = 0; i < b_nc; |
668 i++, idx+=nc, bidx+=b_nr) | |
669 { | |
670 OCTAVE_QUIT; | |
5681 | 671 for (octave_idx_type j = nr; j < nbuf; j++) |
672 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 673 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 674 #if defined(CS_VER) && (CS_VER >= 2) |
675 CXSPARSE_ZNAME (_pvec) (q.S()->q, bvec + bidx, buf, nr); | |
676 #else | |
5648 | 677 CXSPARSE_ZNAME (_pvec) (nr, q.S()->Q, bvec + bidx, buf); |
5792 | 678 #endif |
5648 | 679 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610 | 680 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
681 for (volatile octave_idx_type j = nr-1; j >= 0; j--) | |
682 { | |
683 OCTAVE_QUIT; | |
684 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
6685 | 685 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
686 CXSPARSE_ZNAME (_happly) (q.N()->L, j, B[j], buf); | |
687 #else | |
5648 | 688 CXSPARSE_ZNAME (_happly) |
6685 | 689 (q.N()->L, j, reinterpret_cast<cs_complex_t *>(B)[j], buf); |
690 #endif | |
5610 | 691 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
692 } | |
693 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 694 #if defined(CS_VER) && (CS_VER >= 2) |
695 CXSPARSE_ZNAME (_pvec) (q.S()->pinv, buf, vec + idx, nc); | |
696 #else | |
5648 | 697 CXSPARSE_ZNAME (_pvec) (nc, q.S()->Pinv, buf, vec + idx); |
5792 | 698 #endif |
5610 | 699 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
700 } | |
5797 | 701 info = 0; |
5610 | 702 } |
703 | |
704 return x; | |
705 #else | |
706 return ComplexMatrix (); | |
707 #endif | |
708 } | |
709 | |
710 SparseComplexMatrix | |
711 qrsolve(const SparseComplexMatrix&a, const SparseComplexMatrix &b, octave_idx_type &info) | |
712 { | |
5797 | 713 info = -1; |
5610 | 714 #ifdef HAVE_CXSPARSE |
715 octave_idx_type nr = a.rows(); | |
716 octave_idx_type nc = a.cols(); | |
717 octave_idx_type b_nc = b.cols(); | |
718 octave_idx_type b_nr = b.rows(); | |
719 SparseComplexMatrix x; | |
720 volatile octave_idx_type ii, x_nz; | |
721 | |
6924 | 722 if (nr < 0 || nc < 0 || nr != b_nr) |
5610 | 723 (*current_liboctave_error_handler) |
724 ("matrix dimension mismatch in solution of minimum norm problem"); | |
6924 | 725 else if (nr == 0 || nc == 0 || b_nc == 0) |
726 x = SparseComplexMatrix (nc, b_nc); | |
5610 | 727 else if (nr >= nc) |
728 { | |
729 SparseComplexQR q (a, 2); | |
730 if (! q.ok ()) | |
5797 | 731 return SparseComplexMatrix(); |
5610 | 732 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
733 x.xcidx(0) = 0; | |
734 x_nz = b.nzmax(); | |
735 ii = 0; | |
736 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); | |
5648 | 737 OCTAVE_C99_COMPLEX (buf, q.S()->m2); |
5610 | 738 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
739 { | |
740 OCTAVE_QUIT; | |
741 for (octave_idx_type j = 0; j < b_nr; j++) | |
742 Xx[j] = b.xelem(j,i); | |
5681 | 743 for (octave_idx_type j = nr; j < q.S()->m2; j++) |
744 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 745 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 746 #if defined(CS_VER) && (CS_VER >= 2) |
747 CXSPARSE_ZNAME (_ipvec) | |
6685 | 748 (q.S()->pinv, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 749 #else |
5648 | 750 CXSPARSE_ZNAME (_ipvec) |
6685 | 751 (nr, q.S()->Pinv, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 752 #endif |
5610 | 753 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
754 for (volatile octave_idx_type j = 0; j < nc; j++) | |
755 { | |
756 OCTAVE_QUIT; | |
757 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 758 CXSPARSE_ZNAME (_happly) (q.N()->L, j, q.N()->B[j], buf); |
5610 | 759 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
760 } | |
761 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5648 | 762 CXSPARSE_ZNAME (_usolve) (q.N()->U, buf); |
5792 | 763 #if defined(CS_VER) && (CS_VER >= 2) |
764 CXSPARSE_ZNAME (_ipvec) | |
6685 | 765 (q.S()->q, buf, reinterpret_cast<cs_complex_t *>(Xx), nc); |
5792 | 766 #else |
767 CXSPARSE_ZNAME (_ipvec) | |
6685 | 768 (nc, q.S()->Q, buf, reinterpret_cast<cs_complex_t *>(Xx)); |
5792 | 769 #endif |
5610 | 770 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
771 | |
772 for (octave_idx_type j = 0; j < nc; j++) | |
773 { | |
774 Complex tmp = Xx[j]; | |
775 if (tmp != 0.0) | |
776 { | |
777 if (ii == x_nz) | |
778 { | |
779 // Resize the sparse matrix | |
780 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; | |
781 sz = (sz > 10 ? sz : 10) + x_nz; | |
782 x.change_capacity (sz); | |
783 x_nz = sz; | |
784 } | |
785 x.xdata(ii) = tmp; | |
786 x.xridx(ii++) = j; | |
787 } | |
788 } | |
789 x.xcidx(i+1) = ii; | |
790 } | |
5797 | 791 info = 0; |
5610 | 792 } |
793 else | |
794 { | |
795 SparseComplexMatrix at = a.hermitian(); | |
796 SparseComplexQR q (at, 2); | |
797 if (! q.ok ()) | |
5797 | 798 return SparseComplexMatrix(); |
5610 | 799 x = SparseComplexMatrix (nc, b_nc, b.nzmax()); |
800 x.xcidx(0) = 0; | |
801 x_nz = b.nzmax(); | |
802 ii = 0; | |
5681 | 803 volatile octave_idx_type nbuf = (nc > q.S()->m2 ? nc : q.S()->m2); |
5610 | 804 OCTAVE_LOCAL_BUFFER (Complex, Xx, (b_nr > nc ? b_nr : nc)); |
5681 | 805 OCTAVE_C99_COMPLEX (buf, nbuf); |
6685 | 806 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
807 OCTAVE_LOCAL_BUFFER (double, B, nr); | |
808 for (octave_idx_type i = 0; i < nr; i++) | |
809 B[i] = q.N()->B [i]; | |
810 #else | |
5610 | 811 OCTAVE_LOCAL_BUFFER (Complex, B, nr); |
812 for (octave_idx_type i = 0; i < nr; i++) | |
813 B[i] = conj (reinterpret_cast<Complex *>(q.N()->B) [i]); | |
6685 | 814 #endif |
5610 | 815 for (volatile octave_idx_type i = 0, idx = 0; i < b_nc; i++, idx+=nc) |
816 { | |
817 OCTAVE_QUIT; | |
818 for (octave_idx_type j = 0; j < b_nr; j++) | |
819 Xx[j] = b.xelem(j,i); | |
5681 | 820 for (octave_idx_type j = nr; j < nbuf; j++) |
821 buf[j] = OCTAVE_C99_ZERO; | |
5610 | 822 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5792 | 823 #if defined(CS_VER) && (CS_VER >= 2) |
824 CXSPARSE_ZNAME (_pvec) | |
6685 | 825 (q.S()->q, reinterpret_cast<cs_complex_t *>(Xx), buf, nr); |
5792 | 826 #else |
5648 | 827 CXSPARSE_ZNAME (_pvec) |
6685 | 828 (nr, q.S()->Q, reinterpret_cast<cs_complex_t *>(Xx), buf); |
5792 | 829 #endif |
5648 | 830 CXSPARSE_ZNAME (_utsolve) (q.N()->U, buf); |
5610 | 831 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
832 for (volatile octave_idx_type j = nr-1; j >= 0; j--) | |
833 { | |
834 OCTAVE_QUIT; | |
835 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
6685 | 836 #if defined(CS_VER) && (((CS_VER == 2) && (CS_SUBVER >= 2)) || (CS_VER > 2)) |
837 CXSPARSE_ZNAME (_happly) (q.N()->L, j, B[j], buf); | |
838 #else | |
5648 | 839 CXSPARSE_ZNAME (_happly) |
6685 | 840 (q.N()->L, j, reinterpret_cast<cs_complex_t *>(B)[j], buf); |
841 #endif | |
5610 | 842 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
843 } | |
844 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
5792 | 845 #if defined(CS_VER) && (CS_VER >= 2) |
846 CXSPARSE_ZNAME (_pvec) | |
6685 | 847 (q.S()->pinv, buf, reinterpret_cast<cs_complex_t *>(Xx), nc); |
5792 | 848 #else |
849 CXSPARSE_ZNAME (_pvec) | |
6685 | 850 (nc, q.S()->Pinv, buf, reinterpret_cast<cs_complex_t *>(Xx)); |
5792 | 851 #endif |
5610 | 852 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
853 | |
854 for (octave_idx_type j = 0; j < nc; j++) | |
855 { | |
856 Complex tmp = Xx[j]; | |
857 if (tmp != 0.0) | |
858 { | |
859 if (ii == x_nz) | |
860 { | |
861 // Resize the sparse matrix | |
862 octave_idx_type sz = x_nz * (b_nc - i) / b_nc; | |
863 sz = (sz > 10 ? sz : 10) + x_nz; | |
864 x.change_capacity (sz); | |
865 x_nz = sz; | |
866 } | |
867 x.xdata(ii) = tmp; | |
868 x.xridx(ii++) = j; | |
869 } | |
870 } | |
871 x.xcidx(i+1) = ii; | |
872 } | |
5797 | 873 info = 0; |
5610 | 874 } |
875 | |
876 x.maybe_compress (); | |
877 return x; | |
878 #else | |
879 return SparseComplexMatrix (); | |
880 #endif | |
881 } | |
882 | |
883 /* | |
884 ;;; Local Variables: *** | |
885 ;;; mode: C++ *** | |
886 ;;; End: *** | |
887 */ |