GNU Octave  4.2.1
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-2017 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 "octave-config.h"
27 
28 #include "dNDArray.h"
29 #include "MArray.h"
30 #include "MDiagArray2.h"
31 #include "MatrixType.h"
32 
33 #include "mx-defs.h"
34 #include "mx-op-decl.h"
35 #include "DET.h"
36 
37 class
38 OCTAVE_API
39 Matrix : public NDArray
40 {
41 public:
42 
45 
48 
51 
54 
55  typedef double real_elt_type;
57 
58  typedef void (*solve_singularity_handler) (double rcon);
59 
60  Matrix (void) : NDArray () { }
61 
63  : NDArray (dim_vector (r, c)) { }
64 
66  : NDArray (dim_vector (r, c), val) { }
67 
68  Matrix (const dim_vector& dv) : NDArray (dv.redim (2)) { }
69 
70  Matrix (const dim_vector& dv, double val)
71  : NDArray (dv.redim (2), val) { }
72 
73  Matrix (const Matrix& a) : NDArray (a) { }
74 
75  template <typename U>
76  Matrix (const MArray<U>& a) : NDArray (a.as_matrix ()) { }
77 
78  template <typename U>
79  Matrix (const Array<U>& a) : NDArray (a.as_matrix ()) { }
80 
81  explicit Matrix (const RowVector& rv);
82 
83  explicit Matrix (const ColumnVector& cv);
84 
85  explicit Matrix (const DiagMatrix& a);
86 
87  explicit Matrix (const MDiagArray2<double>& a);
88 
89  explicit Matrix (const DiagArray2<double>& a);
90 
91  explicit Matrix (const PermMatrix& a);
92 
93  explicit Matrix (const boolMatrix& a);
94 
95  explicit Matrix (const charMatrix& a);
96 
97  bool operator == (const Matrix& a) const;
98  bool operator != (const Matrix& a) const;
99 
100  bool is_symmetric (void) const;
101 
102  // destructive insert/delete/reorder operations
103 
108 
109  Matrix& fill (double val);
110  Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1,
112 
113  Matrix append (const Matrix& a) const;
114  Matrix append (const RowVector& a) const;
115  Matrix append (const ColumnVector& a) const;
116  Matrix append (const DiagMatrix& a) const;
117 
118  Matrix stack (const Matrix& a) const;
119  Matrix stack (const RowVector& a) const;
120  Matrix stack (const ColumnVector& a) const;
121  Matrix stack (const DiagMatrix& a) const;
122 
123  friend OCTAVE_API Matrix real (const ComplexMatrix& a);
124  friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
125 
126  friend class ComplexMatrix;
127 
128  Matrix hermitian (void) const { return MArray<double>::transpose (); }
129  Matrix transpose (void) const { return MArray<double>::transpose (); }
130 
131  // resize is the destructive equivalent for this one
132 
134  octave_idx_type r2, octave_idx_type c2) const;
135 
137  octave_idx_type nr, octave_idx_type nc) const;
138 
139  // extract row or column i.
140 
141  RowVector row (octave_idx_type i) const;
142 
144 
145  void resize (octave_idx_type nr, octave_idx_type nc, double rfv = 0)
146  {
147  MArray<double>::resize (dim_vector (nr, nc), rfv);
148  }
149 
150 private:
151  Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
152  bool force, bool calc_cond) const;
153 
154  Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
155  bool force, bool calc_cond) const;
156 
157 public:
158  Matrix inverse (void) const;
159  Matrix inverse (octave_idx_type& info) const;
160  Matrix inverse (octave_idx_type& info, double& rcon, bool force = false,
161  bool calc_cond = true) const;
162 
163  Matrix inverse (MatrixType &mattype) const;
164  Matrix inverse (MatrixType &mattype, octave_idx_type& info) const;
165  Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
166  bool force = false, bool calc_cond = true) const;
167 
168  Matrix pseudo_inverse (double tol = 0.0) const;
169 
170  ComplexMatrix fourier (void) const;
171  ComplexMatrix ifourier (void) const;
172 
173  ComplexMatrix fourier2d (void) const;
174  ComplexMatrix ifourier2d (void) const;
175 
176  DET determinant (void) const;
177  DET determinant (octave_idx_type& info) const;
178  DET determinant (octave_idx_type& info, double& rcon,
179  bool calc_cond = true) const;
180  DET determinant (MatrixType &mattype, octave_idx_type& info,
181  double& rcon, bool calc_cond = true) const;
182 
183  double rcond (void) const;
184  double rcond (MatrixType &mattype) const;
185 
186 private:
187  // Upper triangular matrix solvers
188  Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
189  double& rcon, solve_singularity_handler sing_handler,
190  bool calc_cond = false,
191  blas_trans_type transt = blas_no_trans) const;
192 
193  // Lower triangular matrix solvers
194  Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
195  double& rcon, solve_singularity_handler sing_handler,
196  bool calc_cond = false,
197  blas_trans_type transt = blas_no_trans) const;
198 
199  // Full matrix solvers (lu/cholesky)
200  Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
201  double& rcon, solve_singularity_handler sing_handler,
202  bool calc_cond = false) const;
203 
204 public:
205  // Generic interface to solver with no probing of type
206  Matrix solve (MatrixType &typ, const Matrix& b) const;
207  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
208  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
209  double& rcon) const;
210  Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
211  double& rcon, solve_singularity_handler sing_handler,
212  bool singular_fallback = true,
213  blas_trans_type transt = blas_no_trans) const;
214 
215  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
217  octave_idx_type& info) const;
219  octave_idx_type& info, double& rcon) const;
221  octave_idx_type& info, double& rcon,
222  solve_singularity_handler sing_handler,
223  bool singular_fallback = true,
224  blas_trans_type transt = blas_no_trans) const;
225 
226  ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
228  octave_idx_type& info) const;
230  octave_idx_type& info, double& rcon) const;
232  octave_idx_type& info, double& rcon,
233  solve_singularity_handler sing_handler,
234  blas_trans_type transt = blas_no_trans) const;
235 
237  const ComplexColumnVector& b) const;
239  octave_idx_type& info) const;
241  octave_idx_type& info, double& rcon) const;
243  octave_idx_type& info, double& rcon,
244  solve_singularity_handler sing_handler,
245  blas_trans_type transt = blas_no_trans) const;
246 
247  // Generic interface to solver with probing of type
248  Matrix solve (const Matrix& b) const;
249  Matrix solve (const Matrix& b, octave_idx_type& info) const;
250  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
251  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
252  solve_singularity_handler sing_handler,
253  blas_trans_type transt = blas_no_trans) const;
254 
255  ComplexMatrix solve (const ComplexMatrix& b) const;
256  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
258  double& rcon) const;
260  double& rcon,
261  solve_singularity_handler sing_handler,
262  blas_trans_type transt = blas_no_trans) const;
263 
264  ColumnVector solve (const ColumnVector& b) const;
265  ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
267  double& rcon) const;
269  double& rcon,
270  solve_singularity_handler sing_handler,
271  blas_trans_type transt = blas_no_trans) const;
272 
275  octave_idx_type& info) const;
277  octave_idx_type& info, double& rcon) const;
279  octave_idx_type& info, double& rcon,
280  solve_singularity_handler sing_handler,
281  blas_trans_type transt = blas_no_trans) const;
282 
283  // Singular solvers
284  Matrix lssolve (const Matrix& b) const;
285  Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
286  Matrix lssolve (const Matrix& b, octave_idx_type& info,
287  octave_idx_type& rank) const;
288  Matrix lssolve (const Matrix& b, octave_idx_type& info,
289  octave_idx_type& rank, double& rcon) const;
290 
291  ComplexMatrix lssolve (const ComplexMatrix& b) const;
292  ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
294  octave_idx_type& rank) const;
296  octave_idx_type& rank, double &rcon) const;
297 
298  ColumnVector lssolve (const ColumnVector& b) const;
299  ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const;
301  octave_idx_type& rank) const;
303  octave_idx_type& rank, double& rcon) const;
304 
307  octave_idx_type& info) const;
309  octave_idx_type& info,
310  octave_idx_type& rank) const;
312  octave_idx_type& info,
313  octave_idx_type& rank, double& rcon) const;
314 
315  Matrix& operator += (const DiagMatrix& a);
316  Matrix& operator -= (const DiagMatrix& a);
317 
318  // unary operations
319 
320  // other operations
321 
322  boolMatrix all (int dim = -1) const;
323  boolMatrix any (int dim = -1) const;
324 
325  Matrix cumprod (int dim = -1) const;
326  Matrix cumsum (int dim = -1) const;
327  Matrix prod (int dim = -1) const;
328  Matrix sum (int dim = -1) const;
329  Matrix sumsq (int dim = -1) const;
330  Matrix abs (void) const;
331 
332  Matrix diag (octave_idx_type k = 0) const;
333 
335 
336  ColumnVector row_min (void) const;
337  ColumnVector row_max (void) const;
338 
341 
342  RowVector column_min (void) const;
343  RowVector column_max (void) const;
344 
347 
348  // i/o
349 
350  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
351  const Matrix& a);
352  friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a);
353 };
354 
355 // Publish externally used friend functions.
356 
357 extern OCTAVE_API Matrix real (const ComplexMatrix& a);
358 extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
359 
360 // column vector by row vector -> matrix operations
361 
362 extern OCTAVE_API Matrix operator * (const ColumnVector& a,
363  const RowVector& b);
364 
365 // Other functions.
366 
367 extern OCTAVE_API Matrix Givens (double, double);
368 
369 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&,
370  const Matrix&);
371 
372 extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
374  blas_trans_type transb = blas_no_trans);
375 
376 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
377 
378 extern OCTAVE_API Matrix min (double d, const Matrix& m);
379 extern OCTAVE_API Matrix min (const Matrix& m, double d);
380 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
381 
382 extern OCTAVE_API Matrix max (double d, const Matrix& m);
383 extern OCTAVE_API Matrix max (const Matrix& m, double d);
384 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
385 
386 extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
387  const ColumnVector& x2,
388  octave_idx_type n);
389 
390 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API)
391 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API)
392 
393 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API)
394 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API)
395 
396 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API)
397 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API)
398 
399 MARRAY_FORWARD_DEFS (MArray, Matrix, double)
400 
401 template <typename T>
402 void read_int (std::istream& is, bool swap_bytes, T& val);
403 
404 #endif
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:29
Matrix operator-=(Matrix &x, const double &y)
Definition: dMatrix.h:399
OCTAVE_API Matrix linspace(const ColumnVector &x1, const ColumnVector &x2, octave_idx_type n)
Definition: dMatrix.cc:3075
ComplexMatrix ifourier(void) const
Definition: CMatrix.cc:1009
OCTAVE_EXPORT octave_value_list column
Definition: sparse.cc:123
Matrix(const dim_vector &dv)
Definition: dMatrix.h:68
ComplexDET determinant(void) const
Definition: CMatrix.cc:1298
ComplexMatrix complex_matrix_type
Definition: dMatrix.h:50
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:567
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:145
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
Matrix(const Array< U > &a)
Definition: dMatrix.h:79
ComplexMatrix inverse(void) const
Definition: CMatrix.cc:717
ColumnVector column_vector_type
Definition: dMatrix.h:43
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
ComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, double &rcon, bool force, bool calc_cond) const
Definition: CMatrix.cc:812
for large enough k
Definition: lu.cc:606
DiagMatrix real_diag_matrix_type
Definition: dMatrix.h:52
OCTAVE_API Matrix operator*(const ColumnVector &a, const RowVector &b)
Definition: dMatrix.cc:2394
STL namespace.
MArray< T > transpose(void) const
Definition: MArray.h:103
RowVector row_vector_type
Definition: dMatrix.h:44
OCTAVE_API Matrix real(const ComplexMatrix &a)
Definition: dMatrix.cc:386
OCTAVE_API Matrix Sylvester(const Matrix &, const Matrix &, const Matrix &)
Definition: dMatrix.cc:2768
void(* solve_singularity_handler)(double rcon)
Definition: CMatrix.h:59
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:32
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:114
ComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, double &rcon, bool force, bool calc_cond) const
Definition: CMatrix.cc:757
Matrix(const dim_vector &dv, double val)
Definition: dMatrix.h:70
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray.h:126
OCTAVE_API Matrix imag(const ComplexMatrix &a)
Definition: dMatrix.cc:392
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
ComplexMatrix prod(int dim=-1) const
Definition: CMatrix.cc:2856
ComplexRowVector column_min(void) const
Definition: CMatrix.cc:3086
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:1816
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
OCTAVE_API Matrix max(double d, const Matrix &m)
Definition: dMatrix.cc:3012
double rcond(void) const
Definition: CMatrix.cc:1459
ComplexMatrix cumprod(int dim=-1) const
Definition: CMatrix.cc:2844
void read_int(std::istream &is, bool swap_bytes, T &val)
double real_elt_type
Definition: dMatrix.h:55
ComplexMatrix cumsum(int dim=-1) const
Definition: CMatrix.cc:2850
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:122
OCTAVE_API Matrix min(double d, const Matrix &m)
Definition: dMatrix.cc:2948
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
boolMatrix all(int dim=-1) const
Definition: CMatrix.cc:2832
Definition: DET.h:33
#define MM_CMP_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:139
#define MM_BOOL_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:147
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1028
ComplexColumnVector row_min(void) const
Definition: CMatrix.cc:2936
Complex complex_elt_type
Definition: dMatrix.h:56
SparseBoolMatrix & insert(const SparseBoolMatrix &a, octave_idx_type r, octave_idx_type c)
Definition: boolSparse.cc:75
bool append
Definition: load-save.cc:1582
Matrix(const Matrix &a)
Definition: dMatrix.h:73
Matrix transpose(void) const
Definition: dMatrix.h:129
ComplexMatrix solve(MatrixType &typ, const Matrix &b) const
Definition: CMatrix.cc:2010
static M ltsolve(const SM &L, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:79
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:2850
ComplexMatrix fourier(void) const
Definition: CMatrix.cc:980
ComplexMatrix sumsq(int dim=-1) const
Definition: CMatrix.cc:2868
ComplexColumnVector row_max(void) const
Definition: CMatrix.cc:3011
Definition: dMatrix.h:37
Matrix(octave_idx_type r, octave_idx_type c, double val)
Definition: dMatrix.h:65
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
Matrix(octave_idx_type r, octave_idx_type c)
Definition: dMatrix.h:62
RowVector real_row_vector_type
Definition: dMatrix.h:47
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:538
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:89
ComplexMatrix pseudo_inverse(double tol=0.0) const
Definition: CMatrix.cc:936
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
Matrix(const MArray< U > &a)
Definition: dMatrix.h:76
ColumnVector real_column_vector_type
Definition: dMatrix.h:46
ComplexRowVector column_max(void) const
Definition: CMatrix.cc:3161
Matrix operator+=(Matrix &x, const double &y)
Definition: dMatrix.h:399
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup abs(local error in x(i))<
OCTAVE_API Matrix Givens(double, double)
Definition: dMatrix.cc:2751
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexDiagMatrix complex_diag_matrix_type
Definition: dMatrix.h:53
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:97
b
Definition: cellfun.cc:398
ComplexMatrix sum(int dim=-1) const
Definition: CMatrix.cc:2862
static M utsolve(const SM &U, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:106
Matrix hermitian(void) const
Definition: dMatrix.h:128
ComplexMatrix ifourier2d(void) const
Definition: CMatrix.cc:1052
blas_trans_type
Definition: mx-defs.h:103
ComplexMatrix diag(octave_idx_type k=0) const
Definition: CMatrix.cc:2880
std::complex< double > Complex
Definition: oct-cmplx.h:31
Matrix(void)
Definition: dMatrix.h:60
write the output to stdout if nargout is
Definition: load-save.cc:1576
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
the second is matched to the second specifier and placed in the second column and so forth If there are more words than specifiers then the process is repeated until all words have been processed or the limit imposed by any(non-whitespace) text in the format that is not one of these specifiers is considered a literal.If there is a literal between two format specifiers then that same literal must appear in the input stream between the matching words.The following specifiers are valid
Definition: file-io.cc:1491
ComplexMatrix lssolve(const Matrix &b) const
Definition: CMatrix.cc:2296
ComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: CMatrix.cc:696
dim_vector dv
Definition: sub2ind.cc:263
ComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: CMatrix.cc:686
Matrix real_matrix_type
Definition: dMatrix.h:49
where the brackets indicate optional arguments and and character or cell array For character arrays the conversion is repeated for every row
Definition: str2double.cc:342
Array< Complex > index(const idx_vector &i) const
Indexing without resizing.
ComplexMatrix fourier2d(void) const
Definition: CMatrix.cc:1038