GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
CMatrix.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_CMatrix_h)
27#define octave_CMatrix_h 1
28
29#include "octave-config.h"
30
31#include "CNDArray.h"
32#include "DET.h"
33#include "MArray.h"
34#include "MDiagArray2.h"
35#include "MatrixType.h"
36#include "mx-defs.h"
37#include "mx-op-decl.h"
38#include "oct-cmplx.h"
39
41{
42public:
43
46
49
52
55
56 typedef double real_elt_type;
58
59 typedef void (*solve_singularity_handler) (double rcon);
60
61 ComplexMatrix () = default;
62
63 ComplexMatrix (const ComplexMatrix& a) = default;
64
66
67 ~ComplexMatrix () = default;
68
71
74
75 ComplexMatrix (const dim_vector& dv) : ComplexNDArray (dv.redim (2)) { }
76
77 ComplexMatrix (const dim_vector& dv, const Complex& val)
78 : ComplexNDArray (dv.redim (2), val) { }
79
80 template <typename U>
81 ComplexMatrix (const MArray<U>& a) : ComplexNDArray (a.as_matrix ()) { }
82
83 template <typename U>
84 ComplexMatrix (const Array<U>& a) : ComplexNDArray (a.as_matrix ()) { }
85
86 OCTAVE_API ComplexMatrix (const Matrix& re, const Matrix& im);
87
88 explicit OCTAVE_API ComplexMatrix (const Matrix& a);
89
90 explicit OCTAVE_API ComplexMatrix (const RowVector& rv);
91
92 explicit OCTAVE_API ComplexMatrix (const ColumnVector& cv);
93
94 explicit OCTAVE_API ComplexMatrix (const DiagMatrix& a);
95
97
99
100 explicit OCTAVE_API ComplexMatrix (const ComplexRowVector& rv);
101
102 explicit OCTAVE_API ComplexMatrix (const ComplexColumnVector& cv);
103
104 explicit OCTAVE_API ComplexMatrix (const ComplexDiagMatrix& a);
105
107
108 explicit OCTAVE_API ComplexMatrix (const DiagArray2<Complex>& a);
109
110 explicit OCTAVE_API ComplexMatrix (const boolMatrix& a);
111
112 explicit OCTAVE_API ComplexMatrix (const charMatrix& a);
113
114 OCTAVE_API bool operator == (const ComplexMatrix& a) const;
115 OCTAVE_API bool operator != (const ComplexMatrix& a) const;
116
117 OCTAVE_API bool ishermitian () const;
118
119 // destructive insert/delete/reorder operations
120
122 insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
124 insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
126 insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
128 insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
129
131 insert (const ComplexMatrix& a, octave_idx_type r, octave_idx_type c);
133 insert (const ComplexRowVector& a, octave_idx_type r, octave_idx_type c);
137 insert (const ComplexDiagMatrix& a, octave_idx_type r, octave_idx_type c);
138
139 OCTAVE_API ComplexMatrix& fill (double val);
140 OCTAVE_API ComplexMatrix& fill (const Complex& val);
142 fill (double val, octave_idx_type r1, octave_idx_type c1,
145 fill (const Complex& val, octave_idx_type r1, octave_idx_type c1,
147
148 OCTAVE_API ComplexMatrix append (const Matrix& a) const;
149 OCTAVE_API ComplexMatrix append (const RowVector& a) const;
150 OCTAVE_API ComplexMatrix append (const ColumnVector& a) const;
151 OCTAVE_API ComplexMatrix append (const DiagMatrix& a) const;
152
153 OCTAVE_API ComplexMatrix append (const ComplexMatrix& a) const;
154 OCTAVE_API ComplexMatrix append (const ComplexRowVector& a) const;
155 OCTAVE_API ComplexMatrix append (const ComplexColumnVector& a) const;
156 OCTAVE_API ComplexMatrix append (const ComplexDiagMatrix& a) const;
157
158 OCTAVE_API ComplexMatrix stack (const Matrix& a) const;
159 OCTAVE_API ComplexMatrix stack (const RowVector& a) const;
160 OCTAVE_API ComplexMatrix stack (const ColumnVector& a) const;
161 OCTAVE_API ComplexMatrix stack (const DiagMatrix& a) const;
162
163 OCTAVE_API ComplexMatrix stack (const ComplexMatrix& a) const;
164 OCTAVE_API ComplexMatrix stack (const ComplexRowVector& a) const;
165 OCTAVE_API ComplexMatrix stack (const ComplexColumnVector& a) const;
166 OCTAVE_API ComplexMatrix stack (const ComplexDiagMatrix& a) const;
167
169 { return MArray<Complex>::hermitian (std::conj); }
172
173 friend OCTAVE_API ComplexMatrix conj (const ComplexMatrix& a);
174
175 // resize is the destructive equivalent for this one
176
178 extract (octave_idx_type r1, octave_idx_type c1,
179 octave_idx_type r2, octave_idx_type c2) const;
180
182 extract_n (octave_idx_type r1, octave_idx_type c1,
183 octave_idx_type nr, octave_idx_type nc) const;
184
185 // extract row or column i.
186
188
190
192 const Complex& rfv = Complex (0))
193 {
194 MArray<Complex>::resize (dim_vector (nr, nc), rfv);
195 }
196
197private:
198 ComplexMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
199 double& rcon, bool force, bool calc_cond) const;
200
201 ComplexMatrix finverse (MatrixType& mattype, octave_idx_type& info,
202 double& rcon, bool force, bool calc_cond) const;
203
204public:
205 OCTAVE_API ComplexMatrix inverse () const;
206 OCTAVE_API ComplexMatrix inverse (octave_idx_type& info) const;
208 inverse (octave_idx_type& info, double& rcon,
209 bool force = false, bool calc_cond = true) const;
210
211 OCTAVE_API ComplexMatrix inverse (MatrixType& mattype) const;
213 inverse (MatrixType& mattype, octave_idx_type& info) const;
215 inverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
216 bool force = false, bool calc_cond = true) const;
217
218 OCTAVE_API ComplexMatrix pseudo_inverse (double tol = 0.0) const;
219
220 OCTAVE_API ComplexMatrix fourier () const;
221 OCTAVE_API ComplexMatrix ifourier () const;
222
223 OCTAVE_API ComplexMatrix fourier2d () const;
224 OCTAVE_API ComplexMatrix ifourier2d () const;
225
226 OCTAVE_API ComplexDET determinant () const;
227 OCTAVE_API ComplexDET determinant (octave_idx_type& info) const;
229 determinant (octave_idx_type& info, double& rcon,
230 bool calc_cond = true) const;
232 determinant (MatrixType& mattype, octave_idx_type& info, double& rcon,
233 bool calc_cond = true) const;
234
235 OCTAVE_API double rcond () const;
236 OCTAVE_API double rcond (MatrixType& mattype) const;
237
238private:
239 // Upper triangular matrix solvers
240 ComplexMatrix utsolve (MatrixType& mattype, const ComplexMatrix& b,
241 octave_idx_type& info, double& rcon,
242 solve_singularity_handler sing_handler,
243 bool calc_cond = false,
244 blas_trans_type transt = blas_no_trans) const;
245
246 // Lower triangular matrix solvers
247 ComplexMatrix ltsolve (MatrixType& mattype, const ComplexMatrix& b,
248 octave_idx_type& info, double& rcon,
249 solve_singularity_handler sing_handler,
250 bool calc_cond = false,
251 blas_trans_type transt = blas_no_trans) const;
252
253 // Full matrix solvers (umfpack/cholesky)
254 ComplexMatrix fsolve (MatrixType& mattype, const ComplexMatrix& b,
255 octave_idx_type& info, double& rcon,
256 solve_singularity_handler sing_handler,
257 bool calc_cond = false) const;
258
259public:
260 // Generic interface to solver with no probing of type
261 OCTAVE_API ComplexMatrix solve (MatrixType& mattype, const Matrix& b) const;
263 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info) const;
265 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
266 double& rcon) const;
268 solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
269 double& rcon, solve_singularity_handler sing_handler,
270 bool singular_fallback = true,
271 blas_trans_type transt = blas_no_trans) const;
272
274 solve (MatrixType& mattype, const ComplexMatrix& b) const;
276 solve (MatrixType& mattype, const ComplexMatrix& b,
277 octave_idx_type& info) const;
279 solve (MatrixType& mattype, const ComplexMatrix& b, octave_idx_type& info,
280 double& rcon) const;
282 solve (MatrixType& mattype, const ComplexMatrix& b, octave_idx_type& info,
283 double& rcon, solve_singularity_handler sing_handler,
284 bool singular_fallback = true,
285 blas_trans_type transt = blas_no_trans) const;
286
288 solve (MatrixType& mattype, const ColumnVector& b) const;
290 solve (MatrixType& mattype, const ColumnVector& b,
291 octave_idx_type& info) const;
293 solve (MatrixType& mattype, const ColumnVector& b,
294 octave_idx_type& info, double& rcon) const;
296 solve (MatrixType& mattype, const ColumnVector& b, octave_idx_type& info,
297 double& rcon, solve_singularity_handler sing_handler,
298 blas_trans_type transt = blas_no_trans) const;
299
301 solve (MatrixType& mattype, const ComplexColumnVector& b) const;
303 solve (MatrixType& mattype, const ComplexColumnVector& b,
304 octave_idx_type& info) const;
306 solve (MatrixType& mattype, const ComplexColumnVector& b,
307 octave_idx_type& info, double& rcon) const;
309 solve (MatrixType& mattype, const ComplexColumnVector& b,
310 octave_idx_type& info, double& rcon,
311 solve_singularity_handler sing_handler,
312 blas_trans_type transt = blas_no_trans) const;
313
314 // Generic interface to solver with probing of type
315 OCTAVE_API ComplexMatrix solve (const Matrix& b) const;
317 solve (const Matrix& b, octave_idx_type& info) const;
319 solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
321 solve (const Matrix& b, octave_idx_type& info, double& rcon,
322 solve_singularity_handler sing_handler,
323 blas_trans_type transt = blas_no_trans) const;
324
325 OCTAVE_API ComplexMatrix solve (const ComplexMatrix& b) const;
327 solve (const ComplexMatrix& b, octave_idx_type& info) const;
329 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const;
331 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon,
332 solve_singularity_handler sing_handler,
333 blas_trans_type transt = blas_no_trans) const;
334
335 OCTAVE_API ComplexColumnVector solve (const ColumnVector& b) const;
337 solve (const ColumnVector& b, octave_idx_type& info) const;
339 solve (const ColumnVector& b, octave_idx_type& info, double& rcon) const;
341 solve (const ColumnVector& b, octave_idx_type& info, double& rcon,
342 solve_singularity_handler sing_handler,
343 blas_trans_type transt = blas_no_trans) const;
344
347 solve (const ComplexColumnVector& b, octave_idx_type& info) const;
349 solve (const ComplexColumnVector& b, octave_idx_type& info,
350 double& rcon) const;
352 solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcon,
353 solve_singularity_handler sing_handler,
354 blas_trans_type transt = blas_no_trans) const;
355
356 OCTAVE_API ComplexMatrix lssolve (const Matrix& b) const;
358 lssolve (const Matrix& b, octave_idx_type& info) const;
360 lssolve (const Matrix& b, octave_idx_type& info,
361 octave_idx_type& rank) const;
363 lssolve (const Matrix& b, octave_idx_type& info,
364 octave_idx_type& rank, double& rcon) const;
365
366 OCTAVE_API ComplexMatrix lssolve (const ComplexMatrix& b) const;
368 lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
370 lssolve (const ComplexMatrix& b, octave_idx_type& info,
371 octave_idx_type& rank) const;
373 lssolve (const ComplexMatrix& b, octave_idx_type& info,
374 octave_idx_type& rank, double& rcon) const;
375
376 OCTAVE_API ComplexColumnVector lssolve (const ColumnVector& b) const;
378 lssolve (const ColumnVector& b, octave_idx_type& info) const;
380 lssolve (const ColumnVector& b, octave_idx_type& info,
381 octave_idx_type& rank) const;
383 lssolve (const ColumnVector& b, octave_idx_type& info,
384 octave_idx_type& rank, double& rcon) const;
385
386 OCTAVE_API ComplexColumnVector lssolve (const ComplexColumnVector& b) const;
388 lssolve (const ComplexColumnVector& b, octave_idx_type& info) const;
390 lssolve (const ComplexColumnVector& b, octave_idx_type& info,
391 octave_idx_type& rank) const;
393 lssolve (const ComplexColumnVector& b, octave_idx_type& info,
394 octave_idx_type& rank, double& rcon) const;
395
396 // matrix by diagonal matrix -> matrix operations
397
400
403
404 // matrix by matrix -> matrix operations
405
408
409 // other operations
410
411 OCTAVE_API boolMatrix all (int dim = -1) const;
412 OCTAVE_API boolMatrix any (int dim = -1) const;
413
414 OCTAVE_API ComplexMatrix cumprod (int dim = -1) const;
415 OCTAVE_API ComplexMatrix cumsum (int dim = -1) const;
416 OCTAVE_API ComplexMatrix prod (int dim = -1) const;
417 OCTAVE_API ComplexMatrix sum (int dim = -1) const;
418 OCTAVE_API ComplexMatrix sumsq (int dim = -1) const;
419 OCTAVE_API Matrix abs () const;
420
421 OCTAVE_API ComplexMatrix diag (octave_idx_type k = 0) const;
422
424 diag (octave_idx_type m, octave_idx_type n) const;
425
426 OCTAVE_API bool row_is_real_only (octave_idx_type) const;
427 OCTAVE_API bool column_is_real_only (octave_idx_type) const;
428
429 OCTAVE_API ComplexColumnVector row_min () const;
430 OCTAVE_API ComplexColumnVector row_max () const;
431
434
435 OCTAVE_API ComplexRowVector column_min () const;
436 OCTAVE_API ComplexRowVector column_max () const;
437
438 OCTAVE_API ComplexRowVector column_min (Array<octave_idx_type>& index) const;
439 OCTAVE_API ComplexRowVector column_max (Array<octave_idx_type>& index) const;
440
441 // i/o
442
443 friend OCTAVE_API std::ostream&
444 operator << (std::ostream& os, const ComplexMatrix& a);
445 friend OCTAVE_API std::istream&
446 operator >> (std::istream& is, ComplexMatrix& a);
447};
448
450
451// column vector by row vector -> matrix operations
452
454operator * (const ColumnVector& a, const ComplexRowVector& b);
455
457operator * (const ComplexColumnVector& a, const RowVector& b);
458
461
463Givens (const Complex&, const Complex&);
464
466Sylvester (const ComplexMatrix&, const ComplexMatrix&, const ComplexMatrix&);
467
469xgemm (const ComplexMatrix& a, const ComplexMatrix& b,
472
474 const ComplexMatrix&);
476 const Matrix&);
478 const ComplexMatrix&);
479
480extern OCTAVE_API ComplexMatrix min (const Complex& c, const ComplexMatrix& m);
481extern OCTAVE_API ComplexMatrix min (const ComplexMatrix& m, const Complex& c);
483 const ComplexMatrix& b);
484
485extern OCTAVE_API ComplexMatrix max (const Complex& c, const ComplexMatrix& m);
486extern OCTAVE_API ComplexMatrix max (const ComplexMatrix& m, const Complex& c);
488 const ComplexMatrix& b);
489
491 const ComplexColumnVector& x2,
493
496
499
502
504
505#endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexColumnVector conj(const ComplexColumnVector &a)
ComplexColumnVector operator+=(ComplexColumnVector &x, const Complex &y)
Definition CColVector.h:156
ComplexColumnVector operator-=(ComplexColumnVector &x, const Complex &y)
Definition CColVector.h:156
ComplexMatrix Sylvester(const ComplexMatrix &, const ComplexMatrix &, const ComplexMatrix &)
Definition CMatrix.cc:3249
ComplexMatrix max(const Complex &c, const ComplexMatrix &m)
Definition CMatrix.cc:3554
ComplexMatrix conj(const ComplexMatrix &a)
Definition CMatrix.cc:676
ComplexMatrix linspace(const ComplexColumnVector &x1, const ComplexColumnVector &x2, octave_idx_type n)
Definition CMatrix.cc:3630
ComplexMatrix xgemm(const ComplexMatrix &a, const ComplexMatrix &b, blas_trans_type transa=blas_no_trans, blas_trans_type transb=blas_no_trans)
Definition CMatrix.cc:3347
ComplexMatrix min(const Complex &c, const ComplexMatrix &m)
Definition CMatrix.cc:3482
ComplexMatrix Givens(const Complex &, const Complex &)
Definition CMatrix.cc:3227
ComplexMatrix operator*(const ColumnVector &a, const ComplexRowVector &b)
Definition CMatrix.cc:2624
#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)
ComplexDiagMatrix complex_diag_matrix_type
Definition CMatrix.h:54
ComplexColumnVector column_vector_type
Definition CMatrix.h:44
Matrix real_matrix_type
Definition CMatrix.h:50
Complex complex_elt_type
Definition CMatrix.h:57
ComplexMatrix(const dim_vector &dv)
Definition CMatrix.h:75
double real_elt_type
Definition CMatrix.h:56
RowVector real_row_vector_type
Definition CMatrix.h:48
ComplexRowVector row_vector_type
Definition CMatrix.h:45
ComplexMatrix(octave_idx_type r, octave_idx_type c)
Definition CMatrix.h:69
ComplexMatrix(octave_idx_type r, octave_idx_type c, const Complex &val)
Definition CMatrix.h:72
~ComplexMatrix()=default
ComplexMatrix(const ComplexMatrix &a)=default
DiagMatrix real_diag_matrix_type
Definition CMatrix.h:53
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
Definition CMatrix.h:191
ComplexMatrix complex_matrix_type
Definition CMatrix.h:51
ComplexMatrix transpose() const
Definition CMatrix.h:170
ColumnVector real_column_vector_type
Definition CMatrix.h:47
ComplexMatrix hermitian() const
Definition CMatrix.h:168
ComplexMatrix(const dim_vector &dv, const Complex &val)
Definition CMatrix.h:77
ComplexMatrix()=default
ComplexMatrix(const Array< U > &a)
Definition CMatrix.h:84
ComplexMatrix(const MArray< U > &a)
Definition CMatrix.h:81
ComplexNDArray & operator=(const ComplexNDArray &a)
Definition CNDArray.h:57
Template for N-dimensional array classes with like-type math operators.
Definition MArray.h:61
MArray< T > transpose() const
Definition MArray.h:97
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition MArray.h:100
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
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
#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)