GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
fMatrix.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_fMatrix_h)
27 #define octave_fMatrix_h 1
28 
29 #include "octave-config.h"
30 
31 #include "DET.h"
32 #include "MArray.h"
33 #include "MDiagArray2.h"
34 #include "MatrixType.h"
35 #include "fNDArray.h"
36 #include "mx-defs.h"
37 #include "mx-op-decl.h"
38 
39 class
40 OCTAVE_API
42 {
43 public:
44 
47 
50 
53 
56 
57  typedef float real_elt_type;
59 
60  typedef void (*solve_singularity_handler) (float rcon);
61 
62  FloatMatrix (void) = default;
63 
64  FloatMatrix (const FloatMatrix& a) = default;
65 
66  FloatMatrix& operator = (const FloatMatrix& a) = default;
67 
68  ~FloatMatrix (void) = default;
69 
71  : FloatNDArray (dim_vector (r, c)) { }
72 
74  : FloatNDArray (dim_vector (r, c), val) { }
75 
76  FloatMatrix (const dim_vector& dv) : FloatNDArray (dv.redim (2)) { }
77 
78  FloatMatrix (const dim_vector& dv, float val)
79  : FloatNDArray (dv.redim (2), val) { }
80 
81  template <typename U>
82  FloatMatrix (const MArray<U>& a) : FloatNDArray (a.as_matrix ()) { }
83 
84  template <typename U>
85  FloatMatrix (const Array<U>& a) : FloatNDArray (a.as_matrix ()) { }
86 
87  explicit FloatMatrix (const FloatRowVector& rv);
88 
89  explicit FloatMatrix (const FloatColumnVector& cv);
90 
91  explicit FloatMatrix (const FloatDiagMatrix& a);
92 
93  explicit FloatMatrix (const MDiagArray2<float>& a);
94 
95  explicit FloatMatrix (const DiagArray2<float>& a);
96 
97  explicit FloatMatrix (const PermMatrix& a);
98 
99  explicit FloatMatrix (const boolMatrix& a);
100 
101  explicit FloatMatrix (const charMatrix& a);
102 
103  bool operator == (const FloatMatrix& a) const;
104  bool operator != (const FloatMatrix& a) const;
105 
106  bool issymmetric (void) const;
107 
108  // destructive insert/delete/reorder operations
109 
110  FloatMatrix& insert (const FloatMatrix& a,
112  FloatMatrix& insert (const FloatRowVector& a,
114  FloatMatrix& insert (const FloatColumnVector& a,
116  FloatMatrix& insert (const FloatDiagMatrix& a,
118 
119  FloatMatrix& fill (float val);
120  FloatMatrix& fill (float val, octave_idx_type r1, octave_idx_type c1,
122 
123  FloatMatrix append (const FloatMatrix& a) const;
124  FloatMatrix append (const FloatRowVector& a) const;
125  FloatMatrix append (const FloatColumnVector& a) const;
126  FloatMatrix append (const FloatDiagMatrix& a) const;
127 
128  FloatMatrix stack (const FloatMatrix& a) const;
129  FloatMatrix stack (const FloatRowVector& a) const;
130  FloatMatrix stack (const FloatColumnVector& a) const;
131  FloatMatrix stack (const FloatDiagMatrix& a) const;
132 
133  friend OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
134  friend OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
135 
136  friend class FloatComplexMatrix;
137 
138  FloatMatrix hermitian (void) const { return MArray<float>::transpose (); }
139  FloatMatrix transpose (void) const { return MArray<float>::transpose (); }
140 
141  // resize is the destructive equivalent for this one
142 
144  octave_idx_type r2, octave_idx_type c2) const;
145 
147  octave_idx_type nr, octave_idx_type nc) const;
148 
149  // extract row or column i.
150 
152 
154 
155  void resize (octave_idx_type nr, octave_idx_type nc, float rfv = 0)
156  {
157  MArray<float>::resize (dim_vector (nr, nc), rfv);
158  }
159 
160 private:
162  float& rcon, bool force, bool calc_cond) const;
163 
165  float& rcon, bool force, bool calc_cond) const;
166 
167 public:
168  FloatMatrix inverse (void) const;
169  FloatMatrix inverse (octave_idx_type& info) const;
170  FloatMatrix inverse (octave_idx_type& info, float& rcon, bool force = false,
171  bool calc_cond = true) const;
172 
173  FloatMatrix inverse (MatrixType& mattype) const;
174  FloatMatrix inverse (MatrixType& mattype, octave_idx_type& info) const;
175  FloatMatrix inverse (MatrixType& mattype, octave_idx_type& info, float& rcon,
176  bool force = false, bool calc_cond = true) const;
177 
178  FloatMatrix pseudo_inverse (float tol = 0.0) const;
179 
180  FloatComplexMatrix fourier (void) const;
181  FloatComplexMatrix ifourier (void) const;
182 
183  FloatComplexMatrix fourier2d (void) const;
184  FloatComplexMatrix ifourier2d (void) const;
185 
186  FloatDET determinant (void) const;
187  FloatDET determinant (octave_idx_type& info) const;
188  FloatDET determinant (octave_idx_type& info, float& rcon,
189  bool calc_cond = true) const;
191  float& rcon, bool calc_cond = true) const;
192 
193  float rcond (void) const;
194  float rcond (MatrixType& mattype) const;
195 
196 private:
197  // Upper triangular matrix solvers
198  FloatMatrix utsolve (MatrixType& mattype, const FloatMatrix& b,
199  octave_idx_type& info,
200  float& rcon, solve_singularity_handler sing_handler,
201  bool calc_cond = false,
202  blas_trans_type transt = blas_no_trans) const;
203 
204  // Lower triangular matrix solvers
205  FloatMatrix ltsolve (MatrixType& mattype, const FloatMatrix& b,
206  octave_idx_type& info,
207  float& rcon, solve_singularity_handler sing_handler,
208  bool calc_cond = false,
209  blas_trans_type transt = blas_no_trans) const;
210 
211  // Full matrix solvers (lu/cholesky)
212  FloatMatrix fsolve (MatrixType& mattype, const FloatMatrix& b,
213  octave_idx_type& info,
214  float& rcon, solve_singularity_handler sing_handler,
215  bool calc_cond = false) const;
216 
217 public:
218  // Generic interface to solver with no probing of type
219  FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b) const;
220  FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
221  octave_idx_type& info) const;
222  FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
223  octave_idx_type& info, float& rcon) const;
224  FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
225  octave_idx_type& info, float& rcon,
226  solve_singularity_handler sing_handler,
227  bool singular_fallback = true,
228  blas_trans_type transt = blas_no_trans) const;
229 
231  const FloatComplexMatrix& b) const;
233  octave_idx_type& info) const;
235  octave_idx_type& info, float& rcon) const;
237  octave_idx_type& info, float& rcon,
238  solve_singularity_handler sing_handler,
239  bool singular_fallback = true,
240  blas_trans_type transt = blas_no_trans) const;
241 
242  FloatColumnVector solve (MatrixType& mattype, const FloatColumnVector& b) const;
244  octave_idx_type& info) const;
246  octave_idx_type& info, float& rcon) const;
248  octave_idx_type& info, float& rcon,
249  solve_singularity_handler sing_handler,
250  blas_trans_type transt = blas_no_trans) const;
251 
253  const FloatComplexColumnVector& b) const;
255  const FloatComplexColumnVector& b,
256  octave_idx_type& info) const;
258  const FloatComplexColumnVector& b,
259  octave_idx_type& info, float& rcon) const;
261  const FloatComplexColumnVector& b,
262  octave_idx_type& info, float& rcon,
263  solve_singularity_handler sing_handler,
264  blas_trans_type transt = blas_no_trans) const;
265 
266  // Generic interface to solver with probing of type
267  FloatMatrix solve (const FloatMatrix& b) const;
268  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info) const;
269  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info,
270  float& rcon) const;
271  FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info, float& rcon,
272  solve_singularity_handler sing_handler,
273  blas_trans_type transt = blas_no_trans) const;
274 
277  octave_idx_type& info) const;
279  float& rcon) const;
281  float& rcon,
282  solve_singularity_handler sing_handler,
283  blas_trans_type transt = blas_no_trans) const;
284 
285  FloatColumnVector solve (const FloatColumnVector& b) const;
287  octave_idx_type& info) const;
289  float& rcon) const;
291  float& rcon,
292  solve_singularity_handler sing_handler,
293  blas_trans_type transt = blas_no_trans) const;
294 
297  octave_idx_type& info) const;
299  octave_idx_type& info,
300  float& rcon) const;
302  octave_idx_type& info,
303  float& rcon,
304  solve_singularity_handler sing_handler,
305  blas_trans_type transt = blas_no_trans) const;
306 
307  // Singular solvers
308  FloatMatrix lssolve (const FloatMatrix& b) const;
309  FloatMatrix lssolve (const FloatMatrix& b, octave_idx_type& info) const;
311  octave_idx_type& rank) const;
313  octave_idx_type& rank, float& rcon) const;
314 
317  octave_idx_type& info) const;
319  octave_idx_type& info,
320  octave_idx_type& rank) const;
322  octave_idx_type& info, octave_idx_type& rank,
323  float& rcon) const;
324 
327  octave_idx_type& info) const;
329  octave_idx_type& rank) const;
331  octave_idx_type& rank, float& rcon) const;
332 
335  octave_idx_type& info) const;
337  octave_idx_type& info,
338  octave_idx_type& rank) const;
340  octave_idx_type& info,
341  octave_idx_type& rank, float& rcon) const;
342 
345 
346  FloatMatrix cumprod (int dim = -1) const;
347  FloatMatrix cumsum (int dim = -1) const;
348  FloatMatrix prod (int dim = -1) const;
349  FloatMatrix sum (int dim = -1) const;
350  FloatMatrix sumsq (int dim = -1) const;
351  FloatMatrix abs (void) const;
352 
353  FloatMatrix diag (octave_idx_type k = 0) const;
354 
356 
357  FloatColumnVector row_min (void) const;
358  FloatColumnVector row_max (void) const;
359 
362 
363  FloatRowVector column_min (void) const;
364  FloatRowVector column_max (void) const;
365 
368 
369  // i/o
370 
371  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
372  const FloatMatrix& a);
373  friend OCTAVE_API std::istream& operator >> (std::istream& is,
374  FloatMatrix& a);
375 };
376 
377 // Publish externally used friend functions.
378 
379 extern OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
380 extern OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
381 
382 // column vector by row vector -> matrix operations
383 
384 extern OCTAVE_API FloatMatrix operator * (const FloatColumnVector& a,
385  const FloatRowVector& b);
386 
387 // Other functions.
388 
389 extern OCTAVE_API FloatMatrix Givens (float, float);
390 
391 extern OCTAVE_API FloatMatrix Sylvester (const FloatMatrix&, const FloatMatrix&,
392  const FloatMatrix&);
393 
394 extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
396  blas_trans_type transb = blas_no_trans);
397 
398 extern OCTAVE_API FloatMatrix operator * (const FloatMatrix& a,
399  const FloatMatrix& b);
400 
401 extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
402 extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
403 extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
404 
405 extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
406 extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
407 extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
408 
409 extern OCTAVE_API FloatMatrix linspace (const FloatColumnVector& x1,
410  const FloatColumnVector& x2,
412 
413 MS_CMP_OP_DECLS (FloatMatrix, float, OCTAVE_API)
414 MS_BOOL_OP_DECLS (FloatMatrix, float, OCTAVE_API)
415 
416 SM_CMP_OP_DECLS (float, FloatMatrix, OCTAVE_API)
417 SM_BOOL_OP_DECLS (float, FloatMatrix, OCTAVE_API)
418 
421 
423 
424 template <typename T>
425 void read_int (std::istream& is, bool swap_bytes, T& val);
426 
427 #endif
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray.h:128
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:281
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:32
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:128
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1011
Array< T > index(const idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:698
FloatComplexMatrix sumsq(int dim=-1) const
Definition: fCMatrix.cc:2823
FloatComplexMatrix pseudo_inverse(float tol=0.0) const
Definition: fCMatrix.cc:977
FloatComplexMatrix fourier2d(void) const
Definition: fCMatrix.cc:1080
FloatComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: fCMatrix.cc:686
FloatComplexMatrix lssolve(const FloatMatrix &b) const
Definition: fCMatrix.cc:2221
FloatComplexRowVector row(octave_idx_type i) const
Definition: fCMatrix.cc:705
FloatComplexMatrix fourier(void) const
Definition: fCMatrix.cc:1022
FloatComplexColumnVector column(octave_idx_type i) const
Definition: fCMatrix.cc:711
FloatComplexMatrix ifourier2d(void) const
Definition: fCMatrix.cc:1094
FloatComplexDET determinant(void) const
Definition: fCMatrix.cc:1148
FloatComplexMatrix cumsum(int dim=-1) const
Definition: fCMatrix.cc:2805
FloatComplexMatrix diag(octave_idx_type k=0) const
Definition: fCMatrix.cc:2834
FloatComplexMatrix sum(int dim=-1) const
Definition: fCMatrix.cc:2817
FloatComplexRowVector column_min(void) const
Definition: fCMatrix.cc:3044
void(* solve_singularity_handler)(float rcon)
Definition: fCMatrix.h:61
FloatComplexMatrix ifourier(void) const
Definition: fCMatrix.cc:1051
FloatComplexColumnVector row_max(void) const
Definition: fCMatrix.cc:2969
FloatComplexMatrix solve(MatrixType &mattype, const FloatMatrix &b) const
Definition: fCMatrix.cc:1918
FloatComplexMatrix inverse(void) const
Definition: fCMatrix.cc:740
FloatComplexRowVector column_max(void) const
Definition: fCMatrix.cc:3119
FloatComplexColumnVector row_min(void) const
Definition: fCMatrix.cc:2894
FloatComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, float &rcon, bool force, bool calc_cond) const
Definition: fCMatrix.cc:780
FloatComplexMatrix cumprod(int dim=-1) const
Definition: fCMatrix.cc:2799
float rcond(void) const
Definition: fCMatrix.cc:1327
FloatComplexMatrix prod(int dim=-1) const
Definition: fCMatrix.cc:2811
FloatComplexMatrix fsolve(MatrixType &mattype, const FloatComplexMatrix &b, octave_idx_type &info, float &rcon, solve_singularity_handler sing_handler, bool calc_cond=false) const
Definition: fCMatrix.cc:1706
FloatComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: fCMatrix.cc:696
FloatComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, float &rcon, bool force, bool calc_cond) const
Definition: fCMatrix.cc:839
FloatMatrix(octave_idx_type r, octave_idx_type c, float val)
Definition: fMatrix.h:73
FloatMatrix real_matrix_type
Definition: fMatrix.h:51
FloatComplex complex_elt_type
Definition: fMatrix.h:58
FloatMatrix transpose(void) const
Definition: fMatrix.h:139
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition: fMatrix.h:155
FloatMatrix(const Array< U > &a)
Definition: fMatrix.h:85
FloatMatrix hermitian(void) const
Definition: fMatrix.h:138
FloatMatrix(void)=default
FloatDiagMatrix real_diag_matrix_type
Definition: fMatrix.h:54
~FloatMatrix(void)=default
FloatComplexDiagMatrix complex_diag_matrix_type
Definition: fMatrix.h:55
FloatMatrix(const dim_vector &dv)
Definition: fMatrix.h:76
FloatMatrix(octave_idx_type r, octave_idx_type c)
Definition: fMatrix.h:70
FloatColumnVector column_vector_type
Definition: fMatrix.h:45
FloatMatrix(const dim_vector &dv, float val)
Definition: fMatrix.h:78
FloatMatrix(const MArray< U > &a)
Definition: fMatrix.h:82
FloatMatrix(const FloatMatrix &a)=default
float real_elt_type
Definition: fMatrix.h:57
FloatRowVector row_vector_type
Definition: fMatrix.h:46
FloatRowVector real_row_vector_type
Definition: fMatrix.h:49
FloatComplexMatrix complex_matrix_type
Definition: fMatrix.h:52
FloatColumnVector real_column_vector_type
Definition: fMatrix.h:48
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:63
MArray< T > transpose(void) const
Definition: MArray.h:105
Definition: DET.h:39
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:601
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:585
static M utsolve(const SM &U, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:258
static M ltsolve(const SM &L, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:238
OCTAVE_API FloatMatrix Givens(float, float)
Definition: fMatrix.cc:2681
void read_int(std::istream &is, bool swap_bytes, T &val)
OCTAVE_API FloatMatrix min(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2874
OCTAVE_API FloatMatrix imag(const FloatComplexMatrix &a)
Definition: fMatrix.cc:397
OCTAVE_API FloatMatrix max(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2938
FloatMatrix operator-=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:422
FloatMatrix operator+=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:422
OCTAVE_API FloatMatrix Sylvester(const FloatMatrix &, const FloatMatrix &, const FloatMatrix &)
Definition: fMatrix.cc:2698
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:2776
OCTAVE_API FloatMatrix real(const FloatComplexMatrix &a)
Definition: fMatrix.cc:391
OCTAVE_API FloatMatrix operator*(const FloatColumnVector &a, const FloatRowVector &b)
Definition: fMatrix.cc:2338
OCTAVE_API FloatMatrix linspace(const FloatColumnVector &x1, const FloatColumnVector &x2, octave_idx_type n)
Definition: fMatrix.cc:3001
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
blas_trans_type
Definition: mx-defs.h:109
@ blas_no_trans
Definition: mx-defs.h:110
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:123
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:90
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:115
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:98
#define MM_CMP_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:140
#define MM_BOOL_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:148
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
static T abs(T x)
Definition: pr-output.cc:1678