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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
458
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_Matrix_int_h) |
|
24 #define octave_Matrix_int_h 1 |
|
25 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
27 #pragma interface |
|
28 #endif |
|
29 |
1989
|
30 #include "MArray2.h" |
|
31 #include "MDiagArray2.h" |
458
|
32 |
|
33 #include "mx-defs.h" |
2870
|
34 #include "mx-op-defs.h" |
458
|
35 |
2317
|
36 #include "data-conv.h" |
|
37 #include "mach-info.h" |
|
38 |
3585
|
39 class |
|
40 Matrix : public MArray2<double> |
458
|
41 { |
|
42 public: |
|
43 |
3480
|
44 typedef void (*solve_singularity_handler) (double rcond); |
|
45 |
1214
|
46 Matrix (void) : MArray2<double> () { } |
3585
|
47 |
1214
|
48 Matrix (int r, int c) : MArray2<double> (r, c) { } |
3585
|
49 |
1214
|
50 Matrix (int r, int c, double val) : MArray2<double> (r, c, val) { } |
3585
|
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 |
|
79 Matrix& insert (const Matrix& a, int r, int c); |
|
80 Matrix& insert (const RowVector& a, int r, int c); |
|
81 Matrix& insert (const ColumnVector& a, int r, int c); |
|
82 Matrix& insert (const DiagMatrix& a, int r, int c); |
|
83 |
|
84 Matrix& fill (double val); |
|
85 Matrix& fill (double val, int r1, int c1, int r2, int c2); |
|
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 |
1205
|
97 friend Matrix real (const ComplexMatrix& a); |
|
98 friend Matrix imag (const ComplexMatrix& a); |
|
99 |
3225
|
100 Matrix transpose (void) const { return MArray2<double>::transpose (); } |
|
101 |
1359
|
102 // resize is the destructive equivalent for this one |
458
|
103 |
|
104 Matrix extract (int r1, int c1, int r2, int c2) const; |
|
105 |
4316
|
106 Matrix extract_n (int r1, int c1, int nr, int nc) const; |
|
107 |
1359
|
108 // extract row or column i. |
458
|
109 |
|
110 RowVector row (int i) const; |
|
111 RowVector row (char *s) const; |
|
112 |
|
113 ColumnVector column (int i) const; |
|
114 ColumnVector column (char *s) const; |
|
115 |
|
116 Matrix inverse (void) const; |
|
117 Matrix inverse (int& info) const; |
4329
|
118 Matrix inverse (int& info, double& rcond, int force = 0, |
|
119 int calc_cond = 1) const; |
458
|
120 |
4384
|
121 Matrix pseudo_inverse (double tol = 0.0) const; |
740
|
122 |
458
|
123 ComplexMatrix fourier (void) const; |
|
124 ComplexMatrix ifourier (void) const; |
|
125 |
677
|
126 ComplexMatrix fourier2d (void) const; |
|
127 ComplexMatrix ifourier2d (void) const; |
|
128 |
458
|
129 DET determinant (void) const; |
|
130 DET determinant (int& info) const; |
4329
|
131 DET determinant (int& info, double& rcond, int calc_cond = 1) const; |
458
|
132 |
|
133 Matrix solve (const Matrix& b) const; |
|
134 Matrix solve (const Matrix& b, int& info) const; |
|
135 Matrix solve (const Matrix& b, int& info, double& rcond) const; |
3480
|
136 Matrix solve (const Matrix& b, int& info, double& rcond, |
|
137 solve_singularity_handler sing_handler) const; |
458
|
138 |
|
139 ComplexMatrix solve (const ComplexMatrix& b) const; |
|
140 ComplexMatrix solve (const ComplexMatrix& b, int& info) const; |
|
141 ComplexMatrix solve (const ComplexMatrix& b, int& info, double& rcond) const; |
3480
|
142 ComplexMatrix solve (const ComplexMatrix& b, int& info, double& rcond, |
|
143 solve_singularity_handler sing_handler) const; |
458
|
144 |
|
145 ColumnVector solve (const ColumnVector& b) const; |
|
146 ColumnVector solve (const ColumnVector& b, int& info) const; |
|
147 ColumnVector solve (const ColumnVector& b, int& info, double& rcond) const; |
3480
|
148 ColumnVector solve (const ColumnVector& b, int& info, double& rcond, |
|
149 solve_singularity_handler sing_handler) const; |
458
|
150 |
|
151 ComplexColumnVector solve (const ComplexColumnVector& b) const; |
|
152 ComplexColumnVector solve (const ComplexColumnVector& b, int& info) const; |
|
153 ComplexColumnVector solve (const ComplexColumnVector& b, int& info, |
|
154 double& rcond) const; |
3480
|
155 ComplexColumnVector solve (const ComplexColumnVector& b, int& info, |
|
156 double& rcond, |
|
157 solve_singularity_handler sing_handler) const; |
458
|
158 |
|
159 Matrix lssolve (const Matrix& b) const; |
|
160 Matrix lssolve (const Matrix& b, int& info) const; |
|
161 Matrix lssolve (const Matrix& b, int& info, int& rank) const; |
|
162 |
|
163 ComplexMatrix lssolve (const ComplexMatrix& b) const; |
|
164 ComplexMatrix lssolve (const ComplexMatrix& b, int& info) const; |
|
165 ComplexMatrix lssolve (const ComplexMatrix& b, int& info, |
|
166 int& rank) const; |
|
167 |
|
168 ColumnVector lssolve (const ColumnVector& b) const; |
|
169 ColumnVector lssolve (const ColumnVector& b, int& info) const; |
|
170 ColumnVector lssolve (const ColumnVector& b, int& info, int& rank) const; |
|
171 |
|
172 ComplexColumnVector lssolve (const ComplexColumnVector& b) const; |
|
173 ComplexColumnVector lssolve (const ComplexColumnVector& b, int& info) const; |
|
174 ComplexColumnVector lssolve (const ComplexColumnVector& b, int& info, |
|
175 int& rank) const; |
|
176 |
1819
|
177 Matrix expm (void) const; |
|
178 |
458
|
179 Matrix& operator += (const DiagMatrix& a); |
|
180 Matrix& operator -= (const DiagMatrix& a); |
|
181 |
1359
|
182 // unary operations |
458
|
183 |
2964
|
184 boolMatrix operator ! (void) const; |
458
|
185 |
1359
|
186 // column vector by row vector -> matrix operations |
458
|
187 |
2385
|
188 friend Matrix operator * (const ColumnVector& a, const RowVector& b); |
458
|
189 |
1359
|
190 // other operations |
458
|
191 |
2676
|
192 Matrix map (d_d_Mapper f) const; |
3248
|
193 boolMatrix map (b_d_Mapper f) const; |
2676
|
194 |
|
195 Matrix& apply (d_d_Mapper f); |
458
|
196 |
2385
|
197 bool any_element_is_negative (void) const; |
|
198 bool any_element_is_inf_or_nan (void) const; |
|
199 bool all_elements_are_int_or_inf_or_nan (void) const; |
|
200 bool all_integers (double& max_val, double& min_val) const; |
|
201 bool too_large_for_float (void) const; |
1963
|
202 |
4017
|
203 boolMatrix all (int dim = -1) const; |
|
204 boolMatrix any (int dim = -1) const; |
458
|
205 |
4017
|
206 Matrix cumprod (int dim = -1) const; |
|
207 Matrix cumsum (int dim = -1) const; |
|
208 Matrix prod (int dim = -1) const; |
|
209 Matrix sum (int dim = -1) const; |
|
210 Matrix sumsq (int dim = -1) const; |
2385
|
211 Matrix abs (void) const; |
458
|
212 |
|
213 ColumnVector diag (void) const; |
|
214 ColumnVector diag (int k) const; |
|
215 |
|
216 ColumnVector row_min (void) const; |
2354
|
217 ColumnVector row_max (void) const; |
458
|
218 |
2354
|
219 ColumnVector row_min (Array<int>& index) const; |
|
220 ColumnVector row_max (Array<int>& index) const; |
458
|
221 |
|
222 RowVector column_min (void) const; |
2354
|
223 RowVector column_max (void) const; |
458
|
224 |
2354
|
225 RowVector column_min (Array<int>& index) const; |
|
226 RowVector column_max (Array<int>& index) const; |
458
|
227 |
1359
|
228 // i/o |
458
|
229 |
3504
|
230 friend std::ostream& operator << (std::ostream& os, const Matrix& a); |
|
231 friend std::istream& operator >> (std::istream& is, Matrix& a); |
458
|
232 |
3504
|
233 int read (std::istream& is, int nr, int nc, oct_data_conv::data_type dt, |
2317
|
234 int skip, oct_mach_info::float_format flt_fmt); |
|
235 |
3504
|
236 int write (std::ostream& os, oct_data_conv::data_type dt, int skip, |
2317
|
237 oct_mach_info::float_format flt_fmt); |
458
|
238 |
3933
|
239 static double resize_fill_value (void) { return 0; } |
|
240 |
458
|
241 private: |
|
242 |
1214
|
243 Matrix (double *d, int r, int c) : MArray2<double> (d, r, c) { } |
458
|
244 }; |
|
245 |
1959
|
246 extern Matrix Givens (double, double); |
1819
|
247 |
1959
|
248 extern Matrix Sylvester (const Matrix&, const Matrix&, const Matrix&); |
|
249 |
2828
|
250 extern Matrix operator * (const Matrix& a, const Matrix& b); |
|
251 |
4309
|
252 extern Matrix min (double d, const Matrix& m); |
|
253 extern Matrix min (const Matrix& m, double d); |
|
254 extern Matrix min (const Matrix& a, const Matrix& b); |
|
255 |
|
256 extern Matrix max (double d, const Matrix& m); |
|
257 extern Matrix max (const Matrix& m, double d); |
|
258 extern Matrix max (const Matrix& a, const Matrix& b); |
|
259 |
2870
|
260 MS_CMP_OP_DECLS (Matrix, double) |
|
261 MS_BOOL_OP_DECLS (Matrix, double) |
|
262 |
|
263 SM_CMP_OP_DECLS (double, Matrix) |
|
264 SM_BOOL_OP_DECLS (double, Matrix) |
|
265 |
|
266 MM_CMP_OP_DECLS (Matrix, Matrix) |
|
267 MM_BOOL_OP_DECLS (Matrix, Matrix) |
|
268 |
3573
|
269 MARRAY_FORWARD_DEFS (MArray2, Matrix, double) |
|
270 |
3689
|
271 template <class T> |
|
272 void read_int (std::istream& is, bool swap_bytes, T& val); |
|
273 |
458
|
274 #endif |
|
275 |
|
276 /* |
|
277 ;;; Local Variables: *** |
|
278 ;;; mode: C++ *** |
|
279 ;;; End: *** |
|
280 */ |