458
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
|
4 2004, 2005, 2006, 2007 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
458
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
458
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_Matrix_int_h) |
|
25 #define octave_Matrix_int_h 1 |
|
26 |
1989
|
27 #include "MArray2.h" |
|
28 #include "MDiagArray2.h" |
5785
|
29 #include "MatrixType.h" |
458
|
30 |
|
31 #include "mx-defs.h" |
2870
|
32 #include "mx-op-defs.h" |
458
|
33 |
3585
|
34 class |
6108
|
35 OCTAVE_API |
3585
|
36 Matrix : public MArray2<double> |
458
|
37 { |
|
38 public: |
|
39 |
3480
|
40 typedef void (*solve_singularity_handler) (double rcond); |
|
41 |
1214
|
42 Matrix (void) : MArray2<double> () { } |
3585
|
43 |
5275
|
44 Matrix (octave_idx_type r, octave_idx_type c) : MArray2<double> (r, c) { } |
3585
|
45 |
5275
|
46 Matrix (octave_idx_type r, octave_idx_type c, double val) : MArray2<double> (r, c, val) { } |
3585
|
47 |
6979
|
48 Matrix (const dim_vector& dv) : MArray2<double> (dv) { } |
|
49 |
|
50 Matrix (const dim_vector& dv, double val) : MArray2<double> (dv, val) { } |
|
51 |
1214
|
52 Matrix (const Matrix& a) : MArray2<double> (a) { } |
3585
|
53 |
|
54 Matrix (const MArray2<double>& a) : MArray2<double> (a) { } |
|
55 |
|
56 explicit Matrix (const RowVector& rv); |
458
|
57 |
3585
|
58 explicit Matrix (const ColumnVector& cv); |
|
59 |
|
60 explicit Matrix (const DiagMatrix& a); |
|
61 |
|
62 explicit Matrix (const boolMatrix& a); |
|
63 |
|
64 explicit Matrix (const charMatrix& a); |
1574
|
65 |
458
|
66 Matrix& operator = (const Matrix& a) |
|
67 { |
1214
|
68 MArray2<double>::operator = (a); |
458
|
69 return *this; |
|
70 } |
|
71 |
2385
|
72 bool operator == (const Matrix& a) const; |
|
73 bool operator != (const Matrix& a) const; |
458
|
74 |
3354
|
75 bool is_symmetric (void) const; |
|
76 |
1359
|
77 // destructive insert/delete/reorder operations |
458
|
78 |
5275
|
79 Matrix& insert (const Matrix& a, octave_idx_type r, octave_idx_type c); |
|
80 Matrix& insert (const RowVector& a, octave_idx_type r, octave_idx_type c); |
|
81 Matrix& insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c); |
|
82 Matrix& insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c); |
458
|
83 |
|
84 Matrix& fill (double val); |
5275
|
85 Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2); |
458
|
86 |
|
87 Matrix append (const Matrix& a) const; |
|
88 Matrix append (const RowVector& a) const; |
|
89 Matrix append (const ColumnVector& a) const; |
|
90 Matrix append (const DiagMatrix& a) const; |
|
91 |
|
92 Matrix stack (const Matrix& a) const; |
|
93 Matrix stack (const RowVector& a) const; |
|
94 Matrix stack (const ColumnVector& a) const; |
|
95 Matrix stack (const DiagMatrix& a) const; |
|
96 |
6108
|
97 friend OCTAVE_API Matrix real (const ComplexMatrix& a); |
|
98 friend OCTAVE_API Matrix imag (const ComplexMatrix& a); |
1205
|
99 |
3225
|
100 Matrix transpose (void) const { return MArray2<double>::transpose (); } |
|
101 |
1359
|
102 // resize is the destructive equivalent for this one |
458
|
103 |
5275
|
104 Matrix extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const; |
458
|
105 |
5275
|
106 Matrix extract_n (octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const; |
4316
|
107 |
1359
|
108 // extract row or column i. |
458
|
109 |
5275
|
110 RowVector row (octave_idx_type i) const; |
458
|
111 |
5275
|
112 ColumnVector column (octave_idx_type i) const; |
458
|
113 |
6207
|
114 private: |
|
115 Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
116 int force, int calc_cond) const; |
|
117 |
|
118 Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
119 int force, int calc_cond) const; |
|
120 |
|
121 public: |
458
|
122 Matrix inverse (void) const; |
6479
|
123 Matrix inverse (octave_idx_type& info) const; |
|
124 Matrix inverse (octave_idx_type& info, double& rcond, int force = 0, |
|
125 int calc_cond = 1) const; |
|
126 |
6207
|
127 Matrix inverse (MatrixType &mattype) const; |
|
128 Matrix inverse (MatrixType &mattype, octave_idx_type& info) const; |
|
129 Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
130 int force = 0, int calc_cond = 1) const; |
458
|
131 |
4384
|
132 Matrix pseudo_inverse (double tol = 0.0) const; |
740
|
133 |
458
|
134 ComplexMatrix fourier (void) const; |
|
135 ComplexMatrix ifourier (void) const; |
|
136 |
677
|
137 ComplexMatrix fourier2d (void) const; |
|
138 ComplexMatrix ifourier2d (void) const; |
|
139 |
458
|
140 DET determinant (void) const; |
5275
|
141 DET determinant (octave_idx_type& info) const; |
|
142 DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const; |
458
|
143 |
5785
|
144 private: |
|
145 // Upper triangular matrix solvers |
|
146 Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
147 double& rcond, solve_singularity_handler sing_handler, |
|
148 bool calc_cond = false) const; |
|
149 |
|
150 // Lower triangular matrix solvers |
|
151 Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
152 double& rcond, solve_singularity_handler sing_handler, |
|
153 bool calc_cond = false) const; |
|
154 |
|
155 // Full matrix solvers (lu/cholesky) |
|
156 Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
157 double& rcond, solve_singularity_handler sing_handler, |
|
158 bool calc_cond = false) const; |
|
159 |
|
160 public: |
|
161 // Generic interface to solver with no probing of type |
|
162 Matrix solve (MatrixType &typ, const Matrix& b) const; |
|
163 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const; |
|
164 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
165 double& rcond) const; |
|
166 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
167 double& rcond, solve_singularity_handler sing_handler, |
|
168 bool singular_fallback = true) const; |
|
169 |
|
170 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const; |
|
171 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
172 octave_idx_type& info) const; |
|
173 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
174 octave_idx_type& info, double& rcond) const; |
|
175 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
176 octave_idx_type& info, double& rcond, |
|
177 solve_singularity_handler sing_handler, |
|
178 bool singular_fallback = true) const; |
|
179 |
|
180 ColumnVector solve (MatrixType &typ, const ColumnVector& b) const; |
|
181 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
182 octave_idx_type& info) const; |
|
183 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
184 octave_idx_type& info, double& rcond) const; |
|
185 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
186 octave_idx_type& info, double& rcond, |
|
187 solve_singularity_handler sing_handler) const; |
|
188 |
|
189 ComplexColumnVector solve (MatrixType &typ, |
|
190 const ComplexColumnVector& b) const; |
|
191 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
192 octave_idx_type& info) const; |
|
193 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
194 octave_idx_type& info, double& rcond) const; |
|
195 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
196 octave_idx_type& info, double& rcond, |
|
197 solve_singularity_handler sing_handler) const; |
|
198 |
|
199 // Generic interface to solver with probing of type |
458
|
200 Matrix solve (const Matrix& b) const; |
5275
|
201 Matrix solve (const Matrix& b, octave_idx_type& info) const; |
|
202 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const; |
|
203 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond, |
3480
|
204 solve_singularity_handler sing_handler) const; |
458
|
205 |
|
206 ComplexMatrix solve (const ComplexMatrix& b) const; |
5275
|
207 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
208 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const; |
|
209 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
210 solve_singularity_handler sing_handler) const; |
458
|
211 |
|
212 ColumnVector solve (const ColumnVector& b) const; |
5275
|
213 ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const; |
|
214 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const; |
|
215 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
216 solve_singularity_handler sing_handler) const; |
458
|
217 |
|
218 ComplexColumnVector solve (const ComplexColumnVector& b) const; |
5275
|
219 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
220 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
458
|
221 double& rcond) const; |
5275
|
222 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
3480
|
223 double& rcond, |
|
224 solve_singularity_handler sing_handler) const; |
458
|
225 |
5785
|
226 // Singular solvers |
458
|
227 Matrix lssolve (const Matrix& b) const; |
5275
|
228 Matrix lssolve (const Matrix& b, octave_idx_type& info) const; |
7076
|
229 Matrix lssolve (const Matrix& b, octave_idx_type& info, |
|
230 octave_idx_type& rank) const; |
|
231 Matrix lssolve (const Matrix& b, octave_idx_type& info, |
|
232 octave_idx_type& rank, double& rcond) const; |
458
|
233 |
|
234 ComplexMatrix lssolve (const ComplexMatrix& b) const; |
5275
|
235 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
236 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
237 octave_idx_type& rank) const; |
7076
|
238 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
239 octave_idx_type& rank, double &rcond) const; |
458
|
240 |
|
241 ColumnVector lssolve (const ColumnVector& b) const; |
5275
|
242 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const; |
7076
|
243 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info, |
|
244 octave_idx_type& rank) const; |
|
245 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info, |
|
246 octave_idx_type& rank, double& rcond) const; |
458
|
247 |
|
248 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; |
7076
|
249 ComplexColumnVector lssolve (const ComplexColumnVector& b, |
|
250 octave_idx_type& info) const; |
|
251 ComplexColumnVector lssolve (const ComplexColumnVector& b, |
|
252 octave_idx_type& info, |
5275
|
253 octave_idx_type& rank) const; |
7076
|
254 ComplexColumnVector lssolve (const ComplexColumnVector& b, |
|
255 octave_idx_type& info, |
|
256 octave_idx_type& rank, double& rcond) const; |
458
|
257 |
1819
|
258 Matrix expm (void) const; |
|
259 |
458
|
260 Matrix& operator += (const DiagMatrix& a); |
|
261 Matrix& operator -= (const DiagMatrix& a); |
|
262 |
1359
|
263 // unary operations |
458
|
264 |
2964
|
265 boolMatrix operator ! (void) const; |
458
|
266 |
1359
|
267 // other operations |
458
|
268 |
2676
|
269 Matrix map (d_d_Mapper f) const; |
3248
|
270 boolMatrix map (b_d_Mapper f) const; |
2676
|
271 |
|
272 Matrix& apply (d_d_Mapper f); |
458
|
273 |
4431
|
274 bool any_element_is_negative (bool = false) const; |
2385
|
275 bool any_element_is_inf_or_nan (void) const; |
5943
|
276 bool any_element_not_one_or_zero (void) const; |
2385
|
277 bool all_elements_are_int_or_inf_or_nan (void) const; |
|
278 bool all_integers (double& max_val, double& min_val) const; |
|
279 bool too_large_for_float (void) const; |
1963
|
280 |
4017
|
281 boolMatrix all (int dim = -1) const; |
|
282 boolMatrix any (int dim = -1) const; |
458
|
283 |
4017
|
284 Matrix cumprod (int dim = -1) const; |
|
285 Matrix cumsum (int dim = -1) const; |
|
286 Matrix prod (int dim = -1) const; |
|
287 Matrix sum (int dim = -1) const; |
|
288 Matrix sumsq (int dim = -1) const; |
2385
|
289 Matrix abs (void) const; |
458
|
290 |
|
291 ColumnVector diag (void) const; |
5275
|
292 ColumnVector diag (octave_idx_type k) const; |
458
|
293 |
|
294 ColumnVector row_min (void) const; |
2354
|
295 ColumnVector row_max (void) const; |
458
|
296 |
5275
|
297 ColumnVector row_min (Array<octave_idx_type>& index) const; |
|
298 ColumnVector row_max (Array<octave_idx_type>& index) const; |
458
|
299 |
|
300 RowVector column_min (void) const; |
2354
|
301 RowVector column_max (void) const; |
458
|
302 |
5275
|
303 RowVector column_min (Array<octave_idx_type>& index) const; |
|
304 RowVector column_max (Array<octave_idx_type>& index) const; |
458
|
305 |
1359
|
306 // i/o |
458
|
307 |
6108
|
308 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Matrix& a); |
|
309 friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a); |
458
|
310 |
3933
|
311 static double resize_fill_value (void) { return 0; } |
|
312 |
458
|
313 private: |
|
314 |
5275
|
315 Matrix (double *d, octave_idx_type r, octave_idx_type c) : MArray2<double> (d, r, c) { } |
458
|
316 }; |
|
317 |
5508
|
318 // Publish externally used friend functions. |
|
319 |
6108
|
320 extern OCTAVE_API Matrix real (const ComplexMatrix& a); |
|
321 extern OCTAVE_API Matrix imag (const ComplexMatrix& a); |
5508
|
322 |
|
323 // column vector by row vector -> matrix operations |
|
324 |
6108
|
325 extern OCTAVE_API Matrix operator * (const ColumnVector& a, const RowVector& b); |
5508
|
326 |
|
327 // Other functions. |
|
328 |
6108
|
329 extern OCTAVE_API Matrix Givens (double, double); |
1819
|
330 |
6108
|
331 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&, const Matrix&); |
1959
|
332 |
6108
|
333 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b); |
2828
|
334 |
6108
|
335 extern OCTAVE_API Matrix min (double d, const Matrix& m); |
|
336 extern OCTAVE_API Matrix min (const Matrix& m, double d); |
|
337 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b); |
4309
|
338 |
6108
|
339 extern OCTAVE_API Matrix max (double d, const Matrix& m); |
|
340 extern OCTAVE_API Matrix max (const Matrix& m, double d); |
|
341 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b); |
4309
|
342 |
6708
|
343 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API) |
|
344 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API) |
2870
|
345 |
6708
|
346 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API) |
|
347 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API) |
2870
|
348 |
6708
|
349 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API) |
|
350 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API) |
2870
|
351 |
3573
|
352 MARRAY_FORWARD_DEFS (MArray2, Matrix, double) |
|
353 |
3689
|
354 template <class T> |
|
355 void read_int (std::istream& is, bool swap_bytes, T& val); |
|
356 |
458
|
357 #endif |
|
358 |
|
359 /* |
|
360 ;;; Local Variables: *** |
|
361 ;;; mode: C++ *** |
|
362 ;;; End: *** |
|
363 */ |