00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if !defined (octave_dSparse_h)
00025 #define octave_dSparse_h 1
00026
00027 #include "dMatrix.h"
00028 #include "dNDArray.h"
00029 #include "CMatrix.h"
00030 #include "dColVector.h"
00031 #include "CColVector.h"
00032
00033 #include "DET.h"
00034 #include "MSparse.h"
00035 #include "MSparse-defs.h"
00036 #include "Sparse-op-defs.h"
00037 #include "MatrixType.h"
00038
00039 class PermMatrix;
00040 class DiagMatrix;
00041 class SparseComplexMatrix;
00042 class SparseBoolMatrix;
00043
00044 class
00045 OCTAVE_API
00046 SparseMatrix : public MSparse<double>
00047 {
00048 public:
00049
00050 typedef void (*solve_singularity_handler) (double rcond);
00051
00052 SparseMatrix (void) : MSparse<double> () { }
00053
00054 SparseMatrix (octave_idx_type r, octave_idx_type c) : MSparse<double> (r, c) { }
00055
00056 SparseMatrix (const dim_vector& dv, octave_idx_type nz = 0) :
00057 MSparse<double> (dv, nz) { }
00058
00059 explicit SparseMatrix (octave_idx_type r, octave_idx_type c, double val)
00060 : MSparse<double> (r, c, val) { }
00061
00062 SparseMatrix (const SparseMatrix& a) : MSparse<double> (a) { }
00063
00064 SparseMatrix (const SparseMatrix& a, const dim_vector& dv)
00065 : MSparse<double> (a, dv) { }
00066
00067 SparseMatrix (const MSparse<double>& a) : MSparse<double> (a) { }
00068
00069 explicit SparseMatrix (const SparseBoolMatrix& a);
00070
00071 explicit SparseMatrix (const Matrix& a) : MSparse<double> (a) { }
00072
00073 explicit SparseMatrix (const NDArray& a) : MSparse<double> (a) { }
00074
00075 explicit SparseMatrix (const Array<double> a, const Array<octave_idx_type>& r,
00076 const Array<octave_idx_type>& c, octave_idx_type nr = -1,
00077 octave_idx_type nc = -1, bool sum_terms = true)
00078 : MSparse<double> (a, r, c, nr, nc, sum_terms) { }
00079
00080 explicit SparseMatrix (const Array<double> a, const Array<double>& r,
00081 const Array<double>& c, octave_idx_type nr = -1,
00082 octave_idx_type nc = -1, bool sum_terms = true)
00083 : MSparse<double> (a, r, c, nr, nc, sum_terms) { }
00084
00085 explicit SparseMatrix (const DiagMatrix& a);
00086
00087 explicit SparseMatrix (const PermMatrix& a);
00088
00089 SparseMatrix (octave_idx_type r, octave_idx_type c, octave_idx_type num_nz) : MSparse<double> (r, c, num_nz) { }
00090
00091 SparseMatrix& operator = (const SparseMatrix& a)
00092 {
00093 MSparse<double>::operator = (a);
00094 return *this;
00095 }
00096
00097 bool operator == (const SparseMatrix& a) const;
00098 bool operator != (const SparseMatrix& a) const;
00099
00100 bool is_symmetric (void) const;
00101
00102 SparseMatrix max (int dim = 0) const;
00103 SparseMatrix max (Array2<octave_idx_type>& index, int dim = 0) const;
00104 SparseMatrix min (int dim = 0) const;
00105 SparseMatrix min (Array2<octave_idx_type>& index, int dim = 0) const;
00106
00107
00108
00109 SparseMatrix& insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c);
00110
00111 SparseMatrix& insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
00112
00113 SparseMatrix concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx);
00114 SparseComplexMatrix concat (const SparseComplexMatrix& rb,
00115 const Array<octave_idx_type>& ra_idx);
00116
00117 friend OCTAVE_API SparseMatrix real (const SparseComplexMatrix& a);
00118 friend OCTAVE_API SparseMatrix imag (const SparseComplexMatrix& a);
00119
00120 friend OCTAVE_API SparseMatrix atan2 (const double& x, const SparseMatrix& y);
00121 friend OCTAVE_API SparseMatrix atan2 (const SparseMatrix& x, const double& y);
00122 friend OCTAVE_API SparseMatrix atan2 (const SparseMatrix& x, const SparseMatrix& y);
00123
00124 SparseMatrix transpose (void) const
00125 {
00126 return MSparse<double>::transpose ();
00127 }
00128 SparseMatrix hermitian (void) const { return transpose (); }
00129
00130
00131
00132 RowVector row (octave_idx_type i) const;
00133
00134 ColumnVector column (octave_idx_type i) const;
00135
00136 private:
00137 SparseMatrix dinverse (MatrixType &mattyp, octave_idx_type& info,
00138 double& rcond, const bool force = false,
00139 const bool calccond = true) const;
00140
00141 SparseMatrix tinverse (MatrixType &mattyp, octave_idx_type& info,
00142 double& rcond, const bool force = false,
00143 const bool calccond = true) const;
00144
00145 public:
00146 SparseMatrix inverse (void) const;
00147 SparseMatrix inverse (MatrixType& mattype) const;
00148 SparseMatrix inverse (MatrixType& mattype, octave_idx_type& info) const;
00149 SparseMatrix inverse (MatrixType& mattype, octave_idx_type& info,
00150 double& rcond, int force = 0, int calc_cond = 1) const;
00151
00152 DET determinant (void) const;
00153 DET determinant (octave_idx_type& info) const;
00154 DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const;
00155
00156 private:
00157
00158 Matrix dsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00159 double& rcond, solve_singularity_handler sing_handler,
00160 bool calc_cond = false) const;
00161
00162 ComplexMatrix dsolve (MatrixType &typ, const ComplexMatrix& b,
00163 octave_idx_type& info, double& rcond,
00164 solve_singularity_handler sing_handler,
00165 bool calc_cond = false) const;
00166
00167 SparseMatrix dsolve (MatrixType &typ, const SparseMatrix& b,
00168 octave_idx_type& info, double& rcond,
00169 solve_singularity_handler sing_handler,
00170 bool calc_cond = false) const;
00171
00172 SparseComplexMatrix dsolve (MatrixType &typ, const SparseComplexMatrix& b,
00173 octave_idx_type& info, double& rcond,
00174 solve_singularity_handler sing_handler,
00175 bool calc_cond = false) const;
00176
00177
00178 Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00179 double& rcond, solve_singularity_handler sing_handler,
00180 bool calc_cond = false) const;
00181
00182 ComplexMatrix utsolve (MatrixType &typ, const ComplexMatrix& b,
00183 octave_idx_type& info, double& rcond,
00184 solve_singularity_handler sing_handler,
00185 bool calc_cond = false) const;
00186
00187 SparseMatrix utsolve (MatrixType &typ, const SparseMatrix& b,
00188 octave_idx_type& info, double& rcond,
00189 solve_singularity_handler sing_handler,
00190 bool calc_cond = false) const;
00191
00192 SparseComplexMatrix utsolve (MatrixType &typ, const SparseComplexMatrix& b,
00193 octave_idx_type& info, double& rcond,
00194 solve_singularity_handler sing_handler,
00195 bool calc_cond = false) const;
00196
00197
00198 Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00199 double& rcond, solve_singularity_handler sing_handler,
00200 bool calc_cond = false) const;
00201
00202 ComplexMatrix ltsolve (MatrixType &typ, const ComplexMatrix& b,
00203 octave_idx_type& info, double& rcond,
00204 solve_singularity_handler sing_handler,
00205 bool calc_cond = false) const;
00206
00207 SparseMatrix ltsolve (MatrixType &typ, const SparseMatrix& b,
00208 octave_idx_type& info, double& rcond,
00209 solve_singularity_handler sing_handler,
00210 bool calc_cond = false) const;
00211
00212 SparseComplexMatrix ltsolve (MatrixType &typ, const SparseComplexMatrix& b,
00213 octave_idx_type& info, double& rcond,
00214 solve_singularity_handler sing_handler,
00215 bool calc_cond = false) const;
00216
00217
00218 Matrix trisolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00219 double& rcond, solve_singularity_handler sing_handler,
00220 bool calc_cond = false) const;
00221
00222 ComplexMatrix trisolve (MatrixType &typ, const ComplexMatrix& b,
00223 octave_idx_type& info, double& rcond,
00224 solve_singularity_handler sing_handler,
00225 bool calc_cond = false) const;
00226
00227 SparseMatrix trisolve (MatrixType &typ, const SparseMatrix& b,
00228 octave_idx_type& info, double& rcond,
00229 solve_singularity_handler sing_handler,
00230 bool calc_cond = false) const;
00231
00232 SparseComplexMatrix trisolve (MatrixType &typ, const SparseComplexMatrix& b,
00233 octave_idx_type& info, double& rcond,
00234 solve_singularity_handler sing_handler,
00235 bool calc_cond = false) const;
00236
00237
00238 Matrix bsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00239 double& rcond, solve_singularity_handler sing_handler,
00240 bool calc_cond = false) const;
00241
00242 ComplexMatrix bsolve (MatrixType &typ, const ComplexMatrix& b,
00243 octave_idx_type& info, double& rcond,
00244 solve_singularity_handler sing_handler,
00245 bool calc_cond = false) const;
00246
00247 SparseMatrix bsolve (MatrixType &typ, const SparseMatrix& b,
00248 octave_idx_type& info, double& rcond,
00249 solve_singularity_handler sing_handler,
00250 bool calc_cond = false) const;
00251
00252 SparseComplexMatrix bsolve (MatrixType &typ, const SparseComplexMatrix& b,
00253 octave_idx_type& info, double& rcond,
00254 solve_singularity_handler sing_handler,
00255 bool calc_cond = false) const;
00256
00257
00258 void * factorize (octave_idx_type& err, double &rcond, Matrix &Control,
00259 Matrix &Info, solve_singularity_handler sing_handler,
00260 bool calc_cond = false) const;
00261
00262 Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00263 double& rcond, solve_singularity_handler sing_handler,
00264 bool calc_cond = false) const;
00265
00266 ComplexMatrix fsolve (MatrixType &typ, const ComplexMatrix& b,
00267 octave_idx_type& info, double& rcond,
00268 solve_singularity_handler sing_handler,
00269 bool calc_cond = false) const;
00270
00271 SparseMatrix fsolve (MatrixType &typ, const SparseMatrix& b,
00272 octave_idx_type& info, double& rcond,
00273 solve_singularity_handler sing_handler,
00274 bool calc_cond = false) const;
00275
00276 SparseComplexMatrix fsolve (MatrixType &typ, const SparseComplexMatrix& b,
00277 octave_idx_type& info, double& rcond,
00278 solve_singularity_handler sing_handler,
00279 bool calc_cond = false) const;
00280
00281 public:
00282
00283 Matrix solve (MatrixType &typ, const Matrix& b) const;
00284 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
00285 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00286 double& rcond) const;
00287 Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00288 double& rcond, solve_singularity_handler sing_handler,
00289 bool singular_fallback = true) const;
00290
00291 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
00292 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00293 octave_idx_type& info) const;
00294 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00295 octave_idx_type& info, double& rcond) const;
00296 ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00297 octave_idx_type& info, double& rcond,
00298 solve_singularity_handler sing_handler,
00299 bool singular_fallback = true) const;
00300
00301 SparseMatrix solve (MatrixType &typ, const SparseMatrix& b) const;
00302 SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00303 octave_idx_type& info) const;
00304 SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00305 octave_idx_type& info, double& rcond) const;
00306 SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00307 octave_idx_type& info, double& rcond,
00308 solve_singularity_handler sing_handler,
00309 bool singular_fallback = true) const;
00310
00311 SparseComplexMatrix solve (MatrixType &typ,
00312 const SparseComplexMatrix& b) const;
00313 SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00314 octave_idx_type& info) const;
00315 SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00316 octave_idx_type& info, double& rcond) const;
00317 SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00318 octave_idx_type& info, double& rcond,
00319 solve_singularity_handler sing_handler,
00320 bool singular_fallabck = true) const;
00321
00322 ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
00323 ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00324 octave_idx_type& info) const;
00325 ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00326 octave_idx_type& info, double& rcond) const;
00327 ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00328 octave_idx_type& info, double& rcond,
00329 solve_singularity_handler sing_handler) const;
00330
00331 ComplexColumnVector solve (MatrixType &typ,
00332 const ComplexColumnVector& b) const;
00333 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00334 octave_idx_type& info) const;
00335 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00336 octave_idx_type& info, double& rcond) const;
00337 ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00338 octave_idx_type& info, double& rcond,
00339 solve_singularity_handler sing_handler) const;
00340
00341
00342 Matrix solve (const Matrix& b) const;
00343 Matrix solve (const Matrix& b, octave_idx_type& info) const;
00344 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
00345 Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond,
00346 solve_singularity_handler sing_handler) const;
00347
00348 ComplexMatrix solve (const ComplexMatrix& b) const;
00349 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
00350 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info,
00351 double& rcond) const;
00352 ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
00353 solve_singularity_handler sing_handler) const;
00354
00355 SparseMatrix solve (const SparseMatrix& b) const;
00356 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info) const;
00357 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info,
00358 double& rcond) const;
00359 SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
00360 solve_singularity_handler sing_handler) const;
00361
00362 SparseComplexMatrix solve (const SparseComplexMatrix& b) const;
00363 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
00364 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info,
00365 double& rcond) const;
00366 SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info,
00367 double& rcond,
00368 solve_singularity_handler sing_handler) const;
00369
00370 ColumnVector solve (const ColumnVector& b) const;
00371 ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
00372 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
00373 ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
00374 solve_singularity_handler sing_handler) const;
00375
00376 ComplexColumnVector solve (const ComplexColumnVector& b) const;
00377 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const;
00378 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00379 double& rcond) const;
00380 ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00381 double& rcond,
00382 solve_singularity_handler sing_handler) const;
00383
00384
00385
00386 bool any_element_is_negative (bool = false) const;
00387 bool any_element_is_nan (void) const;
00388 bool any_element_is_inf_or_nan (void) const;
00389 bool all_elements_are_zero (void) const;
00390 bool all_elements_are_int_or_inf_or_nan (void) const;
00391 bool all_integers (double& max_val, double& min_val) const;
00392 bool too_large_for_float (void) const;
00393
00394 SparseBoolMatrix operator ! (void) const;
00395
00396 SparseBoolMatrix all (int dim = -1) const;
00397 SparseBoolMatrix any (int dim = -1) const;
00398
00399 SparseMatrix cumprod (int dim = -1) const;
00400 SparseMatrix cumsum (int dim = -1) const;
00401 SparseMatrix prod (int dim = -1) const;
00402 SparseMatrix sum (int dim = -1) const;
00403 SparseMatrix sumsq (int dim = -1) const;
00404 SparseMatrix abs (void) const;
00405
00406 SparseMatrix diag (octave_idx_type k = 0) const;
00407
00408 Matrix matrix_value (void) const;
00409
00410 SparseMatrix squeeze (void) const;
00411
00412 SparseMatrix index (idx_vector& i, int resize_ok) const;
00413
00414 SparseMatrix index (idx_vector& i, idx_vector& j, int resize_ok) const;
00415
00416 SparseMatrix index (Array<idx_vector>& ra_idx, int resize_ok) const;
00417
00418 SparseMatrix reshape (const dim_vector& new_dims) const;
00419
00420 SparseMatrix permute (const Array<octave_idx_type>& vec, bool inv = false) const;
00421
00422 SparseMatrix ipermute (const Array<octave_idx_type>& vec) const;
00423
00424
00425
00426 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const SparseMatrix& a);
00427 friend OCTAVE_API std::istream& operator >> (std::istream& is, SparseMatrix& a);
00428
00429 typedef double (*dmapper) (double);
00430 typedef Complex (*cmapper) (const Complex&);
00431 typedef bool (*bmapper) (double);
00432 SparseMatrix map (dmapper fcn) const;
00433 SparseComplexMatrix map (cmapper fcn) const;
00434 SparseBoolMatrix map (bmapper fcn) const;
00435 };
00436
00437
00438
00439 extern OCTAVE_API SparseMatrix real (const SparseComplexMatrix& a);
00440 extern OCTAVE_API SparseMatrix imag (const SparseComplexMatrix& a);
00441
00442
00443
00444 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix& a,
00445 const SparseMatrix& b);
00446 extern OCTAVE_API Matrix operator * (const Matrix& a,
00447 const SparseMatrix& b);
00448 extern OCTAVE_API Matrix mul_trans (const Matrix& a,
00449 const SparseMatrix& b);
00450 extern OCTAVE_API Matrix operator * (const SparseMatrix& a,
00451 const Matrix& b);
00452 extern OCTAVE_API Matrix trans_mul (const SparseMatrix& a,
00453 const Matrix& b);
00454
00455 extern OCTAVE_API SparseMatrix operator * (const DiagMatrix&, const SparseMatrix&);
00456 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix&, const DiagMatrix&);
00457
00458 extern OCTAVE_API SparseMatrix operator + (const DiagMatrix&, const SparseMatrix&);
00459 extern OCTAVE_API SparseMatrix operator + (const SparseMatrix&, const DiagMatrix&);
00460 extern OCTAVE_API SparseMatrix operator - (const DiagMatrix&, const SparseMatrix&);
00461 extern OCTAVE_API SparseMatrix operator - (const SparseMatrix&, const DiagMatrix&);
00462
00463 extern OCTAVE_API SparseMatrix operator * (const PermMatrix&, const SparseMatrix&);
00464 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix&, const PermMatrix&);
00465
00466 extern OCTAVE_API SparseMatrix min (double d, const SparseMatrix& m);
00467 extern OCTAVE_API SparseMatrix min (const SparseMatrix& m, double d);
00468 extern OCTAVE_API SparseMatrix min (const SparseMatrix& a, const SparseMatrix& b);
00469
00470 extern OCTAVE_API SparseMatrix max (double d, const SparseMatrix& m);
00471 extern OCTAVE_API SparseMatrix max (const SparseMatrix& m, double d);
00472 extern OCTAVE_API SparseMatrix max (const SparseMatrix& a, const SparseMatrix& b);
00473
00474 SPARSE_SMS_CMP_OP_DECLS (SparseMatrix, double, OCTAVE_API)
00475 SPARSE_SMS_BOOL_OP_DECLS (SparseMatrix, double, OCTAVE_API)
00476
00477 SPARSE_SSM_CMP_OP_DECLS (double, SparseMatrix, OCTAVE_API)
00478 SPARSE_SSM_BOOL_OP_DECLS (double, SparseMatrix, OCTAVE_API)
00479
00480 SPARSE_SMSM_CMP_OP_DECLS (SparseMatrix, SparseMatrix, OCTAVE_API)
00481 SPARSE_SMSM_BOOL_OP_DECLS (SparseMatrix, SparseMatrix, OCTAVE_API)
00482
00483 SPARSE_FORWARD_DEFS (MSparse, SparseMatrix, Matrix, double)
00484
00485 #ifdef IDX_TYPE_LONG
00486 #define UMFPACK_DNAME(name) umfpack_dl_ ## name
00487 #else
00488 #define UMFPACK_DNAME(name) umfpack_di_ ## name
00489 #endif
00490
00491 #endif
00492
00493
00494
00495
00496
00497