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