GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dMatrix.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2013 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_dMatrix_h)
24 #define octave_dMatrix_h 1
25 
26 #include "MArray.h"
27 #include "MDiagArray2.h"
28 #include "MatrixType.h"
29 
30 #include "mx-defs.h"
31 #include "mx-op-decl.h"
32 #include "DET.h"
33 
34 class
35 OCTAVE_API
36 Matrix : public MArray<double>
37 {
38 public:
39 
42 
43  typedef void (*solve_singularity_handler) (double rcon);
44 
45  Matrix (void) : MArray<double> () { }
46 
48  : MArray<double> (dim_vector (r, c)) { }
49 
51  : MArray<double> (dim_vector (r, c), val) { }
52 
53  Matrix (const dim_vector& dv) : MArray<double> (dv.redim (2)) { }
54 
55  Matrix (const dim_vector& dv, double val)
56  : MArray<double> (dv.redim (2), val) { }
57 
58  Matrix (const Matrix& a) : MArray<double> (a) { }
59 
60  template <class U>
61  Matrix (const MArray<U>& a) : MArray<double> (a.as_matrix ()) { }
62 
63  template <class U>
64  Matrix (const Array<U>& a) : MArray<double> (a.as_matrix ()) { }
65 
66  explicit Matrix (const RowVector& rv);
67 
68  explicit Matrix (const ColumnVector& cv);
69 
70  explicit Matrix (const DiagMatrix& a);
71 
72  explicit Matrix (const PermMatrix& a);
73 
74  explicit Matrix (const boolMatrix& a);
75 
76  explicit Matrix (const charMatrix& a);
77 
79  {
81  return *this;
82  }
83 
84  bool operator == (const Matrix& a) const;
85  bool operator != (const Matrix& a) const;
86 
87  bool is_symmetric (void) const;
88 
89  // destructive insert/delete/reorder operations
90 
91  Matrix& insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
92  Matrix& insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
93  Matrix& insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
94  Matrix& insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
95 
96  Matrix& fill (double val);
97  Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1,
99 
100  Matrix append (const Matrix& a) const;
101  Matrix append (const RowVector& a) const;
102  Matrix append (const ColumnVector& a) const;
103  Matrix append (const DiagMatrix& a) const;
104 
105  Matrix stack (const Matrix& a) const;
106  Matrix stack (const RowVector& a) const;
107  Matrix stack (const ColumnVector& a) const;
108  Matrix stack (const DiagMatrix& a) const;
109 
110  friend OCTAVE_API Matrix real (const ComplexMatrix& a);
111  friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
112 
113  friend class ComplexMatrix;
114 
115  Matrix transpose (void) const { return MArray<double>::transpose (); }
116 
117  // resize is the destructive equivalent for this one
118 
121 
123  octave_idx_type nr, octave_idx_type nc) const;
124 
125  // extract row or column i.
126 
127  RowVector row (octave_idx_type i) const;
128 
130 
131  void resize (octave_idx_type nr, octave_idx_type nc, double rfv = 0)
132  {
133  MArray<double>::resize (dim_vector (nr, nc), rfv);
134  }
135 
136 private:
137  Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
138  int force, int calc_cond) const;
139 
140  Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
141  int force, int calc_cond) const;
142 
143 public:
144  Matrix inverse (void) const;
145  Matrix inverse (octave_idx_type& info) const;
146  Matrix inverse (octave_idx_type& info, double& rcon, int force = 0,
147  int calc_cond = 1) const;
148 
149  Matrix inverse (MatrixType &mattype) const;
150  Matrix inverse (MatrixType &mattype, octave_idx_type& info) const;
151  Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
152  int force = 0, int calc_cond = 1) const;
153 
154  Matrix pseudo_inverse (double tol = 0.0) const;
155 
156  ComplexMatrix fourier (void) const;
157  ComplexMatrix ifourier (void) const;
158 
159  ComplexMatrix fourier2d (void) const;
160  ComplexMatrix ifourier2d (void) const;
161 
162  DET determinant (void) const;
163  DET determinant (octave_idx_type& info) const;
164  DET determinant (octave_idx_type& info, double& rcon,
165  int calc_cond = 1) const;
166  DET determinant (MatrixType &mattype, octave_idx_type& info,
167  double& rcon, int calc_cond = 1) const;
168 
169  double rcond (void) const;
170  double rcond (MatrixType &mattype) const;
171 
172 private:
173  // Upper triangular matrix solvers
174  Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
175  double& rcon, solve_singularity_handler sing_handler,
176  bool calc_cond = false,
177  blas_trans_type transt = blas_no_trans) const;
178 
179  // Lower triangular matrix solvers
180  Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
181  double& rcon, solve_singularity_handler sing_handler,
182  bool calc_cond = false,
183  blas_trans_type transt = blas_no_trans) const;
184 
185  // Full matrix solvers (lu/cholesky)
186  Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
187  double& rcon, solve_singularity_handler sing_handler,
188  bool calc_cond = false) const;
189 
190 public:
191  // Generic interface to solver with no probing of type
192  Matrix solve (MatrixType &typ, const Matrix& b) const;
193  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
194  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
195  double& rcon) const;
196  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
197  double& rcon, solve_singularity_handler sing_handler,
198  bool singular_fallback = true,
199  blas_trans_type transt = blas_no_trans) const;
200 
201  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
203  octave_idx_type& info) const;
205  octave_idx_type& info, double& rcon) const;
207  octave_idx_type& info, double& rcon,
208  solve_singularity_handler sing_handler,
209  bool singular_fallback = true,
210  blas_trans_type transt = blas_no_trans) const;
211 
212  ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
213  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
214  octave_idx_type& info) const;
215  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
216  octave_idx_type& info, double& rcon) const;
217  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
218  octave_idx_type& info, double& rcon,
219  solve_singularity_handler sing_handler,
220  blas_trans_type transt = blas_no_trans) const;
221 
223  const ComplexColumnVector& b) const;
225  octave_idx_type& info) const;
227  octave_idx_type& info, double& rcon) const;
229  octave_idx_type& info, double& rcon,
230  solve_singularity_handler sing_handler,
231  blas_trans_type transt = blas_no_trans) const;
232 
233  // Generic interface to solver with probing of type
234  Matrix solve (const Matrix& b) const;
235  Matrix solve (const Matrix& b, octave_idx_type& info) const;
236  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
237  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
238  solve_singularity_handler sing_handler,
239  blas_trans_type transt = blas_no_trans) const;
240 
241  ComplexMatrix solve (const ComplexMatrix& b) const;
242  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
244  double& rcon) const;
246  double& rcon,
247  solve_singularity_handler sing_handler,
248  blas_trans_type transt = blas_no_trans) const;
249 
250  ColumnVector solve (const ColumnVector& b) const;
251  ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
253  double& rcon) const;
255  double& rcon,
256  solve_singularity_handler sing_handler,
257  blas_trans_type transt = blas_no_trans) const;
258 
261  octave_idx_type& info) const;
263  octave_idx_type& info, double& rcon) const;
265  octave_idx_type& info, double& rcon,
266  solve_singularity_handler sing_handler,
267  blas_trans_type transt = blas_no_trans) const;
268 
269  // Singular solvers
270  Matrix lssolve (const Matrix& b) const;
271  Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
272  Matrix lssolve (const Matrix& b, octave_idx_type& info,
273  octave_idx_type& rank) const;
274  Matrix lssolve (const Matrix& b, octave_idx_type& info,
275  octave_idx_type& rank, double& rcon) const;
276 
277  ComplexMatrix lssolve (const ComplexMatrix& b) const;
278  ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
280  octave_idx_type& rank) const;
282  octave_idx_type& rank, double &rcon) const;
283 
284  ColumnVector lssolve (const ColumnVector& b) const;
285  ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const;
287  octave_idx_type& rank) const;
289  octave_idx_type& rank, double& rcon) const;
290 
293  octave_idx_type& info) const;
295  octave_idx_type& info,
296  octave_idx_type& rank) const;
298  octave_idx_type& info,
299  octave_idx_type& rank, double& rcon) const;
300 
301  Matrix& operator += (const DiagMatrix& a);
302  Matrix& operator -= (const DiagMatrix& a);
303 
304  // unary operations
305 
306  boolMatrix operator ! (void) const;
307 
308  // other operations
309 
310  bool any_element_is_negative (bool = false) const;
311  bool any_element_is_positive (bool = false) const;
312  bool any_element_is_nan (void) const;
313  bool any_element_is_inf_or_nan (void) const;
314  bool any_element_not_one_or_zero (void) const;
315  bool all_elements_are_int_or_inf_or_nan (void) const;
316  bool all_integers (double& max_val, double& min_val) const;
317  bool too_large_for_float (void) const;
318 
319  boolMatrix all (int dim = -1) const;
320  boolMatrix any (int dim = -1) const;
321 
322  Matrix cumprod (int dim = -1) const;
323  Matrix cumsum (int dim = -1) const;
324  Matrix prod (int dim = -1) const;
325  Matrix sum (int dim = -1) const;
326  Matrix sumsq (int dim = -1) const;
327  Matrix abs (void) const;
328 
329  Matrix diag (octave_idx_type k = 0) const;
330 
332 
333  ColumnVector row_min (void) const;
334  ColumnVector row_max (void) const;
335 
338 
339  RowVector column_min (void) const;
340  RowVector column_max (void) const;
341 
344 
345  // i/o
346 
347  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
348  const Matrix& a);
349  friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a);
350 };
351 
352 // Publish externally used friend functions.
353 
354 extern OCTAVE_API Matrix real (const ComplexMatrix& a);
355 extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
356 
357 // column vector by row vector -> matrix operations
358 
359 extern OCTAVE_API Matrix operator * (const ColumnVector& a,
360  const RowVector& b);
361 
362 // Other functions.
363 
364 extern OCTAVE_API Matrix Givens (double, double);
365 
366 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&,
367  const Matrix&);
368 
369 extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
371  blas_trans_type transb = blas_no_trans);
372 
373 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
374 
375 extern OCTAVE_API Matrix min (double d, const Matrix& m);
376 extern OCTAVE_API Matrix min (const Matrix& m, double d);
377 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
378 
379 extern OCTAVE_API Matrix max (double d, const Matrix& m);
380 extern OCTAVE_API Matrix max (const Matrix& m, double d);
381 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
382 
383 extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
384  const ColumnVector& x2,
385  octave_idx_type n);
386 
387 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API)
388 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API)
389 
390 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API)
391 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API)
392 
393 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API)
394 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API)
395 
396 MARRAY_FORWARD_DEFS (MArray, Matrix, double)
397 
398 template <class T>
399 void read_int (std::istream& is, bool swap_bytes, T& val);
400 
401 #endif