GNU Octave 7.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-2022 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
39class
42{
43public:
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 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 (void) 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
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
163private:
165 float& rcon, bool force, bool calc_cond) const;
166
168 float& rcon, bool force, bool calc_cond) const;
169
170public:
171 OCTAVE_API FloatMatrix inverse (void) 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
192 OCTAVE_API FloatDET determinant (void) const;
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 (void) const;
202 OCTAVE_API float rcond (MatrixType& mattype) const;
203
204private:
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
225public:
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;
299 float& rcon) const;
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;
343 octave_idx_type& rank) const;
346 octave_idx_type& rank, float& rcon) const;
347
350 lssolve (const FloatColumnVector& b, octave_idx_type& info) const;
353 octave_idx_type& rank) const;
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 (void) 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
415extern OCTAVE_API FloatMatrix Givens (float, float);
416
418 const FloatMatrix&);
419
420extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
423
425 const FloatMatrix& b);
426
427extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
428extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
429extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
430
431extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
432extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
433extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
434
436 const FloatColumnVector& x2,
438
441
444
447
449
450template <typename T>
451void read_int (std::istream& is, bool swap_bytes, T& val);
452
453#endif
template OCTAVE_API 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
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:129
OCTARRAY_API Array< FloatComplex, Alloc > index(const octave::idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:697
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1010
OCTAVE_API FloatComplexMatrix sumsq(int dim=-1) const
Definition: fCMatrix.cc:2842
OCTAVE_API FloatComplexMatrix pseudo_inverse(float tol=0.0) const
Definition: fCMatrix.cc:998
OCTAVE_API FloatComplexMatrix fourier2d(void) const
Definition: fCMatrix.cc:1101
OCTAVE_API FloatComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: fCMatrix.cc:686
OCTAVE_API FloatComplexMatrix lssolve(const FloatMatrix &b) const
Definition: fCMatrix.cc:2240
OCTAVE_API FloatComplexRowVector row(octave_idx_type i) const
Definition: fCMatrix.cc:705
OCTAVE_API FloatComplexMatrix fourier(void) const
Definition: fCMatrix.cc:1043
OCTAVE_API FloatComplexColumnVector column(octave_idx_type i) const
Definition: fCMatrix.cc:711
OCTAVE_API FloatComplexMatrix ifourier2d(void) const
Definition: fCMatrix.cc:1115
OCTAVE_API FloatComplexDET determinant(void) const
Definition: fCMatrix.cc:1169
OCTAVE_API FloatComplexMatrix cumsum(int dim=-1) const
Definition: fCMatrix.cc:2824
OCTAVE_API FloatComplexMatrix diag(octave_idx_type k=0) const
Definition: fCMatrix.cc:2853
OCTAVE_API FloatComplexMatrix sum(int dim=-1) const
Definition: fCMatrix.cc:2836
OCTAVE_API FloatComplexRowVector column_min(void) const
Definition: fCMatrix.cc:3063
void(* solve_singularity_handler)(float rcon)
Definition: fCMatrix.h:61
OCTAVE_API FloatComplexMatrix ifourier(void) const
Definition: fCMatrix.cc:1072
OCTAVE_API FloatComplexColumnVector row_max(void) const
Definition: fCMatrix.cc:2988
OCTAVE_API FloatComplexMatrix solve(MatrixType &mattype, const FloatMatrix &b) const
Definition: fCMatrix.cc:1937
OCTAVE_API FloatComplexMatrix inverse(void) const
Definition: fCMatrix.cc:740
OCTAVE_API FloatComplexRowVector column_max(void) const
Definition: fCMatrix.cc:3138
OCTAVE_API FloatComplexColumnVector row_min(void) const
Definition: fCMatrix.cc:2913
FloatComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, float &rcon, bool force, bool calc_cond) const
Definition: fCMatrix.cc:780
OCTAVE_API FloatComplexMatrix cumprod(int dim=-1) const
Definition: fCMatrix.cc:2818
OCTAVE_API float rcond(void) const
Definition: fCMatrix.cc:1348
OCTAVE_API FloatComplexMatrix prod(int dim=-1) const
Definition: fCMatrix.cc:2830
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:1727
OCTAVE_API 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:140
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
FloatMatrix hermitian(void) const
Definition: fMatrix.h:139
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: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
static M utsolve(const SM &U, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:259
static M ltsolve(const SM &L, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:239
OCTAVE_API FloatMatrix Givens(float, float)
Definition: fMatrix.cc:2692
void read_int(std::istream &is, bool swap_bytes, T &val)
OCTAVE_API FloatMatrix min(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2885
OCTAVE_API FloatMatrix imag(const FloatComplexMatrix &a)
Definition: fMatrix.cc:397
OCTAVE_API FloatMatrix max(float d, const FloatMatrix &m)
Definition: fMatrix.cc:2949
FloatMatrix operator-=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:448
FloatMatrix operator+=(FloatMatrix &x, const float &y)
Definition: fMatrix.h:448
OCTAVE_API FloatMatrix Sylvester(const FloatMatrix &, const FloatMatrix &, const FloatMatrix &)
Definition: fMatrix.cc:2709
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:2787
OCTAVE_API FloatMatrix real(const FloatComplexMatrix &a)
Definition: fMatrix.cc:391
OCTAVE_API FloatMatrix operator*(const FloatColumnVector &a, const FloatRowVector &b)
Definition: fMatrix.cc:2349
OCTAVE_API FloatMatrix linspace(const FloatColumnVector &x1, const FloatColumnVector &x2, octave_idx_type n)
Definition: fMatrix.cc:3012
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.in.cc:55
blas_trans_type
Definition: mx-defs.h:80
@ blas_no_trans
Definition: mx-defs.h:81
class OCTAVE_API FloatMatrix
Definition: mx-fwd.h:33
#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
STL namespace.
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
static T abs(T x)
Definition: pr-output.cc:1678