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; |
6479
|
119 Matrix inverse (octave_idx_type& info) const; |
|
120 Matrix inverse (octave_idx_type& info, double& rcond, int force = 0, |
|
121 int calc_cond = 1) const; |
|
122 |
6207
|
123 Matrix inverse (MatrixType &mattype) const; |
|
124 Matrix inverse (MatrixType &mattype, octave_idx_type& info) const; |
|
125 Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
126 int force = 0, int calc_cond = 1) const; |
458
|
127 |
4384
|
128 Matrix pseudo_inverse (double tol = 0.0) const; |
740
|
129 |
458
|
130 ComplexMatrix fourier (void) const; |
|
131 ComplexMatrix ifourier (void) const; |
|
132 |
677
|
133 ComplexMatrix fourier2d (void) const; |
|
134 ComplexMatrix ifourier2d (void) const; |
|
135 |
458
|
136 DET determinant (void) const; |
5275
|
137 DET determinant (octave_idx_type& info) const; |
|
138 DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const; |
458
|
139 |
5785
|
140 private: |
|
141 // Upper triangular matrix solvers |
|
142 Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
143 double& rcond, solve_singularity_handler sing_handler, |
|
144 bool calc_cond = false) const; |
|
145 |
|
146 // Lower triangular matrix solvers |
|
147 Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
148 double& rcond, solve_singularity_handler sing_handler, |
|
149 bool calc_cond = false) const; |
|
150 |
|
151 // Full matrix solvers (lu/cholesky) |
|
152 Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
153 double& rcond, solve_singularity_handler sing_handler, |
|
154 bool calc_cond = false) const; |
|
155 |
|
156 public: |
|
157 // Generic interface to solver with no probing of type |
|
158 Matrix solve (MatrixType &typ, const Matrix& b) const; |
|
159 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const; |
|
160 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
161 double& rcond) const; |
|
162 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
163 double& rcond, solve_singularity_handler sing_handler, |
|
164 bool singular_fallback = true) const; |
|
165 |
|
166 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const; |
|
167 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
168 octave_idx_type& info) const; |
|
169 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
170 octave_idx_type& info, double& rcond) const; |
|
171 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b, |
|
172 octave_idx_type& info, double& rcond, |
|
173 solve_singularity_handler sing_handler, |
|
174 bool singular_fallback = true) const; |
|
175 |
|
176 ColumnVector solve (MatrixType &typ, const ColumnVector& b) const; |
|
177 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
178 octave_idx_type& info) const; |
|
179 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
180 octave_idx_type& info, double& rcond) const; |
|
181 ColumnVector solve (MatrixType &typ, const ColumnVector& b, |
|
182 octave_idx_type& info, double& rcond, |
|
183 solve_singularity_handler sing_handler) const; |
|
184 |
|
185 ComplexColumnVector solve (MatrixType &typ, |
|
186 const ComplexColumnVector& b) const; |
|
187 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
188 octave_idx_type& info) const; |
|
189 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
190 octave_idx_type& info, double& rcond) const; |
|
191 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b, |
|
192 octave_idx_type& info, double& rcond, |
|
193 solve_singularity_handler sing_handler) const; |
|
194 |
|
195 // Generic interface to solver with probing of type |
458
|
196 Matrix solve (const Matrix& b) const; |
5275
|
197 Matrix solve (const Matrix& b, octave_idx_type& info) const; |
|
198 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const; |
|
199 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond, |
3480
|
200 solve_singularity_handler sing_handler) const; |
458
|
201 |
|
202 ComplexMatrix solve (const ComplexMatrix& b) const; |
5275
|
203 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
204 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const; |
|
205 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
206 solve_singularity_handler sing_handler) const; |
458
|
207 |
|
208 ColumnVector solve (const ColumnVector& b) const; |
5275
|
209 ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const; |
|
210 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const; |
|
211 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
212 solve_singularity_handler sing_handler) const; |
458
|
213 |
|
214 ComplexColumnVector solve (const ComplexColumnVector& b) const; |
5275
|
215 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
216 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
458
|
217 double& rcond) const; |
5275
|
218 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info, |
3480
|
219 double& rcond, |
|
220 solve_singularity_handler sing_handler) const; |
458
|
221 |
5785
|
222 // Singular solvers |
458
|
223 Matrix lssolve (const Matrix& b) const; |
5275
|
224 Matrix lssolve (const Matrix& b, octave_idx_type& info) const; |
|
225 Matrix lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const; |
458
|
226 |
|
227 ComplexMatrix lssolve (const ComplexMatrix& b) const; |
5275
|
228 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const; |
|
229 ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
230 octave_idx_type& rank) const; |
458
|
231 |
|
232 ColumnVector lssolve (const ColumnVector& b) const; |
5275
|
233 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const; |
|
234 ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const; |
458
|
235 |
|
236 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; |
5275
|
237 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info) const; |
|
238 ComplexColumnVector lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
239 octave_idx_type& rank) const; |
458
|
240 |
1819
|
241 Matrix expm (void) const; |
|
242 |
458
|
243 Matrix& operator += (const DiagMatrix& a); |
|
244 Matrix& operator -= (const DiagMatrix& a); |
|
245 |
1359
|
246 // unary operations |
458
|
247 |
2964
|
248 boolMatrix operator ! (void) const; |
458
|
249 |
1359
|
250 // other operations |
458
|
251 |
2676
|
252 Matrix map (d_d_Mapper f) const; |
3248
|
253 boolMatrix map (b_d_Mapper f) const; |
2676
|
254 |
|
255 Matrix& apply (d_d_Mapper f); |
458
|
256 |
4431
|
257 bool any_element_is_negative (bool = false) const; |
2385
|
258 bool any_element_is_inf_or_nan (void) const; |
5943
|
259 bool any_element_not_one_or_zero (void) const; |
2385
|
260 bool all_elements_are_int_or_inf_or_nan (void) const; |
|
261 bool all_integers (double& max_val, double& min_val) const; |
|
262 bool too_large_for_float (void) const; |
1963
|
263 |
4017
|
264 boolMatrix all (int dim = -1) const; |
|
265 boolMatrix any (int dim = -1) const; |
458
|
266 |
4017
|
267 Matrix cumprod (int dim = -1) const; |
|
268 Matrix cumsum (int dim = -1) const; |
|
269 Matrix prod (int dim = -1) const; |
|
270 Matrix sum (int dim = -1) const; |
|
271 Matrix sumsq (int dim = -1) const; |
2385
|
272 Matrix abs (void) const; |
458
|
273 |
|
274 ColumnVector diag (void) const; |
5275
|
275 ColumnVector diag (octave_idx_type k) const; |
458
|
276 |
|
277 ColumnVector row_min (void) const; |
2354
|
278 ColumnVector row_max (void) const; |
458
|
279 |
5275
|
280 ColumnVector row_min (Array<octave_idx_type>& index) const; |
|
281 ColumnVector row_max (Array<octave_idx_type>& index) const; |
458
|
282 |
|
283 RowVector column_min (void) const; |
2354
|
284 RowVector column_max (void) const; |
458
|
285 |
5275
|
286 RowVector column_min (Array<octave_idx_type>& index) const; |
|
287 RowVector column_max (Array<octave_idx_type>& index) const; |
458
|
288 |
1359
|
289 // i/o |
458
|
290 |
6108
|
291 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Matrix& a); |
|
292 friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a); |
458
|
293 |
3933
|
294 static double resize_fill_value (void) { return 0; } |
|
295 |
458
|
296 private: |
|
297 |
5275
|
298 Matrix (double *d, octave_idx_type r, octave_idx_type c) : MArray2<double> (d, r, c) { } |
458
|
299 }; |
|
300 |
5508
|
301 // Publish externally used friend functions. |
|
302 |
6108
|
303 extern OCTAVE_API Matrix real (const ComplexMatrix& a); |
|
304 extern OCTAVE_API Matrix imag (const ComplexMatrix& a); |
5508
|
305 |
|
306 // column vector by row vector -> matrix operations |
|
307 |
6108
|
308 extern OCTAVE_API Matrix operator * (const ColumnVector& a, const RowVector& b); |
5508
|
309 |
|
310 // Other functions. |
|
311 |
6108
|
312 extern OCTAVE_API Matrix Givens (double, double); |
1819
|
313 |
6108
|
314 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&, const Matrix&); |
1959
|
315 |
6108
|
316 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b); |
2828
|
317 |
6108
|
318 extern OCTAVE_API Matrix min (double d, const Matrix& m); |
|
319 extern OCTAVE_API Matrix min (const Matrix& m, double d); |
|
320 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b); |
4309
|
321 |
6108
|
322 extern OCTAVE_API Matrix max (double d, const Matrix& m); |
|
323 extern OCTAVE_API Matrix max (const Matrix& m, double d); |
|
324 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b); |
4309
|
325 |
2870
|
326 MS_CMP_OP_DECLS (Matrix, double) |
|
327 MS_BOOL_OP_DECLS (Matrix, double) |
|
328 |
|
329 SM_CMP_OP_DECLS (double, Matrix) |
|
330 SM_BOOL_OP_DECLS (double, Matrix) |
|
331 |
|
332 MM_CMP_OP_DECLS (Matrix, Matrix) |
|
333 MM_BOOL_OP_DECLS (Matrix, Matrix) |
|
334 |
3573
|
335 MARRAY_FORWARD_DEFS (MArray2, Matrix, double) |
|
336 |
3689
|
337 template <class T> |
|
338 void read_int (std::istream& is, bool swap_bytes, T& val); |
|
339 |
458
|
340 #endif |
|
341 |
|
342 /* |
|
343 ;;; Local Variables: *** |
|
344 ;;; mode: C++ *** |
|
345 ;;; End: *** |
|
346 */ |