GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
dMatrix.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_dMatrix_h)
27#define octave_dMatrix_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 "dNDArray.h"
36#include "mx-defs.h"
37#include "mx-op-decl.h"
38
40{
41public:
42
45
48
51
54
55 typedef double real_elt_type;
57
58 typedef void (*solve_singularity_handler) (double rcon);
59
60 Matrix () = default;
61
62 Matrix (const Matrix& a) = default;
63
64 Matrix& operator = (const Matrix& a) = default;
65
66 ~Matrix () = default;
67
70
72 : NDArray (dim_vector (r, c), val) { }
73
74 Matrix (const dim_vector& dv) : NDArray (dv.redim (2)) { }
75
76 Matrix (const dim_vector& dv, double val)
77 : NDArray (dv.redim (2), val) { }
78
79 template <typename U>
80 Matrix (const MArray<U>& a) : NDArray (a.as_matrix ()) { }
81
82 template <typename U>
83 Matrix (const Array<U>& a) : NDArray (a.as_matrix ()) { }
84
85 explicit OCTAVE_API Matrix (const RowVector& rv);
86
87 explicit OCTAVE_API Matrix (const ColumnVector& cv);
88
89 explicit OCTAVE_API Matrix (const DiagMatrix& a);
90
91 explicit OCTAVE_API Matrix (const MDiagArray2<double>& a);
92
93 explicit OCTAVE_API Matrix (const DiagArray2<double>& a);
94
95 explicit OCTAVE_API Matrix (const PermMatrix& a);
96
97 explicit OCTAVE_API Matrix (const boolMatrix& a);
98
99 explicit Matrix (const charMatrix& a);
100
101 OCTAVE_API bool operator == (const Matrix& a) const;
102 OCTAVE_API bool operator != (const Matrix& a) const;
103
104 OCTAVE_API bool issymmetric () const;
105
106 // destructive insert/delete/reorder operations
107
109 insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
111 insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
113 insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
115 insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
116
117 OCTAVE_API Matrix& fill (double val);
119 fill (double val, octave_idx_type r1, octave_idx_type c1,
121
122 OCTAVE_API Matrix append (const Matrix& a) const;
123 OCTAVE_API Matrix append (const RowVector& a) const;
124 OCTAVE_API Matrix append (const ColumnVector& a) const;
125 OCTAVE_API Matrix append (const DiagMatrix& a) const;
126
127 OCTAVE_API Matrix stack (const Matrix& a) const;
128 OCTAVE_API Matrix stack (const RowVector& a) const;
129 OCTAVE_API Matrix stack (const ColumnVector& a) const;
130 OCTAVE_API Matrix stack (const DiagMatrix& a) const;
131
132 friend OCTAVE_API Matrix real (const ComplexMatrix& a);
133 friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
134
135 friend class ComplexMatrix;
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
154 OCTAVE_API ColumnVector column (octave_idx_type i) const;
155
156 void resize (octave_idx_type nr, octave_idx_type nc, double rfv = 0)
157 {
158 MArray<double>::resize (dim_vector (nr, nc), rfv);
159 }
160
161private:
162 Matrix tinverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
163 bool force, bool calc_cond) const;
164
165 Matrix finverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
166 bool force, bool calc_cond) const;
167
168public:
169 OCTAVE_API Matrix inverse () const;
170 OCTAVE_API Matrix inverse (octave_idx_type& info) const;
172 inverse (octave_idx_type& info, double& rcon,
173 bool force = false, bool calc_cond = true) const;
174
175 OCTAVE_API Matrix inverse (MatrixType& mattype) const;
176 OCTAVE_API Matrix inverse (MatrixType& mattype, octave_idx_type& info) const;
178 inverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
179 bool force = false, bool calc_cond = true) const;
180
181 OCTAVE_API Matrix pseudo_inverse (double tol = 0.0) const;
182
183 OCTAVE_API ComplexMatrix fourier () const;
184 OCTAVE_API ComplexMatrix ifourier () const;
185
186 OCTAVE_API ComplexMatrix fourier2d () const;
187 OCTAVE_API ComplexMatrix ifourier2d () const;
188
189 OCTAVE_API DET determinant () const;
190 OCTAVE_API DET determinant (octave_idx_type& info) const;
192 determinant (octave_idx_type& info, double& rcon,
193 bool calc_cond = true) const;
195 determinant (MatrixType& mattype, octave_idx_type& info,
196 double& rcon, bool calc_cond = true) const;
197
198 OCTAVE_API double rcond () const;
199 OCTAVE_API double rcond (MatrixType& mattype) const;
200
201private:
202 // Upper triangular matrix solvers
203 Matrix utsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
204 double& rcon, solve_singularity_handler sing_handler,
205 bool calc_cond = false,
206 blas_trans_type transt = blas_no_trans) const;
207
208 // Lower triangular matrix solvers
209 Matrix ltsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
210 double& rcon, solve_singularity_handler sing_handler,
211 bool calc_cond = false,
212 blas_trans_type transt = blas_no_trans) const;
213
214 // Full matrix solvers (lu/cholesky)
215 Matrix fsolve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
216 double& rcon, solve_singularity_handler sing_handler,
217 bool calc_cond = false) const;
218
219public:
220 // Generic interface to solver with no probing of type
221 OCTAVE_API Matrix solve (MatrixType& mattype, const Matrix& b) const;
223 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info) const;
225 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
226 double& rcon) const;
228 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
229 double& rcon, solve_singularity_handler sing_handler,
230 bool singular_fallback = true,
231 blas_trans_type transt = blas_no_trans) const;
232
234 solve (MatrixType& mattype, const ComplexMatrix& b) const;
236 solve (MatrixType& mattype, const ComplexMatrix& b,
237 octave_idx_type& info) const;
239 solve (MatrixType& mattype, const ComplexMatrix& b,
240 octave_idx_type& info, double& rcon) const;
242 solve (MatrixType& mattype, const ComplexMatrix& b,
243 octave_idx_type& info, double& rcon,
244 solve_singularity_handler sing_handler,
245 bool singular_fallback = true,
246 blas_trans_type transt = blas_no_trans) const;
247
249 solve (MatrixType& mattype, const ColumnVector& b) const;
251 solve (MatrixType& mattype, const ColumnVector& b,
252 octave_idx_type& info) const;
254 solve (MatrixType& mattype, const ColumnVector& b,
255 octave_idx_type& info, double& rcon) const;
257 solve (MatrixType& mattype, const ColumnVector& b, octave_idx_type& info,
258 double& rcon, solve_singularity_handler sing_handler,
259 blas_trans_type transt = blas_no_trans) const;
260
262 solve (MatrixType& mattype, const ComplexColumnVector& b) const;
264 solve (MatrixType& mattype, const ComplexColumnVector& b,
265 octave_idx_type& info) const;
267 solve (MatrixType& mattype, const ComplexColumnVector& b,
268 octave_idx_type& info, double& rcon) const;
270 solve (MatrixType& mattype, const ComplexColumnVector& b,
271 octave_idx_type& info, double& rcon,
272 solve_singularity_handler sing_handler,
273 blas_trans_type transt = blas_no_trans) const;
274
275 // Generic interface to solver with probing of type
276 OCTAVE_API Matrix solve (const Matrix& b) const;
277 OCTAVE_API Matrix solve (const Matrix& b, octave_idx_type& info) const;
279 solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
281 solve (const Matrix& b, octave_idx_type& info, double& rcon,
282 solve_singularity_handler sing_handler,
283 blas_trans_type transt = blas_no_trans) const;
284
285 OCTAVE_API ComplexMatrix solve (const ComplexMatrix& b) const;
287 solve (const ComplexMatrix& b, octave_idx_type& info) const;
289 solve (const ComplexMatrix& b, octave_idx_type& info,
290 double& rcon) const;
292 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon,
293 solve_singularity_handler sing_handler,
294 blas_trans_type transt = blas_no_trans) const;
295
296 OCTAVE_API ColumnVector solve (const ColumnVector& b) const;
298 solve (const ColumnVector& b, octave_idx_type& info) const;
300 solve (const ColumnVector& b, octave_idx_type& info, double& rcon) const;
302 solve (const ColumnVector& b, octave_idx_type& info, double& rcon,
303 solve_singularity_handler sing_handler,
304 blas_trans_type transt = blas_no_trans) const;
305
308 solve (const ComplexColumnVector& b, octave_idx_type& info) const;
310 solve (const ComplexColumnVector& b, octave_idx_type& info,
311 double& rcon) const;
313 solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcon,
314 solve_singularity_handler sing_handler,
315 blas_trans_type transt = blas_no_trans) const;
316
317 // Singular solvers
318 OCTAVE_API Matrix lssolve (const Matrix& b) const;
319 OCTAVE_API Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
321 lssolve (const Matrix& b, octave_idx_type& info,
322 octave_idx_type& rank) const;
324 lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank,
325 double& rcon) const;
326
327 OCTAVE_API ComplexMatrix lssolve (const ComplexMatrix& b) const;
329 lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
331 lssolve (const ComplexMatrix& b, octave_idx_type& info,
332 octave_idx_type& rank) const;
334 lssolve (const ComplexMatrix& b, octave_idx_type& info,
335 octave_idx_type& rank, double& rcon) const;
336
337 OCTAVE_API ColumnVector lssolve (const ColumnVector& b) const;
339 lssolve (const ColumnVector& b, octave_idx_type& info) const;
341 lssolve (const ColumnVector& b, octave_idx_type& info,
342 octave_idx_type& rank) const;
344 lssolve (const ColumnVector& b, octave_idx_type& info,
345 octave_idx_type& rank, double& rcon) const;
346
347 OCTAVE_API ComplexColumnVector lssolve (const ComplexColumnVector& b) const;
349 lssolve (const ComplexColumnVector& b, octave_idx_type& info) const;
351 lssolve (const ComplexColumnVector& b, octave_idx_type& info,
352 octave_idx_type& rank) const;
354 lssolve (const ComplexColumnVector& b, octave_idx_type& info,
355 octave_idx_type& rank, double& rcon) const;
356
359
360 // unary operations
361
362 // other operations
363
364 OCTAVE_API boolMatrix all (int dim = -1) const;
365 OCTAVE_API boolMatrix any (int dim = -1) const;
366
367 OCTAVE_API Matrix cumprod (int dim = -1) const;
368 OCTAVE_API Matrix cumsum (int dim = -1) const;
369 OCTAVE_API Matrix prod (int dim = -1) const;
370 OCTAVE_API Matrix sum (int dim = -1) const;
371 OCTAVE_API Matrix sumsq (int dim = -1) const;
372 OCTAVE_API Matrix abs () const;
373
374 OCTAVE_API Matrix diag (octave_idx_type k = 0) const;
375
377
378 OCTAVE_API ColumnVector row_min () const;
379 OCTAVE_API ColumnVector row_max () const;
380
381 OCTAVE_API ColumnVector row_min (Array<octave_idx_type>& index) const;
382 OCTAVE_API ColumnVector row_max (Array<octave_idx_type>& index) const;
383
384 OCTAVE_API RowVector column_min () const;
385 OCTAVE_API RowVector column_max () const;
386
387 OCTAVE_API RowVector column_min (Array<octave_idx_type>& index) const;
388 OCTAVE_API RowVector column_max (Array<octave_idx_type>& index) const;
389
390 // i/o
391
392 friend OCTAVE_API std::ostream&
393 operator << (std::ostream& os, const Matrix& a);
394 friend OCTAVE_API std::istream&
395 operator >> (std::istream& is, Matrix& a);
396};
397
398// Publish externally used friend functions.
399
400extern OCTAVE_API Matrix real (const ComplexMatrix& a);
401extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
402
403// column vector by row vector -> matrix operations
404
406 const RowVector& b);
407
408// Other functions.
409
410extern OCTAVE_API Matrix Givens (double, double);
411
412extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&,
413 const Matrix&);
414
415extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
418
419extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
420
421extern OCTAVE_API Matrix min (double d, const Matrix& m);
422extern OCTAVE_API Matrix min (const Matrix& m, double d);
423extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
424
425extern OCTAVE_API Matrix max (double d, const Matrix& m);
426extern OCTAVE_API Matrix max (const Matrix& m, double d);
427extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
428
429extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
430 const ColumnVector& x2,
432
435
438
441
443
444template <typename T>
445void read_int (std::istream& is, bool swap_bytes, T& val);
446
447#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)
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
Matrix(const Array< U > &a)
Definition dMatrix.h:83
Matrix()=default
DiagMatrix real_diag_matrix_type
Definition dMatrix.h:52
RowVector row_vector_type
Definition dMatrix.h:44
ComplexDiagMatrix complex_diag_matrix_type
Definition dMatrix.h:53
Matrix real_matrix_type
Definition dMatrix.h:49
ComplexMatrix complex_matrix_type
Definition dMatrix.h:50
RowVector real_row_vector_type
Definition dMatrix.h:47
Matrix transpose() const
Definition dMatrix.h:138
Matrix hermitian() const
Definition dMatrix.h:137
Complex complex_elt_type
Definition dMatrix.h:56
Matrix(octave_idx_type r, octave_idx_type c)
Definition dMatrix.h:68
Matrix(const Matrix &a)=default
Matrix(octave_idx_type r, octave_idx_type c, double val)
Definition dMatrix.h:71
Matrix(const dim_vector &dv)
Definition dMatrix.h:74
ColumnVector real_column_vector_type
Definition dMatrix.h:46
double real_elt_type
Definition dMatrix.h:55
ColumnVector column_vector_type
Definition dMatrix.h:43
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition dMatrix.h:156
~Matrix()=default
Matrix(const MArray< U > &a)
Definition dMatrix.h:80
Matrix(const dim_vector &dv, double val)
Definition dMatrix.h:76
NDArray & operator=(const NDArray &a)
Definition dNDArray.h:64
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)
Matrix imag(const ComplexMatrix &a)
Definition dMatrix.cc:392
Matrix operator*(const ColumnVector &a, const RowVector &b)
Definition dMatrix.cc:2330
void read_int(std::istream &is, bool swap_bytes, T &val)
Matrix Givens(double, double)
Definition dMatrix.cc:2687
Matrix max(double d, const Matrix &m)
Definition dMatrix.cc:2948
Matrix xgemm(const Matrix &a, const Matrix &b, blas_trans_type transa=blas_no_trans, blas_trans_type transb=blas_no_trans)
Definition dMatrix.cc:2786
Matrix linspace(const ColumnVector &x1, const ColumnVector &x2, octave_idx_type n)
Definition dMatrix.cc:3012
Matrix Sylvester(const Matrix &, const Matrix &, const Matrix &)
Definition dMatrix.cc:2704
Matrix min(double d, const Matrix &m)
Definition dMatrix.cc:2884
Matrix real(const ComplexMatrix &a)
Definition dMatrix.cc:386
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
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< double > Complex
Definition oct-cmplx.h:33
template int8_t abs(int8_t)