GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
fMatrix.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1994-2025 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
40{
41public:
42
45
48
51
54
55 typedef float real_elt_type;
57
58 typedef void (*solve_singularity_handler) (float rcon);
59
60 FloatMatrix () = default;
61
62 FloatMatrix (const FloatMatrix& a) = default;
63
64 FloatMatrix& operator = (const FloatMatrix& a) = default;
65
66 ~FloatMatrix () = default;
67
70
72 : FloatNDArray (dim_vector (r, c), val) { }
73
74 FloatMatrix (const dim_vector& dv) : FloatNDArray (dv.redim (2)) { }
75
76 FloatMatrix (const dim_vector& dv, float val)
77 : FloatNDArray (dv.redim (2), val) { }
78
79 template <typename U>
80 FloatMatrix (const MArray<U>& a) : FloatNDArray (a.as_matrix ()) { }
81
82 template <typename U>
83 FloatMatrix (const Array<U>& a) : FloatNDArray (a.as_matrix ()) { }
84
85 explicit OCTAVE_API FloatMatrix (const FloatRowVector& rv);
86
87 explicit OCTAVE_API FloatMatrix (const FloatColumnVector& cv);
88
89 explicit OCTAVE_API FloatMatrix (const FloatDiagMatrix& a);
90
91 explicit OCTAVE_API FloatMatrix (const MDiagArray2<float>& a);
92
93 explicit OCTAVE_API FloatMatrix (const DiagArray2<float>& a);
94
95 explicit OCTAVE_API FloatMatrix (const PermMatrix& a);
96
97 explicit OCTAVE_API FloatMatrix (const boolMatrix& a);
98
99 explicit OCTAVE_API FloatMatrix (const charMatrix& a);
100
101 OCTAVE_API bool operator == (const FloatMatrix& a) const;
102 OCTAVE_API bool operator != (const FloatMatrix& a) const;
103
104 OCTAVE_API bool issymmetric () const;
105
106 // destructive insert/delete/reorder operations
107
109 insert (const FloatMatrix& a, octave_idx_type r, octave_idx_type c);
111 insert (const FloatRowVector& a, octave_idx_type r, octave_idx_type c);
113 insert (const FloatColumnVector& a, octave_idx_type r, octave_idx_type c);
115 insert (const FloatDiagMatrix& a, octave_idx_type r, octave_idx_type c);
116
117 OCTAVE_API FloatMatrix& fill (float val);
119 fill (float val, octave_idx_type r1, octave_idx_type c1,
121
122 OCTAVE_API FloatMatrix append (const FloatMatrix& a) const;
123 OCTAVE_API FloatMatrix append (const FloatRowVector& a) const;
124 OCTAVE_API FloatMatrix append (const FloatColumnVector& a) const;
125 OCTAVE_API FloatMatrix append (const FloatDiagMatrix& a) const;
126
127 OCTAVE_API FloatMatrix stack (const FloatMatrix& a) const;
128 OCTAVE_API FloatMatrix stack (const FloatRowVector& a) const;
129 OCTAVE_API FloatMatrix stack (const FloatColumnVector& a) const;
130 OCTAVE_API FloatMatrix stack (const FloatDiagMatrix& a) const;
131
134
135 friend class FloatComplexMatrix;
136
139
140 // resize is the destructive equivalent for this one
141
143 extract (octave_idx_type r1, octave_idx_type c1,
144 octave_idx_type r2, octave_idx_type c2) const;
145
147 extract_n (octave_idx_type r1, octave_idx_type c1,
148 octave_idx_type nr, octave_idx_type nc) const;
149
150 // extract row or column i.
151
153
155
156 void resize (octave_idx_type nr, octave_idx_type nc, float rfv = 0)
157 {
158 MArray<float>::resize (dim_vector (nr, nc), rfv);
159 }
160
161private:
162 FloatMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
163 float& rcon, bool force, bool calc_cond) const;
164
165 FloatMatrix finverse (MatrixType& mattype, octave_idx_type& info,
166 float& rcon, bool force, bool calc_cond) const;
167
168public:
169 OCTAVE_API FloatMatrix inverse () const;
170 OCTAVE_API FloatMatrix inverse (octave_idx_type& info) const;
172 inverse (octave_idx_type& info, float& rcon,
173 bool force = false, bool calc_cond = true) const;
174
175 OCTAVE_API FloatMatrix inverse (MatrixType& mattype) const;
177 inverse (MatrixType& mattype, octave_idx_type& info) const;
179 inverse (MatrixType& mattype, octave_idx_type& info, float& rcon,
180 bool force = false, bool calc_cond = true) const;
181
182 OCTAVE_API FloatMatrix pseudo_inverse (float tol = 0.0) const;
183
184 OCTAVE_API FloatComplexMatrix fourier () const;
185 OCTAVE_API FloatComplexMatrix ifourier () const;
186
187 OCTAVE_API FloatComplexMatrix fourier2d () const;
188 OCTAVE_API FloatComplexMatrix ifourier2d () const;
189
190 OCTAVE_API FloatDET determinant () const;
191 OCTAVE_API FloatDET determinant (octave_idx_type& info) const;
193 determinant (octave_idx_type& info, float& rcon,
194 bool calc_cond = true) const;
196 determinant (MatrixType& mattype, octave_idx_type& info, float& rcon,
197 bool calc_cond = true) const;
198
199 OCTAVE_API float rcond () const;
200 OCTAVE_API float rcond (MatrixType& mattype) const;
201
202private:
203 // Upper triangular matrix solvers
204 FloatMatrix utsolve (MatrixType& mattype, const FloatMatrix& b,
205 octave_idx_type& info,
206 float& rcon, solve_singularity_handler sing_handler,
207 bool calc_cond = false,
208 blas_trans_type transt = blas_no_trans) const;
209
210 // Lower triangular matrix solvers
211 FloatMatrix ltsolve (MatrixType& mattype, const FloatMatrix& b,
212 octave_idx_type& info,
213 float& rcon, solve_singularity_handler sing_handler,
214 bool calc_cond = false,
215 blas_trans_type transt = blas_no_trans) const;
216
217 // Full matrix solvers (lu/cholesky)
218 FloatMatrix fsolve (MatrixType& mattype, const FloatMatrix& b,
219 octave_idx_type& info,
220 float& rcon, solve_singularity_handler sing_handler,
221 bool calc_cond = false) const;
222
223public:
224 // Generic interface to solver with no probing of type
226 solve (MatrixType& mattype, const FloatMatrix& b) const;
228 solve (MatrixType& mattype, const FloatMatrix& b,
229 octave_idx_type& info) const;
231 solve (MatrixType& mattype, const FloatMatrix& b, octave_idx_type& info,
232 float& rcon) const;
234 solve (MatrixType& mattype, const FloatMatrix& b, octave_idx_type& info,
235 float& rcon, solve_singularity_handler sing_handler,
236 bool singular_fallback = true,
237 blas_trans_type transt = blas_no_trans) const;
238
240 solve (MatrixType& mattype, const FloatComplexMatrix& b) const;
242 solve (MatrixType& mattype, const FloatComplexMatrix& b,
243 octave_idx_type& info) const;
245 solve (MatrixType& mattype, const FloatComplexMatrix& b,
246 octave_idx_type& info, float& rcon) const;
248 solve (MatrixType& mattype, const FloatComplexMatrix& b,
249 octave_idx_type& info, float& rcon,
250 solve_singularity_handler sing_handler, bool singular_fallback = true,
251 blas_trans_type transt = blas_no_trans) const;
252
254 solve (MatrixType& mattype, const FloatColumnVector& b) const;
256 solve (MatrixType& mattype, const FloatColumnVector& b,
257 octave_idx_type& info) const;
259 solve (MatrixType& mattype, const FloatColumnVector& b,
260 octave_idx_type& info, float& rcon) const;
262 solve (MatrixType& mattype, const FloatColumnVector& b,
263 octave_idx_type& info, float& rcon,
264 solve_singularity_handler sing_handler,
265 blas_trans_type transt = blas_no_trans) const;
266
268 solve (MatrixType& mattype, const FloatComplexColumnVector& b) const;
270 solve (MatrixType& mattype, const FloatComplexColumnVector& b,
271 octave_idx_type& info) const;
273 solve (MatrixType& mattype, const FloatComplexColumnVector& b,
274 octave_idx_type& info, float& rcon) const;
276 solve (MatrixType& mattype, const FloatComplexColumnVector& b,
277 octave_idx_type& info, float& rcon,
278 solve_singularity_handler sing_handler,
279 blas_trans_type transt = blas_no_trans) const;
280
281 // Generic interface to solver with probing of type
282 OCTAVE_API FloatMatrix solve (const FloatMatrix& b) const;
284 solve (const FloatMatrix& b, octave_idx_type& info) const;
286 solve (const FloatMatrix& b, octave_idx_type& info, float& rcon) const;
288 solve (const FloatMatrix& b, octave_idx_type& info, float& rcon,
289 solve_singularity_handler sing_handler,
290 blas_trans_type transt = blas_no_trans) const;
291
292 OCTAVE_API FloatComplexMatrix solve (const FloatComplexMatrix& b) const;
294 solve (const FloatComplexMatrix& b, octave_idx_type& info) const;
296 solve (const FloatComplexMatrix& b, octave_idx_type& info,
297 float& rcon) const;
299 solve (const FloatComplexMatrix& b, octave_idx_type& info,
300 float& rcon, solve_singularity_handler sing_handler,
301 blas_trans_type transt = blas_no_trans) const;
302
303 OCTAVE_API FloatColumnVector solve (const FloatColumnVector& b) const;
305 solve (const FloatColumnVector& b, octave_idx_type& info) const;
307 solve (const FloatColumnVector& b, octave_idx_type& info, float& rcon) const;
309 solve (const FloatColumnVector& b, octave_idx_type& info, float& rcon,
310 solve_singularity_handler sing_handler,
311 blas_trans_type transt = blas_no_trans) const;
312
314 solve (const FloatComplexColumnVector& b) const;
316 solve (const FloatComplexColumnVector& b, octave_idx_type& info) const;
318 solve (const FloatComplexColumnVector& b, octave_idx_type& info,
319 float& rcon) const;
321 solve (const FloatComplexColumnVector& b, octave_idx_type& info, float& rcon,
322 solve_singularity_handler sing_handler,
323 blas_trans_type transt = blas_no_trans) const;
324
325 // Singular solvers
326 OCTAVE_API FloatMatrix lssolve (const FloatMatrix& b) const;
328 lssolve (const FloatMatrix& b, octave_idx_type& info) const;
330 lssolve (const FloatMatrix& b, octave_idx_type& info,
331 octave_idx_type& rank) const;
333 lssolve (const FloatMatrix& b, octave_idx_type& info,
334 octave_idx_type& rank, float& rcon) const;
335
336 OCTAVE_API FloatComplexMatrix lssolve (const FloatComplexMatrix& b) const;
338 lssolve (const FloatComplexMatrix& b, octave_idx_type& info) const;
340 lssolve (const FloatComplexMatrix& b, octave_idx_type& info,
341 octave_idx_type& rank) const;
343 lssolve (const FloatComplexMatrix& b, octave_idx_type& info,
344 octave_idx_type& rank, float& rcon) const;
345
346 OCTAVE_API FloatColumnVector lssolve (const FloatColumnVector& b) const;
348 lssolve (const FloatColumnVector& b, octave_idx_type& info) const;
350 lssolve (const FloatColumnVector& b, octave_idx_type& info,
351 octave_idx_type& rank) const;
353 lssolve (const FloatColumnVector& b, octave_idx_type& info,
354 octave_idx_type& rank, float& rcon) const;
355
357 lssolve (const FloatComplexColumnVector& b) const;
359 lssolve (const FloatComplexColumnVector& b, octave_idx_type& info) const;
361 lssolve (const FloatComplexColumnVector& b, octave_idx_type& info,
362 octave_idx_type& rank) const;
364 lssolve (const FloatComplexColumnVector& b, octave_idx_type& info,
365 octave_idx_type& rank, float& rcon) const;
366
369
370 OCTAVE_API FloatMatrix cumprod (int dim = -1) const;
371 OCTAVE_API FloatMatrix cumsum (int dim = -1) const;
372 OCTAVE_API FloatMatrix prod (int dim = -1) const;
373 OCTAVE_API FloatMatrix sum (int dim = -1) const;
374 OCTAVE_API FloatMatrix sumsq (int dim = -1) const;
375 OCTAVE_API FloatMatrix abs () const;
376
377 OCTAVE_API FloatMatrix diag (octave_idx_type k = 0) const;
378
380
381 OCTAVE_API FloatColumnVector row_min () const;
382 OCTAVE_API FloatColumnVector row_max () const;
383
386
387 OCTAVE_API FloatRowVector column_min () const;
388 OCTAVE_API FloatRowVector column_max () const;
389
390 OCTAVE_API FloatRowVector column_min (Array<octave_idx_type>& index) const;
391 OCTAVE_API FloatRowVector column_max (Array<octave_idx_type>& index) const;
392
393 // i/o
394
395 friend OCTAVE_API std::ostream&
396 operator << (std::ostream& os, const FloatMatrix& a);
397 friend OCTAVE_API std::istream&
398 operator >> (std::istream& is, FloatMatrix& a);
399};
400
401// Publish externally used friend functions.
402
405
406// column vector by row vector -> matrix operations
407
409 const FloatRowVector& b);
410
411// Other functions.
412
413extern OCTAVE_API FloatMatrix Givens (float, float);
414
416 const FloatMatrix&);
417
418extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
421
423 const FloatMatrix& b);
424
425extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
426extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
427extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
428
429extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
430extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
431extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
432
434 const FloatColumnVector& x2,
436
439
442
445
447
448template <typename T>
449void read_int (std::istream& is, bool swap_bytes, T& val);
450
451#endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexColumnVector operator+=(ComplexColumnVector &x, const Complex &y)
Definition CColVector.h:156
ComplexColumnVector operator-=(ComplexColumnVector &x, const Complex &y)
Definition CColVector.h:156
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition MArray.h:128
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
void resize(const dim_vector &dv, const T &rfv)
FloatMatrix(octave_idx_type r, octave_idx_type c, float val)
Definition fMatrix.h:71
FloatMatrix real_matrix_type
Definition fMatrix.h:49
FloatComplex complex_elt_type
Definition fMatrix.h:56
~FloatMatrix()=default
void resize(octave_idx_type nr, octave_idx_type nc, float rfv=0)
Definition fMatrix.h:156
FloatMatrix(const Array< U > &a)
Definition fMatrix.h:83
FloatDiagMatrix real_diag_matrix_type
Definition fMatrix.h:52
FloatComplexDiagMatrix complex_diag_matrix_type
Definition fMatrix.h:53
FloatMatrix hermitian() const
Definition fMatrix.h:137
FloatMatrix(const dim_vector &dv)
Definition fMatrix.h:74
FloatMatrix(octave_idx_type r, octave_idx_type c)
Definition fMatrix.h:68
FloatColumnVector column_vector_type
Definition fMatrix.h:43
FloatMatrix(const dim_vector &dv, float val)
Definition fMatrix.h:76
FloatMatrix(const MArray< U > &a)
Definition fMatrix.h:80
FloatMatrix(const FloatMatrix &a)=default
float real_elt_type
Definition fMatrix.h:55
FloatRowVector row_vector_type
Definition fMatrix.h:44
FloatMatrix()=default
FloatRowVector real_row_vector_type
Definition fMatrix.h:47
FloatComplexMatrix complex_matrix_type
Definition fMatrix.h:50
FloatColumnVector real_column_vector_type
Definition fMatrix.h:46
FloatMatrix transpose() const
Definition fMatrix.h:138
FloatNDArray & operator=(const FloatNDArray &a)
Definition fNDArray.h:61
Template for N-dimensional array classes with like-type math operators.
Definition MArray.h:61
MArray< T > transpose() const
Definition MArray.h:97
Template for two dimensional diagonal array with math operators.
Definition MDiagArray2.h:54
Definition DET.h:38
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
ColumnVector real(const ComplexColumnVector &a)
ColumnVector imag(const ComplexColumnVector &a)
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition dim-vector.h:532
bool operator==(const dim_vector &a, const dim_vector &b)
Definition dim-vector.h:516
void read_int(std::istream &is, bool swap_bytes, T &val)
FloatMatrix Givens(float, float)
Definition fMatrix.cc:2698
FloatMatrix linspace(const FloatColumnVector &x1, const FloatColumnVector &x2, octave_idx_type n)
Definition fMatrix.cc:3019
FloatMatrix min(float d, const FloatMatrix &m)
Definition fMatrix.cc:2891
FloatMatrix operator*(const FloatColumnVector &a, const FloatRowVector &b)
Definition fMatrix.cc:2355
FloatMatrix real(const FloatComplexMatrix &a)
Definition fMatrix.cc:392
FloatMatrix imag(const FloatComplexMatrix &a)
Definition fMatrix.cc:398
FloatMatrix max(float d, const FloatMatrix &m)
Definition fMatrix.cc:2955
FloatMatrix Sylvester(const FloatMatrix &, const FloatMatrix &, const FloatMatrix &)
Definition fMatrix.cc:2715
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:2793
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
#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
template int8_t abs(int8_t)