5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 Copyright (C) 1998-2004 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
|
17 along with this program; see the file COPYING. If not, write to the Free |
|
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
19 |
|
20 */ |
|
21 |
|
22 #if !defined (octave_dSparse_h) |
|
23 #define octave_dSparse_h 1 |
|
24 |
|
25 #include "dMatrix.h" |
|
26 #include "dNDArray.h" |
|
27 #include "CMatrix.h" |
|
28 #include "dColVector.h" |
|
29 #include "CColVector.h" |
|
30 |
|
31 #include "dbleDET.h" |
|
32 #include "MSparse.h" |
|
33 #include "MSparse-defs.h" |
|
34 #include "Sparse-op-defs.h" |
|
35 #include "SparseType.h" |
|
36 |
|
37 class SparseComplexMatrix; |
|
38 class SparseBoolMatrix; |
|
39 |
|
40 class |
|
41 SparseMatrix : public MSparse<double> |
|
42 { |
|
43 public: |
|
44 |
|
45 typedef void (*solve_singularity_handler) (double rcond); |
|
46 |
|
47 SparseMatrix (void) : MSparse<double> () { } |
|
48 |
5275
|
49 SparseMatrix (octave_idx_type r, octave_idx_type c) : MSparse<double> (r, c) { } |
5164
|
50 |
5275
|
51 explicit SparseMatrix (octave_idx_type r, octave_idx_type c, double val) |
5164
|
52 : MSparse<double> (r, c, val) { } |
|
53 |
|
54 SparseMatrix (const SparseMatrix& a) : MSparse<double> (a) { } |
|
55 |
|
56 SparseMatrix (const SparseMatrix& a, const dim_vector& dv) |
|
57 : MSparse<double> (a, dv) { } |
|
58 |
|
59 SparseMatrix (const MSparse<double>& a) : MSparse<double> (a) { } |
|
60 |
|
61 explicit SparseMatrix (const SparseBoolMatrix& a); |
|
62 |
|
63 explicit SparseMatrix (const Matrix& a) : MSparse<double> (a) { } |
|
64 |
|
65 explicit SparseMatrix (const NDArray& a) : MSparse<double> (a) { } |
|
66 |
5275
|
67 explicit SparseMatrix (const Array<double> a, const Array<octave_idx_type>& r, |
|
68 const Array<octave_idx_type>& c, octave_idx_type nr = -1, |
|
69 octave_idx_type nc = -1, bool sum_terms = true) |
5164
|
70 : MSparse<double> (a, r, c, nr, nc, sum_terms) { } |
|
71 |
|
72 explicit SparseMatrix (const Array<double> a, const Array<double>& r, |
5275
|
73 const Array<double>& c, octave_idx_type nr = -1, |
|
74 octave_idx_type nc = -1, bool sum_terms = true) |
5164
|
75 : MSparse<double> (a, r, c, nr, nc, sum_terms) { } |
|
76 |
5275
|
77 SparseMatrix (octave_idx_type r, octave_idx_type c, octave_idx_type num_nz) : MSparse<double> (r, c, num_nz) { } |
5164
|
78 |
|
79 SparseMatrix& operator = (const SparseMatrix& a) |
|
80 { |
|
81 MSparse<double>::operator = (a); |
|
82 return *this; |
|
83 } |
|
84 |
|
85 bool operator == (const SparseMatrix& a) const; |
|
86 bool operator != (const SparseMatrix& a) const; |
|
87 |
|
88 bool is_symmetric (void) const; |
|
89 |
|
90 SparseMatrix max (int dim = 0) const; |
5275
|
91 SparseMatrix max (Array2<octave_idx_type>& index, int dim = 0) const; |
5164
|
92 SparseMatrix min (int dim = 0) const; |
5275
|
93 SparseMatrix min (Array2<octave_idx_type>& index, int dim = 0) const; |
5164
|
94 |
|
95 // destructive insert/delete/reorder operations |
|
96 |
5275
|
97 SparseMatrix& insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c); |
5164
|
98 |
5275
|
99 SparseMatrix concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx); |
5164
|
100 SparseComplexMatrix concat (const SparseComplexMatrix& rb, |
5275
|
101 const Array<octave_idx_type>& ra_idx); |
5164
|
102 |
|
103 friend SparseMatrix real (const SparseComplexMatrix& a); |
|
104 friend SparseMatrix imag (const SparseComplexMatrix& a); |
|
105 |
|
106 friend SparseMatrix atan2 (const double& x, const SparseMatrix& y); |
|
107 friend SparseMatrix atan2 (const SparseMatrix& x, const double& y); |
|
108 friend SparseMatrix atan2 (const SparseMatrix& x, const SparseMatrix& y); |
|
109 |
|
110 SparseMatrix transpose (void) const |
|
111 { |
|
112 return MSparse<double>::transpose (); |
|
113 } |
|
114 |
|
115 SparseMatrix inverse (void) const; |
5275
|
116 SparseMatrix inverse (octave_idx_type& info) const; |
|
117 SparseMatrix inverse (octave_idx_type& info, double& rcond, int force = 0, |
5164
|
118 int calc_cond = 1) const; |
|
119 |
|
120 DET determinant (void) const; |
5275
|
121 DET determinant (octave_idx_type& info) const; |
|
122 DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const; |
5164
|
123 |
|
124 private: |
|
125 // Diagonal matrix solvers |
5275
|
126 Matrix dsolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
127 solve_singularity_handler sing_handler) const; |
|
128 |
5275
|
129 ComplexMatrix dsolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
130 double& rcond, solve_singularity_handler sing_handler) const; |
|
131 |
5275
|
132 SparseMatrix dsolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
133 double& rcond, solve_singularity_handler sing_handler) const; |
|
134 |
|
135 SparseComplexMatrix dsolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
136 octave_idx_type& info, double& rcond, |
5164
|
137 solve_singularity_handler sing_handler) const; |
|
138 |
|
139 // Upper triangular matrix solvers |
5275
|
140 Matrix utsolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
141 solve_singularity_handler sing_handler) const; |
|
142 |
5275
|
143 ComplexMatrix utsolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
144 double& rcond, solve_singularity_handler sing_handler) const; |
|
145 |
5275
|
146 SparseMatrix utsolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
147 double& rcond, solve_singularity_handler sing_handler) const; |
|
148 |
|
149 SparseComplexMatrix utsolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
150 octave_idx_type& info, double& rcond, |
5164
|
151 solve_singularity_handler sing_handler) const; |
|
152 |
|
153 // Lower triangular matrix solvers |
5275
|
154 Matrix ltsolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
155 solve_singularity_handler sing_handler) const; |
|
156 |
5275
|
157 ComplexMatrix ltsolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
158 double& rcond, solve_singularity_handler sing_handler) const; |
|
159 |
5275
|
160 SparseMatrix ltsolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
161 double& rcond, solve_singularity_handler sing_handler) const; |
|
162 |
|
163 SparseComplexMatrix ltsolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
164 octave_idx_type& info, double& rcond, |
5164
|
165 solve_singularity_handler sing_handler) const; |
|
166 |
|
167 // Tridiagonal matrix solvers |
5275
|
168 Matrix trisolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
169 solve_singularity_handler sing_handler) const; |
|
170 |
5275
|
171 ComplexMatrix trisolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
172 double& rcond, solve_singularity_handler sing_handler) const; |
|
173 |
5275
|
174 SparseMatrix trisolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
175 double& rcond, solve_singularity_handler sing_handler) const; |
|
176 |
|
177 SparseComplexMatrix trisolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
178 octave_idx_type& info, double& rcond, |
5164
|
179 solve_singularity_handler sing_handler) const; |
|
180 |
|
181 // Banded matrix solvers (umfpack/cholesky) |
5275
|
182 Matrix bsolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
183 solve_singularity_handler sing_handler) const; |
|
184 |
5275
|
185 ComplexMatrix bsolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
186 double& rcond, solve_singularity_handler sing_handler) const; |
|
187 |
5275
|
188 SparseMatrix bsolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
189 double& rcond, solve_singularity_handler sing_handler) const; |
|
190 |
|
191 SparseComplexMatrix bsolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
192 octave_idx_type& info, double& rcond, |
5164
|
193 solve_singularity_handler sing_handler) const; |
|
194 |
|
195 // Full matrix solvers (umfpack/cholesky) |
5275
|
196 void * factorize (octave_idx_type& err, double &rcond, Matrix &Control, Matrix &Info, |
5164
|
197 solve_singularity_handler sing_handler) const; |
|
198 |
5275
|
199 Matrix fsolve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
200 solve_singularity_handler sing_handler) const; |
|
201 |
5275
|
202 ComplexMatrix fsolve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
203 double& rcond, solve_singularity_handler sing_handler) const; |
|
204 |
5275
|
205 SparseMatrix fsolve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
206 double& rcond, solve_singularity_handler sing_handler) const; |
|
207 |
|
208 SparseComplexMatrix fsolve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
209 octave_idx_type& info, double& rcond, |
5164
|
210 solve_singularity_handler sing_handler) const; |
|
211 |
|
212 public: |
|
213 // Generic interface to solver with no probing of type |
|
214 Matrix solve (SparseType &typ, const Matrix& b) const; |
5275
|
215 Matrix solve (SparseType &typ, const Matrix& b, octave_idx_type& info) const; |
|
216 Matrix solve (SparseType &typ, const Matrix& b, octave_idx_type& info, |
5164
|
217 double& rcond) const; |
5275
|
218 Matrix solve (SparseType &typ, const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
219 solve_singularity_handler sing_handler) const; |
|
220 |
|
221 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b) const; |
|
222 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b, |
5275
|
223 octave_idx_type& info) const; |
|
224 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
225 double& rcond) const; |
5275
|
226 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b, octave_idx_type& info, |
5164
|
227 double& rcond, solve_singularity_handler sing_handler) const; |
|
228 |
|
229 SparseMatrix solve (SparseType &typ, const SparseMatrix& b) const; |
|
230 SparseMatrix solve (SparseType &typ, const SparseMatrix& b, |
5275
|
231 octave_idx_type& info) const; |
|
232 SparseMatrix solve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
233 double& rcond) const; |
5275
|
234 SparseMatrix solve (SparseType &typ, const SparseMatrix& b, octave_idx_type& info, |
5164
|
235 double& rcond, solve_singularity_handler sing_handler) const; |
|
236 |
|
237 SparseComplexMatrix solve (SparseType &typ, |
|
238 const SparseComplexMatrix& b) const; |
|
239 SparseComplexMatrix solve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
240 octave_idx_type& info) const; |
5164
|
241 SparseComplexMatrix solve (SparseType &typ, const SparseComplexMatrix& b, |
5275
|
242 octave_idx_type& info, double& rcond) const; |
|
243 SparseComplexMatrix solve (SparseType &typ, const SparseComplexMatrix& b, octave_idx_type& info, |
5164
|
244 double& rcond, solve_singularity_handler sing_handler) const; |
|
245 |
|
246 ColumnVector solve (SparseType &typ, const ColumnVector& b) const; |
|
247 ColumnVector solve (SparseType &typ, const ColumnVector& b, |
5275
|
248 octave_idx_type& info) const; |
5164
|
249 ColumnVector solve (SparseType &typ, const ColumnVector& b, |
5275
|
250 octave_idx_type& info, double& rcond) const; |
|
251 ColumnVector solve (SparseType &typ, const ColumnVector& b, octave_idx_type& info, |
5164
|
252 double& rcond, solve_singularity_handler sing_handler) const; |
|
253 |
|
254 ComplexColumnVector solve (SparseType &typ, |
|
255 const ComplexColumnVector& b) const; |
|
256 ComplexColumnVector solve (SparseType &typ, |
5275
|
257 const ComplexColumnVector& b, octave_idx_type& info) const; |
5164
|
258 ComplexColumnVector solve (SparseType &typ, const ComplexColumnVector& b, |
5275
|
259 octave_idx_type& info, double& rcond) const; |
5164
|
260 ComplexColumnVector solve (SparseType &typ, const ComplexColumnVector& b, |
5275
|
261 octave_idx_type& info, double& rcond, |
5164
|
262 solve_singularity_handler sing_handler) const; |
|
263 |
|
264 // Generic interface to solver with probing of type |
|
265 Matrix solve (const Matrix& b) const; |
5275
|
266 Matrix solve (const Matrix& b, octave_idx_type& info) const; |
|
267 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const; |
|
268 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond, |
5164
|
269 solve_singularity_handler sing_handler) const; |
|
270 |
|
271 ComplexMatrix solve (const ComplexMatrix& b) const; |
5275
|
272 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
273 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, |
5164
|
274 double& rcond) const; |
5275
|
275 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
5164
|
276 solve_singularity_handler sing_handler) const; |
|
277 |
|
278 SparseMatrix solve (const SparseMatrix& b) const; |
5275
|
279 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info) const; |
|
280 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info, |
5164
|
281 double& rcond) const; |
5275
|
282 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info, double& rcond, |
5164
|
283 solve_singularity_handler sing_handler) const; |
|
284 |
|
285 SparseComplexMatrix solve (const SparseComplexMatrix& b) const; |
5275
|
286 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info) const; |
|
287 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info, |
5164
|
288 double& rcond) const; |
5275
|
289 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info, |
5164
|
290 double& rcond, |
|
291 solve_singularity_handler sing_handler) const; |
|
292 |
|
293 ColumnVector solve (const ColumnVector& b) const; |
5275
|
294 ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const; |
|
295 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const; |
|
296 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
5164
|
297 solve_singularity_handler sing_handler) const; |
|
298 |
|
299 ComplexColumnVector solve (const ComplexColumnVector& b) const; |
5275
|
300 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
301 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
5164
|
302 double& rcond) const; |
5275
|
303 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
5164
|
304 double& rcond, |
|
305 solve_singularity_handler sing_handler) const; |
|
306 |
|
307 // Minimum-norm solvers |
|
308 Matrix lssolve (const Matrix& b) const; |
5275
|
309 Matrix lssolve (const Matrix& b, octave_idx_type& info) const; |
|
310 Matrix lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const; |
5164
|
311 |
|
312 ComplexMatrix lssolve (const ComplexMatrix& b) const; |
5275
|
313 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
314 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
315 octave_idx_type& rank) const; |
5164
|
316 |
|
317 SparseMatrix lssolve (const SparseMatrix& b) const; |
5275
|
318 SparseMatrix lssolve (const SparseMatrix& b, octave_idx_type& info) const; |
|
319 SparseMatrix lssolve (const SparseMatrix& b, octave_idx_type& info, octave_idx_type& rank) const; |
5164
|
320 |
|
321 SparseComplexMatrix lssolve (const SparseComplexMatrix& b) const; |
|
322 SparseComplexMatrix lssolve (const SparseComplexMatrix& b, |
5275
|
323 octave_idx_type& info) const; |
|
324 SparseComplexMatrix lssolve (const SparseComplexMatrix& b, octave_idx_type& info, |
|
325 octave_idx_type& rank) const; |
5164
|
326 |
|
327 ColumnVector lssolve (const ColumnVector& b) const; |
5275
|
328 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const; |
|
329 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const; |
5164
|
330 |
|
331 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; |
5275
|
332 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
333 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
334 octave_idx_type& rank) const; |
5164
|
335 |
|
336 // other operations |
|
337 SparseMatrix map (d_d_Mapper f) const; |
|
338 SparseBoolMatrix map (b_d_Mapper f) const; |
|
339 |
|
340 SparseMatrix& apply (d_d_Mapper f); |
|
341 |
|
342 bool any_element_is_negative (bool = false) const; |
|
343 bool any_element_is_inf_or_nan (void) const; |
|
344 bool all_elements_are_int_or_inf_or_nan (void) const; |
|
345 bool all_integers (double& max_val, double& min_val) const; |
|
346 bool too_large_for_float (void) const; |
|
347 |
|
348 SparseBoolMatrix operator ! (void) const; |
|
349 |
|
350 SparseBoolMatrix all (int dim = -1) const; |
|
351 SparseBoolMatrix any (int dim = -1) const; |
|
352 |
|
353 SparseMatrix cumprod (int dim = -1) const; |
|
354 SparseMatrix cumsum (int dim = -1) const; |
|
355 SparseMatrix prod (int dim = -1) const; |
|
356 SparseMatrix sum (int dim = -1) const; |
|
357 SparseMatrix sumsq (int dim = -1) const; |
|
358 SparseMatrix abs (void) const; |
|
359 |
5275
|
360 SparseMatrix diag (octave_idx_type k = 0) const; |
5164
|
361 |
|
362 Matrix matrix_value (void) const; |
|
363 |
|
364 SparseMatrix squeeze (void) const; |
|
365 |
|
366 SparseMatrix index (idx_vector& i, int resize_ok) const; |
|
367 |
|
368 SparseMatrix index (idx_vector& i, idx_vector& j, int resize_ok) const; |
|
369 |
|
370 SparseMatrix index (Array<idx_vector>& ra_idx, int resize_ok) const; |
|
371 |
|
372 SparseMatrix reshape (const dim_vector& new_dims) const; |
|
373 |
5275
|
374 SparseMatrix permute (const Array<octave_idx_type>& vec, bool inv = false) const; |
5164
|
375 |
5275
|
376 SparseMatrix ipermute (const Array<octave_idx_type>& vec) const; |
5164
|
377 |
|
378 // i/o |
|
379 |
|
380 friend std::ostream& operator << (std::ostream& os, const SparseMatrix& a); |
|
381 friend std::istream& operator >> (std::istream& is, SparseMatrix& a); |
|
382 }; |
|
383 |
|
384 extern SparseMatrix operator * (const SparseMatrix& a, |
|
385 const SparseMatrix& b); |
|
386 |
|
387 extern SparseMatrix min (double d, const SparseMatrix& m); |
|
388 extern SparseMatrix min (const SparseMatrix& m, double d); |
|
389 extern SparseMatrix min (const SparseMatrix& a, const SparseMatrix& b); |
|
390 |
|
391 extern SparseMatrix max (double d, const SparseMatrix& m); |
|
392 extern SparseMatrix max (const SparseMatrix& m, double d); |
|
393 extern SparseMatrix max (const SparseMatrix& a, const SparseMatrix& b); |
|
394 |
|
395 SPARSE_SMS_CMP_OP_DECLS (SparseMatrix, double) |
|
396 SPARSE_SMS_BOOL_OP_DECLS (SparseMatrix, double) |
|
397 |
|
398 SPARSE_SSM_CMP_OP_DECLS (double, SparseMatrix) |
|
399 SPARSE_SSM_BOOL_OP_DECLS (double, SparseMatrix) |
|
400 |
|
401 SPARSE_SMSM_CMP_OP_DECLS (SparseMatrix, SparseMatrix) |
|
402 SPARSE_SMSM_BOOL_OP_DECLS (SparseMatrix, SparseMatrix) |
|
403 |
|
404 SPARSE_FORWARD_DEFS (MSparse, SparseMatrix, Matrix, double) |
|
405 |
|
406 #endif |
|
407 |
|
408 /* |
|
409 ;;; Local Variables: *** |
|
410 ;;; mode: C++ *** |
|
411 ;;; End: *** |
|
412 */ |