Mercurial > octave-nkf
comparison liboctave/dSparse.h @ 5164:57077d0ddc8e
[project @ 2005-02-25 19:55:24 by jwe]
author | jwe |
---|---|
date | Fri, 25 Feb 2005 19:55:28 +0000 |
parents | |
children | 23b37da9fd5b |
comparison
equal
deleted
inserted
replaced
5163:9f3299378193 | 5164:57077d0ddc8e |
---|---|
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 | |
49 SparseMatrix (int r, int c) : MSparse<double> (r, c) { } | |
50 | |
51 explicit SparseMatrix (int r, int c, double val) | |
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 | |
67 explicit SparseMatrix (const Array<double> a, const Array<int>& r, | |
68 const Array<int>& c, int nr = -1, | |
69 int nc = -1, bool sum_terms = true) | |
70 : MSparse<double> (a, r, c, nr, nc, sum_terms) { } | |
71 | |
72 explicit SparseMatrix (const Array<double> a, const Array<double>& r, | |
73 const Array<double>& c, int nr = -1, | |
74 int nc = -1, bool sum_terms = true) | |
75 : MSparse<double> (a, r, c, nr, nc, sum_terms) { } | |
76 | |
77 SparseMatrix (int r, int c, int num_nz) : MSparse<double> (r, c, num_nz) { } | |
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; | |
91 SparseMatrix max (Array2<int>& index, int dim = 0) const; | |
92 SparseMatrix min (int dim = 0) const; | |
93 SparseMatrix min (Array2<int>& index, int dim = 0) const; | |
94 | |
95 // destructive insert/delete/reorder operations | |
96 | |
97 SparseMatrix& insert (const SparseMatrix& a, int r, int c); | |
98 | |
99 SparseMatrix concat (const SparseMatrix& rb, const Array<int>& ra_idx); | |
100 SparseComplexMatrix concat (const SparseComplexMatrix& rb, | |
101 const Array<int>& ra_idx); | |
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; | |
116 SparseMatrix inverse (int& info) const; | |
117 SparseMatrix inverse (int& info, double& rcond, int force = 0, | |
118 int calc_cond = 1) const; | |
119 | |
120 DET determinant (void) const; | |
121 DET determinant (int& info) const; | |
122 DET determinant (int& info, double& rcond, int calc_cond = 1) const; | |
123 | |
124 private: | |
125 // Diagonal matrix solvers | |
126 Matrix dsolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
127 solve_singularity_handler sing_handler) const; | |
128 | |
129 ComplexMatrix dsolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
130 double& rcond, solve_singularity_handler sing_handler) const; | |
131 | |
132 SparseMatrix dsolve (SparseType &typ, const SparseMatrix& b, int& info, | |
133 double& rcond, solve_singularity_handler sing_handler) const; | |
134 | |
135 SparseComplexMatrix dsolve (SparseType &typ, const SparseComplexMatrix& b, | |
136 int& info, double& rcond, | |
137 solve_singularity_handler sing_handler) const; | |
138 | |
139 // Upper triangular matrix solvers | |
140 Matrix utsolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
141 solve_singularity_handler sing_handler) const; | |
142 | |
143 ComplexMatrix utsolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
144 double& rcond, solve_singularity_handler sing_handler) const; | |
145 | |
146 SparseMatrix utsolve (SparseType &typ, const SparseMatrix& b, int& info, | |
147 double& rcond, solve_singularity_handler sing_handler) const; | |
148 | |
149 SparseComplexMatrix utsolve (SparseType &typ, const SparseComplexMatrix& b, | |
150 int& info, double& rcond, | |
151 solve_singularity_handler sing_handler) const; | |
152 | |
153 // Lower triangular matrix solvers | |
154 Matrix ltsolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
155 solve_singularity_handler sing_handler) const; | |
156 | |
157 ComplexMatrix ltsolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
158 double& rcond, solve_singularity_handler sing_handler) const; | |
159 | |
160 SparseMatrix ltsolve (SparseType &typ, const SparseMatrix& b, int& info, | |
161 double& rcond, solve_singularity_handler sing_handler) const; | |
162 | |
163 SparseComplexMatrix ltsolve (SparseType &typ, const SparseComplexMatrix& b, | |
164 int& info, double& rcond, | |
165 solve_singularity_handler sing_handler) const; | |
166 | |
167 // Tridiagonal matrix solvers | |
168 Matrix trisolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
169 solve_singularity_handler sing_handler) const; | |
170 | |
171 ComplexMatrix trisolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
172 double& rcond, solve_singularity_handler sing_handler) const; | |
173 | |
174 SparseMatrix trisolve (SparseType &typ, const SparseMatrix& b, int& info, | |
175 double& rcond, solve_singularity_handler sing_handler) const; | |
176 | |
177 SparseComplexMatrix trisolve (SparseType &typ, const SparseComplexMatrix& b, | |
178 int& info, double& rcond, | |
179 solve_singularity_handler sing_handler) const; | |
180 | |
181 // Banded matrix solvers (umfpack/cholesky) | |
182 Matrix bsolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
183 solve_singularity_handler sing_handler) const; | |
184 | |
185 ComplexMatrix bsolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
186 double& rcond, solve_singularity_handler sing_handler) const; | |
187 | |
188 SparseMatrix bsolve (SparseType &typ, const SparseMatrix& b, int& info, | |
189 double& rcond, solve_singularity_handler sing_handler) const; | |
190 | |
191 SparseComplexMatrix bsolve (SparseType &typ, const SparseComplexMatrix& b, | |
192 int& info, double& rcond, | |
193 solve_singularity_handler sing_handler) const; | |
194 | |
195 // Full matrix solvers (umfpack/cholesky) | |
196 void * factorize (int& err, double &rcond, Matrix &Control, Matrix &Info, | |
197 solve_singularity_handler sing_handler) const; | |
198 | |
199 Matrix fsolve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
200 solve_singularity_handler sing_handler) const; | |
201 | |
202 ComplexMatrix fsolve (SparseType &typ, const ComplexMatrix& b, int& info, | |
203 double& rcond, solve_singularity_handler sing_handler) const; | |
204 | |
205 SparseMatrix fsolve (SparseType &typ, const SparseMatrix& b, int& info, | |
206 double& rcond, solve_singularity_handler sing_handler) const; | |
207 | |
208 SparseComplexMatrix fsolve (SparseType &typ, const SparseComplexMatrix& b, | |
209 int& info, double& rcond, | |
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; | |
215 Matrix solve (SparseType &typ, const Matrix& b, int& info) const; | |
216 Matrix solve (SparseType &typ, const Matrix& b, int& info, | |
217 double& rcond) const; | |
218 Matrix solve (SparseType &typ, const Matrix& b, int& info, double& rcond, | |
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, | |
223 int& info) const; | |
224 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b, int& info, | |
225 double& rcond) const; | |
226 ComplexMatrix solve (SparseType &typ, const ComplexMatrix& b, int& info, | |
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, | |
231 int& info) const; | |
232 SparseMatrix solve (SparseType &typ, const SparseMatrix& b, int& info, | |
233 double& rcond) const; | |
234 SparseMatrix solve (SparseType &typ, const SparseMatrix& b, int& info, | |
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, | |
240 int& info) const; | |
241 SparseComplexMatrix solve (SparseType &typ, const SparseComplexMatrix& b, | |
242 int& info, double& rcond) const; | |
243 SparseComplexMatrix solve (SparseType &typ, const SparseComplexMatrix& b, int& info, | |
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, | |
248 int& info) const; | |
249 ColumnVector solve (SparseType &typ, const ColumnVector& b, | |
250 int& info, double& rcond) const; | |
251 ColumnVector solve (SparseType &typ, const ColumnVector& b, int& info, | |
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, | |
257 const ComplexColumnVector& b, int& info) const; | |
258 ComplexColumnVector solve (SparseType &typ, const ComplexColumnVector& b, | |
259 int& info, double& rcond) const; | |
260 ComplexColumnVector solve (SparseType &typ, const ComplexColumnVector& b, | |
261 int& info, double& rcond, | |
262 solve_singularity_handler sing_handler) const; | |
263 | |
264 // Generic interface to solver with probing of type | |
265 Matrix solve (const Matrix& b) const; | |
266 Matrix solve (const Matrix& b, int& info) const; | |
267 Matrix solve (const Matrix& b, int& info, double& rcond) const; | |
268 Matrix solve (const Matrix& b, int& info, double& rcond, | |
269 solve_singularity_handler sing_handler) const; | |
270 | |
271 ComplexMatrix solve (const ComplexMatrix& b) const; | |
272 ComplexMatrix solve (const ComplexMatrix& b, int& info) const; | |
273 ComplexMatrix solve (const ComplexMatrix& b, int& info, | |
274 double& rcond) const; | |
275 ComplexMatrix solve (const ComplexMatrix& b, int& info, double& rcond, | |
276 solve_singularity_handler sing_handler) const; | |
277 | |
278 SparseMatrix solve (const SparseMatrix& b) const; | |
279 SparseMatrix solve (const SparseMatrix& b, int& info) const; | |
280 SparseMatrix solve (const SparseMatrix& b, int& info, | |
281 double& rcond) const; | |
282 SparseMatrix solve (const SparseMatrix& b, int& info, double& rcond, | |
283 solve_singularity_handler sing_handler) const; | |
284 | |
285 SparseComplexMatrix solve (const SparseComplexMatrix& b) const; | |
286 SparseComplexMatrix solve (const SparseComplexMatrix& b, int& info) const; | |
287 SparseComplexMatrix solve (const SparseComplexMatrix& b, int& info, | |
288 double& rcond) const; | |
289 SparseComplexMatrix solve (const SparseComplexMatrix& b, int& info, | |
290 double& rcond, | |
291 solve_singularity_handler sing_handler) const; | |
292 | |
293 ColumnVector solve (const ColumnVector& b) const; | |
294 ColumnVector solve (const ColumnVector& b, int& info) const; | |
295 ColumnVector solve (const ColumnVector& b, int& info, double& rcond) const; | |
296 ColumnVector solve (const ColumnVector& b, int& info, double& rcond, | |
297 solve_singularity_handler sing_handler) const; | |
298 | |
299 ComplexColumnVector solve (const ComplexColumnVector& b) const; | |
300 ComplexColumnVector solve (const ComplexColumnVector& b, int& info) const; | |
301 ComplexColumnVector solve (const ComplexColumnVector& b, int& info, | |
302 double& rcond) const; | |
303 ComplexColumnVector solve (const ComplexColumnVector& b, int& info, | |
304 double& rcond, | |
305 solve_singularity_handler sing_handler) const; | |
306 | |
307 // Minimum-norm solvers | |
308 Matrix lssolve (const Matrix& b) const; | |
309 Matrix lssolve (const Matrix& b, int& info) const; | |
310 Matrix lssolve (const Matrix& b, int& info, int& rank) const; | |
311 | |
312 ComplexMatrix lssolve (const ComplexMatrix& b) const; | |
313 ComplexMatrix lssolve (const ComplexMatrix& b, int& info) const; | |
314 ComplexMatrix lssolve (const ComplexMatrix& b, int& info, | |
315 int& rank) const; | |
316 | |
317 SparseMatrix lssolve (const SparseMatrix& b) const; | |
318 SparseMatrix lssolve (const SparseMatrix& b, int& info) const; | |
319 SparseMatrix lssolve (const SparseMatrix& b, int& info, int& rank) const; | |
320 | |
321 SparseComplexMatrix lssolve (const SparseComplexMatrix& b) const; | |
322 SparseComplexMatrix lssolve (const SparseComplexMatrix& b, | |
323 int& info) const; | |
324 SparseComplexMatrix lssolve (const SparseComplexMatrix& b, int& info, | |
325 int& rank) const; | |
326 | |
327 ColumnVector lssolve (const ColumnVector& b) const; | |
328 ColumnVector lssolve (const ColumnVector& b, int& info) const; | |
329 ColumnVector lssolve (const ColumnVector& b, int& info, int& rank) const; | |
330 | |
331 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; | |
332 ComplexColumnVector lssolve (const ComplexColumnVector& b, int& info) const; | |
333 ComplexColumnVector lssolve (const ComplexColumnVector& b, int& info, | |
334 int& rank) const; | |
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 | |
360 SparseMatrix diag (int k = 0) const; | |
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 | |
374 SparseMatrix permute (const Array<int>& vec, bool inv = false) const; | |
375 | |
376 SparseMatrix ipermute (const Array<int>& vec) const; | |
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 */ |