458
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
458
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
1214
|
48 Matrix (const Matrix& a) : MArray2<double> (a) { } |
3585
|
49 |
|
50 Matrix (const MArray2<double>& a) : MArray2<double> (a) { } |
|
51 |
|
52 explicit Matrix (const RowVector& rv); |
458
|
53 |
3585
|
54 explicit Matrix (const ColumnVector& cv); |
|
55 |
|
56 explicit Matrix (const DiagMatrix& a); |
|
57 |
|
58 explicit Matrix (const boolMatrix& a); |
|
59 |
|
60 explicit Matrix (const charMatrix& a); |
1574
|
61 |
458
|
62 Matrix& operator = (const Matrix& a) |
|
63 { |
1214
|
64 MArray2<double>::operator = (a); |
458
|
65 return *this; |
|
66 } |
|
67 |
2385
|
68 bool operator == (const Matrix& a) const; |
|
69 bool operator != (const Matrix& a) const; |
458
|
70 |
3354
|
71 bool is_symmetric (void) const; |
|
72 |
1359
|
73 // destructive insert/delete/reorder operations |
458
|
74 |
5275
|
75 Matrix& insert (const Matrix& a, octave_idx_type r, octave_idx_type c); |
|
76 Matrix& insert (const RowVector& a, octave_idx_type r, octave_idx_type c); |
|
77 Matrix& insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c); |
|
78 Matrix& insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c); |
458
|
79 |
|
80 Matrix& fill (double val); |
5275
|
81 Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2); |
458
|
82 |
|
83 Matrix append (const Matrix& a) const; |
|
84 Matrix append (const RowVector& a) const; |
|
85 Matrix append (const ColumnVector& a) const; |
|
86 Matrix append (const DiagMatrix& a) const; |
|
87 |
|
88 Matrix stack (const Matrix& a) const; |
|
89 Matrix stack (const RowVector& a) const; |
|
90 Matrix stack (const ColumnVector& a) const; |
|
91 Matrix stack (const DiagMatrix& a) const; |
|
92 |
6108
|
93 friend OCTAVE_API Matrix real (const ComplexMatrix& a); |
|
94 friend OCTAVE_API Matrix imag (const ComplexMatrix& a); |
1205
|
95 |
3225
|
96 Matrix transpose (void) const { return MArray2<double>::transpose (); } |
|
97 |
1359
|
98 // resize is the destructive equivalent for this one |
458
|
99 |
5275
|
100 Matrix extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const; |
458
|
101 |
5275
|
102 Matrix extract_n (octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const; |
4316
|
103 |
1359
|
104 // extract row or column i. |
458
|
105 |
5275
|
106 RowVector row (octave_idx_type i) const; |
458
|
107 |
5275
|
108 ColumnVector column (octave_idx_type i) const; |
458
|
109 |
6207
|
110 private: |
|
111 Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
112 int force, int calc_cond) const; |
|
113 |
|
114 Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
115 int force, int calc_cond) const; |
|
116 |
|
117 public: |
458
|
118 Matrix inverse (void) const; |
6207
|
119 Matrix inverse (MatrixType &mattype) const; |
|
120 Matrix inverse (MatrixType &mattype, octave_idx_type& info) const; |
|
121 Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
122 int force = 0, int calc_cond = 1) const; |
458
|
123 |
4384
|
124 Matrix pseudo_inverse (double tol = 0.0) const; |
740
|
125 |
458
|
126 ComplexMatrix fourier (void) const; |
|
127 ComplexMatrix ifourier (void) const; |
|
128 |
677
|
129 ComplexMatrix fourier2d (void) const; |
|
130 ComplexMatrix ifourier2d (void) const; |
|
131 |
458
|
132 DET determinant (void) const; |
5275
|
133 DET determinant (octave_idx_type& info) const; |
|
134 DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const; |
458
|
135 |
5785
|
136 private: |
|
137 // Upper triangular matrix solvers |
|
138 Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
139 double& rcond, solve_singularity_handler sing_handler, |
|
140 bool calc_cond = false) const; |
|
141 |
|
142 // Lower triangular matrix solvers |
|
143 Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
144 double& rcond, solve_singularity_handler sing_handler, |
|
145 bool calc_cond = false) const; |
|
146 |
|
147 // Full matrix solvers (lu/cholesky) |
|
148 Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
149 double& rcond, solve_singularity_handler sing_handler, |
|
150 bool calc_cond = false) const; |
|
151 |
|
152 public: |
|
153 // Generic interface to solver with no probing of type |
|
154 Matrix solve (MatrixType &typ, const Matrix& b) const; |
|
155 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const; |
|
156 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
157 double& rcond) const; |
|
158 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
159 double& rcond, solve_singularity_handler sing_handler, |
|
160 bool singular_fallback = true) const; |
|
161 |
|
162 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const; |
|
163 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
164 octave_idx_type& info) const; |
|
165 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
166 octave_idx_type& info, double& rcond) const; |
|
167 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
168 octave_idx_type& info, double& rcond, |
|
169 solve_singularity_handler sing_handler, |
|
170 bool singular_fallback = true) const; |
|
171 |
|
172 ColumnVector solve (MatrixType &typ, const ColumnVector& b) const; |
|
173 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
174 octave_idx_type& info) const; |
|
175 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
176 octave_idx_type& info, double& rcond) const; |
|
177 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
178 octave_idx_type& info, double& rcond, |
|
179 solve_singularity_handler sing_handler) const; |
|
180 |
|
181 ComplexColumnVector solve (MatrixType &typ, |
|
182 const ComplexColumnVector& b) const; |
|
183 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
184 octave_idx_type& info) const; |
|
185 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
186 octave_idx_type& info, double& rcond) const; |
|
187 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
188 octave_idx_type& info, double& rcond, |
|
189 solve_singularity_handler sing_handler) const; |
|
190 |
|
191 // Generic interface to solver with probing of type |
458
|
192 Matrix solve (const Matrix& b) const; |
5275
|
193 Matrix solve (const Matrix& b, octave_idx_type& info) const; |
|
194 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const; |
|
195 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond, |
3480
|
196 solve_singularity_handler sing_handler) const; |
458
|
197 |
|
198 ComplexMatrix solve (const ComplexMatrix& b) const; |
5275
|
199 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
200 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const; |
|
201 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
202 solve_singularity_handler sing_handler) const; |
458
|
203 |
|
204 ColumnVector solve (const ColumnVector& b) const; |
5275
|
205 ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const; |
|
206 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const; |
|
207 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
208 solve_singularity_handler sing_handler) const; |
458
|
209 |
|
210 ComplexColumnVector solve (const ComplexColumnVector& b) const; |
5275
|
211 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
212 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
458
|
213 double& rcond) const; |
5275
|
214 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
3480
|
215 double& rcond, |
|
216 solve_singularity_handler sing_handler) const; |
458
|
217 |
5785
|
218 // Singular solvers |
458
|
219 Matrix lssolve (const Matrix& b) const; |
5275
|
220 Matrix lssolve (const Matrix& b, octave_idx_type& info) const; |
|
221 Matrix lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const; |
458
|
222 |
|
223 ComplexMatrix lssolve (const ComplexMatrix& b) const; |
5275
|
224 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
225 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
226 octave_idx_type& rank) const; |
458
|
227 |
|
228 ColumnVector lssolve (const ColumnVector& b) const; |
5275
|
229 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const; |
|
230 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const; |
458
|
231 |
|
232 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; |
5275
|
233 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
234 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
235 octave_idx_type& rank) const; |
458
|
236 |
1819
|
237 Matrix expm (void) const; |
|
238 |
458
|
239 Matrix& operator += (const DiagMatrix& a); |
|
240 Matrix& operator -= (const DiagMatrix& a); |
|
241 |
1359
|
242 // unary operations |
458
|
243 |
2964
|
244 boolMatrix operator ! (void) const; |
458
|
245 |
1359
|
246 // other operations |
458
|
247 |
2676
|
248 Matrix map (d_d_Mapper f) const; |
3248
|
249 boolMatrix map (b_d_Mapper f) const; |
2676
|
250 |
|
251 Matrix& apply (d_d_Mapper f); |
458
|
252 |
4431
|
253 bool any_element_is_negative (bool = false) const; |
2385
|
254 bool any_element_is_inf_or_nan (void) const; |
5943
|
255 bool any_element_not_one_or_zero (void) const; |
2385
|
256 bool all_elements_are_int_or_inf_or_nan (void) const; |
|
257 bool all_integers (double& max_val, double& min_val) const; |
|
258 bool too_large_for_float (void) const; |
1963
|
259 |
4017
|
260 boolMatrix all (int dim = -1) const; |
|
261 boolMatrix any (int dim = -1) const; |
458
|
262 |
4017
|
263 Matrix cumprod (int dim = -1) const; |
|
264 Matrix cumsum (int dim = -1) const; |
|
265 Matrix prod (int dim = -1) const; |
|
266 Matrix sum (int dim = -1) const; |
|
267 Matrix sumsq (int dim = -1) const; |
2385
|
268 Matrix abs (void) const; |
458
|
269 |
|
270 ColumnVector diag (void) const; |
5275
|
271 ColumnVector diag (octave_idx_type k) const; |
458
|
272 |
|
273 ColumnVector row_min (void) const; |
2354
|
274 ColumnVector row_max (void) const; |
458
|
275 |
5275
|
276 ColumnVector row_min (Array<octave_idx_type>& index) const; |
|
277 ColumnVector row_max (Array<octave_idx_type>& index) const; |
458
|
278 |
|
279 RowVector column_min (void) const; |
2354
|
280 RowVector column_max (void) const; |
458
|
281 |
5275
|
282 RowVector column_min (Array<octave_idx_type>& index) const; |
|
283 RowVector column_max (Array<octave_idx_type>& index) const; |
458
|
284 |
1359
|
285 // i/o |
458
|
286 |
6108
|
287 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Matrix& a); |
|
288 friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a); |
458
|
289 |
3933
|
290 static double resize_fill_value (void) { return 0; } |
|
291 |
458
|
292 private: |
|
293 |
5275
|
294 Matrix (double *d, octave_idx_type r, octave_idx_type c) : MArray2<double> (d, r, c) { } |
458
|
295 }; |
|
296 |
5508
|
297 // Publish externally used friend functions. |
|
298 |
6108
|
299 extern OCTAVE_API Matrix real (const ComplexMatrix& a); |
|
300 extern OCTAVE_API Matrix imag (const ComplexMatrix& a); |
5508
|
301 |
|
302 // column vector by row vector -> matrix operations |
|
303 |
6108
|
304 extern OCTAVE_API Matrix operator * (const ColumnVector& a, const RowVector& b); |
5508
|
305 |
|
306 // Other functions. |
|
307 |
6108
|
308 extern OCTAVE_API Matrix Givens (double, double); |
1819
|
309 |
6108
|
310 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&, const Matrix&); |
1959
|
311 |
6108
|
312 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b); |
2828
|
313 |
6108
|
314 extern OCTAVE_API Matrix min (double d, const Matrix& m); |
|
315 extern OCTAVE_API Matrix min (const Matrix& m, double d); |
|
316 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b); |
4309
|
317 |
6108
|
318 extern OCTAVE_API Matrix max (double d, const Matrix& m); |
|
319 extern OCTAVE_API Matrix max (const Matrix& m, double d); |
|
320 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b); |
4309
|
321 |
2870
|
322 MS_CMP_OP_DECLS (Matrix, double) |
|
323 MS_BOOL_OP_DECLS (Matrix, double) |
|
324 |
|
325 SM_CMP_OP_DECLS (double, Matrix) |
|
326 SM_BOOL_OP_DECLS (double, Matrix) |
|
327 |
|
328 MM_CMP_OP_DECLS (Matrix, Matrix) |
|
329 MM_BOOL_OP_DECLS (Matrix, Matrix) |
|
330 |
3573
|
331 MARRAY_FORWARD_DEFS (MArray2, Matrix, double) |
|
332 |
3689
|
333 template <class T> |
|
334 void read_int (std::istream& is, bool swap_bytes, T& val); |
|
335 |
458
|
336 #endif |
|
337 |
|
338 /* |
|
339 ;;; Local Variables: *** |
|
340 ;;; mode: C++ *** |
|
341 ;;; End: *** |
|
342 */ |