GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
CSparse.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-2023 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_CSparse_h)
27 #define octave_CSparse_h 1
28 
29 #include "octave-config.h"
30 
31 #include "CColVector.h"
32 #include "CMatrix.h"
33 #include "CNDArray.h"
34 #include "DET.h"
35 #include "MSparse.h"
36 #include "MatrixType.h"
37 #include "Sparse-op-decls.h"
38 #include "dColVector.h"
39 #include "dMatrix.h"
40 #include "dNDArray.h"
41 #include "oct-cmplx.h"
42 #include "mx-fwd.h"
43 
44 class
47 {
48 public:
49 
50  // Corresponding dense matrix type for this sparse matrix type.
52 
53  typedef void (*solve_singularity_handler) (double rcond);
54 
56 
58  octave_idx_type c) : MSparse<Complex> (r, c) { }
59 
61  : MSparse<Complex> (dv, nz) { }
62 
64  Complex val)
65  : MSparse<Complex> (r, c, val) { }
66 
68  : MSparse<Complex> (r, c, Complex (val)) { }
69 
71  : MSparse<Complex> (a) { }
72 
74  : MSparse<Complex> (a, dv) { }
75 
77 
79 
80  explicit SparseComplexMatrix (const ComplexMatrix& a)
81  : MSparse<Complex> (a) { }
82 
83  explicit SparseComplexMatrix (const ComplexNDArray& a)
84  : MSparse<Complex> (a) { }
85 
87  const octave::idx_vector& c, octave_idx_type nr = -1,
88  octave_idx_type nc = -1, bool sum_terms = true,
89  octave_idx_type nzm = -1)
90  : MSparse<Complex> (a, r, c, nr, nc, sum_terms, nzm) { }
91 
92  explicit OCTAVE_API SparseComplexMatrix (const SparseMatrix& a);
93 
95 
97 
99  octave_idx_type num_nz)
100  : MSparse<Complex> (r, c, num_nz) { }
101 
103  {
105  return *this;
106  }
107 
108  OCTAVE_API bool operator == (const SparseComplexMatrix& a) const;
109  OCTAVE_API bool operator != (const SparseComplexMatrix& a) const;
110 
111  OCTAVE_API bool ishermitian (void) const;
112 
113  OCTAVE_API SparseComplexMatrix max (int dim = -1) const;
115  max (Array<octave_idx_type>& index, int dim = -1) const;
116  OCTAVE_API SparseComplexMatrix min (int dim = -1) const;
118  min (Array<octave_idx_type>& index, int dim = -1) const;
119 
123  insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c);
125  insert (const SparseComplexMatrix& a, const Array<octave_idx_type>& indx);
127  insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
128 
132  concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx);
133 
134  OCTAVE_API ComplexMatrix matrix_value (void) const;
135 
136  OCTAVE_API SparseComplexMatrix hermitian (void) const; // complex conjugate transpose
138  { return MSparse<Complex>::transpose (); }
139 
141 
142  // extract row or column i.
143 
145 
147 
148 private:
149  SparseComplexMatrix dinverse (MatrixType& mattype, octave_idx_type& info,
150  double& rcond, const bool force = false,
151  const bool calccond = true) const;
152 
153  SparseComplexMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
154  double& rcond, const bool force = false,
155  const bool calccond = true) const;
156 
157 public:
158  OCTAVE_API SparseComplexMatrix inverse (void) const;
159  OCTAVE_API SparseComplexMatrix inverse (MatrixType& mattype) const;
161  inverse (MatrixType& mattype, octave_idx_type& info) const;
163  inverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
164  bool force = false, bool calc_cond = true) const;
165 
166  OCTAVE_API ComplexDET determinant (void) const;
167  OCTAVE_API ComplexDET determinant (octave_idx_type& info) const;
169  determinant (octave_idx_type& info, double& rcond,
170  bool calc_cond = true) const;
171 
172 private:
173  // Diagonal matrix solvers
174  ComplexMatrix dsolve (MatrixType& mattype, const Matrix& b,
175  octave_idx_type& info, double& rcond,
176  solve_singularity_handler sing_handler,
177  bool calc_cond = false) const;
178 
179  ComplexMatrix dsolve (MatrixType& mattype, const ComplexMatrix& 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& mattype, const SparseMatrix& b,
185  octave_idx_type& info, double& rcond,
186  solve_singularity_handler sing_handler,
187  bool calc_cond = false) const;
188 
189  SparseComplexMatrix dsolve (MatrixType& mattype, const SparseComplexMatrix& b,
190  octave_idx_type& info, double& rcond,
191  solve_singularity_handler sing_handler,
192  bool calc_cond = false) const;
193 
194  // Upper triangular matrix solvers
195  ComplexMatrix utsolve (MatrixType& mattype, const Matrix& b,
196  octave_idx_type& info, double& rcond,
197  solve_singularity_handler sing_handler,
198  bool calc_cond = false) const;
199 
200  ComplexMatrix utsolve (MatrixType& mattype, const ComplexMatrix& b,
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 
211  octave_idx_type& info, double& rcond,
212  solve_singularity_handler sing_handler,
213  bool calc_cond = false) const;
214 
215  // Lower triangular matrix solvers
216  ComplexMatrix ltsolve (MatrixType& mattype, const Matrix& b,
217  octave_idx_type& info, double& rcond,
218  solve_singularity_handler sing_handler,
219  bool calc_cond = false) const;
220 
221  ComplexMatrix ltsolve (MatrixType& mattype, const ComplexMatrix& b,
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 
232  octave_idx_type& info, double& rcond,
233  solve_singularity_handler sing_handler,
234  bool calc_cond = false) const;
235 
236  // Tridiagonal matrix solvers
237  ComplexMatrix trisolve (MatrixType& mattype, const Matrix& b,
238  octave_idx_type& info, double& rcond,
239  solve_singularity_handler sing_handler,
240  bool calc_cond = false) const;
241 
242  ComplexMatrix trisolve (MatrixType& mattype, const ComplexMatrix& 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& mattype, const SparseMatrix& b,
248  octave_idx_type& info, double& rcond,
249  solve_singularity_handler sing_handler,
250  bool calc_cond = false) const;
251 
252  SparseComplexMatrix trisolve (MatrixType& mattype, const SparseComplexMatrix& b,
253  octave_idx_type& info, double& rcond,
254  solve_singularity_handler sing_handler,
255  bool calc_cond = false) const;
256 
257  // Banded matrix solvers (umfpack/cholesky)
258  ComplexMatrix bsolve (MatrixType& mattype, const Matrix& b,
259  octave_idx_type& info, double& rcond,
260  solve_singularity_handler sing_handler,
261  bool calc_cond = false) const;
262 
263  ComplexMatrix bsolve (MatrixType& mattype, const ComplexMatrix& 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& mattype, const SparseMatrix& b,
269  octave_idx_type& info, double& rcond,
270  solve_singularity_handler sing_handler,
271  bool calc_cond = false) const;
272 
273  SparseComplexMatrix bsolve (MatrixType& mattype, const SparseComplexMatrix& b,
274  octave_idx_type& info, double& rcond,
275  solve_singularity_handler sing_handler,
276  bool calc_cond = false) const;
277 
278  // Full matrix solvers (umfpack/cholesky)
279  void * factorize (octave_idx_type& err, double& rcond, Matrix& Control,
280  Matrix& Info, solve_singularity_handler sing_handler,
281  bool calc_cond) const;
282 
283  ComplexMatrix fsolve (MatrixType& mattype, const Matrix& b,
284  octave_idx_type& info, double& rcond,
285  solve_singularity_handler sing_handler,
286  bool calc_cond = false) const;
287 
288  ComplexMatrix fsolve (MatrixType& mattype, const ComplexMatrix& 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& mattype, const SparseMatrix& b,
294  octave_idx_type& info, double& rcond,
295  solve_singularity_handler sing_handler,
296  bool calc_cond = false) const;
297 
298  SparseComplexMatrix fsolve (MatrixType& mattype, const SparseComplexMatrix& b,
299  octave_idx_type& info, double& rcond,
300  solve_singularity_handler sing_handler,
301  bool calc_cond = false) const;
302 
303 public:
304  // Generic interface to solver with no probing of type
305  OCTAVE_API ComplexMatrix solve (MatrixType& mattype, const Matrix& b) const;
307  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info) const;
309  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
310  double& rcond) const;
312  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
313  double& rcond, solve_singularity_handler sing_handler,
314  bool singular_fallback = true) const;
315 
317  solve (MatrixType& mattype, const ComplexMatrix& b) const;
319  solve (MatrixType& mattype, const ComplexMatrix& b,
320  octave_idx_type& info) const;
322  solve (MatrixType& mattype, const ComplexMatrix& b,
323  octave_idx_type& info, double& rcond) const;
325  solve (MatrixType& mattype, const ComplexMatrix& b,
326  octave_idx_type& info, double& rcond,
327  solve_singularity_handler sing_handler,
328  bool singular_fallback = true) const;
329 
331  solve (MatrixType& mattype, const SparseMatrix& b) const;
333  solve (MatrixType& mattype, const SparseMatrix& b,
334  octave_idx_type& info) const;
336  solve (MatrixType& mattype, const SparseMatrix& b, octave_idx_type& info,
337  double& rcond) const;
339  solve (MatrixType& mattype, const SparseMatrix& b, octave_idx_type& info,
340  double& rcond, solve_singularity_handler sing_handler,
341  bool singular_fallback = true) const;
342 
344  solve (MatrixType& mattype, const SparseComplexMatrix& b) const;
346  solve (MatrixType& mattype, const SparseComplexMatrix& b,
347  octave_idx_type& info) const;
349  solve (MatrixType& mattype, const SparseComplexMatrix& b,
350  octave_idx_type& info, double& rcond) const;
352  solve (MatrixType& mattype, const SparseComplexMatrix& b,
353  octave_idx_type& info, double& rcond,
354  solve_singularity_handler sing_handler,
355  bool singular_fallback = true) const;
356 
358  solve (MatrixType& mattype, const ColumnVector& b) const;
360  solve (MatrixType& mattype, const ColumnVector& b,
361  octave_idx_type& info) const;
363  solve (MatrixType& mattype, const ColumnVector& b,
364  octave_idx_type& info, double& rcond) const;
366  solve (MatrixType& mattype, const ColumnVector& b,
367  octave_idx_type& info, double& rcond,
368  solve_singularity_handler sing_handler) const;
369 
371  solve (MatrixType& mattype, const ComplexColumnVector& b) const;
373  solve (MatrixType& mattype, const ComplexColumnVector& b,
374  octave_idx_type& info) const;
376  solve (MatrixType& mattype, const ComplexColumnVector& b,
377  octave_idx_type& info, double& rcond) const;
379  solve (MatrixType& mattype, const ComplexColumnVector& b,
380  octave_idx_type& info, double& rcond,
381  solve_singularity_handler sing_handler) const;
382 
383  // Generic interface to solver with probing of type
384  OCTAVE_API ComplexMatrix solve (const Matrix& b) const;
386  solve (const Matrix& b, octave_idx_type& info) const;
388  solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
390  solve (const Matrix& b, octave_idx_type& info, double& rcond,
391  solve_singularity_handler sing_handler) const;
392 
395  solve (const ComplexMatrix& b, octave_idx_type& info) const;
397  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const;
399  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
400  solve_singularity_handler sing_handler) const;
401 
402  OCTAVE_API SparseComplexMatrix solve (const SparseMatrix& b) const;
404  solve (const SparseMatrix& b, octave_idx_type& info) const;
406  solve (const SparseMatrix& b, octave_idx_type& info, double& rcond) const;
408  solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
409  solve_singularity_handler sing_handler) const;
410 
411  OCTAVE_API SparseComplexMatrix solve (const SparseComplexMatrix& b) const;
413  solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
415  solve (const SparseComplexMatrix& b, octave_idx_type& info,
416  double& rcond) const;
418  solve (const SparseComplexMatrix& b, octave_idx_type& info, double& rcond,
419  solve_singularity_handler sing_handler) const;
420 
421  OCTAVE_API ComplexColumnVector solve (const ColumnVector& b) const;
423  solve (const ColumnVector& b, octave_idx_type& info) const;
425  solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
427  solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
428  solve_singularity_handler sing_handler) const;
429 
430  OCTAVE_API ComplexColumnVector solve (const ComplexColumnVector& b) const;
432  solve (const ComplexColumnVector& b, octave_idx_type& info) const;
434  solve (const ComplexColumnVector& b, octave_idx_type& info,
435  double& rcond) const;
437  solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond,
438  solve_singularity_handler sing_handler) const;
439 
440  OCTAVE_API SparseComplexMatrix squeeze (void) const;
441 
442  OCTAVE_API SparseComplexMatrix reshape (const dim_vector& new_dims) const;
443 
445  permute (const Array<octave_idx_type>& vec, bool inv = false) const;
446 
448  ipermute (const Array<octave_idx_type>& vec) const;
449 
450  OCTAVE_API bool any_element_is_nan (void) const;
451  OCTAVE_API bool any_element_is_inf_or_nan (void) const;
452  OCTAVE_API bool all_elements_are_real (void) const;
453  OCTAVE_API bool all_integers (double& max_val, double& min_val) const;
454  OCTAVE_API bool too_large_for_float (void) const;
455 
457 
458  OCTAVE_API SparseBoolMatrix all (int dim = -1) const;
459  OCTAVE_API SparseBoolMatrix any (int dim = -1) const;
460 
461  OCTAVE_API SparseComplexMatrix cumprod (int dim = -1) const;
462  OCTAVE_API SparseComplexMatrix cumsum (int dim = -1) const;
463  OCTAVE_API SparseComplexMatrix prod (int dim = -1) const;
464  OCTAVE_API SparseComplexMatrix sum (int dim = -1) const;
465  OCTAVE_API SparseComplexMatrix sumsq (int dim = -1) const;
466  OCTAVE_API SparseMatrix abs (void) const;
467 
469 
470  // i/o
471  friend OCTAVE_API std::ostream&
472  operator << (std::ostream& os, const SparseComplexMatrix& a);
473  friend OCTAVE_API std::istream&
474  operator >> (std::istream& is, SparseComplexMatrix& a);
475 };
476 
483 
485 operator * (const Matrix&, const SparseComplexMatrix&);
487 operator * (const ComplexMatrix&, const SparseMatrix&);
494 
496 operator * (const SparseMatrix&, const ComplexMatrix&);
498 operator * (const SparseComplexMatrix&, const Matrix&);
505 
510 
515 
520 
533 
546 
551 
553 min (const Complex& c, const SparseComplexMatrix& m);
555 min (const SparseComplexMatrix& m, const Complex& c);
557 min (const SparseComplexMatrix& a, const SparseComplexMatrix& b);
558 
560 max (const Complex& c, const SparseComplexMatrix& m);
562 max (const SparseComplexMatrix& m, const Complex& c);
564 max (const SparseComplexMatrix& a, const SparseComplexMatrix& b);
565 
568 
571 
574 
576 
577 #endif
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:418
OCTAVE_API ComplexMatrix mul_trans(const ComplexMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7511
OCTAVE_API SparseComplexMatrix operator+(const ComplexDiagMatrix &, const SparseMatrix &)
Definition: CSparse.cc:7587
OCTAVE_API SparseComplexMatrix operator*(const SparseMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7481
OCTAVE_API ComplexMatrix mul_herm(const ComplexMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7517
OCTAVE_API SparseComplexMatrix operator-(const ComplexDiagMatrix &, const SparseMatrix &)
Definition: CSparse.cc:7618
OCTAVE_API ComplexMatrix trans_mul(const SparseComplexMatrix &, const ComplexMatrix &)
Definition: CSparse.cc:7541
OCTAVE_API SparseComplexMatrix min(const Complex &c, const SparseComplexMatrix &m)
Definition: CSparse.cc:7669
OCTAVE_API ComplexMatrix herm_mul(const SparseComplexMatrix &, const ComplexMatrix &)
Definition: CSparse.cc:7547
OCTAVE_API SparseComplexMatrix max(const Complex &c, const SparseComplexMatrix &m)
Definition: CSparse.cc:7786
#define SPARSE_FORWARD_DEFS(B, R, F, T)
Definition: MSparse.h:194
#define SPARSE_SMSM_CMP_OP_DECLS(M1, M2, API)
#define SPARSE_SMSM_BOOL_OP_DECLS(M1, M2, API)
#define SPARSE_SSM_CMP_OP_DECLS(S, M, API)
#define SPARSE_SMS_BOOL_OP_DECLS(M, S, API)
#define SPARSE_SSM_BOOL_OP_DECLS(S, M, API)
#define SPARSE_SMS_CMP_OP_DECLS(M, S, API)
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
MSparse< T > transpose(void) const
Definition: MSparse.h:98
MSparse< T > & operator=(const MSparse< T > &a)
Definition: MSparse.h:80
Definition: dMatrix.h:42
SparseComplexMatrix(const ComplexNDArray &a)
Definition: CSparse.h:83
SparseComplexMatrix(const ComplexMatrix &a)
Definition: CSparse.h:80
SparseComplexMatrix(const Sparse< Complex > &a)
Definition: CSparse.h:78
SparseComplexMatrix(octave_idx_type r, octave_idx_type c, double val)
Definition: CSparse.h:67
SparseComplexMatrix(octave_idx_type r, octave_idx_type c)
Definition: CSparse.h:57
SparseComplexMatrix(const SparseComplexMatrix &a)
Definition: CSparse.h:70
SparseComplexMatrix(const dim_vector &dv, octave_idx_type nz=0)
Definition: CSparse.h:60
SparseComplexMatrix(const MSparse< Complex > &a)
Definition: CSparse.h:76
SparseComplexMatrix transpose(void) const
Definition: CSparse.h:137
SparseComplexMatrix(void)
Definition: CSparse.h:55
ComplexMatrix dense_matrix_type
Definition: CSparse.h:51
SparseComplexMatrix(octave_idx_type r, octave_idx_type c, Complex val)
Definition: CSparse.h:63
SparseComplexMatrix(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition: CSparse.h:98
OCTAVE_API ComplexMatrix solve(const ComplexMatrix &b) const
SparseComplexMatrix(const SparseComplexMatrix &a, const dim_vector &dv)
Definition: CSparse.h:73
SparseComplexMatrix(const Array< Complex > &a, const octave::idx_vector &r, const octave::idx_vector &c, octave_idx_type nr=-1, octave_idx_type nc=-1, bool sum_terms=true, octave_idx_type nzm=-1)
Definition: CSparse.h:86
Definition: Sparse.h:49
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
static M utsolve(const SM &U, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:259
static M ltsolve(const SM &L, const ColumnVector &Q, const M &m)
Definition: eigs-base.cc:239
octave::idx_vector idx_vector
Definition: idx-vector.h:1039
bool too_large_for_float(double x)
Definition: lo-utils.cc:55
#define OCTAVE_API
Definition: main.in.cc:55
class OCTAVE_API SparseComplexMatrix
Definition: mx-fwd.h:56
T octave_idx_type m
Definition: mx-inlines.cc:773
T * r
Definition: mx-inlines.cc:773
std::complex< double > Complex
Definition: oct-cmplx.h:33
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_value operator!(const octave_value &a)
Definition: ov.h:1838
static T abs(T x)
Definition: pr-output.cc:1678