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
fMatrix.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_fMatrix_h)
24 #define octave_fMatrix_h 1
25 
26 #include "fNDArray.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
38 {
39 public:
40 
43 
44  typedef void (*solve_singularity_handler) (float rcon);
45 
46  FloatMatrix (void) : FloatNDArray () { }
47 
49  : FloatNDArray (dim_vector (r, c)) { }
50 
52  : FloatNDArray (dim_vector (r, c), val) { }
53 
54  FloatMatrix (const dim_vector& dv) : FloatNDArray (dv.redim (2)) { }
55 
56  FloatMatrix (const dim_vector& dv, float val)
57  : FloatNDArray (dv.redim (2), val) { }
58 
59  FloatMatrix (const FloatMatrix& a) : FloatNDArray (a) { }
60 
61  template <class U>
62  FloatMatrix (const MArray<U>& a) : FloatNDArray (a.as_matrix ()) { }
63 
64  template <class U>
65  FloatMatrix (const Array<U>& a) : FloatNDArray (a.as_matrix ()) { }
66 
67  explicit FloatMatrix (const FloatRowVector& rv);
68 
69  explicit FloatMatrix (const FloatColumnVector& cv);
70 
71  explicit FloatMatrix (const FloatDiagMatrix& a);
72 
73  explicit FloatMatrix (const MDiagArray2<float>& a);
74 
75  explicit FloatMatrix (const DiagArray2<float>& a);
76 
77  explicit FloatMatrix (const PermMatrix& a);
78 
79  explicit FloatMatrix (const boolMatrix& a);
80 
81  explicit FloatMatrix (const charMatrix& a);
82 
83  bool operator == (const FloatMatrix& a) const;
84  bool operator != (const FloatMatrix& a) const;
85 
86  bool is_symmetric (void) const;
87 
88  // destructive insert/delete/reorder operations
89 
90  FloatMatrix& insert (const FloatMatrix& a,
92  FloatMatrix& insert (const FloatRowVector& a,
94  FloatMatrix& insert (const FloatColumnVector& a,
96  FloatMatrix& insert (const FloatDiagMatrix& a,
98 
99  FloatMatrix& fill (float val);
100  FloatMatrix& fill (float val, octave_idx_type r1, octave_idx_type c1,
102 
103  FloatMatrix append (const FloatMatrix& a) const;
104  FloatMatrix append (const FloatRowVector& a) const;
105  FloatMatrix append (const FloatColumnVector& a) const;
106  FloatMatrix append (const FloatDiagMatrix& a) const;
107 
108  FloatMatrix stack (const FloatMatrix& a) const;
109  FloatMatrix stack (const FloatRowVector& a) const;
110  FloatMatrix stack (const FloatColumnVector& a) const;
111  FloatMatrix stack (const FloatDiagMatrix& a) const;
112 
113  friend OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
114  friend OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
115 
116  friend class FloatComplexMatrix;
117 
118  FloatMatrix transpose (void) const { return MArray<float>::transpose (); }
119 
120  // resize is the destructive equivalent for this one
121 
124 
126  octave_idx_type nr, octave_idx_type nc) const;
127 
128  // extract row or column i.
129 
131 
133 
134  void resize (octave_idx_type nr, octave_idx_type nc, float rfv = 0)
135  {
136  MArray<float>::resize (dim_vector (nr, nc), rfv);
137  }
138 
139 private:
141  float& rcon, int force, int calc_cond) const;
142 
144  float& rcon, int force, int calc_cond) const;
145 
146 public:
147  FloatMatrix inverse (void) const;
148  FloatMatrix inverse (octave_idx_type& info) const;
149  FloatMatrix inverse (octave_idx_type& info, float& rcon, int force = 0,
150  int calc_cond = 1) const;
151 
152  FloatMatrix inverse (MatrixType &mattype) const;
153  FloatMatrix inverse (MatrixType &mattype, octave_idx_type& info) const;
154  FloatMatrix inverse (MatrixType &mattype, octave_idx_type& info, float& rcon,
155  int force = 0, int calc_cond = 1) const;
156 
157  FloatMatrix pseudo_inverse (float tol = 0.0) const;
158 
159  FloatComplexMatrix fourier (void) const;
160  FloatComplexMatrix ifourier (void) const;
161 
162  FloatComplexMatrix fourier2d (void) const;
163  FloatComplexMatrix ifourier2d (void) const;
164 
165  FloatDET determinant (void) const;
166  FloatDET determinant (octave_idx_type& info) const;
167  FloatDET determinant (octave_idx_type& info, float& rcon,
168  int calc_cond = 1) const;
170  float& rcon, int calc_cond = 1) const;
171 
172  float rcond (void) const;
173  float rcond (MatrixType &mattype) const;
174 
175 private:
176  // Upper triangular matrix solvers
177  FloatMatrix utsolve (MatrixType &typ, const FloatMatrix& b,
178  octave_idx_type& info,
179  float& rcon, solve_singularity_handler sing_handler,
180  bool calc_cond = false,
181  blas_trans_type transt = blas_no_trans) const;
182 
183  // Lower triangular matrix solvers
184  FloatMatrix ltsolve (MatrixType &typ, const FloatMatrix& b,
185  octave_idx_type& info,
186  float& rcon, solve_singularity_handler sing_handler,
187  bool calc_cond = false,
188  blas_trans_type transt = blas_no_trans) const;
189 
190  // Full matrix solvers (lu/cholesky)
191  FloatMatrix fsolve (MatrixType &typ, const FloatMatrix& b,
192  octave_idx_type& info,
193  float& rcon, solve_singularity_handler sing_handler,
194  bool calc_cond = false) const;
195 
196 public:
197  // Generic interface to solver with no probing of type
198  FloatMatrix solve (MatrixType &typ, const FloatMatrix& b) const;
199  FloatMatrix solve (MatrixType &typ, const FloatMatrix& b,
200  octave_idx_type& info) const;
201  FloatMatrix solve (MatrixType &typ, const FloatMatrix& b,
202  octave_idx_type& info, float& rcon) const;
203  FloatMatrix solve (MatrixType &typ, const FloatMatrix& b,
204  octave_idx_type& info, float& rcon,
205  solve_singularity_handler sing_handler,
206  bool singular_fallback = true,
207  blas_trans_type transt = blas_no_trans) const;
208 
209  FloatComplexMatrix solve (MatrixType &typ, const FloatComplexMatrix& b) const;
211  octave_idx_type& info) const;
213  octave_idx_type& info, float& rcon) const;
215  octave_idx_type& info, float& rcon,
216  solve_singularity_handler sing_handler,
217  bool singular_fallback = true,
218  blas_trans_type transt = blas_no_trans) const;
219 
220  FloatColumnVector solve (MatrixType &typ, const FloatColumnVector& b) const;
222  octave_idx_type& info) const;
224  octave_idx_type& info, float& rcon) const;
226  octave_idx_type& info, float& rcon,
227  solve_singularity_handler sing_handler,
228  blas_trans_type transt = blas_no_trans) const;
229 
231  const FloatComplexColumnVector& b) const;
233  const FloatComplexColumnVector& b,
234  octave_idx_type& info) const;
236  const FloatComplexColumnVector& b,
237  octave_idx_type& info, float& rcon) const;
239  const FloatComplexColumnVector& b,
240  octave_idx_type& info, float& rcon,
241  solve_singularity_handler sing_handler,
242  blas_trans_type transt = blas_no_trans) const;
243 
244  // Generic interface to solver with probing of type
245  FloatMatrix solve (const FloatMatrix& b) const;
246  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info) const;
247  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info,
248  float& rcon) const;
249  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info, float& rcon,
250  solve_singularity_handler sing_handler,
251  blas_trans_type transt = blas_no_trans) const;
252 
255  octave_idx_type& info) const;
257  float& rcon) const;
259  float& rcon,
260  solve_singularity_handler sing_handler,
261  blas_trans_type transt = blas_no_trans) const;
262 
263  FloatColumnVector solve (const FloatColumnVector& b) const;
265  octave_idx_type& info) const;
267  float& rcon) const;
269  float& 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,
278  float& rcon) const;
280  octave_idx_type& info,
281  float& rcon,
282  solve_singularity_handler sing_handler,
283  blas_trans_type transt = blas_no_trans) const;
284 
285  // Singular solvers
286  FloatMatrix lssolve (const FloatMatrix& b) const;
287  FloatMatrix lssolve (const FloatMatrix& b, octave_idx_type& info) const;
289  octave_idx_type& rank) const;
291  octave_idx_type& rank, float& rcon) const;
292 
295  octave_idx_type& info) const;
297  octave_idx_type& info,
298  octave_idx_type& rank) const;
300  octave_idx_type& info, octave_idx_type& rank,
301  float &rcon) const;
302 
305  octave_idx_type& info) const;
307  octave_idx_type& rank) const;
309  octave_idx_type& rank, float& rcon) const;
310 
313  octave_idx_type& info) const;
315  octave_idx_type& info,
316  octave_idx_type& rank) const;
318  octave_idx_type& info,
319  octave_idx_type& rank, float& rcon) const;
320 
323 
324  FloatMatrix cumprod (int dim = -1) const;
325  FloatMatrix cumsum (int dim = -1) const;
326  FloatMatrix prod (int dim = -1) const;
327  FloatMatrix sum (int dim = -1) const;
328  FloatMatrix sumsq (int dim = -1) const;
329  FloatMatrix abs (void) const;
330 
331  FloatMatrix diag (octave_idx_type k = 0) const;
332 
334 
335  FloatColumnVector row_min (void) const;
336  FloatColumnVector row_max (void) const;
337 
340 
341  FloatRowVector column_min (void) const;
342  FloatRowVector column_max (void) const;
343 
346 
347  // i/o
348 
349  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
350  const FloatMatrix& a);
351  friend OCTAVE_API std::istream& operator >> (std::istream& is,
352  FloatMatrix& a);
353 };
354 
355 // Publish externally used friend functions.
356 
357 extern OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
358 extern OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
359 
360 // column vector by row vector -> matrix operations
361 
362 extern OCTAVE_API FloatMatrix operator * (const FloatColumnVector& a,
363  const FloatRowVector& b);
364 
365 // Other functions.
366 
367 extern OCTAVE_API FloatMatrix Givens (float, float);
368 
369 extern OCTAVE_API FloatMatrix Sylvester (const FloatMatrix&, const FloatMatrix&,
370  const FloatMatrix&);
371 
372 extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
374  blas_trans_type transb = blas_no_trans);
375 
376 extern OCTAVE_API FloatMatrix operator * (const FloatMatrix& a,
377  const FloatMatrix& b);
378 
379 extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
380 extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
381 extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
382 
383 extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
384 extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
385 extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
386 
387 extern OCTAVE_API FloatMatrix linspace (const FloatColumnVector& x1,
388  const FloatColumnVector& x2,
389  octave_idx_type n);
390 
391 
392 MS_CMP_OP_DECLS (FloatMatrix, float, OCTAVE_API)
393 MS_BOOL_OP_DECLS (FloatMatrix, float, OCTAVE_API)
394 
395 SM_CMP_OP_DECLS (float, FloatMatrix, OCTAVE_API)
396 SM_BOOL_OP_DECLS (float, FloatMatrix, OCTAVE_API)
397 
398 MM_CMP_OP_DECLS (FloatMatrix, FloatMatrix, OCTAVE_API)
399 MM_BOOL_OP_DECLS (FloatMatrix, FloatMatrix, OCTAVE_API)
400 
401 MARRAY_FORWARD_DEFS (MArray, FloatMatrix, float)
402 
403 template <class T>
404 void read_int (std::istream& is, bool swap_bytes, T& val);
405 
406 #endif
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:27
FloatComplexRowVector column_min(void) const
Definition: fCMatrix.cc:3395
OCTAVE_API FloatMatrix Sylvester(const FloatMatrix &, const FloatMatrix &, const FloatMatrix &)
Definition: fMatrix.cc:3021
FloatComplexMatrix solve(MatrixType &typ, const FloatMatrix &b) const
Definition: fCMatrix.cc:2298
FloatComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: fCMatrix.cc:977
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition: fMatrix.h:134
void(* solve_singularity_handler)(float rcon)
Definition: fCMatrix.h:45
FloatMatrix(const dim_vector &dv, float val)
Definition: fMatrix.h:56
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:548
FloatMatrix(const FloatMatrix &a)
Definition: fMatrix.h:59
FloatMatrix(octave_idx_type r, octave_idx_type c, float val)
Definition: fMatrix.h:51
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
static ComplexMatrix ltsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
FloatComplexMatrix fsolve(MatrixType &typ, const FloatComplexMatrix &b, octave_idx_type &info, float &rcon, solve_singularity_handler sing_handler, bool calc_cond=false) const
Definition: fCMatrix.cc:2116
FloatMatrix transpose(void) const
Definition: fMatrix.h:118
FloatMatrix(octave_idx_type r, octave_idx_type c)
Definition: fMatrix.h:48
STL namespace.
MArray< T > transpose(void) const
Definition: MArray.h:83
FloatMatrix(void)
Definition: fMatrix.h:46
FloatComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, float &rcon, int force, int calc_cond) const
Definition: fCMatrix.cc:1105
FloatColumnVector column_vector_type
Definition: fMatrix.h:41
Definition: MArray.h:36
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:112
FloatComplexMatrix inverse(void) const
Definition: fCMatrix.cc:1008
FloatComplexMatrix lssolve(const FloatMatrix &b) const
Definition: fCMatrix.cc:2597
void read_int(std::istream &is, bool swap_bytes, T &val)
F77_RET_T const double const double double * d
OCTAVE_API FloatMatrix xgemm(const FloatMatrix &a, const FloatMatrix &b, blas_trans_type transa=blas_no_trans, blas_trans_type transb=blas_no_trans)
Definition: fMatrix.cc:3100
FloatComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, float &rcon, int force, int calc_cond) const
Definition: fCMatrix.cc:1048
FloatComplexMatrix cumprod(int dim=-1) const
Definition: fCMatrix.cc:3149
FloatComplexMatrix diag(octave_idx_type k=0) const
Definition: fCMatrix.cc:3184
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type r2
FloatComplexMatrix pseudo_inverse(float tol=0.0) const
Definition: fCMatrix.cc:1227
FloatMatrix(const dim_vector &dv)
Definition: fMatrix.h:54
OCTAVE_API FloatMatrix Givens(float, float)
Definition: fMatrix.cc:3004
FloatComplexColumnVector column(octave_idx_type i) const
Definition: fCMatrix.cc:1002
FloatComplexMatrix fourier2d(void) const
Definition: fCMatrix.cc:1328
OCTAVE_API FloatMatrix real(const FloatComplexMatrix &a)
Definition: fMatrix.cc:615
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:120
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
FloatComplexColumnVector row_min(void) const
Definition: fCMatrix.cc:3245
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
FloatComplexMatrix sum(int dim=-1) const
Definition: fCMatrix.cc:3167
OCTAVE_API FloatMatrix imag(const FloatComplexMatrix &a)
Definition: fMatrix.cc:621
FloatComplexMatrix sumsq(int dim=-1) const
Definition: fCMatrix.cc:3173
FloatMatrix(const MArray< U > &a)
Definition: fMatrix.h:62
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray-decl.h:236
FloatComplexMatrix cumsum(int dim=-1) const
Definition: fCMatrix.cc:3155
FloatComplexMatrix ifourier(void) const
Definition: fCMatrix.cc:1299
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:519
FloatComplexRowVector column_max(void) const
Definition: fCMatrix.cc:3470
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:87
Handles the reference counting for all the derived classes.
Definition: Array.h:45
OCTAVE_API FloatMatrix linspace(const FloatColumnVector &x1, const FloatColumnVector &x2, octave_idx_type n)
Definition: fMatrix.cc:3333
FloatComplexMatrix prod(int dim=-1) const
Definition: fCMatrix.cc:3161
FloatMatrix operator+=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:401
static ComplexMatrix utsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
FloatRowVector row_vector_type
Definition: fMatrix.h:42
OCTAVE_API FloatMatrix max(float d, const FloatMatrix &m)
Definition: fMatrix.cc:3267
FloatComplexMatrix fourier(void) const
Definition: fCMatrix.cc:1270
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:95
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
FloatComplexColumnVector row_max(void) const
Definition: fCMatrix.cc:3320
FloatComplexDET determinant(void) const
Definition: fCMatrix.cc:1594
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
float rcond(void) const
Definition: fCMatrix.cc:1754
FloatComplexMatrix ifourier2d(void) const
Definition: fCMatrix.cc:1342
FloatMatrix operator-=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:401
FloatComplexRowVector row(octave_idx_type i) const
Definition: fCMatrix.cc:996
OCTAVE_API FloatMatrix min(float d, const FloatMatrix &m)
Definition: fMatrix.cc:3200
OCTAVE_API FloatMatrix operator*(const FloatColumnVector &a, const FloatRowVector &b)
Definition: fMatrix.cc:2658
T abs(T x)
Definition: pr-output.cc:3062
FloatComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: fCMatrix.cc:987
FloatMatrix(const Array< U > &a)
Definition: fMatrix.h:65
Array< FloatComplex > index(const idx_vector &i) const
Indexing without resizing.