GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
dSparse.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2025 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 SparseMatrix : public MSparse<double>
44{
45public:
46
47 // Corresponding dense matrix type for this sparse matrix type.
49
50 typedef void (*solve_singularity_handler) (double rcond);
51
53
56
58 MSparse<double> (dv, nz) { }
59
60 explicit SparseMatrix (octave_idx_type r, octave_idx_type c, double val)
61 : MSparse<double> (r, c, val) { }
62
64
65 SparseMatrix (const SparseMatrix& a, const dim_vector& dv)
66 : MSparse<double> (a, dv) { }
67
69
71
72 explicit OCTAVE_API SparseMatrix (const SparseBoolMatrix& a);
73
74 explicit SparseMatrix (const Matrix& a) : MSparse<double> (a) { }
75
76 explicit SparseMatrix (const NDArray& a) : MSparse<double> (a) { }
77
78 SparseMatrix (const Array<double>& a, const octave::idx_vector& r,
79 const octave::idx_vector& c, octave_idx_type nr = -1,
80 octave_idx_type nc = -1, bool sum_terms = true,
81 octave_idx_type nzm = -1)
82 : MSparse<double> (a, r, c, nr, nc, sum_terms, nzm) { }
83
84 explicit OCTAVE_API SparseMatrix (const DiagMatrix& a);
85
86 explicit SparseMatrix (const PermMatrix& a) : MSparse<double> (a) { }
87
90
92 {
94 return *this;
95 }
96
97 ~SparseMatrix () = default;
98
99 OCTAVE_API bool operator == (const SparseMatrix& a) const;
100 OCTAVE_API bool operator != (const SparseMatrix& a) const;
101
102 OCTAVE_API bool issymmetric () const;
103
104 OCTAVE_API SparseMatrix max (int dim = -1) const;
106 OCTAVE_API SparseMatrix min (int dim = -1) const;
108
109 // destructive insert/delete/reorder operations
110
113
115 insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
116
121
124
126 {
128 }
129 SparseMatrix hermitian () const { return transpose (); }
130
131 // extract row or column i.
132
134
136
137private:
139 dinverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
140 const bool force = false, const bool calccond = true) const;
141
143 tinverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
144 const bool force = false, const bool calccond = true) const;
145
146public:
148 OCTAVE_API SparseMatrix inverse (MatrixType& mattype) const;
150 inverse (MatrixType& mattype, octave_idx_type& info) const;
152 inverse (MatrixType& mattype, octave_idx_type& info, double& rcond,
153 bool force = false, bool calc_cond = true) const;
154
155 OCTAVE_API DET determinant () const;
157 OCTAVE_API DET determinant (octave_idx_type& info, double& rcond,
158 bool calc_cond = true) const;
159
160private:
161 // Diagonal matrix solvers
163 dsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
164 double& rcond, solve_singularity_handler sing_handler,
165 bool calc_cond = false) const;
166
168 dsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
169 double& rcond, solve_singularity_handler sing_handler,
170 bool calc_cond = false) const;
171
173 dsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
174 double& rcond, solve_singularity_handler sing_handler,
175 bool calc_cond = false) const;
176
178 dsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
179 double& rcond, solve_singularity_handler sing_handler,
180 bool calc_cond = false) const;
181
182 // Upper triangular matrix solvers
184 utsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
185 double& rcond, solve_singularity_handler sing_handler,
186 bool calc_cond = false) const;
187
189 utsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
190 double& rcond, solve_singularity_handler sing_handler,
191 bool calc_cond = false) const;
192
194 utsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
195 double& rcond, solve_singularity_handler sing_handler,
196 bool calc_cond = false) const;
197
199 utsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
200 double& rcond, solve_singularity_handler sing_handler,
201 bool calc_cond = false) const;
202
203 // Lower triangular matrix solvers
205 ltsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
206 double& rcond, solve_singularity_handler sing_handler,
207 bool calc_cond = false) const;
208
210 ltsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
211 double& rcond, solve_singularity_handler sing_handler,
212 bool calc_cond = false) const;
213
215 ltsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
216 double& rcond, solve_singularity_handler sing_handler,
217 bool calc_cond = false) const;
218
220 ltsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
221 double& rcond, solve_singularity_handler sing_handler,
222 bool calc_cond = false) const;
223
224 // Tridiagonal matrix solvers
226 trisolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
227 double& rcond, solve_singularity_handler sing_handler,
228 bool calc_cond = false) const;
229
231 trisolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
232 double& rcond, solve_singularity_handler sing_handler,
233 bool calc_cond = false) const;
234
236 trisolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
237 double& rcond, solve_singularity_handler sing_handler,
238 bool calc_cond = false) const;
239
241 trisolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
242 double& rcond, solve_singularity_handler sing_handler,
243 bool calc_cond = false) const;
244
245 // Banded matrix solvers (umfpack/cholesky)
247 bsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
248 double& rcond, solve_singularity_handler sing_handler,
249 bool calc_cond = false) const;
250
252 bsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
253 double& rcond, solve_singularity_handler sing_handler,
254 bool calc_cond = false) const;
255
257 bsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
258 double& rcond, solve_singularity_handler sing_handler,
259 bool calc_cond = false) const;
260
262 bsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
263 double& rcond, solve_singularity_handler sing_handler,
264 bool calc_cond = false) const;
265
266 // Full matrix solvers (umfpack/cholesky)
267 OCTAVE_API void *
268 factorize (octave_idx_type& err, double& rcond, Matrix& Control,
269 Matrix& Info, solve_singularity_handler sing_handler,
270 bool calc_cond = false) const;
271
273 fsolve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
274 double& rcond, solve_singularity_handler sing_handler,
275 bool calc_cond = false) const;
276
278 fsolve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
279 double& rcond, solve_singularity_handler sing_handler,
280 bool calc_cond = false) const;
281
283 fsolve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
284 double& rcond, solve_singularity_handler sing_handler,
285 bool calc_cond = false) const;
286
288 fsolve (MatrixType& typ, const SparseComplexMatrix& b, octave_idx_type& info,
289 double& rcond, solve_singularity_handler sing_handler,
290 bool calc_cond = false) const;
291
292public:
293 // Generic interface to solver with no probing of type
294 OCTAVE_API Matrix solve (MatrixType& typ, const Matrix& b) const;
296 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info) const;
298 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
299 double& rcond) const;
301 solve (MatrixType& typ, const Matrix& b, octave_idx_type& info,
302 double& rcond, solve_singularity_handler sing_handler,
303 bool singular_fallback = true) const;
304
306 solve (MatrixType& typ, const ComplexMatrix& b) const;
308 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info) const;
310 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
311 double& rcond) const;
313 solve (MatrixType& typ, const ComplexMatrix& b, octave_idx_type& info,
314 double& rcond, solve_singularity_handler sing_handler,
315 bool singular_fallback = true) const;
316
317 OCTAVE_API SparseMatrix solve (MatrixType& typ, const SparseMatrix& b) const;
319 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info) const;
321 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
322 double& rcond) const;
324 solve (MatrixType& typ, const SparseMatrix& b, octave_idx_type& info,
325 double& rcond, solve_singularity_handler sing_handler,
326 bool singular_fallback = true) const;
327
329 solve (MatrixType& typ, const SparseComplexMatrix& b) const;
331 solve (MatrixType& typ, const SparseComplexMatrix& b,
332 octave_idx_type& info) const;
334 solve (MatrixType& typ, const SparseComplexMatrix& b,
335 octave_idx_type& info, double& rcond) const;
337 solve (MatrixType& typ, const SparseComplexMatrix& b,
338 octave_idx_type& info, double& rcond,
339 solve_singularity_handler sing_handler,
340 bool singular_fallabck = true) const;
341
342 OCTAVE_API ColumnVector solve (MatrixType& typ, const ColumnVector& b) const;
344 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info) const;
346 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info,
347 double& rcond) const;
349 solve (MatrixType& typ, const ColumnVector& b, octave_idx_type& info,
350 double& rcond, solve_singularity_handler sing_handler) const;
351
353 solve (MatrixType& typ, const ComplexColumnVector& b) const;
355 solve (MatrixType& typ, const ComplexColumnVector& b,
356 octave_idx_type& info) const;
358 solve (MatrixType& typ, const ComplexColumnVector& b,
359 octave_idx_type& info, double& rcond) const;
361 solve (MatrixType& typ, const ComplexColumnVector& b,
362 octave_idx_type& info, double& rcond,
363 solve_singularity_handler sing_handler) const;
364
365 // Generic interface to solver with probing of type
366 OCTAVE_API Matrix solve (const Matrix& b) const;
367 OCTAVE_API Matrix solve (const Matrix& b, octave_idx_type& info) const;
369 solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
371 solve (const Matrix& b, octave_idx_type& info, double& rcond,
372 solve_singularity_handler sing_handler) const;
373
376 solve (const ComplexMatrix& b, octave_idx_type& info) const;
378 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const;
380 solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
381 solve_singularity_handler sing_handler) const;
382
383 OCTAVE_API SparseMatrix solve (const SparseMatrix& b) const;
385 solve (const SparseMatrix& b, octave_idx_type& info) const;
387 solve (const SparseMatrix& b, octave_idx_type& info, double& rcond) const;
389 solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
390 solve_singularity_handler sing_handler) const;
391
394 solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
397 double& rcond) const;
400 double& rcond, solve_singularity_handler sing_handler) const;
401
402 OCTAVE_API ColumnVector solve (const ColumnVector& b) const;
404 solve (const ColumnVector& b, octave_idx_type& info) const;
406 solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
408 solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
409 solve_singularity_handler sing_handler) const;
410
413 solve (const ComplexColumnVector& b, octave_idx_type& info) const;
416 double& rcond) const;
419 double& rcond, solve_singularity_handler sing_handler) const;
420
421 // other operations
422
423 OCTAVE_API bool any_element_is_negative (bool = false) const;
424 OCTAVE_API bool any_element_is_nan () const;
427 OCTAVE_API bool all_elements_are_zero () const;
429 OCTAVE_API bool all_integers (double& max_val, double& min_val) const;
430 OCTAVE_API bool too_large_for_float () const;
431
433
434 OCTAVE_API SparseBoolMatrix all (int dim = -1) const;
435 OCTAVE_API SparseBoolMatrix any (int dim = -1) const;
436
437 OCTAVE_API SparseMatrix cumprod (int dim = -1) const;
438 OCTAVE_API SparseMatrix cumsum (int dim = -1) const;
439 OCTAVE_API SparseMatrix prod (int dim = -1) const;
440 OCTAVE_API SparseMatrix sum (int dim = -1) const;
441 OCTAVE_API SparseMatrix sumsq (int dim = -1) const;
442 OCTAVE_API SparseMatrix abs () const;
443
445
447
449
450 OCTAVE_API SparseMatrix reshape (const dim_vector& new_dims) const;
451
453 permute (const Array<octave_idx_type>& vec, bool inv = false) const;
454
456
457 // i/o
458
459 friend OCTAVE_API std::ostream& operator << (std::ostream& os,
460 const SparseMatrix& a);
461 friend OCTAVE_API std::istream& operator >> (std::istream& is,
462 SparseMatrix& a);
463
464};
465
466// Publish externally used friend functions.
467
470
471// Other operators.
472
474 const SparseMatrix& b);
475extern OCTAVE_API Matrix operator * (const Matrix& a,
476 const SparseMatrix& b);
477extern OCTAVE_API Matrix mul_trans (const Matrix& a,
478 const SparseMatrix& b);
480 const Matrix& b);
481extern OCTAVE_API Matrix trans_mul (const SparseMatrix& a,
482 const Matrix& b);
483
485 const SparseMatrix&);
487 const DiagMatrix&);
488
490 const SparseMatrix&);
492 const DiagMatrix&);
494 const SparseMatrix&);
496 const DiagMatrix&);
497
499 const SparseMatrix&);
501 const PermMatrix&);
502
503extern OCTAVE_API SparseMatrix min (double d, const SparseMatrix& m);
504extern OCTAVE_API SparseMatrix min (const SparseMatrix& m, double d);
505extern OCTAVE_API SparseMatrix min (const SparseMatrix& a,
506 const SparseMatrix& b);
507
508extern OCTAVE_API SparseMatrix max (double d, const SparseMatrix& m);
509extern OCTAVE_API SparseMatrix max (const SparseMatrix& m, double d);
510extern OCTAVE_API SparseMatrix max (const SparseMatrix& a,
511 const SparseMatrix& b);
512
515
518
521
523
524#endif
#define SPARSE_FORWARD_DEFS(B, R, F, T)
Definition MSparse.h:192
#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)
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
MSparse< T > transpose() const
Definition MSparse.h:96
MSparse< T > & operator=(const MSparse< T > &a)
Definition MSparse.h:78
SparseMatrix & insert(const SparseMatrix &a, octave_idx_type r, octave_idx_type c)
Definition dSparse.cc:168
SparseMatrix squeeze() const
Definition dSparse.cc:7433
bool issymmetric() const
Definition dSparse.cc:128
bool any_element_not_one_or_zero() const
Definition dSparse.cc:7202
SparseMatrix reshape(const dim_vector &new_dims) const
Definition dSparse.cc:7439
SparseMatrix sum(int dim=-1) const
Definition dSparse.cc:7355
SparseBoolMatrix any(int dim=-1) const
Definition dSparse.cc:7325
bool any_element_is_inf_or_nan() const
Definition dSparse.cc:7187
bool all_integers(double &max_val, double &min_val) const
Definition dSparse.cc:7249
bool any_element_is_nan() const
Definition dSparse.cc:7172
bool too_large_for_float() const
Definition dSparse.cc:7277
bool operator==(const SparseMatrix &a) const
Definition dSparse.cc:98
SparseMatrix transpose() const
Definition dSparse.h:125
SparseMatrix(const Sparse< double > &a)
Definition dSparse.h:70
DET determinant() const
Definition dSparse.cc:1017
SparseMatrix abs() const
Definition dSparse.cc:7379
SparseMatrix inverse() const
Definition dSparse.cc:608
bool operator!=(const SparseMatrix &a) const
Definition dSparse.cc:122
RowVector row(octave_idx_type i) const
Definition dSparse.cc:500
ComplexMatrix solve(const ComplexMatrix &b) const
SparseMatrix cumprod(int dim=-1) const
Definition dSparse.cc:7331
friend SparseMatrix real(const SparseComplexMatrix &a)
Definition dSparse.cc:551
SparseMatrix diag(octave_idx_type k=0) const
Definition dSparse.cc:7392
SparseMatrix prod(int dim=-1) const
Definition dSparse.cc:7343
SparseMatrix(const SparseMatrix &a, const dim_vector &dv)
Definition dSparse.h:65
bool all_elements_are_zero() const
Definition dSparse.cc:7217
~SparseMatrix()=default
SparseBoolMatrix all(int dim=-1) const
Definition dSparse.cc:7319
SparseMatrix(const SparseMatrix &a)
Definition dSparse.h:63
SparseMatrix min(int dim=-1) const
Definition dSparse.cc:334
SparseMatrix permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition dSparse.cc:7445
SparseMatrix cumsum(int dim=-1) const
Definition dSparse.cc:7337
SparseMatrix(octave_idx_type r, octave_idx_type c)
Definition dSparse.h:54
ColumnVector column(octave_idx_type i) const
Definition dSparse.cc:519
SparseMatrix concat(const SparseMatrix &rb, const Array< octave_idx_type > &ra_idx)
Definition dSparse.cc:531
SparseMatrix(const MSparse< double > &a)
Definition dSparse.h:68
void(* solve_singularity_handler)(double rcond)
Definition dSparse.h:50
SparseMatrix ipermute(const Array< octave_idx_type > &vec) const
Definition dSparse.cc:7451
Matrix dense_matrix_type
Definition dSparse.h:48
SparseBoolMatrix operator!() const
Definition dSparse.cc:7283
SparseMatrix sumsq(int dim=-1) const
Definition dSparse.cc:7361
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:78
SparseMatrix hermitian() const
Definition dSparse.h:129
SparseMatrix(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition dSparse.h:88
SparseMatrix(const NDArray &a)
Definition dSparse.h:76
bool all_elements_are_int_or_inf_or_nan() const
Definition dSparse.cc:7229
Matrix solve(MatrixType &typ, const Matrix &b) const
Definition dSparse.cc:6661
SparseMatrix(const PermMatrix &a)
Definition dSparse.h:86
SparseMatrix(const Matrix &a)
Definition dSparse.h:74
friend SparseMatrix imag(const SparseComplexMatrix &a)
Definition dSparse.cc:572
friend std::ostream & operator<<(std::ostream &os, const SparseMatrix &a)
Definition dSparse.cc:7404
SparseMatrix(const dim_vector &dv, octave_idx_type nz=0)
Definition dSparse.h:57
SparseMatrix & operator=(const SparseMatrix &a)
Definition dSparse.h:91
SparseMatrix max(int dim=-1) const
Definition dSparse.cc:183
bool any_element_is_negative(bool=false) const
Definition dSparse.cc:7151
Matrix matrix_value() const
Definition dSparse.cc:7398
friend std::istream & operator>>(std::istream &is, SparseMatrix &a)
Definition dSparse.cc:7425
SparseMatrix(octave_idx_type r, octave_idx_type c, double val)
Definition dSparse.h:60
Sparse< T, Alloc > index(const octave::idx_vector &i, bool resize_ok=false) const
Definition Sparse.cc:1433
Definition DET.h:38
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
SparseMatrix operator+(const DiagMatrix &, const SparseMatrix &)
Definition dSparse.cc:7503
Matrix trans_mul(const SparseMatrix &a, const Matrix &b)
Definition dSparse.cc:7483
SparseMatrix operator*(const SparseMatrix &a, const SparseMatrix &b)
Definition dSparse.cc:7459
SparseMatrix real(const SparseComplexMatrix &a)
Definition dSparse.cc:551
SparseMatrix min(double d, const SparseMatrix &m)
Definition dSparse.cc:7547
SparseMatrix operator-(const DiagMatrix &, const SparseMatrix &)
Definition dSparse.cc:7509
SparseMatrix max(double d, const SparseMatrix &m)
Definition dSparse.cc:7697
Matrix mul_trans(const Matrix &a, const SparseMatrix &b)
Definition dSparse.cc:7471
SparseMatrix imag(const SparseComplexMatrix &a)
Definition dSparse.cc:572
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition main.in.cc:55
const octave_base_value const Array< octave_idx_type > & ra_idx