GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
CMatrix.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_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 
40 class
43 {
44 public:
45 
48 
51 
54 
57 
58  typedef double real_elt_type;
60 
61  typedef void (*solve_singularity_handler) (double rcon);
62 
63  ComplexMatrix () = default;
64 
65  ComplexMatrix (const ComplexMatrix& a) = default;
66 
67  ComplexMatrix& operator = (const ComplexMatrix& a) = default;
68 
69  ~ComplexMatrix () = default;
70 
72  : ComplexNDArray (dim_vector (r, c)) { }
73 
75  : ComplexNDArray (dim_vector (r, c), val) { }
76 
77  ComplexMatrix (const dim_vector& dv) : ComplexNDArray (dv.redim (2)) { }
78 
79  ComplexMatrix (const dim_vector& dv, const Complex& val)
80  : ComplexNDArray (dv.redim (2), val) { }
81 
82  template <typename U>
83  ComplexMatrix (const MArray<U>& a) : ComplexNDArray (a.as_matrix ()) { }
84 
85  template <typename U>
86  ComplexMatrix (const Array<U>& a) : ComplexNDArray (a.as_matrix ()) { }
87 
88  OCTAVE_API ComplexMatrix (const Matrix& re, const Matrix& im);
89 
90  explicit OCTAVE_API ComplexMatrix (const Matrix& a);
91 
92  explicit OCTAVE_API ComplexMatrix (const RowVector& rv);
93 
94  explicit OCTAVE_API ComplexMatrix (const ColumnVector& cv);
95 
96  explicit OCTAVE_API ComplexMatrix (const DiagMatrix& a);
97 
98  explicit OCTAVE_API ComplexMatrix (const MDiagArray2<double>& a);
99 
100  explicit OCTAVE_API ComplexMatrix (const DiagArray2<double>& a);
101 
102  explicit OCTAVE_API ComplexMatrix (const ComplexRowVector& rv);
103 
104  explicit OCTAVE_API ComplexMatrix (const ComplexColumnVector& cv);
105 
106  explicit OCTAVE_API ComplexMatrix (const ComplexDiagMatrix& a);
107 
108  explicit OCTAVE_API ComplexMatrix (const MDiagArray2<Complex>& a);
109 
110  explicit OCTAVE_API ComplexMatrix (const DiagArray2<Complex>& a);
111 
112  explicit OCTAVE_API ComplexMatrix (const boolMatrix& a);
113 
114  explicit OCTAVE_API ComplexMatrix (const charMatrix& a);
115 
116  OCTAVE_API bool operator == (const ComplexMatrix& a) const;
117  OCTAVE_API bool operator != (const ComplexMatrix& a) const;
118 
119  OCTAVE_API bool ishermitian () const;
120 
121  // destructive insert/delete/reorder operations
122 
124  insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
126  insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
128  insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
130  insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
131 
133  insert (const ComplexMatrix& a, octave_idx_type r, octave_idx_type c);
135  insert (const ComplexRowVector& a, octave_idx_type r, octave_idx_type c);
139  insert (const ComplexDiagMatrix& a, octave_idx_type r, octave_idx_type c);
140 
141  OCTAVE_API ComplexMatrix& fill (double val);
142  OCTAVE_API ComplexMatrix& fill (const Complex& val);
144  fill (double val, octave_idx_type r1, octave_idx_type c1,
147  fill (const Complex& val, octave_idx_type r1, octave_idx_type c1,
149 
150  OCTAVE_API ComplexMatrix append (const Matrix& a) const;
151  OCTAVE_API ComplexMatrix append (const RowVector& a) const;
152  OCTAVE_API ComplexMatrix append (const ColumnVector& a) const;
153  OCTAVE_API ComplexMatrix append (const DiagMatrix& a) const;
154 
155  OCTAVE_API ComplexMatrix append (const ComplexMatrix& a) const;
156  OCTAVE_API ComplexMatrix append (const ComplexRowVector& a) const;
157  OCTAVE_API ComplexMatrix append (const ComplexColumnVector& a) const;
158  OCTAVE_API ComplexMatrix append (const ComplexDiagMatrix& a) const;
159 
160  OCTAVE_API ComplexMatrix stack (const Matrix& a) const;
161  OCTAVE_API ComplexMatrix stack (const RowVector& a) const;
162  OCTAVE_API ComplexMatrix stack (const ColumnVector& a) const;
163  OCTAVE_API ComplexMatrix stack (const DiagMatrix& a) const;
164 
165  OCTAVE_API ComplexMatrix stack (const ComplexMatrix& a) const;
166  OCTAVE_API ComplexMatrix stack (const ComplexRowVector& a) const;
167  OCTAVE_API ComplexMatrix stack (const ComplexColumnVector& a) const;
168  OCTAVE_API ComplexMatrix stack (const ComplexDiagMatrix& a) const;
169 
173  { return MArray<Complex>::transpose (); }
174 
175  friend OCTAVE_API ComplexMatrix conj (const ComplexMatrix& a);
176 
177  // resize is the destructive equivalent for this one
178 
180  extract (octave_idx_type r1, octave_idx_type c1,
181  octave_idx_type r2, octave_idx_type c2) const;
182 
184  extract_n (octave_idx_type r1, octave_idx_type c1,
185  octave_idx_type nr, octave_idx_type nc) const;
186 
187  // extract row or column i.
188 
190 
192 
194  const Complex& rfv = Complex (0))
195  {
196  MArray<Complex>::resize (dim_vector (nr, nc), rfv);
197  }
198 
199 private:
200  ComplexMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
201  double& rcon, bool force, bool calc_cond) const;
202 
203  ComplexMatrix finverse (MatrixType& mattype, octave_idx_type& info,
204  double& rcon, bool force, bool calc_cond) const;
205 
206 public:
207  OCTAVE_API ComplexMatrix inverse () const;
208  OCTAVE_API ComplexMatrix inverse (octave_idx_type& info) const;
210  inverse (octave_idx_type& info, double& rcon,
211  bool force = false, bool calc_cond = true) const;
212 
213  OCTAVE_API ComplexMatrix inverse (MatrixType& mattype) const;
215  inverse (MatrixType& mattype, octave_idx_type& info) const;
217  inverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
218  bool force = false, bool calc_cond = true) const;
219 
220  OCTAVE_API ComplexMatrix pseudo_inverse (double tol = 0.0) const;
221 
222  OCTAVE_API ComplexMatrix fourier () const;
223  OCTAVE_API ComplexMatrix ifourier () const;
224 
225  OCTAVE_API ComplexMatrix fourier2d () const;
226  OCTAVE_API ComplexMatrix ifourier2d () const;
227 
228  OCTAVE_API ComplexDET determinant () const;
229  OCTAVE_API ComplexDET determinant (octave_idx_type& info) const;
231  determinant (octave_idx_type& info, double& rcon,
232  bool calc_cond = true) const;
234  determinant (MatrixType& mattype, octave_idx_type& info, double& rcon,
235  bool calc_cond = true) const;
236 
237  OCTAVE_API double rcond () const;
238  OCTAVE_API double rcond (MatrixType& mattype) const;
239 
240 private:
241  // Upper triangular matrix solvers
242  ComplexMatrix utsolve (MatrixType& mattype, const ComplexMatrix& b,
243  octave_idx_type& info, double& rcon,
244  solve_singularity_handler sing_handler,
245  bool calc_cond = false,
246  blas_trans_type transt = blas_no_trans) const;
247 
248  // Lower triangular matrix solvers
249  ComplexMatrix ltsolve (MatrixType& mattype, const ComplexMatrix& b,
250  octave_idx_type& info, double& rcon,
251  solve_singularity_handler sing_handler,
252  bool calc_cond = false,
253  blas_trans_type transt = blas_no_trans) const;
254 
255  // Full matrix solvers (umfpack/cholesky)
256  ComplexMatrix fsolve (MatrixType& mattype, const ComplexMatrix& b,
257  octave_idx_type& info, double& rcon,
258  solve_singularity_handler sing_handler,
259  bool calc_cond = false) const;
260 
261 public:
262  // Generic interface to solver with no probing of type
263  OCTAVE_API ComplexMatrix solve (MatrixType& mattype, const Matrix& b) const;
265  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info) const;
267  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
268  double& rcon) const;
270  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
271  double& rcon, solve_singularity_handler sing_handler,
272  bool singular_fallback = true,
273  blas_trans_type transt = blas_no_trans) const;
274 
276  solve (MatrixType& mattype, const ComplexMatrix& b) const;
278  solve (MatrixType& mattype, const ComplexMatrix& b,
279  octave_idx_type& info) const;
281  solve (MatrixType& mattype, const ComplexMatrix& b, octave_idx_type& info,
282  double& rcon) const;
284  solve (MatrixType& mattype, const ComplexMatrix& b, octave_idx_type& info,
285  double& rcon, solve_singularity_handler sing_handler,
286  bool singular_fallback = true,
287  blas_trans_type transt = blas_no_trans) const;
288 
290  solve (MatrixType& mattype, const ColumnVector& b) const;
292  solve (MatrixType& mattype, const ColumnVector& b,
293  octave_idx_type& info) const;
295  solve (MatrixType& mattype, const ColumnVector& b,
296  octave_idx_type& info, double& rcon) const;
298  solve (MatrixType& mattype, const ColumnVector& b, octave_idx_type& info,
299  double& rcon, solve_singularity_handler sing_handler,
300  blas_trans_type transt = blas_no_trans) const;
301 
303  solve (MatrixType& mattype, const ComplexColumnVector& b) const;
305  solve (MatrixType& mattype, const ComplexColumnVector& b,
306  octave_idx_type& info) const;
308  solve (MatrixType& mattype, const ComplexColumnVector& b,
309  octave_idx_type& info, double& rcon) const;
311  solve (MatrixType& mattype, const ComplexColumnVector& b,
312  octave_idx_type& info, double& rcon,
313  solve_singularity_handler sing_handler,
314  blas_trans_type transt = blas_no_trans) const;
315 
316  // Generic interface to solver with probing of type
317  OCTAVE_API ComplexMatrix solve (const Matrix& b) const;
319  solve (const Matrix& b, octave_idx_type& info) const;
321  solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
323  solve (const Matrix& b, octave_idx_type& info, double& rcon,
324  solve_singularity_handler sing_handler,
325  blas_trans_type transt = blas_no_trans) const;
326 
327  OCTAVE_API ComplexMatrix solve (const ComplexMatrix& b) const;
329  solve (const ComplexMatrix& b, octave_idx_type& info) const;
331  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const;
333  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon,
334  solve_singularity_handler sing_handler,
335  blas_trans_type transt = blas_no_trans) const;
336 
337  OCTAVE_API ComplexColumnVector solve (const ColumnVector& b) const;
339  solve (const ColumnVector& b, octave_idx_type& info) const;
341  solve (const ColumnVector& b, octave_idx_type& info, double& rcon) const;
343  solve (const ColumnVector& b, octave_idx_type& info, double& rcon,
344  solve_singularity_handler sing_handler,
345  blas_trans_type transt = blas_no_trans) const;
346 
347  OCTAVE_API ComplexColumnVector solve (const ComplexColumnVector& b) const;
349  solve (const ComplexColumnVector& b, octave_idx_type& info) const;
351  solve (const ComplexColumnVector& b, octave_idx_type& info,
352  double& rcon) const;
354  solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcon,
355  solve_singularity_handler sing_handler,
356  blas_trans_type transt = blas_no_trans) const;
357 
358  OCTAVE_API ComplexMatrix lssolve (const Matrix& b) const;
360  lssolve (const Matrix& b, octave_idx_type& info) const;
362  lssolve (const Matrix& b, octave_idx_type& info,
363  octave_idx_type& rank) const;
365  lssolve (const Matrix& b, octave_idx_type& info,
366  octave_idx_type& rank, double& rcon) const;
367 
368  OCTAVE_API ComplexMatrix lssolve (const ComplexMatrix& b) const;
370  lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
372  lssolve (const ComplexMatrix& b, octave_idx_type& info,
373  octave_idx_type& rank) const;
375  lssolve (const ComplexMatrix& b, octave_idx_type& info,
376  octave_idx_type& rank, double& rcon) const;
377 
378  OCTAVE_API ComplexColumnVector lssolve (const ColumnVector& b) const;
380  lssolve (const ColumnVector& b, octave_idx_type& info) const;
382  lssolve (const ColumnVector& b, octave_idx_type& info,
383  octave_idx_type& rank) const;
385  lssolve (const ColumnVector& b, octave_idx_type& info,
386  octave_idx_type& rank, double& rcon) const;
387 
388  OCTAVE_API ComplexColumnVector lssolve (const ComplexColumnVector& b) const;
390  lssolve (const ComplexColumnVector& b, octave_idx_type& info) const;
392  lssolve (const ComplexColumnVector& b, octave_idx_type& info,
393  octave_idx_type& rank) const;
395  lssolve (const ComplexColumnVector& b, octave_idx_type& info,
396  octave_idx_type& rank, double& rcon) const;
397 
398  // matrix by diagonal matrix -> matrix operations
399 
402 
405 
406  // matrix by matrix -> matrix operations
407 
410 
411  // other operations
412 
413  OCTAVE_API boolMatrix all (int dim = -1) const;
414  OCTAVE_API boolMatrix any (int dim = -1) const;
415 
416  OCTAVE_API ComplexMatrix cumprod (int dim = -1) const;
417  OCTAVE_API ComplexMatrix cumsum (int dim = -1) const;
418  OCTAVE_API ComplexMatrix prod (int dim = -1) const;
419  OCTAVE_API ComplexMatrix sum (int dim = -1) const;
420  OCTAVE_API ComplexMatrix sumsq (int dim = -1) const;
421  OCTAVE_API Matrix abs () const;
422 
423  OCTAVE_API ComplexMatrix diag (octave_idx_type k = 0) const;
424 
426  diag (octave_idx_type m, octave_idx_type n) const;
427 
428  OCTAVE_API bool row_is_real_only (octave_idx_type) const;
429  OCTAVE_API bool column_is_real_only (octave_idx_type) const;
430 
431  OCTAVE_API ComplexColumnVector row_min () const;
432  OCTAVE_API ComplexColumnVector row_max () const;
433 
436 
437  OCTAVE_API ComplexRowVector column_min () const;
438  OCTAVE_API ComplexRowVector column_max () const;
439 
440  OCTAVE_API ComplexRowVector column_min (Array<octave_idx_type>& index) const;
441  OCTAVE_API ComplexRowVector column_max (Array<octave_idx_type>& index) const;
442 
443  // i/o
444 
445  friend OCTAVE_API std::ostream&
446  operator << (std::ostream& os, const ComplexMatrix& a);
447  friend OCTAVE_API std::istream&
448  operator >> (std::istream& is, ComplexMatrix& a);
449 };
450 
451 extern OCTAVE_API ComplexMatrix conj (const ComplexMatrix& a);
452 
453 // column vector by row vector -> matrix operations
454 
456 operator * (const ColumnVector& a, const ComplexRowVector& b);
457 
459 operator * (const ComplexColumnVector& a, const RowVector& b);
460 
463 
465 Givens (const Complex&, const Complex&);
466 
468 Sylvester (const ComplexMatrix&, const ComplexMatrix&, const ComplexMatrix&);
469 
471 xgemm (const ComplexMatrix& a, const ComplexMatrix& b,
473  blas_trans_type transb = blas_no_trans);
474 
476  const ComplexMatrix&);
478  const Matrix&);
480  const ComplexMatrix&);
481 
482 extern OCTAVE_API ComplexMatrix min (const Complex& c, const ComplexMatrix& m);
483 extern OCTAVE_API ComplexMatrix min (const ComplexMatrix& m, const Complex& c);
484 extern OCTAVE_API ComplexMatrix min (const ComplexMatrix& a,
485  const ComplexMatrix& b);
486 
487 extern OCTAVE_API ComplexMatrix max (const Complex& c, const ComplexMatrix& m);
488 extern OCTAVE_API ComplexMatrix max (const ComplexMatrix& m, const Complex& c);
489 extern OCTAVE_API ComplexMatrix max (const ComplexMatrix& a,
490  const ComplexMatrix& b);
491 
493  const ComplexColumnVector& x2,
495 
498 
501 
504 
506 
507 #endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexMatrix operator-=(ComplexMatrix &x, const Complex &y)
Definition: CMatrix.h:505
ComplexMatrix Sylvester(const ComplexMatrix &, const ComplexMatrix &, const ComplexMatrix &)
Definition: CMatrix.cc:3248
ComplexMatrix max(const Complex &c, const ComplexMatrix &m)
Definition: CMatrix.cc:3553
ComplexMatrix conj(const ComplexMatrix &a)
Definition: CMatrix.cc:675
ComplexMatrix linspace(const ComplexColumnVector &x1, const ComplexColumnVector &x2, octave_idx_type n)
Definition: CMatrix.cc:3629
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:3346
ComplexMatrix min(const Complex &c, const ComplexMatrix &m)
Definition: CMatrix.cc:3481
ComplexMatrix operator+=(ComplexMatrix &x, const Complex &y)
Definition: CMatrix.h:505
ComplexMatrix Givens(const Complex &, const Complex &)
Definition: CMatrix.cc:3226
ComplexMatrix operator*(const ColumnVector &a, const ComplexRowVector &b)
Definition: CMatrix.cc:2623
#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
void resize(const dim_vector &dv, const T &rfv)
Definition: Array-base.cc:1023
ComplexDiagMatrix complex_diag_matrix_type
Definition: CMatrix.h:56
ComplexColumnVector column_vector_type
Definition: CMatrix.h:46
Matrix real_matrix_type
Definition: CMatrix.h:52
Complex complex_elt_type
Definition: CMatrix.h:59
ComplexMatrix(const dim_vector &dv)
Definition: CMatrix.h:77
double real_elt_type
Definition: CMatrix.h:58
RowVector real_row_vector_type
Definition: CMatrix.h:50
ComplexRowVector row_vector_type
Definition: CMatrix.h:47
ComplexMatrix(octave_idx_type r, octave_idx_type c)
Definition: CMatrix.h:71
ComplexMatrix(octave_idx_type r, octave_idx_type c, const Complex &val)
Definition: CMatrix.h:74
~ComplexMatrix()=default
ComplexMatrix(const ComplexMatrix &a)=default
DiagMatrix real_diag_matrix_type
Definition: CMatrix.h:55
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
Definition: CMatrix.h:193
ComplexMatrix complex_matrix_type
Definition: CMatrix.h:53
ComplexMatrix transpose() const
Definition: CMatrix.h:172
ColumnVector real_column_vector_type
Definition: CMatrix.h:49
ComplexMatrix hermitian() const
Definition: CMatrix.h:170
ComplexMatrix(const dim_vector &dv, const Complex &val)
Definition: CMatrix.h:79
ComplexMatrix()=default
ComplexMatrix(const Array< U > &a)
Definition: CMatrix.h:86
ComplexMatrix(const MArray< U > &a)
Definition: CMatrix.h:83
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:63
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: MArray.h:102
MArray< T > transpose() const
Definition: MArray.h:99
Definition: dMatrix.h:42
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
#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< double > Complex
Definition: oct-cmplx.h:33
template int8_t abs(int8_t)