GNU Octave  9.1.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-2024 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
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 () = default;
63 
64  FloatMatrix (const FloatMatrix& a) = default;
65 
66  FloatMatrix& operator = (const FloatMatrix& a) = default;
67 
68  ~FloatMatrix () = 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 OCTAVE_API FloatMatrix (const FloatRowVector& rv);
88 
89  explicit OCTAVE_API FloatMatrix (const FloatColumnVector& cv);
90 
91  explicit OCTAVE_API FloatMatrix (const FloatDiagMatrix& a);
92 
93  explicit OCTAVE_API FloatMatrix (const MDiagArray2<float>& a);
94 
95  explicit OCTAVE_API FloatMatrix (const DiagArray2<float>& a);
96 
97  explicit OCTAVE_API FloatMatrix (const PermMatrix& a);
98 
99  explicit OCTAVE_API FloatMatrix (const boolMatrix& a);
100 
101  explicit OCTAVE_API FloatMatrix (const charMatrix& a);
102 
103  OCTAVE_API bool operator == (const FloatMatrix& a) const;
104  OCTAVE_API bool operator != (const FloatMatrix& a) const;
105 
106  OCTAVE_API bool issymmetric () const;
107 
108  // destructive insert/delete/reorder operations
109 
111  insert (const FloatMatrix& a, octave_idx_type r, octave_idx_type c);
113  insert (const FloatRowVector& a, octave_idx_type r, octave_idx_type c);
115  insert (const FloatColumnVector& a, octave_idx_type r, octave_idx_type c);
117  insert (const FloatDiagMatrix& a, octave_idx_type r, octave_idx_type c);
118 
119  OCTAVE_API FloatMatrix& fill (float val);
121  fill (float val, octave_idx_type r1, octave_idx_type c1,
123 
124  OCTAVE_API FloatMatrix append (const FloatMatrix& a) const;
125  OCTAVE_API FloatMatrix append (const FloatRowVector& a) const;
126  OCTAVE_API FloatMatrix append (const FloatColumnVector& a) const;
127  OCTAVE_API FloatMatrix append (const FloatDiagMatrix& a) const;
128 
129  OCTAVE_API FloatMatrix stack (const FloatMatrix& a) const;
130  OCTAVE_API FloatMatrix stack (const FloatRowVector& a) const;
131  OCTAVE_API FloatMatrix stack (const FloatColumnVector& a) const;
132  OCTAVE_API FloatMatrix stack (const FloatDiagMatrix& a) const;
133 
134  friend OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
135  friend OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
136 
137  friend class FloatComplexMatrix;
138 
141 
142  // resize is the destructive equivalent for this one
143 
146  octave_idx_type r2, octave_idx_type c2) const;
147 
150  octave_idx_type nr, octave_idx_type nc) const;
151 
152  // extract row or column i.
153 
155 
157 
158  void resize (octave_idx_type nr, octave_idx_type nc, float rfv = 0)
159  {
160  MArray<float>::resize (dim_vector (nr, nc), rfv);
161  }
162 
163 private:
164  FloatMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
165  float& rcon, bool force, bool calc_cond) const;
166 
167  FloatMatrix finverse (MatrixType& mattype, octave_idx_type& info,
168  float& rcon, bool force, bool calc_cond) const;
169 
170 public:
171  OCTAVE_API FloatMatrix inverse () const;
174  inverse (octave_idx_type& info, float& rcon, bool force = false,
175  bool calc_cond = true) const;
176 
177  OCTAVE_API FloatMatrix inverse (MatrixType& mattype) const;
179  inverse (MatrixType& mattype, octave_idx_type& info) const;
181  inverse (MatrixType& mattype, octave_idx_type& info, float& rcon,
182  bool force = false, bool calc_cond = true) const;
183 
184  OCTAVE_API FloatMatrix pseudo_inverse (float tol = 0.0) const;
185 
188 
191 
195  determinant (octave_idx_type& info, float& rcon,
196  bool calc_cond = true) const;
198  determinant (MatrixType& mattype, octave_idx_type& info, float& rcon,
199  bool calc_cond = true) const;
200 
201  OCTAVE_API float rcond () const;
202  OCTAVE_API float rcond (MatrixType& mattype) const;
203 
204 private:
205  // Upper triangular matrix solvers
206  FloatMatrix utsolve (MatrixType& mattype, const FloatMatrix& b,
207  octave_idx_type& info,
208  float& rcon, solve_singularity_handler sing_handler,
209  bool calc_cond = false,
210  blas_trans_type transt = blas_no_trans) const;
211 
212  // Lower triangular matrix solvers
213  FloatMatrix ltsolve (MatrixType& mattype, const FloatMatrix& b,
214  octave_idx_type& info,
215  float& rcon, solve_singularity_handler sing_handler,
216  bool calc_cond = false,
217  blas_trans_type transt = blas_no_trans) const;
218 
219  // Full matrix solvers (lu/cholesky)
220  FloatMatrix fsolve (MatrixType& mattype, const FloatMatrix& b,
221  octave_idx_type& info,
222  float& rcon, solve_singularity_handler sing_handler,
223  bool calc_cond = false) const;
224 
225 public:
226  // Generic interface to solver with no probing of type
228  solve (MatrixType& mattype, const FloatMatrix& b) const;
230  solve (MatrixType& mattype, const FloatMatrix& b,
231  octave_idx_type& info) const;
233  solve (MatrixType& mattype, const FloatMatrix& b, octave_idx_type& info,
234  float& rcon) const;
236  solve (MatrixType& mattype, const FloatMatrix& b, octave_idx_type& info,
237  float& rcon, solve_singularity_handler sing_handler,
238  bool singular_fallback = true,
239  blas_trans_type transt = blas_no_trans) const;
240 
242  solve (MatrixType& mattype, const FloatComplexMatrix& b) const;
244  solve (MatrixType& mattype, const FloatComplexMatrix& b,
245  octave_idx_type& info) const;
247  solve (MatrixType& mattype, const FloatComplexMatrix& b,
248  octave_idx_type& info, float& rcon) const;
250  solve (MatrixType& mattype, const FloatComplexMatrix& b,
251  octave_idx_type& info, float& rcon,
252  solve_singularity_handler sing_handler, bool singular_fallback = true,
253  blas_trans_type transt = blas_no_trans) const;
254 
256  solve (MatrixType& mattype, const FloatColumnVector& b) const;
258  solve (MatrixType& mattype, const FloatColumnVector& b,
259  octave_idx_type& info) const;
261  solve (MatrixType& mattype, const FloatColumnVector& b,
262  octave_idx_type& info, float& rcon) const;
264  solve (MatrixType& mattype, const FloatColumnVector& b,
265  octave_idx_type& info, float& rcon,
266  solve_singularity_handler sing_handler,
267  blas_trans_type transt = blas_no_trans) const;
268 
270  solve (MatrixType& mattype, const FloatComplexColumnVector& b) const;
272  solve (MatrixType& mattype, const FloatComplexColumnVector& b,
273  octave_idx_type& info) const;
275  solve (MatrixType& mattype, const FloatComplexColumnVector& b,
276  octave_idx_type& info, float& rcon) const;
278  solve (MatrixType& mattype, const FloatComplexColumnVector& b,
279  octave_idx_type& info, float& rcon,
280  solve_singularity_handler sing_handler,
281  blas_trans_type transt = blas_no_trans) const;
282 
283  // Generic interface to solver with probing of type
284  OCTAVE_API FloatMatrix solve (const FloatMatrix& b) const;
286  solve (const FloatMatrix& b, octave_idx_type& info) const;
288  solve (const FloatMatrix& b, octave_idx_type& info, float& rcon) const;
290  solve (const FloatMatrix& b, octave_idx_type& info, float& rcon,
291  solve_singularity_handler sing_handler,
292  blas_trans_type transt = blas_no_trans) const;
293 
296  solve (const FloatComplexMatrix& b, octave_idx_type& info) const;
298  solve (const FloatComplexMatrix& b, octave_idx_type& info,
299  float& rcon) const;
301  solve (const FloatComplexMatrix& b, octave_idx_type& info,
302  float& rcon, solve_singularity_handler sing_handler,
303  blas_trans_type transt = blas_no_trans) const;
304 
307  solve (const FloatColumnVector& b, octave_idx_type& info) const;
309  solve (const FloatColumnVector& b, octave_idx_type& info, float& rcon) const;
311  solve (const FloatColumnVector& b, octave_idx_type& info, float& rcon,
312  solve_singularity_handler sing_handler,
313  blas_trans_type transt = blas_no_trans) const;
314 
316  solve (const FloatComplexColumnVector& b) const;
318  solve (const FloatComplexColumnVector& b, octave_idx_type& info) const;
321  float& rcon) const;
323  solve (const FloatComplexColumnVector& b, octave_idx_type& info, float& rcon,
324  solve_singularity_handler sing_handler,
325  blas_trans_type transt = blas_no_trans) const;
326 
327  // Singular solvers
328  OCTAVE_API FloatMatrix lssolve (const FloatMatrix& b) const;
330  lssolve (const FloatMatrix& b, octave_idx_type& info) const;
332  lssolve (const FloatMatrix& b, octave_idx_type& info,
333  octave_idx_type& rank) const;
335  lssolve (const FloatMatrix& b, octave_idx_type& info,
336  octave_idx_type& rank, float& rcon) const;
337 
340  lssolve (const FloatComplexMatrix& b, octave_idx_type& info) const;
342  lssolve (const FloatComplexMatrix& b, octave_idx_type& info,
343  octave_idx_type& rank) const;
345  lssolve (const FloatComplexMatrix& b, octave_idx_type& info,
346  octave_idx_type& rank, float& rcon) const;
347 
350  lssolve (const FloatColumnVector& b, octave_idx_type& info) const;
352  lssolve (const FloatColumnVector& b, octave_idx_type& info,
353  octave_idx_type& rank) const;
355  lssolve (const FloatColumnVector& b, octave_idx_type& info,
356  octave_idx_type& rank, float& rcon) const;
357 
359  lssolve (const FloatComplexColumnVector& b) const;
361  lssolve (const FloatComplexColumnVector& b, octave_idx_type& info) const;
364  octave_idx_type& rank) const;
367  octave_idx_type& rank, float& rcon) const;
368 
371 
372  OCTAVE_API FloatMatrix cumprod (int dim = -1) const;
373  OCTAVE_API FloatMatrix cumsum (int dim = -1) const;
374  OCTAVE_API FloatMatrix prod (int dim = -1) const;
375  OCTAVE_API FloatMatrix sum (int dim = -1) const;
376  OCTAVE_API FloatMatrix sumsq (int dim = -1) const;
377  OCTAVE_API FloatMatrix abs () const;
378 
380 
382 
385 
388 
391 
394 
395  // i/o
396 
397  friend OCTAVE_API std::ostream&
398  operator << (std::ostream& os, const FloatMatrix& a);
399  friend OCTAVE_API std::istream&
400  operator >> (std::istream& is, FloatMatrix& a);
401 };
402 
403 // Publish externally used friend functions.
404 
407 
408 // column vector by row vector -> matrix operations
409 
411  const FloatRowVector& b);
412 
413 // Other functions.
414 
415 extern OCTAVE_API FloatMatrix Givens (float, float);
416 
418  const FloatMatrix&);
419 
420 extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
422  blas_trans_type transb = blas_no_trans);
423 
425  const FloatMatrix& b);
426 
427 extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
428 extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
429 extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
430 
431 extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
432 extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
433 extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
434 
436  const FloatColumnVector& x2,
438 
441 
444 
447 
449 
450 template <typename T>
451 void read_int (std::istream& is, bool swap_bytes, T& val);
452 
453 #endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray.h:130
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
Array< T, Alloc > index(const octave::idx_vector &i) const
Indexing without resizing.
Definition: Array-base.cc:710
void resize(const dim_vector &dv, const T &rfv)
Definition: Array-base.cc:1023
FloatComplexColumnVector row_min() const
Definition: fCMatrix.cc:2919
FloatComplexMatrix ifourier() const
Definition: fCMatrix.cc:1077
FloatComplexMatrix sumsq(int dim=-1) const
Definition: fCMatrix.cc:2847
FloatComplexMatrix pseudo_inverse(float tol=0.0) const
Definition: fCMatrix.cc:1003
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:2245
FloatComplexRowVector row(octave_idx_type i) const
Definition: fCMatrix.cc:705
float rcond() const
Definition: fCMatrix.cc:1353
FloatComplexColumnVector column(octave_idx_type i) const
Definition: fCMatrix.cc:711
FloatComplexMatrix fourier2d() const
Definition: fCMatrix.cc:1106
FloatMatrix abs() const
Definition: fCMatrix.cc:2853
FloatComplexMatrix cumsum(int dim=-1) const
Definition: fCMatrix.cc:2829
FloatComplexMatrix diag(octave_idx_type k=0) const
Definition: fCMatrix.cc:2859
FloatComplexMatrix ifourier2d() const
Definition: fCMatrix.cc:1120
FloatComplexMatrix fourier() const
Definition: fCMatrix.cc:1048
FloatComplexMatrix sum(int dim=-1) const
Definition: fCMatrix.cc:2841
void(* solve_singularity_handler)(float rcon)
Definition: fCMatrix.h:61
FloatComplexRowVector column_max() const
Definition: fCMatrix.cc:3144
FloatComplexMatrix solve(MatrixType &mattype, const FloatMatrix &b) const
Definition: fCMatrix.cc:1942
FloatComplexRowVector column_min() const
Definition: fCMatrix.cc:3069
FloatComplexDET determinant() const
Definition: fCMatrix.cc:1174
FloatComplexMatrix inverse() const
Definition: fCMatrix.cc:740
FloatComplexMatrix cumprod(int dim=-1) const
Definition: fCMatrix.cc:2823
FloatComplexColumnVector row_max() const
Definition: fCMatrix.cc:2994
FloatComplexMatrix prod(int dim=-1) const
Definition: fCMatrix.cc:2835
FloatComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: fCMatrix.cc:696
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()=default
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition: fMatrix.h:158
FloatMatrix(const Array< U > &a)
Definition: fMatrix.h:85
FloatDiagMatrix real_diag_matrix_type
Definition: fMatrix.h:54
FloatComplexDiagMatrix complex_diag_matrix_type
Definition: fMatrix.h:55
FloatMatrix hermitian() const
Definition: fMatrix.h:139
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
FloatMatrix()=default
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
FloatMatrix transpose() const
Definition: fMatrix.h:140
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:63
MArray< T > transpose() const
Definition: MArray.h:99
Definition: DET.h:39
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:536
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:520
void read_int(std::istream &is, bool swap_bytes, T &val)
FloatMatrix Givens(float, float)
Definition: fMatrix.cc:2697
FloatMatrix operator-=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:448
FloatMatrix linspace(const FloatColumnVector &x1, const FloatColumnVector &x2, octave_idx_type n)
Definition: fMatrix.cc:3018
FloatMatrix operator+=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:448
FloatMatrix min(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2890
FloatMatrix operator*(const FloatColumnVector &a, const FloatRowVector &b)
Definition: fMatrix.cc:2354
FloatMatrix real(const FloatComplexMatrix &a)
Definition: fMatrix.cc:391
FloatMatrix imag(const FloatComplexMatrix &a)
Definition: fMatrix.cc:397
FloatMatrix max(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2954
FloatMatrix Sylvester(const FloatMatrix &, const FloatMatrix &, const FloatMatrix &)
Definition: fMatrix.cc:2714
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:2792
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.cc:55
blas_trans_type
Definition: mx-defs.h:80
@ blas_no_trans
Definition: mx-defs.h:81
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
T * r
Definition: mx-inlines.cc:781
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:122
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:89
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:114
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:97
#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
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34