dSparse.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2004-2012 David Bateman
00004 Copyright (C) 1998-2004 Andy Adler
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
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   SparseMatrix (const Sparse<double>& a) : MSparse<double> (a) { }
00070 
00071   explicit SparseMatrix (const SparseBoolMatrix& a);
00072 
00073   explicit SparseMatrix (const Matrix& a) : MSparse<double> (a) { }
00074 
00075   explicit SparseMatrix (const NDArray& a) : MSparse<double> (a) { }
00076 
00077   SparseMatrix (const Array<double>& a, const idx_vector& r,
00078                 const idx_vector& c, octave_idx_type nr = -1,
00079                 octave_idx_type nc = -1, bool sum_terms = true,
00080                 octave_idx_type nzm = -1)
00081     : MSparse<double> (a, r, c, nr, nc, sum_terms, nzm) { }
00082 
00083   explicit SparseMatrix (const DiagMatrix& a);
00084 
00085   explicit SparseMatrix (const PermMatrix& a) : MSparse<double>(a) { }
00086 
00087   SparseMatrix (octave_idx_type r, octave_idx_type c, octave_idx_type num_nz) : MSparse<double> (r, c, num_nz) { }
00088 
00089   SparseMatrix& operator = (const SparseMatrix& a)
00090     {
00091       MSparse<double>::operator = (a);
00092       return *this;
00093     }
00094 
00095   bool operator == (const SparseMatrix& a) const;
00096   bool operator != (const SparseMatrix& a) const;
00097 
00098   bool is_symmetric (void) const;
00099 
00100   SparseMatrix max (int dim = -1) const;
00101   SparseMatrix max (Array<octave_idx_type>& index, int dim = -1) const;
00102   SparseMatrix min (int dim = -1) const;
00103   SparseMatrix min (Array<octave_idx_type>& index, int dim = -1) const;
00104 
00105   // destructive insert/delete/reorder operations
00106 
00107   SparseMatrix& insert (const SparseMatrix& a, octave_idx_type r, octave_idx_type c);
00108 
00109   SparseMatrix& insert (const SparseMatrix& a, const Array<octave_idx_type>& indx);
00110 
00111   SparseMatrix concat (const SparseMatrix& rb, const Array<octave_idx_type>& ra_idx);
00112   SparseComplexMatrix concat (const SparseComplexMatrix& rb,
00113                               const Array<octave_idx_type>& ra_idx);
00114 
00115   friend OCTAVE_API SparseMatrix real (const SparseComplexMatrix& a);
00116   friend OCTAVE_API SparseMatrix imag (const SparseComplexMatrix& a);
00117 
00118   friend OCTAVE_API SparseMatrix atan2 (const double& x, const SparseMatrix& y);
00119   friend OCTAVE_API SparseMatrix atan2 (const SparseMatrix& x, const double& y);
00120   friend OCTAVE_API SparseMatrix atan2 (const SparseMatrix& x, const SparseMatrix& y);
00121 
00122   SparseMatrix transpose (void) const
00123     {
00124       return MSparse<double>::transpose ();
00125     }
00126   SparseMatrix hermitian (void) const { return transpose (); }
00127 
00128   // extract row or column i.
00129 
00130   RowVector row (octave_idx_type i) const;
00131 
00132   ColumnVector column (octave_idx_type i) const;
00133 
00134 private:
00135   SparseMatrix dinverse (MatrixType &mattyp, octave_idx_type& info,
00136                          double& rcond, const bool force = false,
00137                          const bool calccond = true) const;
00138 
00139   SparseMatrix tinverse (MatrixType &mattyp, octave_idx_type& info,
00140                          double& rcond, const bool force = false,
00141                          const bool calccond = true) const;
00142 
00143 public:
00144   SparseMatrix inverse (void) const;
00145   SparseMatrix inverse (MatrixType& mattype) const;
00146   SparseMatrix inverse (MatrixType& mattype, octave_idx_type& info) const;
00147   SparseMatrix inverse (MatrixType& mattype, octave_idx_type& info,
00148                         double& rcond, int force = 0, int calc_cond = 1) const;
00149 
00150   DET determinant (void) const;
00151   DET determinant (octave_idx_type& info) const;
00152   DET determinant (octave_idx_type& info, double& rcond, int calc_cond = 1) const;
00153 
00154 private:
00155   // Diagonal matrix solvers
00156   Matrix dsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00157                 double& rcond, solve_singularity_handler sing_handler,
00158                 bool calc_cond = false) const;
00159 
00160   ComplexMatrix dsolve (MatrixType &typ, const ComplexMatrix& b,
00161                 octave_idx_type& info, double& rcond,
00162                 solve_singularity_handler sing_handler,
00163                 bool calc_cond = false) const;
00164 
00165   SparseMatrix dsolve (MatrixType &typ, const SparseMatrix& b,
00166                 octave_idx_type& info, double& rcond,
00167                 solve_singularity_handler sing_handler,
00168                 bool calc_cond = false) const;
00169 
00170   SparseComplexMatrix dsolve (MatrixType &typ, const SparseComplexMatrix& b,
00171                 octave_idx_type& info, double& rcond,
00172                 solve_singularity_handler sing_handler,
00173                 bool calc_cond = false) const;
00174 
00175   // Upper triangular matrix solvers
00176   Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00177                 double& rcond, solve_singularity_handler sing_handler,
00178                 bool calc_cond = false) const;
00179 
00180   ComplexMatrix utsolve (MatrixType &typ, const ComplexMatrix& b,
00181                 octave_idx_type& info, double& rcond,
00182                 solve_singularity_handler sing_handler,
00183                 bool calc_cond = false) const;
00184 
00185   SparseMatrix utsolve (MatrixType &typ, const SparseMatrix& b,
00186                 octave_idx_type& info, double& rcond,
00187                 solve_singularity_handler sing_handler,
00188                 bool calc_cond = false) const;
00189 
00190   SparseComplexMatrix utsolve (MatrixType &typ, const SparseComplexMatrix& b,
00191                 octave_idx_type& info, double& rcond,
00192                 solve_singularity_handler sing_handler,
00193                 bool calc_cond = false) const;
00194 
00195   // Lower triangular matrix solvers
00196   Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00197                 double& rcond, solve_singularity_handler sing_handler,
00198                 bool calc_cond = false) const;
00199 
00200   ComplexMatrix ltsolve (MatrixType &typ, const ComplexMatrix& b,
00201                 octave_idx_type& info, double& rcond,
00202                 solve_singularity_handler sing_handler,
00203                 bool calc_cond = false) const;
00204 
00205   SparseMatrix ltsolve (MatrixType &typ, const SparseMatrix& b,
00206                 octave_idx_type& info, double& rcond,
00207                 solve_singularity_handler sing_handler,
00208                 bool calc_cond = false) const;
00209 
00210   SparseComplexMatrix ltsolve (MatrixType &typ, const SparseComplexMatrix& b,
00211                 octave_idx_type& info, double& rcond,
00212                 solve_singularity_handler sing_handler,
00213                 bool calc_cond = false) const;
00214 
00215   // Tridiagonal matrix solvers
00216   Matrix trisolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00217                 double& rcond, solve_singularity_handler sing_handler,
00218                 bool calc_cond = false) const;
00219 
00220   ComplexMatrix trisolve (MatrixType &typ, const ComplexMatrix& b,
00221                 octave_idx_type& info, double& rcond,
00222                 solve_singularity_handler sing_handler,
00223                 bool calc_cond = false) const;
00224 
00225   SparseMatrix trisolve (MatrixType &typ, const SparseMatrix& b,
00226                 octave_idx_type& info, double& rcond,
00227                 solve_singularity_handler sing_handler,
00228                 bool calc_cond = false) const;
00229 
00230   SparseComplexMatrix trisolve (MatrixType &typ, const SparseComplexMatrix& b,
00231                 octave_idx_type& info, double& rcond,
00232                 solve_singularity_handler sing_handler,
00233                 bool calc_cond = false) const;
00234 
00235   // Banded matrix solvers (umfpack/cholesky)
00236   Matrix bsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00237                 double& rcond, solve_singularity_handler sing_handler,
00238                 bool calc_cond = false) const;
00239 
00240   ComplexMatrix bsolve (MatrixType &typ, const ComplexMatrix& b,
00241                 octave_idx_type& info, double& rcond,
00242                 solve_singularity_handler sing_handler,
00243                 bool calc_cond = false) const;
00244 
00245   SparseMatrix bsolve (MatrixType &typ, const SparseMatrix& b,
00246                 octave_idx_type& info, double& rcond,
00247                 solve_singularity_handler sing_handler,
00248                 bool calc_cond = false) const;
00249 
00250   SparseComplexMatrix bsolve (MatrixType &typ, const SparseComplexMatrix& b,
00251                 octave_idx_type& info, double& rcond,
00252                 solve_singularity_handler sing_handler,
00253                 bool calc_cond = false) const;
00254 
00255   // Full matrix solvers (umfpack/cholesky)
00256   void * factorize (octave_idx_type& err, double &rcond, Matrix &Control,
00257                     Matrix &Info, solve_singularity_handler sing_handler,
00258                     bool calc_cond = false) const;
00259 
00260   Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00261                 double& rcond, solve_singularity_handler sing_handler,
00262                 bool calc_cond = false) const;
00263 
00264   ComplexMatrix fsolve (MatrixType &typ, const ComplexMatrix& b,
00265                 octave_idx_type& info, double& rcond,
00266                 solve_singularity_handler sing_handler,
00267                 bool calc_cond = false) const;
00268 
00269   SparseMatrix fsolve (MatrixType &typ, const SparseMatrix& b,
00270                 octave_idx_type& info, double& rcond,
00271                 solve_singularity_handler sing_handler,
00272                 bool calc_cond = false) const;
00273 
00274   SparseComplexMatrix fsolve (MatrixType &typ, const SparseComplexMatrix& b,
00275                 octave_idx_type& info, double& rcond,
00276                 solve_singularity_handler sing_handler,
00277                 bool calc_cond = false) const;
00278 
00279 public:
00280   // Generic interface to solver with no probing of type
00281   Matrix solve (MatrixType &typ, const Matrix& b) const;
00282   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
00283   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00284                 double& rcond) const;
00285   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00286                 double& rcond, solve_singularity_handler sing_handler,
00287                 bool singular_fallback = true) const;
00288 
00289   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
00290   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00291                        octave_idx_type& info) const;
00292   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00293                        octave_idx_type& info, double& rcond) const;
00294   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00295                        octave_idx_type& info, double& rcond,
00296                        solve_singularity_handler sing_handler,
00297                        bool singular_fallback = true) const;
00298 
00299   SparseMatrix solve (MatrixType &typ, const SparseMatrix& b) const;
00300   SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00301                       octave_idx_type& info) const;
00302   SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00303                       octave_idx_type& info, double& rcond) const;
00304   SparseMatrix solve (MatrixType &typ, const SparseMatrix& b,
00305                       octave_idx_type& info, double& rcond,
00306                       solve_singularity_handler sing_handler,
00307                       bool singular_fallback = true) const;
00308 
00309   SparseComplexMatrix solve (MatrixType &typ,
00310                              const SparseComplexMatrix& b) const;
00311   SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00312                              octave_idx_type& info) const;
00313   SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00314                              octave_idx_type& info, double& rcond) const;
00315   SparseComplexMatrix solve (MatrixType &typ, const SparseComplexMatrix& b,
00316                              octave_idx_type& info, double& rcond,
00317                              solve_singularity_handler sing_handler,
00318                              bool singular_fallabck = true) const;
00319 
00320   ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
00321   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00322                       octave_idx_type& info) const;
00323   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00324                       octave_idx_type& info, double& rcond) const;
00325   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00326                       octave_idx_type& info, double& rcond,
00327                       solve_singularity_handler sing_handler) const;
00328 
00329   ComplexColumnVector solve (MatrixType &typ,
00330                              const ComplexColumnVector& b) const;
00331   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00332                              octave_idx_type& info) const;
00333   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00334                              octave_idx_type& info, double& rcond) const;
00335   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00336                              octave_idx_type& info, double& rcond,
00337                              solve_singularity_handler sing_handler) const;
00338 
00339   // Generic interface to solver with probing of type
00340   Matrix solve (const Matrix& b) const;
00341   Matrix solve (const Matrix& b, octave_idx_type& info) const;
00342   Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond) const;
00343   Matrix solve (const Matrix& b, octave_idx_type& info, double& rcond,
00344                 solve_singularity_handler sing_handler) const;
00345 
00346   ComplexMatrix solve (const ComplexMatrix& b) const;
00347   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
00348   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info,
00349                        double& rcond) const;
00350   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond,
00351                        solve_singularity_handler sing_handler) const;
00352 
00353   SparseMatrix solve (const SparseMatrix& b) const;
00354   SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info) const;
00355   SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info,
00356                       double& rcond) const;
00357   SparseMatrix solve (const SparseMatrix& b, octave_idx_type& info, double& rcond,
00358                 solve_singularity_handler sing_handler) const;
00359 
00360   SparseComplexMatrix solve (const SparseComplexMatrix& b) const;
00361   SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info) const;
00362   SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info,
00363                              double& rcond) const;
00364   SparseComplexMatrix solve (const SparseComplexMatrix& b, octave_idx_type& info,
00365                              double& rcond,
00366                              solve_singularity_handler sing_handler) const;
00367 
00368   ColumnVector solve (const ColumnVector& b) const;
00369   ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
00370   ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const;
00371   ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcond,
00372                       solve_singularity_handler sing_handler) const;
00373 
00374   ComplexColumnVector solve (const ComplexColumnVector& b) const;
00375   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const;
00376   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00377                              double& rcond) const;
00378   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00379                              double& rcond,
00380                              solve_singularity_handler sing_handler) const;
00381 
00382   // other operations
00383 
00384   bool any_element_is_negative (bool = false) const;
00385   bool any_element_is_nan (void) const;
00386   bool any_element_is_inf_or_nan (void) const;
00387   bool any_element_not_one_or_zero (void) const;
00388   bool all_elements_are_zero (void) const;
00389   bool all_elements_are_int_or_inf_or_nan (void) const;
00390   bool all_integers (double& max_val, double& min_val) const;
00391   bool too_large_for_float (void) const;
00392 
00393   SparseBoolMatrix operator ! (void) const;
00394 
00395   SparseBoolMatrix all (int dim = -1) const;
00396   SparseBoolMatrix any (int dim = -1) const;
00397 
00398   SparseMatrix cumprod (int dim = -1) const;
00399   SparseMatrix cumsum (int dim = -1) const;
00400   SparseMatrix prod (int dim = -1) const;
00401   SparseMatrix sum (int dim = -1) const;
00402   SparseMatrix sumsq (int dim = -1) const;
00403   SparseMatrix abs (void) const;
00404 
00405   SparseMatrix diag (octave_idx_type k = 0) const;
00406 
00407   Matrix matrix_value (void) const;
00408 
00409   SparseMatrix squeeze (void) const;
00410 
00411   SparseMatrix reshape (const dim_vector& new_dims) const;
00412 
00413   SparseMatrix permute (const Array<octave_idx_type>& vec, bool inv = false) const;
00414 
00415   SparseMatrix ipermute (const Array<octave_idx_type>& vec) const;
00416 
00417   // i/o
00418 
00419   friend OCTAVE_API std::ostream& operator << (std::ostream& os, const SparseMatrix& a);
00420   friend OCTAVE_API std::istream& operator >> (std::istream& is, SparseMatrix& a);
00421 
00422 };
00423 
00424 // Publish externally used friend functions.
00425 
00426 extern OCTAVE_API SparseMatrix real (const SparseComplexMatrix& a);
00427 extern OCTAVE_API SparseMatrix imag (const SparseComplexMatrix& a);
00428 
00429 // Other operators.
00430 
00431 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix& a,
00432                                 const SparseMatrix& b);
00433 extern OCTAVE_API Matrix operator * (const Matrix& a,
00434                                 const SparseMatrix& b);
00435 extern OCTAVE_API Matrix mul_trans (const Matrix& a,
00436                                 const SparseMatrix& b);
00437 extern OCTAVE_API Matrix operator * (const SparseMatrix& a,
00438                                 const Matrix& b);
00439 extern OCTAVE_API Matrix trans_mul (const SparseMatrix& a,
00440                                 const Matrix& b);
00441 
00442 extern OCTAVE_API SparseMatrix operator * (const DiagMatrix&, const SparseMatrix&);
00443 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix&, const DiagMatrix&);
00444 
00445 extern OCTAVE_API SparseMatrix operator + (const DiagMatrix&, const SparseMatrix&);
00446 extern OCTAVE_API SparseMatrix operator + (const SparseMatrix&, const DiagMatrix&);
00447 extern OCTAVE_API SparseMatrix operator - (const DiagMatrix&, const SparseMatrix&);
00448 extern OCTAVE_API SparseMatrix operator - (const SparseMatrix&, const DiagMatrix&);
00449 
00450 extern OCTAVE_API SparseMatrix operator * (const PermMatrix&, const SparseMatrix&);
00451 extern OCTAVE_API SparseMatrix operator * (const SparseMatrix&, const PermMatrix&);
00452 
00453 extern OCTAVE_API SparseMatrix min (double d, const SparseMatrix& m);
00454 extern OCTAVE_API SparseMatrix min (const SparseMatrix& m, double d);
00455 extern OCTAVE_API SparseMatrix min (const SparseMatrix& a, const SparseMatrix& b);
00456 
00457 extern OCTAVE_API SparseMatrix max (double d, const SparseMatrix& m);
00458 extern OCTAVE_API SparseMatrix max (const SparseMatrix& m, double d);
00459 extern OCTAVE_API SparseMatrix max (const SparseMatrix& a, const SparseMatrix& b);
00460 
00461 SPARSE_SMS_CMP_OP_DECLS (SparseMatrix, double, OCTAVE_API)
00462 SPARSE_SMS_BOOL_OP_DECLS (SparseMatrix, double, OCTAVE_API)
00463 
00464 SPARSE_SSM_CMP_OP_DECLS (double, SparseMatrix, OCTAVE_API)
00465 SPARSE_SSM_BOOL_OP_DECLS (double, SparseMatrix, OCTAVE_API)
00466 
00467 SPARSE_SMSM_CMP_OP_DECLS (SparseMatrix, SparseMatrix, OCTAVE_API)
00468 SPARSE_SMSM_BOOL_OP_DECLS (SparseMatrix, SparseMatrix, OCTAVE_API)
00469 
00470 SPARSE_FORWARD_DEFS (MSparse, SparseMatrix, Matrix, double)
00471 
00472 #ifdef IDX_TYPE_LONG
00473 #define UMFPACK_DNAME(name) umfpack_dl_ ## name
00474 #else
00475 #define UMFPACK_DNAME(name) umfpack_di_ ## name
00476 #endif
00477 
00478 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines