Mercurial > hg > octave-nkf
annotate liboctave/sparse-dmsolve.cc @ 11574:a83bad07f7e3
attempt better backward compatibility for Array resize functions
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 00:12:05 -0500 |
parents | 57632dea2446 |
children | 12df7854fa7c |
rev | line source |
---|---|
5683 | 1 /* |
2 | |
11523 | 3 Copyright (C) 2006-2011 David Bateman |
5683 | 4 |
7016 | 5 This file is part of Octave. |
6 | |
5683 | 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. | |
5683 | 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/>. | |
5683 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
5797 | 27 #include <vector> |
5794 | 28 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
29 #include "MArray.h" |
5683 | 30 #include "MSparse.h" |
31 #include "SparseQR.h" | |
32 #include "SparseCmplxQR.h" | |
5785 | 33 #include "MatrixType.h" |
5683 | 34 #include "oct-sort.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
35 #include "oct-locbuf.h" |
5683 | 36 |
37 template <class T> | |
38 static MSparse<T> | |
39 dmsolve_extract (const MSparse<T> &A, const octave_idx_type *Pinv, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
40 const octave_idx_type *Q, octave_idx_type rst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
41 octave_idx_type rend, octave_idx_type cst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
42 octave_idx_type cend, octave_idx_type maxnz = -1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
43 bool lazy = false) |
5683 | 44 { |
45 octave_idx_type nz = (rend - rst) * (cend - cst); | |
46 maxnz = (maxnz < 0 ? A.nnz () : maxnz); | |
47 MSparse<T> B (rend - rst, cend - cst, (nz < maxnz ? nz : maxnz)); | |
48 // Some sparse functions can support lazy indexing (where elements | |
49 // in the row are in no particular order), even though octave in | |
50 // general can't. For those functions that can using it is a big | |
51 // win here in terms of speed. | |
52 if (lazy) | |
53 { | |
54 nz = 0; | |
55 for (octave_idx_type j = cst ; j < cend ; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
56 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
57 octave_idx_type qq = (Q ? Q [j] : j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
58 B.xcidx (j - cst) = nz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
59 for (octave_idx_type p = A.cidx(qq) ; p < A.cidx (qq+1) ; p++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
60 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
61 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
62 octave_idx_type r = (Pinv ? Pinv [A.ridx (p)] : A.ridx (p)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
63 if (r >= rst && r < rend) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
64 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
65 B.xdata (nz) = A.data (p); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
66 B.xridx (nz++) = r - rst ; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
67 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
68 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
69 } |
5683 | 70 B.xcidx (cend - cst) = nz ; |
71 } | |
72 else | |
73 { | |
74 OCTAVE_LOCAL_BUFFER (T, X, rend - rst); | |
75 octave_sort<octave_idx_type> sort; | |
76 octave_idx_type *ri = B.xridx(); | |
77 nz = 0; | |
78 for (octave_idx_type j = cst ; j < cend ; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
79 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
80 octave_idx_type qq = (Q ? Q [j] : j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
81 B.xcidx (j - cst) = nz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
82 for (octave_idx_type p = A.cidx(qq) ; p < A.cidx (qq+1) ; p++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
83 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
84 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
85 octave_idx_type r = (Pinv ? Pinv [A.ridx (p)] : A.ridx (p)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
86 if (r >= rst && r < rend) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
87 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
88 X [r-rst] = A.data (p); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
89 B.xridx (nz++) = r - rst ; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
90 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
91 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
92 sort.sort (ri + B.xcidx (j - cst), nz - B.xcidx (j - cst)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
93 for (octave_idx_type p = B.cidx (j - cst); p < nz; p++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
94 B.xdata (p) = X [B.xridx (p)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
95 } |
5683 | 96 B.xcidx (cend - cst) = nz ; |
97 } | |
98 | |
99 return B; | |
100 } | |
101 | |
102 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
103 static MSparse<double> | |
104 dmsolve_extract (const MSparse<double> &A, const octave_idx_type *Pinv, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
105 const octave_idx_type *Q, octave_idx_type rst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
106 octave_idx_type rend, octave_idx_type cst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
107 octave_idx_type cend, octave_idx_type maxnz, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
108 bool lazy); |
5683 | 109 |
110 static MSparse<Complex> | |
111 dmsolve_extract (const MSparse<Complex> &A, const octave_idx_type *Pinv, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
112 const octave_idx_type *Q, octave_idx_type rst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
113 octave_idx_type rend, octave_idx_type cst, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
114 octave_idx_type cend, octave_idx_type maxnz, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
115 bool lazy); |
5683 | 116 #endif |
117 | |
118 template <class T> | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
119 static MArray<T> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
120 dmsolve_extract (const MArray<T> &m, const octave_idx_type *, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
121 const octave_idx_type *, octave_idx_type r1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
122 octave_idx_type r2, octave_idx_type c1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
123 octave_idx_type c2) |
5683 | 124 { |
125 r2 -= 1; | |
126 c2 -= 1; | |
127 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } | |
128 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
129 | |
130 octave_idx_type new_r = r2 - r1 + 1; | |
131 octave_idx_type new_c = c2 - c1 + 1; | |
132 | |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
133 MArray<T> result (dim_vector (new_r, new_c)); |
5683 | 134 |
135 for (octave_idx_type j = 0; j < new_c; j++) | |
136 for (octave_idx_type i = 0; i < new_r; i++) | |
137 result.xelem (i, j) = m.elem (r1+i, c1+j); | |
138 | |
139 return result; | |
140 } | |
141 | |
142 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
143 static MArray<double> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
144 dmsolve_extract (const MArray<double> &m, const octave_idx_type *, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
145 const octave_idx_type *, octave_idx_type r1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
146 octave_idx_type r2, octave_idx_type c1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
147 octave_idx_type c2) |
5683 | 148 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
149 static MArray<Complex> |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
150 dmsolve_extract (const MArray<Complex> &m, const octave_idx_type *, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 const octave_idx_type *, octave_idx_type r1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
152 octave_idx_type r2, octave_idx_type c1, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
153 octave_idx_type c2) |
5683 | 154 #endif |
155 | |
156 template <class T> | |
157 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
158 dmsolve_insert (MArray<T> &a, const MArray<T> &b, const octave_idx_type *Q, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
159 octave_idx_type r, octave_idx_type c) |
5683 | 160 { |
161 T *ax = a.fortran_vec(); | |
162 const T *bx = b.fortran_vec(); | |
163 octave_idx_type anr = a.rows(); | |
164 octave_idx_type nr = b.rows(); | |
165 octave_idx_type nc = b.cols(); | |
166 for (octave_idx_type j = 0; j < nc; j++) | |
167 { | |
168 octave_idx_type aoff = (c + j) * anr; | |
169 octave_idx_type boff = j * nr; | |
170 for (octave_idx_type i = 0; i < nr; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
171 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
172 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
173 ax [Q [r + i] + aoff] = bx [i + boff]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
174 } |
5683 | 175 } |
176 } | |
177 | |
178 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
179 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
180 dmsolve_insert (MArray<double> &a, const MArray<double> &b, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
181 const octave_idx_type *Q, octave_idx_type r, octave_idx_type c); |
5683 | 182 |
183 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
184 dmsolve_insert (MArray<Complex> &a, const MArray<Complex> &b, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
185 const octave_idx_type *Q, octave_idx_type r, octave_idx_type c); |
5683 | 186 #endif |
187 | |
188 template <class T> | |
189 static void | |
190 dmsolve_insert (MSparse<T> &a, const MSparse<T> &b, const octave_idx_type *Q, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
191 octave_idx_type r, octave_idx_type c) |
5683 | 192 { |
193 octave_idx_type b_rows = b.rows (); | |
194 octave_idx_type b_cols = b.cols (); | |
195 octave_idx_type nr = a.rows (); | |
196 octave_idx_type nc = a.cols (); | |
197 | |
198 OCTAVE_LOCAL_BUFFER (octave_idx_type, Qinv, nr); | |
199 for (octave_idx_type i = 0; i < nr; i++) | |
200 Qinv [Q [i]] = i; | |
201 | |
202 // First count the number of elements in the final array | |
203 octave_idx_type nel = a.xcidx(c) + b.nnz (); | |
204 | |
205 if (c + b_cols < nc) | |
206 nel += a.xcidx(nc) - a.xcidx(c + b_cols); | |
207 | |
208 for (octave_idx_type i = c; i < c + b_cols; i++) | |
209 for (octave_idx_type j = a.xcidx(i); j < a.xcidx(i+1); j++) | |
210 if (Qinv [a.xridx(j)] < r || Qinv [a.xridx(j)] >= r + b_rows) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
211 nel++; |
5683 | 212 |
213 OCTAVE_LOCAL_BUFFER (T, X, nr); | |
214 octave_sort<octave_idx_type> sort; | |
215 MSparse<T> tmp (a); | |
216 a = MSparse<T> (nr, nc, nel); | |
217 octave_idx_type *ri = a.xridx(); | |
218 | |
219 for (octave_idx_type i = 0; i < tmp.cidx(c); i++) | |
220 { | |
221 a.xdata(i) = tmp.xdata(i); | |
222 a.xridx(i) = tmp.xridx(i); | |
223 } | |
224 for (octave_idx_type i = 0; i < c + 1; i++) | |
225 a.xcidx(i) = tmp.xcidx(i); | |
226 | |
227 octave_idx_type ii = a.xcidx(c); | |
228 | |
229 for (octave_idx_type i = c; i < c + b_cols; i++) | |
230 { | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
231 octave_quit (); |
5683 | 232 |
233 for (octave_idx_type j = tmp.xcidx(i); j < tmp.xcidx(i+1); j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
234 if (Qinv [tmp.xridx(j)] < r || Qinv [tmp.xridx(j)] >= r + b_rows) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
235 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
236 X [tmp.xridx(j)] = tmp.xdata(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
237 a.xridx(ii++) = tmp.xridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
238 } |
5683 | 239 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
240 octave_quit (); |
5683 | 241 |
242 for (octave_idx_type j = b.cidx(i-c); j < b.cidx(i-c+1); j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
243 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
244 X [Q [r + b.ridx(j)]] = b.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
245 a.xridx(ii++) = Q [r + b.ridx(j)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
246 } |
5683 | 247 |
248 sort.sort (ri + a.xcidx (i), ii - a.xcidx (i)); | |
249 for (octave_idx_type p = a.xcidx (i); p < ii; p++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
250 a.xdata (p) = X [a.xridx (p)]; |
5683 | 251 a.xcidx(i+1) = ii; |
252 } | |
253 | |
254 for (octave_idx_type i = c + b_cols; i < nc; i++) | |
255 { | |
256 for (octave_idx_type j = tmp.xcidx(i); j < tmp.cidx(i+1); j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
257 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
258 a.xdata(ii) = tmp.xdata(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
259 a.xridx(ii++) = tmp.xridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
260 } |
5683 | 261 a.xcidx(i+1) = ii; |
262 } | |
263 } | |
264 | |
265 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
266 static void | |
267 dmsolve_insert (MSparse<double> &a, const SparseMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
268 const octave_idx_type *Q, octave_idx_type r, octave_idx_type c); |
5683 | 269 |
270 static void | |
271 dmsolve_insert (MSparse<Complex> &a, const MSparse<Complex> &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
272 const octave_idx_type *Q, octave_idx_type r, octave_idx_type c); |
5683 | 273 #endif |
274 | |
275 template <class T, class RT> | |
276 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
277 dmsolve_permute (MArray<RT> &a, const MArray<T>& b, const octave_idx_type *p) |
5683 | 278 { |
279 octave_idx_type b_nr = b.rows (); | |
280 octave_idx_type b_nc = b.cols (); | |
281 const T *Bx = b.fortran_vec(); | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
282 a.resize (dim_vector (b_nr, b_nc)); |
5683 | 283 RT *Btx = a.fortran_vec(); |
284 for (octave_idx_type j = 0; j < b_nc; j++) | |
285 { | |
286 octave_idx_type off = j * b_nr; | |
287 for (octave_idx_type i = 0; i < b_nr; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
288 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
289 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
290 Btx [p [i] + off] = Bx [ i + off]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
291 } |
5683 | 292 } |
293 } | |
294 | |
295 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
296 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
297 dmsolve_permute (MArray<double> &a, const MArray<double>& b, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
298 const octave_idx_type *p); |
5683 | 299 |
300 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
301 dmsolve_permute (MArray<Complex> &a, const MArray<double>& b, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
302 const octave_idx_type *p); |
5683 | 303 |
304 static void | |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
305 dmsolve_permute (MArray<Complex> &a, const MArray<Complex>& b, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
306 const octave_idx_type *p); |
5683 | 307 #endif |
308 | |
309 template <class T, class RT> | |
310 static void | |
311 dmsolve_permute (MSparse<RT> &a, const MSparse<T>& b, const octave_idx_type *p) | |
312 { | |
313 octave_idx_type b_nr = b.rows (); | |
314 octave_idx_type b_nc = b.cols (); | |
315 octave_idx_type b_nz = b.nnz (); | |
316 octave_idx_type nz = 0; | |
317 a = MSparse<RT> (b_nr, b_nc, b_nz); | |
318 octave_sort<octave_idx_type> sort; | |
319 octave_idx_type *ri = a.xridx(); | |
320 OCTAVE_LOCAL_BUFFER (RT, X, b_nr); | |
321 a.xcidx(0) = 0; | |
322 for (octave_idx_type j = 0; j < b_nc; j++) | |
323 { | |
324 for (octave_idx_type i = b.cidx(j); i < b.cidx(j+1); i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
325 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
326 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
327 octave_idx_type r = p [b.ridx (i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
328 X [r] = b.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
329 a.xridx(nz++) = p [b.ridx (i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
330 } |
5683 | 331 sort.sort (ri + a.xcidx (j), nz - a.xcidx (j)); |
332 for (octave_idx_type i = a.cidx (j); i < nz; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
333 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
335 a.xdata (i) = X [a.xridx (i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
336 } |
5683 | 337 a.xcidx(j+1) = nz; |
338 } | |
339 } | |
340 | |
341 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
342 static void | |
343 dmsolve_permute (MSparse<double> &a, const MSparse<double>& b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
344 const octave_idx_type *p); |
5683 | 345 |
346 static void | |
347 dmsolve_permute (MSparse<Complex> &a, const MSparse<double>& b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
348 const octave_idx_type *p); |
5683 | 349 |
350 static void | |
351 dmsolve_permute (MSparse<Complex> &a, const MSparse<Complex>& b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
352 const octave_idx_type *p); |
5683 | 353 #endif |
354 | |
355 static void | |
356 solve_singularity_warning (double) | |
357 { | |
358 // Dummy singularity handler so that LU solver doesn't flag | |
359 // an error for numerically rank defficient matrices | |
360 } | |
361 | |
362 template <class RT, class ST, class T> | |
363 RT | |
364 dmsolve (const ST &a, const T &b, octave_idx_type &info) | |
365 { | |
5684 | 366 #ifdef HAVE_CXSPARSE |
5683 | 367 octave_idx_type nr = a.rows (); |
368 octave_idx_type nc = a.cols (); | |
369 octave_idx_type b_nr = b.rows (); | |
370 octave_idx_type b_nc = b.cols (); | |
371 RT retval; | |
372 | |
6924 | 373 if (nr < 0 || nc < 0 || nr != b_nr) |
5683 | 374 (*current_liboctave_error_handler) |
375 ("matrix dimension mismatch in solution of minimum norm problem"); | |
6924 | 376 else if (nr == 0 || nc == 0 || b_nc == 0) |
377 retval = RT (nc, b_nc, 0.0); | |
5683 | 378 else |
379 { | |
380 octave_idx_type nnz_remaining = a.nnz (); | |
381 CXSPARSE_DNAME () csm; | |
382 csm.m = nr; | |
383 csm.n = nc; | |
7520 | 384 csm.x = 0; |
5683 | 385 csm.nz = -1; |
386 csm.nzmax = a.nnz (); | |
387 // Cast away const on A, with full knowledge that CSparse won't touch it. | |
388 // Prevents the methods below making a copy of the data. | |
389 csm.p = const_cast<octave_idx_type *>(a.cidx ()); | |
390 csm.i = const_cast<octave_idx_type *>(a.ridx ()); | |
391 | |
5792 | 392 #if defined(CS_VER) && (CS_VER >= 2) |
393 CXSPARSE_DNAME (d) *dm = CXSPARSE_DNAME(_dmperm) (&csm, 0); | |
394 octave_idx_type *p = dm->p; | |
395 octave_idx_type *q = dm->q; | |
396 #else | |
5683 | 397 CXSPARSE_DNAME (d) *dm = CXSPARSE_DNAME(_dmperm) (&csm); |
398 octave_idx_type *p = dm->P; | |
399 octave_idx_type *q = dm->Q; | |
5792 | 400 #endif |
5683 | 401 OCTAVE_LOCAL_BUFFER (octave_idx_type, pinv, nr); |
402 for (octave_idx_type i = 0; i < nr; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
403 pinv [p [i]] = i; |
5683 | 404 RT btmp; |
405 dmsolve_permute (btmp, b, pinv); | |
406 info = 0; | |
407 retval.resize (nc, b_nc); | |
408 | |
409 // Leading over-determined block | |
410 if (dm->rr [2] < nr && dm->cc [3] < nc) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
411 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
412 ST m = dmsolve_extract (a, pinv, q, dm->rr [2], nr, dm->cc [3], nc, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
413 nnz_remaining, true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
414 nnz_remaining -= m.nnz(); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
415 RT mtmp = |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
416 qrsolve (m, dmsolve_extract (btmp, 0, 0, dm->rr[2], b_nr, 0, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
417 b_nc), info); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
418 dmsolve_insert (retval, mtmp, q, dm->cc [3], 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
419 if (dm->rr [2] > 0 && !info) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
420 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
421 m = dmsolve_extract (a, pinv, q, 0, dm->rr [2], |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
422 dm->cc [3], nc, nnz_remaining, true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
423 nnz_remaining -= m.nnz(); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
424 RT ctmp = dmsolve_extract (btmp, 0, 0, 0, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
425 dm->rr[2], 0, b_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
426 btmp.insert (ctmp - m * mtmp, 0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
427 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
428 } |
5683 | 429 |
430 // Structurally non-singular blocks | |
5775 | 431 // FIXME Should use fine Dulmange-Mendelsohn decomposition here. |
5797 | 432 if (dm->rr [1] < dm->rr [2] && dm->cc [2] < dm->cc [3] && !info) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
433 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
434 ST m = dmsolve_extract (a, pinv, q, dm->rr [1], dm->rr [2], |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
435 dm->cc [2], dm->cc [3], nnz_remaining, false); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
436 nnz_remaining -= m.nnz(); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
437 RT btmp2 = dmsolve_extract (btmp, 0, 0, dm->rr [1], dm->rr [2], |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
438 0, b_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
439 double rcond = 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
440 MatrixType mtyp (MatrixType::Full); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
441 RT mtmp = m.solve (mtyp, btmp2, info, rcond, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
442 solve_singularity_warning, false); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
443 if (info != 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
444 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
445 info = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
446 mtmp = qrsolve (m, btmp2, info); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
447 } |
5683 | 448 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
449 dmsolve_insert (retval, mtmp, q, dm->cc [2], 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
450 if (dm->rr [1] > 0 && !info) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
451 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
452 m = dmsolve_extract (a, pinv, q, 0, dm->rr [1], dm->cc [2], |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
453 dm->cc [3], nnz_remaining, true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
454 nnz_remaining -= m.nnz(); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
455 RT ctmp = dmsolve_extract (btmp, 0, 0, 0, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
456 dm->rr[1], 0, b_nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
457 btmp.insert (ctmp - m * mtmp, 0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
458 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
459 } |
5683 | 460 |
461 // Trailing under-determined block | |
5797 | 462 if (dm->rr [1] > 0 && dm->cc [2] > 0 && !info) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
463 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
464 ST m = dmsolve_extract (a, pinv, q, 0, dm->rr [1], 0, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
465 dm->cc [2], nnz_remaining, true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
466 RT mtmp = |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
467 qrsolve (m, dmsolve_extract(btmp, 0, 0, 0, dm->rr [1] , 0, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
468 b_nc), info); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
469 dmsolve_insert (retval, mtmp, q, 0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
470 } |
5683 | 471 |
472 CXSPARSE_DNAME (_dfree) (dm); | |
473 } | |
474 return retval; | |
5684 | 475 #else |
476 return RT (); | |
477 #endif | |
5683 | 478 } |
479 | |
480 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) | |
481 extern Matrix | |
482 dmsolve (const SparseMatrix &a, const Matrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
483 octave_idx_type &info); |
5683 | 484 |
485 extern ComplexMatrix | |
486 dmsolve (const SparseMatrix &a, const ComplexMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
487 octave_idx_type &info); |
5683 | 488 |
489 extern ComplexMatrix | |
490 dmsolve (const SparseComplexMatrix &a, const Matrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
491 octave_idx_type &info); |
5683 | 492 |
493 extern ComplexMatrix | |
494 dmsolve (const SparseComplexMatrix &a, const ComplexMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
495 octave_idx_type &info); |
5683 | 496 |
497 extern SparseMatrix | |
498 dmsolve (const SparseMatrix &a, const SparseMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
499 octave_idx_type &info); |
5683 | 500 |
501 extern SparseComplexMatrix | |
502 dmsolve (const SparseMatrix &a, const SparseComplexMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
503 octave_idx_type &info); |
5683 | 504 |
505 extern SparseComplexMatrix | |
506 dmsolve (const SparseComplexMatrix &a, const SparseMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
507 octave_idx_type &info); |
5683 | 508 |
509 extern SparseComplexMatrix | |
510 dmsolve (const SparseComplexMatrix &a, const SparseComplexMatrix &b, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
511 octave_idx_type &info); |
5683 | 512 #endif |