GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CMatrix.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_CMatrix_h)
24 #define octave_CMatrix_h 1
25 
26 #include "MArray.h"
27 #include "MDiagArray2.h"
28 #include "MatrixType.h"
29 #include "CNDArray.h"
30 
31 #include "mx-defs.h"
32 #include "mx-op-decl.h"
33 #include "oct-cmplx.h"
34 #include "DET.h"
35 
36 class
37 OCTAVE_API
39 {
40 public:
41 
44 
45  typedef void (*solve_singularity_handler) (double rcon);
46 
47  ComplexMatrix (void) : ComplexNDArray () { }
48 
50  : ComplexNDArray (dim_vector (r, c)) { }
51 
53  : ComplexNDArray (dim_vector (r, c), val) { }
54 
55  ComplexMatrix (const dim_vector& dv) : ComplexNDArray (dv.redim (2)) { }
56 
57  ComplexMatrix (const dim_vector& dv, const Complex& val)
58  : ComplexNDArray (dv.redim (2), val) { }
59 
61 
62  template <class U>
64 
65  template <class U>
67 
68  ComplexMatrix (const Matrix& re, const Matrix& im);
69 
70  explicit ComplexMatrix (const Matrix& a);
71 
72  explicit ComplexMatrix (const RowVector& rv);
73 
74  explicit ComplexMatrix (const ColumnVector& cv);
75 
76  explicit ComplexMatrix (const DiagMatrix& a);
77 
78  explicit ComplexMatrix (const MDiagArray2<double>& a);
79 
80  explicit ComplexMatrix (const DiagArray2<double>& a);
81 
82  explicit ComplexMatrix (const ComplexRowVector& rv);
83 
84  explicit ComplexMatrix (const ComplexColumnVector& cv);
85 
86  explicit ComplexMatrix (const ComplexDiagMatrix& a);
87 
88  explicit ComplexMatrix (const MDiagArray2<Complex>& a);
89 
90  explicit ComplexMatrix (const DiagArray2<Complex>& a);
91 
92  explicit ComplexMatrix (const boolMatrix& a);
93 
94  explicit ComplexMatrix (const charMatrix& a);
95 
96  bool operator == (const ComplexMatrix& a) const;
97  bool operator != (const ComplexMatrix& a) const;
98 
99  bool is_hermitian (void) const;
100 
101  // destructive insert/delete/reorder operations
102 
104  ComplexMatrix& insert (const RowVector& a,
106  ComplexMatrix& insert (const ColumnVector& a,
108  ComplexMatrix& insert (const DiagMatrix& a,
110 
119 
120  ComplexMatrix& fill (double val);
121  ComplexMatrix& fill (const Complex& val);
122  ComplexMatrix& fill (double val,
125  ComplexMatrix& fill (const Complex& val,
128 
129  ComplexMatrix append (const Matrix& a) const;
130  ComplexMatrix append (const RowVector& a) const;
131  ComplexMatrix append (const ColumnVector& a) const;
132  ComplexMatrix append (const DiagMatrix& a) const;
133 
134  ComplexMatrix append (const ComplexMatrix& a) const;
135  ComplexMatrix append (const ComplexRowVector& a) const;
136  ComplexMatrix append (const ComplexColumnVector& a) const;
137  ComplexMatrix append (const ComplexDiagMatrix& a) const;
138 
139  ComplexMatrix stack (const Matrix& a) const;
140  ComplexMatrix stack (const RowVector& a) const;
141  ComplexMatrix stack (const ColumnVector& a) const;
142  ComplexMatrix stack (const DiagMatrix& a) const;
143 
144  ComplexMatrix stack (const ComplexMatrix& a) const;
145  ComplexMatrix stack (const ComplexRowVector& a) const;
146  ComplexMatrix stack (const ComplexColumnVector& a) const;
147  ComplexMatrix stack (const ComplexDiagMatrix& a) const;
148 
152  { return MArray<Complex>::transpose (); }
153 
154  friend OCTAVE_API ComplexMatrix conj (const ComplexMatrix& a);
155 
156  // resize is the destructive equivalent for this one
157 
160 
162  octave_idx_type nr, octave_idx_type nc) const;
163 
164  // extract row or column i.
165 
167 
169 
171  const Complex& rfv = Complex (0))
172  {
173  MArray<Complex>::resize (dim_vector (nr, nc), rfv);
174  }
175 
176 private:
178  double& rcon, int force, int calc_cond) const;
179 
181  double& rcon, int force, int calc_cond) const;
182 
183 public:
184  ComplexMatrix inverse (void) const;
185  ComplexMatrix inverse (octave_idx_type& info) const;
186  ComplexMatrix inverse (octave_idx_type& info, double& rcon, int force = 0,
187  int calc_cond = 1) const;
188 
189  ComplexMatrix inverse (MatrixType &mattype) const;
190  ComplexMatrix inverse (MatrixType &mattype, octave_idx_type& info) const;
192  double& rcon, int force = 0,
193  int calc_cond = 1) const;
194 
195  ComplexMatrix pseudo_inverse (double tol = 0.0) const;
196 
197  ComplexMatrix fourier (void) const;
198  ComplexMatrix ifourier (void) const;
199 
200  ComplexMatrix fourier2d (void) const;
201  ComplexMatrix ifourier2d (void) const;
202 
203  ComplexDET determinant (void) const;
204  ComplexDET determinant (octave_idx_type& info) const;
205  ComplexDET determinant (octave_idx_type& info, double& rcon,
206  int calc_cond = 1) const;
208  double& rcon, int calc_cond = 1) const;
209 
210  double rcond (void) const;
211  double rcond (MatrixType &mattype) const;
212 
213 private:
214  // Upper triangular matrix solvers
216  octave_idx_type& info, double& rcon,
217  solve_singularity_handler sing_handler,
218  bool calc_cond = false,
219  blas_trans_type transt = blas_no_trans) const;
220 
221  // Lower triangular matrix solvers
223  octave_idx_type& info, double& rcon,
224  solve_singularity_handler sing_handler,
225  bool calc_cond = false,
226  blas_trans_type transt = blas_no_trans) const;
227 
228  // Full matrix solvers (umfpack/cholesky)
230  octave_idx_type& info, double& rcon,
231  solve_singularity_handler sing_handler,
232  bool calc_cond = false) const;
233 
234 public:
235  // Generic interface to solver with no probing of type
236  ComplexMatrix solve (MatrixType &typ, const Matrix& b) const;
237  ComplexMatrix solve (MatrixType &typ, const Matrix& b,
238  octave_idx_type& info) const;
239  ComplexMatrix solve (MatrixType &typ, const Matrix& b,
240  octave_idx_type& info, double& rcon) const;
241  ComplexMatrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
242  double& rcon, solve_singularity_handler sing_handler,
243  bool singular_fallback = true,
244  blas_trans_type transt = blas_no_trans) const;
245 
246  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
248  octave_idx_type& info) const;
250  octave_idx_type& info, double& rcon) const;
252  octave_idx_type& info, double& rcon,
253  solve_singularity_handler sing_handler,
254  bool singular_fallback = true,
255  blas_trans_type transt = blas_no_trans) const;
256 
257  ComplexColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
259  octave_idx_type& info) const;
261  octave_idx_type& info, double& rcon) const;
263  octave_idx_type& info, double& rcon,
264  solve_singularity_handler sing_handler,
265  blas_trans_type transt = blas_no_trans) const;
266 
268  const ComplexColumnVector& b) const;
270  octave_idx_type& info) const;
272  octave_idx_type& info, double& rcon) const;
274  octave_idx_type& info, double& rcon,
275  solve_singularity_handler sing_handler,
276  blas_trans_type transt = blas_no_trans) const;
277 
278  // Generic interface to solver with probing of type
279  ComplexMatrix solve (const Matrix& b) const;
280  ComplexMatrix solve (const Matrix& b, octave_idx_type& info) const;
281  ComplexMatrix solve (const Matrix& b, octave_idx_type& info,
282  double& rcon) const;
283  ComplexMatrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
284  solve_singularity_handler sing_handler,
285  blas_trans_type transt = blas_no_trans) const;
286 
287  ComplexMatrix solve (const ComplexMatrix& b) const;
288  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
290  double& rcon) const;
292  double& rcon,
293  solve_singularity_handler sing_handler,
294  blas_trans_type transt = blas_no_trans) const;
295 
296  ComplexColumnVector solve (const ColumnVector& b) const;
297  ComplexColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
299  double& rcon) const;
301  double& rcon,
302  solve_singularity_handler sing_handler,
303  blas_trans_type transt = blas_no_trans) const;
304 
307  octave_idx_type& info) const;
309  octave_idx_type& info,
310  double& rcon) const;
312  octave_idx_type& info,
313  double& rcon,
314  solve_singularity_handler sing_handler,
315  blas_trans_type transt = blas_no_trans) const;
316 
317  ComplexMatrix lssolve (const Matrix& b) const;
318  ComplexMatrix lssolve (const Matrix& b, octave_idx_type& info) const;
319  ComplexMatrix lssolve (const Matrix& b, octave_idx_type& info,
320  octave_idx_type& rank) const;
321  ComplexMatrix lssolve (const Matrix& b, octave_idx_type& info,
322  octave_idx_type& rank, double& rcon) const;
323 
324  ComplexMatrix lssolve (const ComplexMatrix& b) const;
325  ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
327  octave_idx_type& rank) const;
329  octave_idx_type& rank, double& rcon) const;
330 
331  ComplexColumnVector lssolve (const ColumnVector& b) const;
333  octave_idx_type& info) const;
335  octave_idx_type& rank) const;
337  octave_idx_type& rank, double& rcon) const;
338 
341  octave_idx_type& info) const;
343  octave_idx_type& info,
344  octave_idx_type& rank) const;
346  octave_idx_type& info,
347  octave_idx_type& rank, double& rcon) const;
348 
349  // matrix by diagonal matrix -> matrix operations
350 
353 
356 
357  // matrix by matrix -> matrix operations
358 
359  ComplexMatrix& operator += (const Matrix& a);
360  ComplexMatrix& operator -= (const Matrix& a);
361 
362  // other operations
363 
364  boolMatrix all (int dim = -1) const;
365  boolMatrix any (int dim = -1) const;
366 
367  ComplexMatrix cumprod (int dim = -1) const;
368  ComplexMatrix cumsum (int dim = -1) const;
369  ComplexMatrix prod (int dim = -1) const;
370  ComplexMatrix sum (int dim = -1) const;
371  ComplexMatrix sumsq (int dim = -1) const;
372  Matrix abs (void) const;
373 
374  ComplexMatrix diag (octave_idx_type k = 0) const;
375 
377 
378  bool row_is_real_only (octave_idx_type) const;
380 
381  ComplexColumnVector row_min (void) const;
382  ComplexColumnVector row_max (void) const;
383 
386 
387  ComplexRowVector column_min (void) const;
388  ComplexRowVector column_max (void) const;
389 
392 
393  // i/o
394 
395  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
396  const ComplexMatrix& a);
397  friend OCTAVE_API std::istream& operator >> (std::istream& is,
398  ComplexMatrix& a);
399 };
400 
401 extern OCTAVE_API ComplexMatrix conj (const ComplexMatrix& a);
402 
403 // column vector by row vector -> matrix operations
404 
405 extern OCTAVE_API ComplexMatrix
406 operator * (const ColumnVector& a, const ComplexRowVector& b);
407 
408 extern OCTAVE_API ComplexMatrix
409 operator * (const ComplexColumnVector& a, const RowVector& b);
410 
411 extern OCTAVE_API ComplexMatrix
413 
414 extern OCTAVE_API ComplexMatrix
415 Givens (const Complex&, const Complex&);
416 
417 extern OCTAVE_API ComplexMatrix
418 Sylvester (const ComplexMatrix&, const ComplexMatrix&, const ComplexMatrix&);
419 
420 extern OCTAVE_API ComplexMatrix
421 xgemm (const ComplexMatrix& a, const ComplexMatrix& b,
423  blas_trans_type transb = blas_no_trans);
424 
425 extern OCTAVE_API ComplexMatrix operator * (const Matrix&,
426  const ComplexMatrix&);
427 extern OCTAVE_API ComplexMatrix operator * (const ComplexMatrix&,
428  const Matrix&);
429 extern OCTAVE_API ComplexMatrix operator * (const ComplexMatrix&,
430  const ComplexMatrix&);
431 
432 extern OCTAVE_API ComplexMatrix min (const Complex& c, const ComplexMatrix& m);
433 extern OCTAVE_API ComplexMatrix min (const ComplexMatrix& m, const Complex& c);
434 extern OCTAVE_API ComplexMatrix min (const ComplexMatrix& a,
435  const ComplexMatrix& b);
436 
437 extern OCTAVE_API ComplexMatrix max (const Complex& c, const ComplexMatrix& m);
438 extern OCTAVE_API ComplexMatrix max (const ComplexMatrix& m, const Complex& c);
439 extern OCTAVE_API ComplexMatrix max (const ComplexMatrix& a,
440  const ComplexMatrix& b);
441 
442 extern OCTAVE_API ComplexMatrix linspace (const ComplexColumnVector& x1,
443  const ComplexColumnVector& x2,
444  octave_idx_type n);
445 
446 
449 
450 SM_CMP_OP_DECLS (Complex, ComplexMatrix, OCTAVE_API)
451 SM_BOOL_OP_DECLS (Complex, ComplexMatrix, OCTAVE_API)
452 
453 MM_CMP_OP_DECLS (ComplexMatrix, ComplexMatrix, OCTAVE_API)
454 MM_BOOL_OP_DECLS (ComplexMatrix, ComplexMatrix, OCTAVE_API)
455 
456 MARRAY_FORWARD_DEFS (MArray, ComplexMatrix, Complex)
457 
458 #endif
Array< Complex > as_matrix(void) const
Return the array as a matrix.
Definition: Array.h:299
ComplexMatrix stack(const Matrix &a) const
Definition: CMatrix.cc:811
ComplexMatrix ifourier(void) const
Definition: CMatrix.cc:1292
ComplexRowVector row_vector_type
Definition: CMatrix.h:43
ComplexDET determinant(void) const
Definition: CMatrix.cc:1590
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:548
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
static ComplexMatrix ltsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
ComplexMatrix inverse(void) const
Definition: CMatrix.cc:1002
OCTAVE_API ComplexMatrix linspace(const ComplexColumnVector &x1, const ComplexColumnVector &x2, octave_idx_type n)
Definition: CMatrix.cc:3997
ComplexMatrix(const dim_vector &dv, const Complex &val)
Definition: CMatrix.h:57
ComplexMatrix(octave_idx_type r, octave_idx_type c)
Definition: CMatrix.h:49
MArray< T > transpose(void) const
Definition: MArray.h:83
bool column_is_real_only(octave_idx_type) const
Definition: CMatrix.cc:3209
ComplexRowVector row(octave_idx_type i) const
Definition: CMatrix.cc:990
void(* solve_singularity_handler)(double rcon)
Definition: CMatrix.h:45
Definition: MArray.h:36
#define SM_CMP_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:112
OCTAVE_API ComplexMatrix Sylvester(const ComplexMatrix &, const ComplexMatrix &, const ComplexMatrix &)
Definition: CMatrix.cc:3588
ComplexMatrix operator-=(ComplexMatrix &x, const Complex &y)
Definition: CMatrix.h:456
OCTAVE_API 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:3686
ComplexMatrix prod(int dim=-1) const
Definition: CMatrix.cc:3143
ComplexRowVector column_min(void) const
Definition: CMatrix.cc:3378
ComplexMatrix fsolve(MatrixType &typ, const ComplexMatrix &b, octave_idx_type &info, double &rcon, solve_singularity_handler sing_handler, bool calc_cond=false) const
Definition: CMatrix.cc:2112
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type r2
double rcond(void) const
Definition: CMatrix.cc:1750
ComplexMatrix cumprod(int dim=-1) const
Definition: CMatrix.cc:3131
ComplexMatrix hermitian(void) const
Definition: CMatrix.h:149
ComplexMatrix cumsum(int dim=-1) const
Definition: CMatrix.cc:3137
bool is_hermitian(void) const
Definition: CMatrix.cc:379
ComplexMatrix(const dim_vector &dv)
Definition: CMatrix.h:55
#define SM_BOOL_OP_DECLS(S, M, API)
Definition: mx-op-decl.h:120
boolMatrix all(int dim=-1) const
Definition: CMatrix.cc:3119
Definition: DET.h:31
ComplexMatrix(const MArray< U > &a)
Definition: CMatrix.h:63
#define MM_CMP_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:137
bool row_is_real_only(octave_idx_type) const
Definition: CMatrix.cc:3190
#define MM_BOOL_OP_DECLS(M1, M2, API)
Definition: mx-op-decl.h:145
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
ComplexColumnVector row_min(void) const
Definition: CMatrix.cc:3228
boolMatrix any(int dim=-1) const
Definition: CMatrix.cc:3125
ComplexMatrix solve(MatrixType &typ, const Matrix &b) const
Definition: CMatrix.cc:2294
ComplexMatrix fourier(void) const
Definition: CMatrix.cc:1263
#define MARRAY_FORWARD_DEFS(B, R, T)
Definition: MArray-decl.h:236
ComplexMatrix sumsq(int dim=-1) const
Definition: CMatrix.cc:3155
ComplexColumnVector row_max(void) const
Definition: CMatrix.cc:3303
Definition: dMatrix.h:35
ComplexMatrix transpose(void) const
Definition: CMatrix.h:151
OCTAVE_API ComplexMatrix Givens(const Complex &, const Complex &)
Definition: CMatrix.cc:3570
OCTAVE_API ComplexMatrix min(const Complex &c, const ComplexMatrix &m)
Definition: CMatrix.cc:3819
ComplexMatrix & insert(const Matrix &a, octave_idx_type r, octave_idx_type c)
Definition: CMatrix.cc:400
MArray< T > hermitian(T(*fcn)(const T &)=0) const
Definition: MArray.h:86
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:519
#define MS_CMP_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:87
ComplexMatrix(const ComplexMatrix &a)
Definition: CMatrix.h:60
ComplexMatrix pseudo_inverse(double tol=0.0) const
Definition: CMatrix.cc:1220
Handles the reference counting for all the derived classes.
Definition: Array.h:45
ComplexRowVector column_max(void) const
Definition: CMatrix.cc:3453
ComplexMatrix finverse(MatrixType &mattype, octave_idx_type &info, double &rcon, int force, int calc_cond) const
Definition: CMatrix.cc:1099
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
Definition: CMatrix.h:170
static ComplexMatrix utsolve(const SparseComplexMatrix &, const ColumnVector &, const ComplexMatrix &)
OCTAVE_API ComplexMatrix operator*(const ColumnVector &a, const ComplexRowVector &b)
Definition: CMatrix.cc:2942
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexColumnVector column_vector_type
Definition: CMatrix.h:42
OCTAVE_API ComplexMatrix conj(const ComplexMatrix &a)
Definition: CMatrix.cc:963
ComplexMatrix(octave_idx_type r, octave_idx_type c, const Complex &val)
Definition: CMatrix.h:52
#define MS_BOOL_OP_DECLS(M, S, API)
Definition: mx-op-decl.h:95
ComplexMatrix sum(int dim=-1) const
Definition: CMatrix.cc:3149
ComplexMatrix(void)
Definition: CMatrix.h:47
ComplexMatrix & fill(double val)
Definition: CMatrix.cc:573
ComplexMatrix ifourier2d(void) const
Definition: CMatrix.cc:1335
OCTAVE_API ComplexMatrix max(const Complex &c, const ComplexMatrix &m)
Definition: CMatrix.cc:3907
blas_trans_type
Definition: mx-defs.h:128
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
ComplexMatrix diag(octave_idx_type k=0) const
Definition: CMatrix.cc:3167
ComplexMatrix(const Array< U > &a)
Definition: CMatrix.h:66
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
std::complex< double > Complex
Definition: oct-cmplx.h:29
ComplexMatrix lssolve(const Matrix &b) const
Definition: CMatrix.cc:2583
ComplexMatrix tinverse(MatrixType &mattype, octave_idx_type &info, double &rcon, int force, int calc_cond) const
Definition: CMatrix.cc:1042
T abs(T x)
Definition: pr-output.cc:3062
ComplexColumnVector column(octave_idx_type i) const
Definition: CMatrix.cc:996
ComplexMatrix extract_n(octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const
Definition: CMatrix.cc:981
ComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: CMatrix.cc:971
ComplexMatrix append(const Matrix &a) const
Definition: CMatrix.cc:667
Array< Complex > index(const idx_vector &i) const
Indexing without resizing.
ComplexMatrix fourier2d(void) const
Definition: CMatrix.cc:1321
ComplexMatrix operator+=(ComplexMatrix &x, const Complex &y)
Definition: CMatrix.h:456