GNU Octave  4.0.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-2015 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 "dNDArray.h"
27 #include "MArray.h"
28 #include "MDiagArray2.h"
29 #include "MatrixType.h"
30 
31 #include "mx-defs.h"
32 #include "mx-op-decl.h"
33 #include "DET.h"
34 
35 class
36 OCTAVE_API
37 Matrix : public NDArray
38 {
39 public:
40 
43 
44  typedef void (*solve_singularity_handler) (double rcon);
45 
46  Matrix (void) : NDArray () { }
47 
49  : NDArray (dim_vector (r, c)) { }
50 
52  : NDArray (dim_vector (r, c), val) { }
53 
54  Matrix (const dim_vector& dv) : NDArray (dv.redim (2)) { }
55 
56  Matrix (const dim_vector& dv, double val)
57  : NDArray (dv.redim (2), val) { }
58 
59  Matrix (const Matrix& a) : NDArray (a) { }
60 
61  template <class U>
62  Matrix (const MArray<U>& a) : NDArray (a.as_matrix ()) { }
63 
64  template <class U>
65  Matrix (const Array<U>& a) : NDArray (a.as_matrix ()) { }
66 
67  explicit Matrix (const RowVector& rv);
68 
69  explicit Matrix (const ColumnVector& cv);
70 
71  explicit Matrix (const DiagMatrix& a);
72 
73  explicit Matrix (const MDiagArray2<double>& a);
74 
75  explicit Matrix (const DiagArray2<double>& a);
76 
77  explicit Matrix (const PermMatrix& a);
78 
79  explicit Matrix (const boolMatrix& a);
80 
81  explicit Matrix (const charMatrix& a);
82 
83  bool operator == (const Matrix& a) const;
84  bool operator != (const Matrix& a) const;
85 
86  bool is_symmetric (void) const;
87 
88  // destructive insert/delete/reorder operations
89 
90  Matrix& insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
91  Matrix& insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
92  Matrix& insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
93  Matrix& insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
94 
95  Matrix& fill (double val);
96  Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1,
98 
99  Matrix append (const Matrix& a) const;
100  Matrix append (const RowVector& a) const;
101  Matrix append (const ColumnVector& a) const;
102  Matrix append (const DiagMatrix& a) const;
103 
104  Matrix stack (const Matrix& a) const;
105  Matrix stack (const RowVector& a) const;
106  Matrix stack (const ColumnVector& a) const;
107  Matrix stack (const DiagMatrix& a) const;
108 
109  friend OCTAVE_API Matrix real (const ComplexMatrix& a);
110  friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
111 
112  friend class ComplexMatrix;
113 
114  Matrix transpose (void) const { return MArray<double>::transpose (); }
115 
116  // resize is the destructive equivalent for this one
117 
120 
122  octave_idx_type nr, octave_idx_type nc) const;
123 
124  // extract row or column i.
125 
126  RowVector row (octave_idx_type i) const;
127 
129 
130  void resize (octave_idx_type nr, octave_idx_type nc, double rfv = 0)
131  {
132  MArray<double>::resize (dim_vector (nr, nc), rfv);
133  }
134 
135 private:
136  Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
137  int force, int calc_cond) const;
138 
139  Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
140  int force, int calc_cond) const;
141 
142 public:
143  Matrix inverse (void) const;
144  Matrix inverse (octave_idx_type& info) const;
145  Matrix inverse (octave_idx_type& info, double& rcon, int force = 0,
146  int calc_cond = 1) const;
147 
148  Matrix inverse (MatrixType &mattype) const;
149  Matrix inverse (MatrixType &mattype, octave_idx_type& info) const;
150  Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
151  int force = 0, int calc_cond = 1) const;
152 
153  Matrix pseudo_inverse (double tol = 0.0) const;
154 
155  ComplexMatrix fourier (void) const;
156  ComplexMatrix ifourier (void) const;
157 
158  ComplexMatrix fourier2d (void) const;
159  ComplexMatrix ifourier2d (void) const;
160 
161  DET determinant (void) const;
162  DET determinant (octave_idx_type& info) const;
163  DET determinant (octave_idx_type& info, double& rcon,
164  int calc_cond = 1) const;
165  DET determinant (MatrixType &mattype, octave_idx_type& info,
166  double& rcon, int calc_cond = 1) const;
167 
168  double rcond (void) const;
169  double rcond (MatrixType &mattype) const;
170 
171 private:
172  // Upper triangular matrix solvers
173  Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
174  double& rcon, solve_singularity_handler sing_handler,
175  bool calc_cond = false,
176  blas_trans_type transt = blas_no_trans) const;
177 
178  // Lower triangular matrix solvers
179  Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
180  double& rcon, solve_singularity_handler sing_handler,
181  bool calc_cond = false,
182  blas_trans_type transt = blas_no_trans) const;
183 
184  // Full matrix solvers (lu/cholesky)
185  Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
186  double& rcon, solve_singularity_handler sing_handler,
187  bool calc_cond = false) const;
188 
189 public:
190  // Generic interface to solver with no probing of type
191  Matrix solve (MatrixType &typ, const Matrix& b) const;
192  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
193  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
194  double& rcon) const;
195  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
196  double& rcon, solve_singularity_handler sing_handler,
197  bool singular_fallback = true,
198  blas_trans_type transt = blas_no_trans) const;
199 
200  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
202  octave_idx_type& info) const;
204  octave_idx_type& info, double& rcon) const;
206  octave_idx_type& info, double& rcon,
207  solve_singularity_handler sing_handler,
208  bool singular_fallback = true,
209  blas_trans_type transt = blas_no_trans) const;
210 
211  ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
212  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
213  octave_idx_type& info) const;
214  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
215  octave_idx_type& info, double& rcon) const;
216  ColumnVector solve (MatrixType &typ, const ColumnVector& b,
217  octave_idx_type& info, double& rcon,
218  solve_singularity_handler sing_handler,
219  blas_trans_type transt = blas_no_trans) const;
220 
222  const ComplexColumnVector& b) const;
224  octave_idx_type& info) const;
226  octave_idx_type& info, double& rcon) const;
228  octave_idx_type& info, double& rcon,
229  solve_singularity_handler sing_handler,
230  blas_trans_type transt = blas_no_trans) const;
231 
232  // Generic interface to solver with probing of type
233  Matrix solve (const Matrix& b) const;
234  Matrix solve (const Matrix& b, octave_idx_type& info) const;
235  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
236  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
237  solve_singularity_handler sing_handler,
238  blas_trans_type transt = blas_no_trans) const;
239 
240  ComplexMatrix solve (const ComplexMatrix& b) const;
241  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
243  double& rcon) const;
245  double& rcon,
246  solve_singularity_handler sing_handler,
247  blas_trans_type transt = blas_no_trans) const;
248 
249  ColumnVector solve (const ColumnVector& b) const;
250  ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
252  double& rcon) const;
254  double& rcon,
255  solve_singularity_handler sing_handler,
256  blas_trans_type transt = blas_no_trans) const;
257 
260  octave_idx_type& info) const;
262  octave_idx_type& info, double& rcon) const;
264  octave_idx_type& info, double& rcon,
265  solve_singularity_handler sing_handler,
266  blas_trans_type transt = blas_no_trans) const;
267 
268  // Singular solvers
269  Matrix lssolve (const Matrix& b) const;
270  Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
271  Matrix lssolve (const Matrix& b, octave_idx_type& info,
272  octave_idx_type& rank) const;
273  Matrix lssolve (const Matrix& b, octave_idx_type& info,
274  octave_idx_type& rank, double& rcon) const;
275 
276  ComplexMatrix lssolve (const ComplexMatrix& b) const;
277  ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
279  octave_idx_type& rank) const;
281  octave_idx_type& rank, double &rcon) const;
282 
283  ColumnVector lssolve (const ColumnVector& b) const;
284  ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const;
286  octave_idx_type& rank) const;
288  octave_idx_type& rank, double& rcon) const;
289 
292  octave_idx_type& info) const;
294  octave_idx_type& info,
295  octave_idx_type& rank) const;
297  octave_idx_type& info,
298  octave_idx_type& rank, double& rcon) const;
299 
300  Matrix& operator += (const DiagMatrix& a);
301  Matrix& operator -= (const DiagMatrix& a);
302 
303  // unary operations
304 
305  // other operations
306 
307  boolMatrix all (int dim = -1) const;
308  boolMatrix any (int dim = -1) const;
309 
310  Matrix cumprod (int dim = -1) const;
311  Matrix cumsum (int dim = -1) const;
312  Matrix prod (int dim = -1) const;
313  Matrix sum (int dim = -1) const;
314  Matrix sumsq (int dim = -1) const;
315  Matrix abs (void) const;
316 
317  Matrix diag (octave_idx_type k = 0) const;
318 
320 
321  ColumnVector row_min (void) const;
322  ColumnVector row_max (void) const;
323 
326 
327  RowVector column_min (void) const;
328  RowVector column_max (void) const;
329 
332 
333  // i/o
334 
335  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
336  const Matrix& a);
337  friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a);
338 };
339 
340 // Publish externally used friend functions.
341 
342 extern OCTAVE_API Matrix real (const ComplexMatrix& a);
343 extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
344 
345 // column vector by row vector -> matrix operations
346 
347 extern OCTAVE_API Matrix operator * (const ColumnVector& a,
348  const RowVector& b);
349 
350 // Other functions.
351 
352 extern OCTAVE_API Matrix Givens (double, double);
353 
354 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&,
355  const Matrix&);
356 
357 extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
359  blas_trans_type transb = blas_no_trans);
360 
361 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
362 
363 extern OCTAVE_API Matrix min (double d, const Matrix& m);
364 extern OCTAVE_API Matrix min (const Matrix& m, double d);
365 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
366 
367 extern OCTAVE_API Matrix max (double d, const Matrix& m);
368 extern OCTAVE_API Matrix max (const Matrix& m, double d);
369 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
370 
371 extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
372  const ColumnVector& x2,
373  octave_idx_type n);
374 
375 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API)
376 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API)
377 
378 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API)
379 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API)
380 
381 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API)
382 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API)
383 
384 MARRAY_FORWARD_DEFS (MArray, Matrix, double)
385 
386 template <class T>
387 void read_int (std::istream& is, bool swap_bytes, T& val);
388 
389 #endif
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:27
Matrix operator-=(Matrix &x, const double &y)
Definition: dMatrix.h:384
OCTAVE_API Matrix linspace(const ColumnVector &x1, const ColumnVector &x2, octave_idx_type n)
Definition: dMatrix.cc:3328
ComplexMatrix ifourier(void) const
Definition: CMatrix.cc:1292
Matrix(const dim_vector &dv)
Definition: dMatrix.h:54
ComplexDET determinant(void) const
Definition: CMatrix.cc:1590
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:548
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:130
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
Matrix(const Array< U > &a)
Definition: dMatrix.h:65
static ComplexMatrix ltsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
ComplexMatrix inverse(void) const
Definition: CMatrix.cc:1002
ColumnVector column_vector_type
Definition: dMatrix.h:41
OCTAVE_API Matrix operator*(const ColumnVector &a, const RowVector &b)
Definition: dMatrix.cc:2635
STL namespace.
MArray< T > transpose(void) const
Definition: MArray.h:83
RowVector row_vector_type
Definition: dMatrix.h:42
ComplexRowVector row(octave_idx_type i) const
Definition: CMatrix.cc:990
OCTAVE_API Matrix real(const ComplexMatrix &a)
Definition: dMatrix.cc:608
OCTAVE_API Matrix Sylvester(const Matrix &, const Matrix &, const Matrix &)
Definition: dMatrix.cc:3012
void(* solve_singularity_handler)(double rcon)
Definition: CMatrix.h:45
Definition: MArray.h:36
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:112
Matrix(const dim_vector &dv, double val)
Definition: dMatrix.h:56
OCTAVE_API Matrix imag(const ComplexMatrix &a)
Definition: dMatrix.cc:614
F77_RET_T const double const double double * d
ComplexMatrix prod(int dim=-1) const
Definition: CMatrix.cc:3143
ComplexRowVector column_min(void) const
Definition: CMatrix.cc:3378
ComplexMatrix fsolve(MatrixType &typ, const ComplexMatrix &b, octave_idx_type &info, double &rcon, solve_singularity_handler sing_handler, bool calc_cond=false) const
Definition: CMatrix.cc:2112
OCTAVE_API Matrix max(double d, const Matrix &m)
Definition: dMatrix.cc:3262
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type r2
double rcond(void) const
Definition: CMatrix.cc:1750
ComplexMatrix cumprod(int dim=-1) const
Definition: CMatrix.cc:3131
void read_int(std::istream &is, bool swap_bytes, T &val)
ComplexMatrix cumsum(int dim=-1) const
Definition: CMatrix.cc:3137
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:120
OCTAVE_API Matrix min(double d, const Matrix &m)
Definition: dMatrix.cc:3195
boolMatrix all(int dim=-1) const
Definition: CMatrix.cc:3119
Definition: DET.h:31
#define MM_CMP_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:137
#define MM_BOOL_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:145
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
ComplexColumnVector row_min(void) const
Definition: CMatrix.cc:3228
boolMatrix any(int dim=-1) const
Definition: CMatrix.cc:3125
Matrix(const Matrix &a)
Definition: dMatrix.h:59
Matrix transpose(void) const
Definition: dMatrix.h:114
ComplexMatrix solve(MatrixType &typ, const Matrix &b) const
Definition: CMatrix.cc:2294
OCTAVE_API Matrix xgemm(const Matrix &a, const Matrix &b, blas_trans_type transa=blas_no_trans, blas_trans_type transb=blas_no_trans)
Definition: dMatrix.cc:3095
ComplexMatrix fourier(void) const
Definition: CMatrix.cc:1263
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray-decl.h:236
ComplexMatrix sumsq(int dim=-1) const
Definition: CMatrix.cc:3155
ComplexColumnVector row_max(void) const
Definition: CMatrix.cc:3303
Definition: dMatrix.h:35
Matrix(octave_idx_type r, octave_idx_type c, double val)
Definition: dMatrix.h:51
Matrix(octave_idx_type r, octave_idx_type c)
Definition: dMatrix.h:48
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:519
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:87
ComplexMatrix pseudo_inverse(double tol=0.0) const
Definition: CMatrix.cc:1220
Handles the reference counting for all the derived classes.
Definition: Array.h:45
Matrix(const MArray< U > &a)
Definition: dMatrix.h:62
ComplexRowVector column_max(void) const
Definition: CMatrix.cc:3453
ComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, double &rcon, int force, int calc_cond) const
Definition: CMatrix.cc:1099
Matrix operator+=(Matrix &x, const double &y)
Definition: dMatrix.h:384
OCTAVE_API Matrix Givens(double, double)
Definition: dMatrix.cc:2995
static ComplexMatrix utsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:95
ComplexMatrix sum(int dim=-1) const
Definition: CMatrix.cc:3149
ComplexMatrix ifourier2d(void) const
Definition: CMatrix.cc:1335
blas_trans_type
Definition: mx-defs.h:128
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
ComplexMatrix diag(octave_idx_type k=0) const
Definition: CMatrix.cc:3167
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
Matrix(void)
Definition: dMatrix.h:46
ComplexMatrix lssolve(const Matrix &b) const
Definition: CMatrix.cc:2583
ComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, double &rcon, int force, int calc_cond) const
Definition: CMatrix.cc:1042
T abs(T x)
Definition: pr-output.cc:3062
ComplexColumnVector column(octave_idx_type i) const
Definition: CMatrix.cc:996
ComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: CMatrix.cc:981
ComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: CMatrix.cc:971
Array< Complex > index(const idx_vector &i) const
Indexing without resizing.
ComplexMatrix fourier2d(void) const
Definition: CMatrix.cc:1321