GNU Octave  3.8.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
CSparse.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2013 David Bateman
4 Copyright (C) 1998-2004 Andy Adler
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_CSparse_h)
25 #define octave_CSparse_h 1
26 
27 #include "dMatrix.h"
28 #include "dNDArray.h"
29 #include "CNDArray.h"
30 #include "dColVector.h"
31 #include "CColVector.h"
32 #include "oct-cmplx.h"
33 
34 #include "DET.h"
35 #include "MSparse.h"
36 #include "MSparse-defs.h"
37 #include "Sparse-op-defs.h"
38 #include "MatrixType.h"
39 
40 class PermMatrix;
41 class DiagMatrix;
42 class ComplexDiagMatrix;
43 class SparseMatrix;
44 class SparseBoolMatrix;
45 
46 class
47 OCTAVE_API
49 {
50 public:
51 
52  typedef void (*solve_singularity_handler) (double rcond);
53 
55 
57  octave_idx_type c) : MSparse<Complex> (r, c) { }
58 
60  : MSparse<Complex> (dv, nz) { }
61 
63  Complex val)
64  : MSparse<Complex> (r, c, val) { }
65 
67  : MSparse<Complex> (r, c, Complex (val)) { }
68 
70  : MSparse<Complex> (a) { }
71 
73  : MSparse<Complex> (a, dv) { }
74 
76 
78 
79  explicit SparseComplexMatrix (const ComplexMatrix& a)
80  : MSparse<Complex> (a) { }
81 
82  explicit SparseComplexMatrix (const ComplexNDArray& a)
83  : MSparse<Complex> (a) { }
84 
86  const idx_vector& c, octave_idx_type nr = -1,
87  octave_idx_type nc = -1, bool sum_terms = true,
88  octave_idx_type nzm = -1)
89  : MSparse<Complex> (a, r, c, nr, nc, sum_terms, nzm) { }
90 
91  explicit SparseComplexMatrix (const SparseMatrix& a);
92 
93  explicit SparseComplexMatrix (const SparseBoolMatrix& a);
94 
95  explicit SparseComplexMatrix (const ComplexDiagMatrix& a);
96 
98  octave_idx_type num_nz)
99  : MSparse<Complex> (r, c, num_nz) { }
100 
102  {
104  return *this;
105  }
106 
107  bool operator == (const SparseComplexMatrix& a) const;
108  bool operator != (const SparseComplexMatrix& a) const;
109 
110  bool is_hermitian (void) const;
111 
112  SparseComplexMatrix max (int dim = -1) const;
113  SparseComplexMatrix max (Array<octave_idx_type>& index, int dim = -1) const;
114  SparseComplexMatrix min (int dim = -1) const;
115  SparseComplexMatrix min (Array<octave_idx_type>& index, int dim = -1) const;
116 
122  const Array<octave_idx_type>& indx);
124  const Array<octave_idx_type>& indx);
125 
127  const Array<octave_idx_type>& ra_idx);
129  const Array<octave_idx_type>& ra_idx);
130 
131  ComplexMatrix matrix_value (void) const;
132 
133  SparseComplexMatrix hermitian (void) const; // complex conjugate transpose
135  { return MSparse<Complex>::transpose (); }
136 
137  friend SparseComplexMatrix conj (const SparseComplexMatrix& a);
138 
139  // extract row or column i.
140 
141  ComplexRowVector row (octave_idx_type i) const;
142 
143  ComplexColumnVector column (octave_idx_type i) const;
144 
145 private:
146  SparseComplexMatrix dinverse (MatrixType &mattyp, octave_idx_type& info,
147  double& rcond, const bool force = false,
148  const bool calccond = true) const;
149 
150  SparseComplexMatrix tinverse (MatrixType &mattyp, octave_idx_type& info,
151  double& rcond, const bool force = false,
152  const bool calccond = true) const;
153 
154 public:
155  SparseComplexMatrix inverse (void) const;
156  SparseComplexMatrix inverse (MatrixType& mattype) const;
157  SparseComplexMatrix inverse (MatrixType& mattype,
158  octave_idx_type& info) const;
159  SparseComplexMatrix inverse (MatrixType& mattype, octave_idx_type& info,
160  double& rcond, int force = 0,
161  int calc_cond = 1) const;
162 
163  ComplexDET determinant (void) const;
164  ComplexDET determinant (octave_idx_type& info) const;
165  ComplexDET determinant (octave_idx_type& info, double& rcond,
166  int calc_cond = 1) const;
167 
168 private:
169  // Diagonal matrix solvers
170  ComplexMatrix dsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
171  double& rcond, solve_singularity_handler sing_handler,
172  bool calc_cond = false) const;
173 
174  ComplexMatrix dsolve (MatrixType &typ, const ComplexMatrix& b,
175  octave_idx_type& info, double& rcond,
176  solve_singularity_handler sing_handler,
177  bool calc_cond = false) const;
178 
179  SparseComplexMatrix dsolve (MatrixType &typ, const SparseMatrix& b,
180  octave_idx_type& info, double& rcond,
181  solve_singularity_handler sing_handler,
182  bool calc_cond = false) const;
183 
184  SparseComplexMatrix dsolve (MatrixType &typ, const SparseComplexMatrix& b,
185  octave_idx_type& info, double& rcond,
186  solve_singularity_handler sing_handler,
187  bool calc_cond = false) const;
188 
189  // Upper triangular matrix solvers
190  ComplexMatrix utsolve (MatrixType &typ, const Matrix& b,
191  octave_idx_type& info, double& rcond,
192  solve_singularity_handler sing_handler,
193  bool calc_cond = false) const;
194 
196  octave_idx_type& info, double& rcond,
197  solve_singularity_handler sing_handler,
198  bool calc_cond = false) const;
199 
201  octave_idx_type& info, double& rcond,
202  solve_singularity_handler sing_handler,
203  bool calc_cond = false) const;
204 
206  octave_idx_type& info, double& rcond,
207  solve_singularity_handler sing_handler,
208  bool calc_cond = false) const;
209 
210  // Lower triangular matrix solvers
211  ComplexMatrix ltsolve (MatrixType &typ, const Matrix& b,
212  octave_idx_type& info, double& rcond,
213  solve_singularity_handler sing_handler,
214  bool calc_cond = false) const;
215 
217  octave_idx_type& info, double& rcond,
218  solve_singularity_handler sing_handler,
219  bool calc_cond = false) const;
220 
222  octave_idx_type& info, double& rcond,
223  solve_singularity_handler sing_handler,
224  bool calc_cond = false) const;
225 
227  octave_idx_type& info, double& rcond,
228  solve_singularity_handler sing_handler,
229  bool calc_cond = false) const;
230 
231  // Tridiagonal matrix solvers
232  ComplexMatrix trisolve (MatrixType &typ, const Matrix& b,
233  octave_idx_type& info, double& rcond,
234  solve_singularity_handler sing_handler,
235  bool calc_cond = false) const;
236 
237  ComplexMatrix trisolve (MatrixType &typ, const ComplexMatrix& b,
238  octave_idx_type& info, double& rcond,
239  solve_singularity_handler sing_handler,
240  bool calc_cond = false) const;
241 
242  SparseComplexMatrix trisolve (MatrixType &typ, const SparseMatrix& b,
243  octave_idx_type& info, double& rcond,
244  solve_singularity_handler sing_handler,
245  bool calc_cond = false) const;
246 
247  SparseComplexMatrix trisolve (MatrixType &typ, const SparseComplexMatrix& b,
248  octave_idx_type& info, double& rcond,
249  solve_singularity_handler sing_handler,
250  bool calc_cond = false) const;
251 
252  // Banded matrix solvers (umfpack/cholesky)
253  ComplexMatrix bsolve (MatrixType &typ, const Matrix& b,
254  octave_idx_type& info, double& rcond,
255  solve_singularity_handler sing_handler,
256  bool calc_cond = false) const;
257 
258  ComplexMatrix bsolve (MatrixType &typ, const ComplexMatrix& b,
259  octave_idx_type& info, double& rcond,
260  solve_singularity_handler sing_handler,
261  bool calc_cond = false) const;
262 
263  SparseComplexMatrix bsolve (MatrixType &typ, const SparseMatrix& b,
264  octave_idx_type& info, double& rcond,
265  solve_singularity_handler sing_handler,
266  bool calc_cond = false) const;
267 
268  SparseComplexMatrix bsolve (MatrixType &typ, const SparseComplexMatrix& b,
269  octave_idx_type& info, double& rcond,
270  solve_singularity_handler sing_handler,
271  bool calc_cond = false) const;
272 
273  // Full matrix solvers (umfpack/cholesky)
274  void * factorize (octave_idx_type& err, double &rcond, Matrix &Control,
275  Matrix &Info, solve_singularity_handler sing_handler,
276  bool calc_cond) const;
277 
278  ComplexMatrix fsolve (MatrixType &typ, const Matrix& b,
279  octave_idx_type& info, double& rcond,
280  solve_singularity_handler sing_handler,
281  bool calc_cond = false) const;
282 
283  ComplexMatrix fsolve (MatrixType &typ, const ComplexMatrix& b,
284  octave_idx_type& info, double& rcond,
285  solve_singularity_handler sing_handler,
286  bool calc_cond = false) const;
287 
288  SparseComplexMatrix fsolve (MatrixType &typ, const SparseMatrix& b,
289  octave_idx_type& info, double& rcond,
290  solve_singularity_handler sing_handler,
291  bool calc_cond = false) const;
292 
293  SparseComplexMatrix fsolve (MatrixType &typ, const SparseComplexMatrix& b,
294  octave_idx_type& info, double& rcond,
295  solve_singularity_handler sing_handler,
296  bool calc_cond = false) const;
297 
298 public:
299  // Generic interface to solver with no probing of type
300  ComplexMatrix solve (MatrixType &typ, const Matrix& b) const;
301  ComplexMatrix solve (MatrixType &typ, const Matrix& b,
302  octave_idx_type& info) const;
303  ComplexMatrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
304  double& rcond) const;
305  ComplexMatrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
306  double& rcond, solve_singularity_handler sing_handler,
307  bool singular_fallback = true) const;
308 
309  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
310  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
311  octave_idx_type& info) const;
312  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
313  octave_idx_type& info, double& rcond) const;
314  ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
315  octave_idx_type& info, double& rcond,
316  solve_singularity_handler sing_handler,
317  bool singular_fallback = true) const;
318 
319  SparseComplexMatrix solve (MatrixType &typ, const SparseMatrix& b) const;
320  SparseComplexMatrix solve (MatrixType &typ, const SparseMatrix& b,
321  octave_idx_type& info) const;
322  SparseComplexMatrix solve (MatrixType &typ, const SparseMatrix& b,
323  octave_idx_type& info, double& rcond) const;
324  SparseComplexMatrix solve (MatrixType &typ, const SparseMatrix& b,
325  octave_idx_type& info, double& rcond,
326  solve_singularity_handler sing_handler,
327  bool singular_fallback = true) const;
328 
329  SparseComplexMatrix solve (MatrixType &typ,
330  const SparseComplexMatrix& b) const;
331  SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
332  octave_idx_type& info) const;
333  SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
334  octave_idx_type& info, double& rcond) const;
335  SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
336  octave_idx_type& info, double& rcond,
337  solve_singularity_handler sing_handler,
338  bool singular_fallback = true) const;
339 
340  ComplexColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
341  ComplexColumnVector solve (MatrixType &typ, const ColumnVector& b,
342  octave_idx_type& info) const;
343  ComplexColumnVector solve (MatrixType &typ, const ColumnVector& b,
344  octave_idx_type& info, double& rcond) const;
345  ComplexColumnVector solve (MatrixType &typ, const ColumnVector& b,
346  octave_idx_type& info, double& rcond,
347  solve_singularity_handler sing_handler) const;
348 
349  ComplexColumnVector solve (MatrixType &typ,
350  const ComplexColumnVector& b) const;
351  ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
352  octave_idx_type& info) const;
353  ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
354  octave_idx_type& info, double& rcond) const;
355  ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
356  octave_idx_type& info, double& rcond,
357  solve_singularity_handler sing_handler) const;
358 
359  // Generic interface to solver with probing of type
360  ComplexMatrix solve (const Matrix& b) const;
361  ComplexMatrix solve (const Matrix& b, octave_idx_type& info) const;
362  ComplexMatrix solve (const Matrix& b, octave_idx_type& info,
363  double& rcond) const;
364  ComplexMatrix solve (const Matrix& b, octave_idx_type& info, double& rcond,
365  solve_singularity_handler sing_handler) const;
366 
367  ComplexMatrix solve (const ComplexMatrix& b) const;
368  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
369  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info,
370  double& rcond) const;
371  ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info,
372  double& rcond,
373  solve_singularity_handler sing_handler) const;
374 
375  SparseComplexMatrix solve (const SparseMatrix& b) const;
376  SparseComplexMatrix solve (const SparseMatrix& b,
377  octave_idx_type& info) const;
378  SparseComplexMatrix solve (const SparseMatrix& b, octave_idx_type& info,
379  double& rcond) const;
380  SparseComplexMatrix solve (const SparseMatrix& b, octave_idx_type& info,
381  double& rcond,
382  solve_singularity_handler sing_handler) const;
383 
384  SparseComplexMatrix solve (const SparseComplexMatrix& b) const;
386  octave_idx_type& info) const;
388  octave_idx_type& info, double& rcond) const;
390  octave_idx_type& info, double& rcond,
391  solve_singularity_handler sing_handler) const;
392 
393  ComplexColumnVector solve (const ColumnVector& b) const;
394  ComplexColumnVector solve (const ColumnVector& b,
395  octave_idx_type& info) const;
396  ComplexColumnVector solve (const ColumnVector& b, octave_idx_type& info,
397  double& rcond) const;
398  ComplexColumnVector solve (const ColumnVector& b, octave_idx_type& info,
399  double& rcond,
400  solve_singularity_handler sing_handler) const;
401 
402  ComplexColumnVector solve (const ComplexColumnVector& b) const;
404  octave_idx_type& info) const;
406  octave_idx_type& info, double& rcond) const;
408  octave_idx_type& info, double& rcond,
409  solve_singularity_handler sing_handler) const;
410 
411  SparseComplexMatrix squeeze (void) const;
412 
413  SparseComplexMatrix reshape (const dim_vector& new_dims) const;
414 
416  bool inv = false) const;
417 
419 
420  bool any_element_is_nan (void) const;
421  bool any_element_is_inf_or_nan (void) const;
422  bool all_elements_are_real (void) const;
423  bool all_integers (double& max_val, double& min_val) const;
424  bool too_large_for_float (void) const;
425 
426  SparseBoolMatrix operator ! (void) const;
427 
428  SparseBoolMatrix all (int dim = -1) const;
429  SparseBoolMatrix any (int dim = -1) const;
430 
431  SparseComplexMatrix cumprod (int dim = -1) const;
432  SparseComplexMatrix cumsum (int dim = -1) const;
433  SparseComplexMatrix prod (int dim = -1) const;
434  SparseComplexMatrix sum (int dim = -1) const;
435  SparseComplexMatrix sumsq (int dim = -1) const;
436  SparseMatrix abs (void) const;
437 
439 
440  // i/o
441  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
442  const SparseComplexMatrix& a);
443  friend OCTAVE_API std::istream& operator >> (std::istream& is,
445 };
446 
447 extern OCTAVE_API SparseComplexMatrix operator * (const SparseMatrix&,
448  const SparseComplexMatrix&);
449 extern OCTAVE_API SparseComplexMatrix operator * (const SparseComplexMatrix&,
450  const SparseMatrix&);
451 extern OCTAVE_API SparseComplexMatrix operator * (const SparseComplexMatrix&,
452  const SparseComplexMatrix&);
453 
454 extern OCTAVE_API ComplexMatrix operator * (const Matrix&,
455  const SparseComplexMatrix&);
456 extern OCTAVE_API ComplexMatrix operator * (const ComplexMatrix&,
457  const SparseMatrix&);
458 extern OCTAVE_API ComplexMatrix operator * (const ComplexMatrix&,
459  const SparseComplexMatrix&);
460 extern OCTAVE_API ComplexMatrix mul_trans (const ComplexMatrix&,
461  const SparseComplexMatrix&);
462 extern OCTAVE_API ComplexMatrix mul_herm (const ComplexMatrix&,
463  const SparseComplexMatrix&);
464 
465 extern OCTAVE_API ComplexMatrix operator * (const SparseMatrix&,
466  const ComplexMatrix&);
467 extern OCTAVE_API ComplexMatrix operator * (const SparseComplexMatrix&,
468  const Matrix&);
469 extern OCTAVE_API ComplexMatrix operator * (const SparseComplexMatrix&,
470  const ComplexMatrix&);
471 extern OCTAVE_API ComplexMatrix trans_mul (const SparseComplexMatrix&,
472  const ComplexMatrix&);
473 extern OCTAVE_API ComplexMatrix herm_mul (const SparseComplexMatrix&,
474  const ComplexMatrix&);
475 
476 extern OCTAVE_API SparseComplexMatrix operator * (const DiagMatrix&,
477  const SparseComplexMatrix&);
478 extern OCTAVE_API SparseComplexMatrix operator * (const SparseComplexMatrix&,
479  const DiagMatrix&);
480 
481 extern OCTAVE_API SparseComplexMatrix operator * (const ComplexDiagMatrix&,
482  const SparseMatrix&);
483 extern OCTAVE_API SparseComplexMatrix operator * (const SparseMatrix&,
484  const ComplexDiagMatrix&);
485 
486 extern OCTAVE_API SparseComplexMatrix operator * (const ComplexDiagMatrix&,
487  const SparseComplexMatrix&);
488 extern OCTAVE_API SparseComplexMatrix operator * (const SparseComplexMatrix&,
489  const ComplexDiagMatrix&);
490 
491 extern OCTAVE_API SparseComplexMatrix operator + (const ComplexDiagMatrix&,
492  const SparseMatrix&);
493 extern OCTAVE_API SparseComplexMatrix operator + (const DiagMatrix&,
494  const SparseComplexMatrix&);
495 extern OCTAVE_API SparseComplexMatrix operator + (const ComplexDiagMatrix&,
496  const SparseComplexMatrix&);
497 extern OCTAVE_API SparseComplexMatrix operator + (const SparseMatrix&,
498  const ComplexDiagMatrix&);
499 extern OCTAVE_API SparseComplexMatrix operator + (const SparseComplexMatrix&,
500  const DiagMatrix&);
501 extern OCTAVE_API SparseComplexMatrix operator + (const SparseComplexMatrix&,
502  const ComplexDiagMatrix&);
503 
504 extern OCTAVE_API SparseComplexMatrix operator - (const ComplexDiagMatrix&,
505  const SparseMatrix&);
506 extern OCTAVE_API SparseComplexMatrix operator - (const DiagMatrix&,
507  const SparseComplexMatrix&);
508 extern OCTAVE_API SparseComplexMatrix operator - (const ComplexDiagMatrix&,
509  const SparseComplexMatrix&);
510 extern OCTAVE_API SparseComplexMatrix operator - (const SparseMatrix&,
511  const ComplexDiagMatrix&);
512 extern OCTAVE_API SparseComplexMatrix operator - (const SparseComplexMatrix&,
513  const DiagMatrix&);
514 extern OCTAVE_API SparseComplexMatrix operator - (const SparseComplexMatrix&,
515  const ComplexDiagMatrix&);
516 
517 extern OCTAVE_API SparseComplexMatrix operator * (const PermMatrix&,
518  const SparseComplexMatrix&);
519 extern OCTAVE_API SparseComplexMatrix operator * (const SparseComplexMatrix&,
520  const PermMatrix&);
521 
522 extern OCTAVE_API SparseComplexMatrix min (const Complex& c,
523  const SparseComplexMatrix& m);
524 extern OCTAVE_API SparseComplexMatrix min (const SparseComplexMatrix& m,
525  const Complex& c);
526 extern OCTAVE_API SparseComplexMatrix min (const SparseComplexMatrix& a,
527  const SparseComplexMatrix& b);
528 
529 extern OCTAVE_API SparseComplexMatrix max (const Complex& c,
530  const SparseComplexMatrix& m);
531 extern OCTAVE_API SparseComplexMatrix max (const SparseComplexMatrix& m,
532  const Complex& c);
533 extern OCTAVE_API SparseComplexMatrix max (const SparseComplexMatrix& a,
534  const SparseComplexMatrix& b);
535 
538 
539 SPARSE_SSM_CMP_OP_DECLS (Complex, SparseComplexMatrix, OCTAVE_API)
540 SPARSE_SSM_BOOL_OP_DECLS (Complex, SparseComplexMatrix, OCTAVE_API)
541 
542 SPARSE_SMSM_CMP_OP_DECLS (SparseComplexMatrix, SparseComplexMatrix, OCTAVE_API)
543 SPARSE_SMSM_BOOL_OP_DECLS (SparseComplexMatrix, SparseComplexMatrix, OCTAVE_API)
544 
545 SPARSE_FORWARD_DEFS (MSparse, SparseComplexMatrix, ComplexMatrix, Complex)
546 
547 #ifdef USE_64_BIT_IDX_T
548 #define UMFPACK_ZNAME(name) umfpack_zl_ ## name
549 #else
550 #define UMFPACK_ZNAME(name) umfpack_zi_ ## name
551 #endif
552 
553 #endif