GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
dMatrix.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://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 "DET.h"
29 #include "MArray.h"
30 #include "MDiagArray2.h"
31 #include "MatrixType.h"
32 #include "dNDArray.h"
33 #include "mx-defs.h"
34 #include "mx-op-decl.h"
35 
36 class
37 OCTAVE_API
38 Matrix : public NDArray
39 {
40 public:
41 
44 
47 
50 
53 
54  typedef double real_elt_type;
56 
57  typedef void (*solve_singularity_handler) (double rcon);
58 
59  Matrix (void) : NDArray () { }
60 
62  : NDArray (dim_vector (r, c)) { }
63 
65  : NDArray (dim_vector (r, c), val) { }
66 
67  Matrix (const dim_vector& dv) : NDArray (dv.redim (2)) { }
68 
69  Matrix (const dim_vector& dv, double val)
70  : NDArray (dv.redim (2), val) { }
71 
72  Matrix (const Matrix& a) : NDArray (a) { }
73 
74  template <typename U>
75  Matrix (const MArray<U>& a) : NDArray (a.as_matrix ()) { }
76 
77  template <typename U>
78  Matrix (const Array<U>& a) : NDArray (a.as_matrix ()) { }
79 
80  explicit Matrix (const RowVector& rv);
81 
82  explicit Matrix (const ColumnVector& cv);
83 
84  explicit Matrix (const DiagMatrix& a);
85 
86  explicit Matrix (const MDiagArray2<double>& a);
87 
88  explicit Matrix (const DiagArray2<double>& a);
89 
90  explicit Matrix (const PermMatrix& a);
91 
92  explicit Matrix (const boolMatrix& a);
93 
94  explicit Matrix (const charMatrix& a);
95 
96  bool operator == (const Matrix& a) const;
97  bool operator != (const Matrix& a) const;
98 
99  bool issymmetric (void) const;
100 
101  OCTAVE_DEPRECATED (4.4, "use 'issymmetric' instead")
102  bool is_symmetric (void) const
103  { return issymmetric (); }
104 
105  // destructive insert/delete/reorder operations
106 
111 
112  Matrix& fill (double val);
113  Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1,
115 
116  Matrix append (const Matrix& a) const;
117  Matrix append (const RowVector& a) const;
118  Matrix append (const ColumnVector& a) const;
119  Matrix append (const DiagMatrix& a) const;
120 
121  Matrix stack (const Matrix& a) const;
122  Matrix stack (const RowVector& a) const;
123  Matrix stack (const ColumnVector& a) const;
124  Matrix stack (const DiagMatrix& a) const;
125 
126  friend OCTAVE_API Matrix real (const ComplexMatrix& a);
127  friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
128 
129  friend class ComplexMatrix;
130 
131  Matrix hermitian (void) const { return MArray<double>::transpose (); }
132  Matrix transpose (void) const { return MArray<double>::transpose (); }
133 
134  // resize is the destructive equivalent for this one
135 
137  octave_idx_type r2, octave_idx_type c2) const;
138 
140  octave_idx_type nr, octave_idx_type nc) const;
141 
142  // extract row or column i.
143 
144  RowVector row (octave_idx_type i) const;
145 
147 
148  void resize (octave_idx_type nr, octave_idx_type nc, double rfv = 0)
149  {
150  MArray<double>::resize (dim_vector (nr, nc), rfv);
151  }
152 
153 private:
154  Matrix tinverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
155  bool force, bool calc_cond) const;
156 
157  Matrix finverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
158  bool force, bool calc_cond) const;
159 
160 public:
161  Matrix inverse (void) const;
162  Matrix inverse (octave_idx_type& info) const;
163  Matrix inverse (octave_idx_type& info, double& rcon, bool force = false,
164  bool calc_cond = true) const;
165 
166  Matrix inverse (MatrixType& mattype) const;
167  Matrix inverse (MatrixType& mattype, octave_idx_type& info) const;
168  Matrix inverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
169  bool force = false, bool calc_cond = true) const;
170 
171  Matrix pseudo_inverse (double tol = 0.0) const;
172 
173  ComplexMatrix fourier (void) const;
174  ComplexMatrix ifourier (void) const;
175 
176  ComplexMatrix fourier2d (void) const;
177  ComplexMatrix ifourier2d (void) const;
178 
179  DET determinant (void) const;
180  DET determinant (octave_idx_type& info) const;
181  DET determinant (octave_idx_type& info, double& rcon,
182  bool calc_cond = true) const;
183  DET determinant (MatrixType& mattype, octave_idx_type& info,
184  double& rcon, bool calc_cond = true) const;
185 
186  double rcond (void) const;
187  double rcond (MatrixType& mattype) const;
188 
189 private:
190  // Upper triangular matrix solvers
191  Matrix utsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
192  double& rcon, solve_singularity_handler sing_handler,
193  bool calc_cond = false,
194  blas_trans_type transt = blas_no_trans) const;
195 
196  // Lower triangular matrix solvers
197  Matrix ltsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
198  double& rcon, solve_singularity_handler sing_handler,
199  bool calc_cond = false,
200  blas_trans_type transt = blas_no_trans) const;
201 
202  // Full matrix solvers (lu/cholesky)
203  Matrix fsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
204  double& rcon, solve_singularity_handler sing_handler,
205  bool calc_cond = false) const;
206 
207 public:
208  // Generic interface to solver with no probing of type
209  Matrix solve (MatrixType& mattype, const Matrix& b) const;
210  Matrix solve (MatrixType& mattype, const Matrix& b,
211  octave_idx_type& info) const;
212  Matrix solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
213  double& rcon) const;
214  Matrix solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
215  double& rcon, solve_singularity_handler sing_handler,
216  bool singular_fallback = true,
217  blas_trans_type transt = blas_no_trans) const;
218 
219  ComplexMatrix solve (MatrixType& mattype, const ComplexMatrix& b) const;
220  ComplexMatrix solve (MatrixType& mattype, const ComplexMatrix& b,
221  octave_idx_type& info) const;
222  ComplexMatrix solve (MatrixType& mattype, const ComplexMatrix& b,
223  octave_idx_type& info, double& rcon) const;
224  ComplexMatrix solve (MatrixType& mattype, const ComplexMatrix& b,
225  octave_idx_type& info, double& rcon,
226  solve_singularity_handler sing_handler,
227  bool singular_fallback = true,
228  blas_trans_type transt = blas_no_trans) const;
229 
230  ColumnVector solve (MatrixType& mattype, const ColumnVector& b) const;
231  ColumnVector solve (MatrixType& mattype, const ColumnVector& b,
232  octave_idx_type& info) const;
233  ColumnVector solve (MatrixType& mattype, const ColumnVector& b,
234  octave_idx_type& info, double& rcon) const;
235  ColumnVector solve (MatrixType& mattype, const ColumnVector& b,
236  octave_idx_type& info, double& rcon,
237  solve_singularity_handler sing_handler,
238  blas_trans_type transt = blas_no_trans) const;
239 
241  const ComplexColumnVector& b) const;
243  octave_idx_type& info) const;
245  octave_idx_type& info, double& rcon) const;
247  octave_idx_type& info, double& rcon,
248  solve_singularity_handler sing_handler,
249  blas_trans_type transt = blas_no_trans) const;
250 
251  // Generic interface to solver with probing of type
252  Matrix solve (const Matrix& b) const;
253  Matrix solve (const Matrix& b, octave_idx_type& info) const;
254  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
255  Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
256  solve_singularity_handler sing_handler,
257  blas_trans_type transt = blas_no_trans) const;
258 
259  ComplexMatrix solve (const ComplexMatrix& b) const;
260  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
262  double& rcon) const;
264  double& rcon,
265  solve_singularity_handler sing_handler,
266  blas_trans_type transt = blas_no_trans) const;
267 
268  ColumnVector solve (const ColumnVector& b) const;
269  ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
271  double& rcon) const;
273  double& rcon,
274  solve_singularity_handler sing_handler,
275  blas_trans_type transt = blas_no_trans) const;
276 
279  octave_idx_type& info) const;
281  octave_idx_type& info, double& rcon) const;
283  octave_idx_type& info, double& rcon,
284  solve_singularity_handler sing_handler,
285  blas_trans_type transt = blas_no_trans) const;
286 
287  // Singular solvers
288  Matrix lssolve (const Matrix& b) const;
289  Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
290  Matrix lssolve (const Matrix& b, octave_idx_type& info,
291  octave_idx_type& rank) const;
292  Matrix lssolve (const Matrix& b, octave_idx_type& info,
293  octave_idx_type& rank, double& rcon) const;
294 
295  ComplexMatrix lssolve (const ComplexMatrix& b) const;
296  ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
298  octave_idx_type& rank) const;
300  octave_idx_type& rank, double& rcon) const;
301 
302  ColumnVector lssolve (const ColumnVector& b) const;
303  ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const;
305  octave_idx_type& rank) const;
307  octave_idx_type& rank, double& rcon) const;
308 
311  octave_idx_type& info) const;
313  octave_idx_type& info,
314  octave_idx_type& rank) const;
316  octave_idx_type& info,
317  octave_idx_type& rank, double& rcon) const;
318 
319  Matrix& operator += (const DiagMatrix& a);
320  Matrix& operator -= (const DiagMatrix& a);
321 
322  // unary operations
323 
324  // other operations
325 
326  boolMatrix all (int dim = -1) const;
327  boolMatrix any (int dim = -1) const;
328 
329  Matrix cumprod (int dim = -1) const;
330  Matrix cumsum (int dim = -1) const;
331  Matrix prod (int dim = -1) const;
332  Matrix sum (int dim = -1) const;
333  Matrix sumsq (int dim = -1) const;
334  Matrix abs (void) const;
335 
336  Matrix diag (octave_idx_type k = 0) const;
337 
339 
340  ColumnVector row_min (void) const;
341  ColumnVector row_max (void) const;
342 
345 
346  RowVector column_min (void) const;
347  RowVector column_max (void) const;
348 
351 
352  // i/o
353 
354  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
355  const Matrix& a);
356  friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a);
357 };
358 
359 // Publish externally used friend functions.
360 
361 extern OCTAVE_API Matrix real (const ComplexMatrix& a);
362 extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
363 
364 // column vector by row vector -> matrix operations
365 
366 extern OCTAVE_API Matrix operator * (const ColumnVector& a,
367  const RowVector& b);
368 
369 // Other functions.
370 
371 extern OCTAVE_API Matrix Givens (double, double);
372 
373 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&,
374  const Matrix&);
375 
376 extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
378  blas_trans_type transb = blas_no_trans);
379 
380 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
381 
382 extern OCTAVE_API Matrix min (double d, const Matrix& m);
383 extern OCTAVE_API Matrix min (const Matrix& m, double d);
384 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
385 
386 extern OCTAVE_API Matrix max (double d, const Matrix& m);
387 extern OCTAVE_API Matrix max (const Matrix& m, double d);
388 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
389 
390 extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
391  const ColumnVector& x2,
392  octave_idx_type n);
393 
394 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API)
395 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API)
396 
397 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API)
398 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API)
399 
400 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API)
401 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API)
402 
404 
405 template <typename T>
406 void read_int (std::istream& is, bool swap_bytes, T& val);
407 
408 #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:403
ComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: CMatrix.cc:692
ComplexMatrix lssolve(const Matrix &b) const
Definition: CMatrix.cc:2386
OCTAVE_API Matrix linspace(const ColumnVector &x1, const ColumnVector &x2, octave_idx_type n)
Definition: dMatrix.cc:3187
ComplexMatrix sumsq(int dim=-1) const
Definition: CMatrix.cc:2987
OCTAVE_EXPORT octave_value_list column
Definition: sparse.cc:123
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:32
Matrix(const dim_vector &dv)
Definition: dMatrix.h:67
ComplexRowVector column_min(void) const
Definition: CMatrix.cc:3205
ComplexMatrix complex_matrix_type
Definition: dMatrix.h:49
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:566
ComplexDET determinant(void) const
Definition: CMatrix.cc:1340
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:148
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
Matrix(const Array< U > &a)
Definition: dMatrix.h:78
void(* solve_singularity_handler)(double rcon)
Definition: CMatrix.h:58
ColumnVector column_vector_type
Definition: dMatrix.h:42
void fill(const T &val)
Definition: Array.cc:72
ComplexMatrix fourier(void) const
Definition: CMatrix.cc:1020
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
for large enough k
Definition: lu.cc:617
DiagMatrix real_diag_matrix_type
Definition: dMatrix.h:51
OCTAVE_API Matrix operator*(const ColumnVector &a, const RowVector &b)
Definition: dMatrix.cc:2506
ComplexMatrix cumsum(int dim=-1) const
Definition: CMatrix.cc:2969
STL namespace.
static T abs(T x)
Definition: pr-output.cc:1696
RowVector row_vector_type
Definition: dMatrix.h:43
ComplexMatrix fourier2d(void) const
Definition: CMatrix.cc:1078
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:1499
OCTAVE_API Matrix real(const ComplexMatrix &a)
Definition: dMatrix.cc:384
OCTAVE_API Matrix Sylvester(const Matrix &, const Matrix &, const Matrix &)
Definition: dMatrix.cc:2880
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:114
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
MArray< T > transpose(void) const
Definition: MArray.h:103
Matrix(const dim_vector &dv, double val)
Definition: dMatrix.h:69
Array< T > as_matrix(void) const
Return the array as a matrix.
Definition: Array.h:390
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray.h:126
OCTAVE_API Matrix imag(const ComplexMatrix &a)
Definition: dMatrix.cc:390
ComplexMatrix cumprod(int dim=-1) const
Definition: CMatrix.cc:2963
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
ComplexMatrix inverse(void) const
Definition: CMatrix.cc:736
ComplexMatrix fsolve(MatrixType &mattype, const ComplexMatrix &b, octave_idx_type &info, double &rcon, solve_singularity_handler sing_handler, bool calc_cond=false) const
Definition: CMatrix.cc:1887
Array< T > & insert(const Array< T > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array.cc:1583
ComplexMatrix ifourier2d(void) const
Definition: CMatrix.cc:1092
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:400
boolMatrix all(int dim=-1) const
Definition: CMatrix.cc:2951
OCTAVE_API Matrix max(double d, const Matrix &m)
Definition: dMatrix.cc:3124
ComplexColumnVector row_min(void) const
Definition: CMatrix.cc:3055
Matrix transpose(void) const
Definition: dMatrix.h:132
ComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, double &rcon, bool force, bool calc_cond) const
Definition: CMatrix.cc:776
void read_int(std::istream &is, bool swap_bytes, T &val)
Array< Complex > index(const idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:697
double real_elt_type
Definition: dMatrix.h:54
ComplexMatrix diag(octave_idx_type k=0) const
Definition: CMatrix.cc:2999
#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:3060
Matrix hermitian(void) const
Definition: dMatrix.h:131
Definition: DET.h:34
#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)
Resizing (with fill).
Definition: Array.cc:1010
Complex complex_elt_type
Definition: dMatrix.h:55
bool append
Definition: load-save.cc:1618
Matrix(const Matrix &a)
Definition: dMatrix.h:72
double rcond(void) const
Definition: CMatrix.cc:1516
static M ltsolve(const SM &L, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:80
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:2962
ComplexMatrix solve(MatrixType &mattype, const Matrix &b) const
Definition: CMatrix.cc:2097
ComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, double &rcon, bool force, bool calc_cond) const
Definition: CMatrix.cc:835
Definition: dMatrix.h:36
Matrix(octave_idx_type r, octave_idx_type c, double val)
Definition: dMatrix.h:64
Matrix(octave_idx_type r, octave_idx_type c)
Definition: dMatrix.h:61
RowVector real_row_vector_type
Definition: dMatrix.h:46
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:550
ComplexColumnVector row_max(void) const
Definition: CMatrix.cc:3130
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:89
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
Matrix(const MArray< U > &a)
Definition: dMatrix.h:75
ColumnVector real_column_vector_type
Definition: dMatrix.h:45
ComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: CMatrix.cc:682
Matrix operator+=(Matrix &x, const double &y)
Definition: dMatrix.h:403
OCTAVE_API Matrix Givens(double, double)
Definition: dMatrix.cc:2863
ComplexMatrix ifourier(void) const
Definition: CMatrix.cc:1049
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexMatrix pseudo_inverse(double tol=0.0) const
Definition: CMatrix.cc:975
ComplexDiagMatrix complex_diag_matrix_type
Definition: dMatrix.h:52
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:97
b
Definition: cellfun.cc:400
static M utsolve(const SM &U, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:100
for i
Definition: data.cc:5264
blas_trans_type
Definition: mx-defs.h:105
ComplexMatrix prod(int dim=-1) const
Definition: CMatrix.cc:2975
std::complex< double > Complex
Definition: oct-cmplx.h:31
ComplexMatrix sum(int dim=-1) const
Definition: CMatrix.cc:2981
Matrix(void)
Definition: dMatrix.h:59
write the output to stdout if nargout is
Definition: load-save.cc:1612
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
ComplexRowVector column_max(void) const
Definition: CMatrix.cc:3280
dim_vector dv
Definition: sub2ind.cc:263
Matrix real_matrix_type
Definition: dMatrix.h:48
octave::stream os
Definition: file-io.cc:627
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