GNU Octave  9.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-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_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  ~SparseComplexMatrix () = default;
109 
110  OCTAVE_API bool operator == (const SparseComplexMatrix& a) const;
111  OCTAVE_API bool operator != (const SparseComplexMatrix& a) const;
112 
113  OCTAVE_API bool ishermitian () const;
114 
115  OCTAVE_API SparseComplexMatrix max (int dim = -1) const;
117  max (Array<octave_idx_type>& index, int dim = -1) const;
118  OCTAVE_API SparseComplexMatrix min (int dim = -1) const;
120  min (Array<octave_idx_type>& index, int dim = -1) const;
121 
125  insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c);
127  insert (const SparseComplexMatrix& a, const Array<octave_idx_type>& indx);
129  insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
130 
134  concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx);
135 
136  OCTAVE_API ComplexMatrix matrix_value () const;
137 
138  OCTAVE_API SparseComplexMatrix hermitian () const; // complex conjugate transpose
140  { return MSparse<Complex>::transpose (); }
141 
143 
144  // extract row or column i.
145 
147 
149 
150 private:
151  SparseComplexMatrix dinverse (MatrixType& mattype, octave_idx_type& info,
152  double& rcond, const bool force = false,
153  const bool calccond = true) const;
154 
155  SparseComplexMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
156  double& rcond, const bool force = false,
157  const bool calccond = true) const;
158 
159 public:
160  OCTAVE_API SparseComplexMatrix inverse () const;
161  OCTAVE_API SparseComplexMatrix inverse (MatrixType& mattype) const;
163  inverse (MatrixType& mattype, octave_idx_type& info) const;
165  inverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
166  bool force = false, bool calc_cond = true) const;
167 
168  OCTAVE_API ComplexDET determinant () const;
169  OCTAVE_API ComplexDET determinant (octave_idx_type& info) const;
171  determinant (octave_idx_type& info, double& rcond,
172  bool calc_cond = true) const;
173 
174 private:
175  // Diagonal matrix solvers
176  ComplexMatrix dsolve (MatrixType& mattype, const Matrix& b,
177  octave_idx_type& info, double& rcond,
178  solve_singularity_handler sing_handler,
179  bool calc_cond = false) const;
180 
181  ComplexMatrix dsolve (MatrixType& mattype, const ComplexMatrix& b,
182  octave_idx_type& info, double& rcond,
183  solve_singularity_handler sing_handler,
184  bool calc_cond = false) const;
185 
186  SparseComplexMatrix dsolve (MatrixType& mattype, const SparseMatrix& b,
187  octave_idx_type& info, double& rcond,
188  solve_singularity_handler sing_handler,
189  bool calc_cond = false) const;
190 
191  SparseComplexMatrix dsolve (MatrixType& mattype, const SparseComplexMatrix& b,
192  octave_idx_type& info, double& rcond,
193  solve_singularity_handler sing_handler,
194  bool calc_cond = false) const;
195 
196  // Upper triangular matrix solvers
197  ComplexMatrix utsolve (MatrixType& mattype, const Matrix& b,
198  octave_idx_type& info, double& rcond,
199  solve_singularity_handler sing_handler,
200  bool calc_cond = false) const;
201 
202  ComplexMatrix utsolve (MatrixType& mattype, const ComplexMatrix& b,
203  octave_idx_type& info, double& rcond,
204  solve_singularity_handler sing_handler,
205  bool calc_cond = false) const;
206 
207  SparseComplexMatrix utsolve (MatrixType& mattype, const SparseMatrix& b,
208  octave_idx_type& info, double& rcond,
209  solve_singularity_handler sing_handler,
210  bool calc_cond = false) const;
211 
212  SparseComplexMatrix utsolve (MatrixType& mattype, const SparseComplexMatrix& b,
213  octave_idx_type& info, double& rcond,
214  solve_singularity_handler sing_handler,
215  bool calc_cond = false) const;
216 
217  // Lower triangular matrix solvers
218  ComplexMatrix ltsolve (MatrixType& mattype, const Matrix& b,
219  octave_idx_type& info, double& rcond,
220  solve_singularity_handler sing_handler,
221  bool calc_cond = false) const;
222 
223  ComplexMatrix ltsolve (MatrixType& mattype, const ComplexMatrix& b,
224  octave_idx_type& info, double& rcond,
225  solve_singularity_handler sing_handler,
226  bool calc_cond = false) const;
227 
228  SparseComplexMatrix ltsolve (MatrixType& mattype, const SparseMatrix& b,
229  octave_idx_type& info, double& rcond,
230  solve_singularity_handler sing_handler,
231  bool calc_cond = false) const;
232 
233  SparseComplexMatrix ltsolve (MatrixType& mattype, const SparseComplexMatrix& b,
234  octave_idx_type& info, double& rcond,
235  solve_singularity_handler sing_handler,
236  bool calc_cond = false) const;
237 
238  // Tridiagonal matrix solvers
239  ComplexMatrix trisolve (MatrixType& mattype, const Matrix& b,
240  octave_idx_type& info, double& rcond,
241  solve_singularity_handler sing_handler,
242  bool calc_cond = false) const;
243 
244  ComplexMatrix trisolve (MatrixType& mattype, const ComplexMatrix& b,
245  octave_idx_type& info, double& rcond,
246  solve_singularity_handler sing_handler,
247  bool calc_cond = false) const;
248 
249  SparseComplexMatrix trisolve (MatrixType& mattype, const SparseMatrix& b,
250  octave_idx_type& info, double& rcond,
251  solve_singularity_handler sing_handler,
252  bool calc_cond = false) const;
253 
254  SparseComplexMatrix trisolve (MatrixType& mattype, const SparseComplexMatrix& b,
255  octave_idx_type& info, double& rcond,
256  solve_singularity_handler sing_handler,
257  bool calc_cond = false) const;
258 
259  // Banded matrix solvers (umfpack/cholesky)
260  ComplexMatrix bsolve (MatrixType& mattype, const Matrix& b,
261  octave_idx_type& info, double& rcond,
262  solve_singularity_handler sing_handler,
263  bool calc_cond = false) const;
264 
265  ComplexMatrix bsolve (MatrixType& mattype, const ComplexMatrix& b,
266  octave_idx_type& info, double& rcond,
267  solve_singularity_handler sing_handler,
268  bool calc_cond = false) const;
269 
270  SparseComplexMatrix bsolve (MatrixType& mattype, const SparseMatrix& b,
271  octave_idx_type& info, double& rcond,
272  solve_singularity_handler sing_handler,
273  bool calc_cond = false) const;
274 
275  SparseComplexMatrix bsolve (MatrixType& mattype, const SparseComplexMatrix& b,
276  octave_idx_type& info, double& rcond,
277  solve_singularity_handler sing_handler,
278  bool calc_cond = false) const;
279 
280  // Full matrix solvers (umfpack/cholesky)
281  void * factorize (octave_idx_type& err, double& rcond, Matrix& Control,
282  Matrix& Info, solve_singularity_handler sing_handler,
283  bool calc_cond) const;
284 
285  ComplexMatrix fsolve (MatrixType& mattype, const Matrix& b,
286  octave_idx_type& info, double& rcond,
287  solve_singularity_handler sing_handler,
288  bool calc_cond = false) const;
289 
290  ComplexMatrix fsolve (MatrixType& mattype, const ComplexMatrix& b,
291  octave_idx_type& info, double& rcond,
292  solve_singularity_handler sing_handler,
293  bool calc_cond = false) const;
294 
295  SparseComplexMatrix fsolve (MatrixType& mattype, const SparseMatrix& b,
296  octave_idx_type& info, double& rcond,
297  solve_singularity_handler sing_handler,
298  bool calc_cond = false) const;
299 
300  SparseComplexMatrix fsolve (MatrixType& mattype, const SparseComplexMatrix& b,
301  octave_idx_type& info, double& rcond,
302  solve_singularity_handler sing_handler,
303  bool calc_cond = false) const;
304 
305 public:
306  // Generic interface to solver with no probing of type
307  OCTAVE_API ComplexMatrix solve (MatrixType& mattype, const Matrix& b) const;
309  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info) const;
311  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
312  double& rcond) const;
314  solve (MatrixType& mattype, const Matrix& b, octave_idx_type& info,
315  double& rcond, solve_singularity_handler sing_handler,
316  bool singular_fallback = true) const;
317 
319  solve (MatrixType& mattype, const ComplexMatrix& b) const;
321  solve (MatrixType& mattype, const ComplexMatrix& b,
322  octave_idx_type& info) const;
324  solve (MatrixType& mattype, const ComplexMatrix& b,
325  octave_idx_type& info, double& rcond) const;
327  solve (MatrixType& mattype, const ComplexMatrix& b,
328  octave_idx_type& info, double& rcond,
329  solve_singularity_handler sing_handler,
330  bool singular_fallback = true) const;
331 
333  solve (MatrixType& mattype, const SparseMatrix& b) const;
335  solve (MatrixType& mattype, const SparseMatrix& b,
336  octave_idx_type& info) const;
338  solve (MatrixType& mattype, const SparseMatrix& b, octave_idx_type& info,
339  double& rcond) const;
341  solve (MatrixType& mattype, const SparseMatrix& b, octave_idx_type& info,
342  double& rcond, solve_singularity_handler sing_handler,
343  bool singular_fallback = true) const;
344 
346  solve (MatrixType& mattype, const SparseComplexMatrix& b) const;
348  solve (MatrixType& mattype, const SparseComplexMatrix& b,
349  octave_idx_type& info) const;
351  solve (MatrixType& mattype, const SparseComplexMatrix& b,
352  octave_idx_type& info, double& rcond) const;
354  solve (MatrixType& mattype, const SparseComplexMatrix& b,
355  octave_idx_type& info, double& rcond,
356  solve_singularity_handler sing_handler,
357  bool singular_fallback = true) const;
358 
360  solve (MatrixType& mattype, const ColumnVector& b) const;
362  solve (MatrixType& mattype, const ColumnVector& b,
363  octave_idx_type& info) const;
365  solve (MatrixType& mattype, const ColumnVector& b,
366  octave_idx_type& info, double& rcond) const;
368  solve (MatrixType& mattype, const ColumnVector& b,
369  octave_idx_type& info, double& rcond,
370  solve_singularity_handler sing_handler) const;
371 
373  solve (MatrixType& mattype, const ComplexColumnVector& b) const;
375  solve (MatrixType& mattype, const ComplexColumnVector& b,
376  octave_idx_type& info) const;
378  solve (MatrixType& mattype, const ComplexColumnVector& b,
379  octave_idx_type& info, double& rcond) const;
381  solve (MatrixType& mattype, const ComplexColumnVector& b,
382  octave_idx_type& info, double& rcond,
383  solve_singularity_handler sing_handler) const;
384 
385  // Generic interface to solver with probing of type
386  OCTAVE_API ComplexMatrix solve (const Matrix& b) const;
388  solve (const Matrix& b, octave_idx_type& info) const;
390  solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
392  solve (const Matrix& b, octave_idx_type& info, double& rcond,
393  solve_singularity_handler sing_handler) const;
394 
397  solve (const ComplexMatrix& b, octave_idx_type& info) const;
399  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const;
401  solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
402  solve_singularity_handler sing_handler) const;
403 
404  OCTAVE_API SparseComplexMatrix solve (const SparseMatrix& b) const;
406  solve (const SparseMatrix& b, octave_idx_type& info) const;
408  solve (const SparseMatrix& b, octave_idx_type& info, double& rcond) const;
410  solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
411  solve_singularity_handler sing_handler) const;
412 
413  OCTAVE_API SparseComplexMatrix solve (const SparseComplexMatrix& b) const;
415  solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
417  solve (const SparseComplexMatrix& b, octave_idx_type& info,
418  double& rcond) const;
420  solve (const SparseComplexMatrix& b, octave_idx_type& info, double& rcond,
421  solve_singularity_handler sing_handler) const;
422 
423  OCTAVE_API ComplexColumnVector solve (const ColumnVector& b) const;
425  solve (const ColumnVector& b, octave_idx_type& info) const;
427  solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
429  solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
430  solve_singularity_handler sing_handler) const;
431 
432  OCTAVE_API ComplexColumnVector solve (const ComplexColumnVector& b) const;
434  solve (const ComplexColumnVector& b, octave_idx_type& info) const;
436  solve (const ComplexColumnVector& b, octave_idx_type& info,
437  double& rcond) const;
439  solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond,
440  solve_singularity_handler sing_handler) const;
441 
442  OCTAVE_API SparseComplexMatrix squeeze () const;
443 
444  OCTAVE_API SparseComplexMatrix reshape (const dim_vector& new_dims) const;
445 
447  permute (const Array<octave_idx_type>& vec, bool inv = false) const;
448 
450  ipermute (const Array<octave_idx_type>& vec) const;
451 
452  OCTAVE_API bool any_element_is_nan () const;
453  OCTAVE_API bool any_element_is_inf_or_nan () const;
454  OCTAVE_API bool all_elements_are_real () const;
455  OCTAVE_API bool all_integers (double& max_val, double& min_val) const;
456  OCTAVE_API bool too_large_for_float () const;
457 
459 
460  OCTAVE_API SparseBoolMatrix all (int dim = -1) const;
461  OCTAVE_API SparseBoolMatrix any (int dim = -1) const;
462 
463  OCTAVE_API SparseComplexMatrix cumprod (int dim = -1) const;
464  OCTAVE_API SparseComplexMatrix cumsum (int dim = -1) const;
465  OCTAVE_API SparseComplexMatrix prod (int dim = -1) const;
466  OCTAVE_API SparseComplexMatrix sum (int dim = -1) const;
467  OCTAVE_API SparseComplexMatrix sumsq (int dim = -1) const;
468  OCTAVE_API SparseMatrix abs () const;
469 
471 
472  // i/o
473  friend OCTAVE_API std::ostream&
474  operator << (std::ostream& os, const SparseComplexMatrix& a);
475  friend OCTAVE_API std::istream&
476  operator >> (std::istream& is, SparseComplexMatrix& a);
477 };
478 
485 
487 operator * (const Matrix&, const SparseComplexMatrix&);
489 operator * (const ComplexMatrix&, const SparseMatrix&);
496 
498 operator * (const SparseMatrix&, const ComplexMatrix&);
500 operator * (const SparseComplexMatrix&, const Matrix&);
507 
512 
517 
522 
535 
548 
553 
555 min (const Complex& c, const SparseComplexMatrix& m);
557 min (const SparseComplexMatrix& m, const Complex& c);
559 min (const SparseComplexMatrix& a, const SparseComplexMatrix& b);
560 
562 max (const Complex& c, const SparseComplexMatrix& m);
564 max (const SparseComplexMatrix& m, const Complex& c);
566 max (const SparseComplexMatrix& a, const SparseComplexMatrix& b);
567 
570 
573 
576 
578 
579 #endif
template 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
SparseComplexMatrix max(const Complex &c, const SparseComplexMatrix &m)
Definition: CSparse.cc:7791
ComplexMatrix herm_mul(const SparseComplexMatrix &, const ComplexMatrix &)
Definition: CSparse.cc:7552
SparseComplexMatrix operator+(const ComplexDiagMatrix &, const SparseMatrix &)
Definition: CSparse.cc:7592
SparseComplexMatrix operator*(const SparseMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7486
ComplexMatrix mul_herm(const ComplexMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7522
ComplexMatrix mul_trans(const ComplexMatrix &, const SparseComplexMatrix &)
Definition: CSparse.cc:7516
SparseComplexMatrix min(const Complex &c, const SparseComplexMatrix &m)
Definition: CSparse.cc:7674
ComplexMatrix trans_mul(const SparseComplexMatrix &, const ComplexMatrix &)
Definition: CSparse.cc:7546
SparseComplexMatrix operator-(const ComplexDiagMatrix &, const SparseMatrix &)
Definition: CSparse.cc:7623
#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() 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
ComplexMatrix solve(const ComplexMatrix &b) const
ComplexMatrix dense_matrix_type
Definition: CSparse.h:51
SparseComplexMatrix(octave_idx_type r, octave_idx_type c, Complex val)
Definition: CSparse.h:63
~SparseComplexMatrix()=default
SparseComplexMatrix(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition: CSparse.h:98
SparseComplexMatrix transpose() const
Definition: CSparse.h:139
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
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
bool too_large_for_float(double x)
Definition: lo-utils.cc:56
#define OCTAVE_API
Definition: main.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:781
T * r
Definition: mx-inlines.cc:781
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:1633
template int8_t abs(int8_t)