GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
dSparse.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2022 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_dSparse_h)
27#define octave_dSparse_h 1
28
29#include "octave-config.h"
30
31#include "mx-fwd.h"
32
33#include "CColVector.h"
34#include "CMatrix.h"
35#include "DET.h"
36#include "MSparse.h"
37#include "MatrixType.h"
38#include "Sparse-op-decls.h"
39#include "dColVector.h"
40#include "dMatrix.h"
41#include "dNDArray.h"
42
43class
45{
46public:
47
48 // Corresponding dense matrix type for this sparse matrix type.
50
51 typedef void (*solve_singularity_handler) (double rcond);
52
53 SparseMatrix (void) : MSparse<double> () { }
54
56 : MSparse<double> (r, c) { }
57
59 MSparse<double> (dv, nz) { }
60
61 explicit SparseMatrix (octave_idx_type r, octave_idx_type c, double val)
62 : MSparse<double> (r, c, val) { }
63
65
66 SparseMatrix (const SparseMatrix& a, const dim_vector& dv)
67 : MSparse<double> (a, dv) { }
68
70
72
73 explicit OCTAVE_API SparseMatrix (const SparseBoolMatrix& a);
74
75 explicit SparseMatrix (const Matrix& a) : MSparse<double> (a) { }
76
77 explicit SparseMatrix (const NDArray& a) : MSparse<double> (a) { }
78
80 const octave::idx_vector& c, octave_idx_type nr = -1,
81 octave_idx_type nc = -1, bool sum_terms = true,
82 octave_idx_type nzm = -1)
83 : MSparse<double> (a, r, c, nr, nc, sum_terms, nzm) { }
84
85 explicit OCTAVE_API SparseMatrix (const DiagMatrix& a);
86
87 explicit SparseMatrix (const PermMatrix& a) : MSparse<double>(a) { }
88
90 octave_idx_type num_nz) : MSparse<double> (r, c, num_nz) { }
91
92 SparseMatrix& operator = (const SparseMatrix& a)
93 {
95 return *this;
96 }
97
98 OCTAVE_API bool operator == (const SparseMatrix& a) const;
99 OCTAVE_API bool operator != (const SparseMatrix& a) const;
100
101 OCTAVE_API bool issymmetric (void) const;
102
103 OCTAVE_API SparseMatrix max (int dim = -1) const;
104 OCTAVE_API SparseMatrix max (Array<octave_idx_type>& index, int dim = -1) const;
105 OCTAVE_API SparseMatrix min (int dim = -1) const;
106 OCTAVE_API SparseMatrix min (Array<octave_idx_type>& index, int dim = -1) const;
107
108 // destructive insert/delete/reorder operations
109
111 insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c);
112
114 insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
115
120
123
125 {
127 }
128 SparseMatrix hermitian (void) const { return transpose (); }
129
130 // extract row or column i.
131
133
134 OCTAVE_API ColumnVector column (octave_idx_type i) const;
135
136private:
138 dinverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
139 const bool force = false, const bool calccond = true) const;
140
142 tinverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
143 const bool force = false, const bool calccond = true) const;
144
145public:
146 OCTAVE_API SparseMatrix inverse (void) const;
147 OCTAVE_API SparseMatrix inverse (MatrixType& mattype) const;
149 inverse (MatrixType& mattype, octave_idx_type& info) const;
151 inverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
152 bool force = false, bool calc_cond = true) const;
153
154 OCTAVE_API DET determinant (void) const;
155 OCTAVE_API DET determinant (octave_idx_type& info) const;
156 OCTAVE_API DET determinant (octave_idx_type& info, double& rcond,
157 bool calc_cond = true) const;
158
159private:
160 // Diagonal matrix solvers
162 dsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
163 double& rcond, solve_singularity_handler sing_handler,
164 bool calc_cond = false) const;
165
167 dsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
168 double& rcond, solve_singularity_handler sing_handler,
169 bool calc_cond = false) const;
170
172 dsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
173 double& rcond, solve_singularity_handler sing_handler,
174 bool calc_cond = false) const;
175
177 dsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
178 double& rcond, solve_singularity_handler sing_handler,
179 bool calc_cond = false) const;
180
181 // Upper triangular matrix solvers
183 utsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
184 double& rcond, solve_singularity_handler sing_handler,
185 bool calc_cond = false) const;
186
188 utsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
189 double& rcond, solve_singularity_handler sing_handler,
190 bool calc_cond = false) const;
191
193 utsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
194 double& rcond, solve_singularity_handler sing_handler,
195 bool calc_cond = false) const;
196
199 double& rcond, solve_singularity_handler sing_handler,
200 bool calc_cond = false) const;
201
202 // Lower triangular matrix solvers
204 ltsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
205 double& rcond, solve_singularity_handler sing_handler,
206 bool calc_cond = false) const;
207
209 ltsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
210 double& rcond, solve_singularity_handler sing_handler,
211 bool calc_cond = false) const;
212
214 ltsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
215 double& rcond, solve_singularity_handler sing_handler,
216 bool calc_cond = false) const;
217
220 double& rcond, solve_singularity_handler sing_handler,
221 bool calc_cond = false) const;
222
223 // Tridiagonal matrix solvers
225 trisolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
226 double& rcond, solve_singularity_handler sing_handler,
227 bool calc_cond = false) const;
228
230 trisolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
231 double& rcond, solve_singularity_handler sing_handler,
232 bool calc_cond = false) const;
233
235 trisolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
236 double& rcond, solve_singularity_handler sing_handler,
237 bool calc_cond = false) const;
238
240 trisolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
241 double& rcond, solve_singularity_handler sing_handler,
242 bool calc_cond = false) const;
243
244 // Banded matrix solvers (umfpack/cholesky)
246 bsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
247 double& rcond, solve_singularity_handler sing_handler,
248 bool calc_cond = false) const;
249
251 bsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
252 double& rcond, solve_singularity_handler sing_handler,
253 bool calc_cond = false) const;
254
256 bsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
257 double& rcond, solve_singularity_handler sing_handler,
258 bool calc_cond = false) const;
259
261 bsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
262 double& rcond, solve_singularity_handler sing_handler,
263 bool calc_cond = false) const;
264
265 // Full matrix solvers (umfpack/cholesky)
266 OCTAVE_API void *
267 factorize (octave_idx_type& err, double& rcond, Matrix& Control,
268 Matrix& Info, solve_singularity_handler sing_handler,
269 bool calc_cond = false) const;
270
272 fsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
273 double& rcond, solve_singularity_handler sing_handler,
274 bool calc_cond = false) const;
275
277 fsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
278 double& rcond, solve_singularity_handler sing_handler,
279 bool calc_cond = false) const;
280
282 fsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
283 double& rcond, solve_singularity_handler sing_handler,
284 bool calc_cond = false) const;
285
287 fsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
288 double& rcond, solve_singularity_handler sing_handler,
289 bool calc_cond = false) const;
290
291public:
292 // Generic interface to solver with no probing of type
293 OCTAVE_API Matrix solve (MatrixType& typ, const Matrix& b) const;
295 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info) const;
297 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
298 double& rcond) const;
300 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
301 double& rcond, solve_singularity_handler sing_handler,
302 bool singular_fallback = true) const;
303
305 solve (MatrixType& typ, const ComplexMatrix& b) const;
307 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info) const;
309 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
310 double& rcond) const;
312 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
313 double& rcond, solve_singularity_handler sing_handler,
314 bool singular_fallback = true) const;
315
316 OCTAVE_API SparseMatrix solve (MatrixType& typ, const SparseMatrix& b) const;
318 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info) const;
320 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
321 double& rcond) const;
323 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
324 double& rcond, solve_singularity_handler sing_handler,
325 bool singular_fallback = true) const;
326
328 solve (MatrixType& typ, const SparseComplexMatrix& b) const;
330 solve (MatrixType& typ, const SparseComplexMatrix& b,
331 octave_idx_type& info) const;
333 solve (MatrixType& typ, const SparseComplexMatrix& b,
334 octave_idx_type& info, double& rcond) const;
336 solve (MatrixType& typ, const SparseComplexMatrix& b,
337 octave_idx_type& info, double& rcond,
338 solve_singularity_handler sing_handler,
339 bool singular_fallabck = true) const;
340
341 OCTAVE_API ColumnVector solve (MatrixType& typ, const ColumnVector& b) const;
343 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info) const;
345 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info,
346 double& rcond) const;
348 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info,
349 double& rcond, solve_singularity_handler sing_handler) const;
350
352 solve (MatrixType& typ, const ComplexColumnVector& b) const;
354 solve (MatrixType& typ, const ComplexColumnVector& b,
355 octave_idx_type& info) const;
357 solve (MatrixType& typ, const ComplexColumnVector& b,
358 octave_idx_type& info, double& rcond) const;
360 solve (MatrixType& typ, const ComplexColumnVector& b,
361 octave_idx_type& info, double& rcond,
362 solve_singularity_handler sing_handler) const;
363
364 // Generic interface to solver with probing of type
365 OCTAVE_API Matrix solve (const Matrix& b) const;
366 OCTAVE_API Matrix solve (const Matrix& b, octave_idx_type& info) const;
368 solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
370 solve (const Matrix& b, octave_idx_type& info, double& rcond,
371 solve_singularity_handler sing_handler) const;
372
375 solve (const ComplexMatrix& b, octave_idx_type& info) const;
377 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const;
379 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
380 solve_singularity_handler sing_handler) const;
381
382 OCTAVE_API SparseMatrix solve (const SparseMatrix& b) const;
384 solve (const SparseMatrix& b, octave_idx_type& info) const;
386 solve (const SparseMatrix& b, octave_idx_type& info, double& rcond) const;
388 solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
389 solve_singularity_handler sing_handler) const;
390
393 solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
395 solve (const SparseComplexMatrix& b, octave_idx_type& info,
396 double& rcond) const;
398 solve (const SparseComplexMatrix& b, octave_idx_type& info,
399 double& rcond, solve_singularity_handler sing_handler) const;
400
401 OCTAVE_API ColumnVector solve (const ColumnVector& b) const;
403 solve (const ColumnVector& b, octave_idx_type& info) const;
405 solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
407 solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
408 solve_singularity_handler sing_handler) const;
409
412 solve (const ComplexColumnVector& b, octave_idx_type& info) const;
414 solve (const ComplexColumnVector& b, octave_idx_type& info,
415 double& rcond) const;
417 solve (const ComplexColumnVector& b, octave_idx_type& info,
418 double& rcond, solve_singularity_handler sing_handler) const;
419
420 // other operations
421
422 OCTAVE_API bool any_element_is_negative (bool = false) const;
423 OCTAVE_API bool any_element_is_nan (void) const;
424 OCTAVE_API bool any_element_is_inf_or_nan (void) const;
425 OCTAVE_API bool any_element_not_one_or_zero (void) const;
426 OCTAVE_API bool all_elements_are_zero (void) const;
427 OCTAVE_API bool all_elements_are_int_or_inf_or_nan (void) const;
428 OCTAVE_API bool all_integers (double& max_val, double& min_val) const;
429 OCTAVE_API bool too_large_for_float (void) const;
430
432
433 OCTAVE_API SparseBoolMatrix all (int dim = -1) const;
434 OCTAVE_API SparseBoolMatrix any (int dim = -1) const;
435
436 OCTAVE_API SparseMatrix cumprod (int dim = -1) const;
437 OCTAVE_API SparseMatrix cumsum (int dim = -1) const;
438 OCTAVE_API SparseMatrix prod (int dim = -1) const;
439 OCTAVE_API SparseMatrix sum (int dim = -1) const;
440 OCTAVE_API SparseMatrix sumsq (int dim = -1) const;
441 OCTAVE_API SparseMatrix abs (void) const;
442
443 OCTAVE_API SparseMatrix diag (octave_idx_type k = 0) const;
444
445 OCTAVE_API Matrix matrix_value (void) const;
446
447 OCTAVE_API SparseMatrix squeeze (void) const;
448
449 OCTAVE_API SparseMatrix reshape (const dim_vector& new_dims) const;
450
452 permute (const Array<octave_idx_type>& vec, bool inv = false) const;
453
454 OCTAVE_API SparseMatrix ipermute (const Array<octave_idx_type>& vec) const;
455
456 // i/o
457
458 friend OCTAVE_API std::ostream& operator << (std::ostream& os,
459 const SparseMatrix& a);
460 friend OCTAVE_API std::istream& operator >> (std::istream& is,
461 SparseMatrix& a);
462
463};
464
465// Publish externally used friend functions.
466
469
470// Other operators.
471
473 const SparseMatrix& b);
474extern OCTAVE_API Matrix operator * (const Matrix& a,
475 const SparseMatrix& b);
476extern OCTAVE_API Matrix mul_trans (const Matrix& a,
477 const SparseMatrix& b);
479 const Matrix& b);
480extern OCTAVE_API Matrix trans_mul (const SparseMatrix& a,
481 const Matrix& b);
482
484 const SparseMatrix&);
486 const DiagMatrix&);
487
489 const SparseMatrix&);
491 const DiagMatrix&);
493 const SparseMatrix&);
495 const DiagMatrix&);
496
498 const SparseMatrix&);
500 const PermMatrix&);
501
502extern OCTAVE_API SparseMatrix min (double d, const SparseMatrix& m);
503extern OCTAVE_API SparseMatrix min (const SparseMatrix& m, double d);
504extern OCTAVE_API SparseMatrix min (const SparseMatrix& a,
505 const SparseMatrix& b);
506
507extern OCTAVE_API SparseMatrix max (double d, const SparseMatrix& m);
508extern OCTAVE_API SparseMatrix max (const SparseMatrix& m, double d);
509extern OCTAVE_API SparseMatrix max (const SparseMatrix& a,
510 const SparseMatrix& b);
511
514
517
520
522
523#endif
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:418
#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
MSparse< T > & operator=(const MSparse< T > &a)
Definition: MSparse.h:80
MSparse< T > transpose(void) const
Definition: MSparse.h:98
Definition: dMatrix.h:42
SparseMatrix(void)
Definition: dSparse.h:53
SparseMatrix(const Sparse< double > &a)
Definition: dSparse.h:71
SparseMatrix(const SparseMatrix &a, const dim_vector &dv)
Definition: dSparse.h:66
SparseMatrix(const SparseMatrix &a)
Definition: dSparse.h:64
SparseMatrix(octave_idx_type r, octave_idx_type c)
Definition: dSparse.h:55
SparseMatrix transpose(void) const
Definition: dSparse.h:124
SparseMatrix(const MSparse< double > &a)
Definition: dSparse.h:69
Matrix dense_matrix_type
Definition: dSparse.h:49
OCTAVE_API ComplexMatrix solve(const ComplexMatrix &b) const
SparseMatrix(const Array< double > &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: dSparse.h:79
SparseMatrix(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition: dSparse.h:89
SparseMatrix(const NDArray &a)
Definition: dSparse.h:77
SparseMatrix(const PermMatrix &a)
Definition: dSparse.h:87
SparseMatrix(const Matrix &a)
Definition: dSparse.h:75
SparseMatrix hermitian(void) const
Definition: dSparse.h:128
SparseMatrix(const dim_vector &dv, octave_idx_type nz=0)
Definition: dSparse.h:58
SparseMatrix(octave_idx_type r, octave_idx_type c, double val)
Definition: dSparse.h:61
Definition: DET.h:39
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_API Matrix trans_mul(const SparseMatrix &a, const Matrix &b)
Definition: dSparse.cc:7530
OCTAVE_API SparseMatrix operator+(const DiagMatrix &, const SparseMatrix &)
Definition: dSparse.cc:7550
OCTAVE_API SparseMatrix min(double d, const SparseMatrix &m)
Definition: dSparse.cc:7594
OCTAVE_API SparseMatrix real(const SparseComplexMatrix &a)
Definition: dSparse.cc:554
OCTAVE_API SparseMatrix operator*(const SparseMatrix &a, const SparseMatrix &b)
Definition: dSparse.cc:7506
OCTAVE_API Matrix mul_trans(const Matrix &a, const SparseMatrix &b)
Definition: dSparse.cc:7518
OCTAVE_API SparseMatrix imag(const SparseComplexMatrix &a)
Definition: dSparse.cc:575
OCTAVE_API SparseMatrix max(double d, const SparseMatrix &m)
Definition: dSparse.cc:7744
OCTAVE_API SparseMatrix operator-(const DiagMatrix &, const SparseMatrix &)
Definition: dSparse.cc:7556
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
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.in.cc:55
class OCTAVE_API SparseMatrix
Definition: mx-fwd.h:55
bool too_large_for_float(double x)
Definition: lo-utils.cc:55
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_value operator!(const octave_value &a)
Definition: ov.h:1835
static T abs(T x)
Definition: pr-output.cc:1678
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)
Definition: symrcm.cc:391