dMatrix.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1994-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_Matrix_int_h)
00024 #define octave_Matrix_int_h 1
00025 
00026 #include "MArray.h"
00027 #include "MDiagArray2.h"
00028 #include "MatrixType.h"
00029 
00030 #include "mx-defs.h"
00031 #include "mx-op-decl.h"
00032 #include "DET.h"
00033 
00034 class
00035 OCTAVE_API
00036 Matrix : public MArray<double>
00037 {
00038 public:
00039 
00040   typedef ColumnVector column_vector_type;
00041   typedef RowVector row_vector_type;
00042 
00043   typedef void (*solve_singularity_handler) (double rcon);
00044 
00045   Matrix (void) : MArray<double> () { }
00046 
00047   Matrix (octave_idx_type r, octave_idx_type c)
00048     : MArray<double> (dim_vector (r, c)) { }
00049 
00050   Matrix (octave_idx_type r, octave_idx_type c, double val)
00051     : MArray<double> (dim_vector (r, c), val) { }
00052 
00053   Matrix (const dim_vector& dv) : MArray<double> (dv.redim (2)) { }
00054 
00055   Matrix (const dim_vector& dv, double val)
00056     : MArray<double> (dv.redim (2), val) { }
00057 
00058   Matrix (const Matrix& a) : MArray<double> (a) { }
00059 
00060   template <class U>
00061   Matrix (const MArray<U>& a) : MArray<double> (a.as_matrix ()) { }
00062 
00063   template <class U>
00064   Matrix (const Array<U>& a) : MArray<double> (a.as_matrix ()) { }
00065 
00066   explicit Matrix (const RowVector& rv);
00067 
00068   explicit Matrix (const ColumnVector& cv);
00069 
00070   explicit Matrix (const DiagMatrix& a);
00071 
00072   explicit Matrix (const PermMatrix& a);
00073 
00074   explicit Matrix (const boolMatrix& a);
00075 
00076   explicit Matrix (const charMatrix& a);
00077 
00078   Matrix& operator = (const Matrix& a)
00079     {
00080       MArray<double>::operator = (a);
00081       return *this;
00082     }
00083 
00084   bool operator == (const Matrix& a) const;
00085   bool operator != (const Matrix& a) const;
00086 
00087   bool is_symmetric (void) const;
00088 
00089   // destructive insert/delete/reorder operations
00090 
00091   Matrix& insert (const Matrix& a, octave_idx_type r, octave_idx_type c);
00092   Matrix& insert (const RowVector& a, octave_idx_type r, octave_idx_type c);
00093   Matrix& insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c);
00094   Matrix& insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c);
00095 
00096   Matrix& fill (double val);
00097   Matrix& fill (double val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2);
00098 
00099   Matrix append (const Matrix& a) const;
00100   Matrix append (const RowVector& a) const;
00101   Matrix append (const ColumnVector& a) const;
00102   Matrix append (const DiagMatrix& a) const;
00103 
00104   Matrix stack (const Matrix& a) const;
00105   Matrix stack (const RowVector& a) const;
00106   Matrix stack (const ColumnVector& a) const;
00107   Matrix stack (const DiagMatrix& a) const;
00108 
00109   friend OCTAVE_API Matrix real (const ComplexMatrix& a);
00110   friend OCTAVE_API Matrix imag (const ComplexMatrix& a);
00111 
00112   friend class ComplexMatrix;
00113 
00114   Matrix transpose (void) const { return MArray<double>::transpose (); }
00115 
00116   // resize is the destructive equivalent for this one
00117 
00118   Matrix extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const;
00119 
00120   Matrix extract_n (octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const;
00121 
00122   // extract row or column i.
00123 
00124   RowVector row (octave_idx_type i) const;
00125 
00126   ColumnVector column (octave_idx_type i) const;
00127 
00128   void resize (octave_idx_type nr, octave_idx_type nc,
00129                double rfv = resize_fill_value ())
00130   {
00131     MArray<double>::resize (dim_vector (nr, nc), rfv);
00132   }
00133 
00134 private:
00135   Matrix tinverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
00136                    int force, int calc_cond) const;
00137 
00138   Matrix finverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
00139                    int force, int calc_cond) const;
00140 
00141 public:
00142   Matrix inverse (void) const;
00143   Matrix inverse (octave_idx_type& info) const;
00144   Matrix inverse (octave_idx_type& info, double& rcon, int force = 0,
00145                   int calc_cond = 1) const;
00146 
00147   Matrix inverse (MatrixType &mattype) const;
00148   Matrix inverse (MatrixType &mattype, octave_idx_type& info) const;
00149   Matrix inverse (MatrixType &mattype, octave_idx_type& info, double& rcon,
00150                   int force = 0, int calc_cond = 1) const;
00151 
00152   Matrix pseudo_inverse (double tol = 0.0) const;
00153 
00154   ComplexMatrix fourier (void) const;
00155   ComplexMatrix ifourier (void) const;
00156 
00157   ComplexMatrix fourier2d (void) const;
00158   ComplexMatrix ifourier2d (void) const;
00159 
00160   DET determinant (void) const;
00161   DET determinant (octave_idx_type& info) const;
00162   DET determinant (octave_idx_type& info, double& rcon, int calc_cond = 1) const;
00163   DET determinant (MatrixType &mattype, octave_idx_type& info,
00164                    double& rcon, int calc_cond = 1) const;
00165 
00166   double rcond (void) const;
00167   double rcond (MatrixType &mattype) const;
00168 
00169 private:
00170   // Upper triangular matrix solvers
00171   Matrix utsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00172                   double& rcon, solve_singularity_handler sing_handler,
00173                   bool calc_cond = false, blas_trans_type transt = blas_no_trans) const;
00174 
00175   // Lower triangular matrix solvers
00176   Matrix ltsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00177                   double& rcon, solve_singularity_handler sing_handler,
00178                   bool calc_cond = false, blas_trans_type transt = blas_no_trans) const;
00179 
00180   // Full matrix solvers (lu/cholesky)
00181   Matrix fsolve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00182                  double& rcon, solve_singularity_handler sing_handler,
00183                  bool calc_cond = false) const;
00184 
00185 public:
00186   // Generic interface to solver with no probing of type
00187   Matrix solve (MatrixType &typ, const Matrix& b) const;
00188   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info) const;
00189   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00190                 double& rcon) const;
00191   Matrix solve (MatrixType &typ, const Matrix& b, octave_idx_type& info,
00192                 double& rcon, solve_singularity_handler sing_handler,
00193                 bool singular_fallback = true, blas_trans_type transt = blas_no_trans) const;
00194 
00195   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b) const;
00196   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00197                        octave_idx_type& info) const;
00198   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00199                        octave_idx_type& info, double& rcon) const;
00200   ComplexMatrix solve (MatrixType &typ, const ComplexMatrix& b,
00201                        octave_idx_type& info, double& rcon,
00202                        solve_singularity_handler sing_handler,
00203                        bool singular_fallback = true,
00204                        blas_trans_type transt = blas_no_trans) const;
00205 
00206   ColumnVector solve (MatrixType &typ, const ColumnVector& b) const;
00207   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00208                       octave_idx_type& info) const;
00209   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00210                       octave_idx_type& info, double& rcon) const;
00211   ColumnVector solve (MatrixType &typ, const ColumnVector& b,
00212                       octave_idx_type& info, double& rcon,
00213                       solve_singularity_handler sing_handler,
00214                       blas_trans_type transt = blas_no_trans) const;
00215 
00216   ComplexColumnVector solve (MatrixType &typ,
00217                              const ComplexColumnVector& b) const;
00218   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00219                              octave_idx_type& info) const;
00220   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00221                              octave_idx_type& info, double& rcon) const;
00222   ComplexColumnVector solve (MatrixType &typ, const ComplexColumnVector& b,
00223                              octave_idx_type& info, double& rcon,
00224                              solve_singularity_handler sing_handler,
00225                              blas_trans_type transt = blas_no_trans) const;
00226 
00227   // Generic interface to solver with probing of type
00228   Matrix solve (const Matrix& b) const;
00229   Matrix solve (const Matrix& b, octave_idx_type& info) const;
00230   Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon) const;
00231   Matrix solve (const Matrix& b, octave_idx_type& info, double& rcon,
00232                 solve_singularity_handler sing_handler,
00233                 blas_trans_type transt = blas_no_trans) const;
00234 
00235   ComplexMatrix solve (const ComplexMatrix& b) const;
00236   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info) const;
00237   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon) const;
00238   ComplexMatrix solve (const ComplexMatrix& b, octave_idx_type& info, double& rcon,
00239                        solve_singularity_handler sing_handler,
00240                        blas_trans_type transt = blas_no_trans) const;
00241 
00242   ColumnVector solve (const ColumnVector& b) const;
00243   ColumnVector solve (const ColumnVector& b, octave_idx_type& info) const;
00244   ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcon) const;
00245   ColumnVector solve (const ColumnVector& b, octave_idx_type& info, double& rcon,
00246                       solve_singularity_handler sing_handler,
00247                       blas_trans_type transt = blas_no_trans) const;
00248 
00249   ComplexColumnVector solve (const ComplexColumnVector& b) const;
00250   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info) const;
00251   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00252                              double& rcon) const;
00253   ComplexColumnVector solve (const ComplexColumnVector& b, octave_idx_type& info,
00254                              double& rcon, solve_singularity_handler sing_handler,
00255                              blas_trans_type transt = blas_no_trans) const;
00256 
00257   // Singular solvers
00258   Matrix lssolve (const Matrix& b) const;
00259   Matrix lssolve (const Matrix& b, octave_idx_type& info) const;
00260   Matrix lssolve (const Matrix& b, octave_idx_type& info,
00261                   octave_idx_type& rank) const;
00262   Matrix lssolve (const Matrix& b, octave_idx_type& info,
00263                   octave_idx_type& rank, double& rcon) const;
00264 
00265   ComplexMatrix lssolve (const ComplexMatrix& b) const;
00266   ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info) const;
00267   ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info,
00268                          octave_idx_type& rank) const;
00269   ComplexMatrix lssolve (const ComplexMatrix& b, octave_idx_type& info,
00270                          octave_idx_type& rank, double &rcon) const;
00271 
00272   ColumnVector lssolve (const ColumnVector& b) const;
00273   ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info) const;
00274   ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info,
00275                         octave_idx_type& rank) const;
00276   ColumnVector lssolve (const ColumnVector& b, octave_idx_type& info,
00277                         octave_idx_type& rank, double& rcon) const;
00278 
00279   ComplexColumnVector lssolve (const ComplexColumnVector& b) const;
00280   ComplexColumnVector lssolve (const ComplexColumnVector& b,
00281                                octave_idx_type& info) const;
00282   ComplexColumnVector lssolve (const ComplexColumnVector& b,
00283                                octave_idx_type& info,
00284                                octave_idx_type& rank) const;
00285   ComplexColumnVector lssolve (const ComplexColumnVector& b,
00286                                octave_idx_type& info,
00287                                octave_idx_type& rank, double& rcon) const;
00288 
00289   Matrix& operator += (const DiagMatrix& a);
00290   Matrix& operator -= (const DiagMatrix& a);
00291 
00292   // unary operations
00293 
00294   boolMatrix operator ! (void) const;
00295 
00296   // other operations
00297 
00298   bool any_element_is_negative (bool = false) const;
00299   bool any_element_is_positive (bool = false) const;
00300   bool any_element_is_nan (void) const;
00301   bool any_element_is_inf_or_nan (void) const;
00302   bool any_element_not_one_or_zero (void) const;
00303   bool all_elements_are_int_or_inf_or_nan (void) const;
00304   bool all_integers (double& max_val, double& min_val) const;
00305   bool too_large_for_float (void) const;
00306 
00307   boolMatrix all (int dim = -1) const;
00308   boolMatrix any (int dim = -1) const;
00309 
00310   Matrix cumprod (int dim = -1) const;
00311   Matrix cumsum (int dim = -1) const;
00312   Matrix prod (int dim = -1) const;
00313   Matrix sum (int dim = -1) const;
00314   Matrix sumsq (int dim = -1) const;
00315   Matrix abs (void) const;
00316 
00317   Matrix diag (octave_idx_type k = 0) const;
00318 
00319   ColumnVector row_min (void) const;
00320   ColumnVector row_max (void) const;
00321 
00322   ColumnVector row_min (Array<octave_idx_type>& index) const;
00323   ColumnVector row_max (Array<octave_idx_type>& index) const;
00324 
00325   RowVector column_min (void) const;
00326   RowVector column_max (void) const;
00327 
00328   RowVector column_min (Array<octave_idx_type>& index) const;
00329   RowVector column_max (Array<octave_idx_type>& index) const;
00330 
00331   // i/o
00332 
00333   friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Matrix& a);
00334   friend OCTAVE_API std::istream& operator >> (std::istream& is, Matrix& a);
00335 
00336   static double resize_fill_value (void) { return 0; }
00337 };
00338 
00339 // Publish externally used friend functions.
00340 
00341 extern OCTAVE_API Matrix real (const ComplexMatrix& a);
00342 extern OCTAVE_API Matrix imag (const ComplexMatrix& a);
00343 
00344 // column vector by row vector -> matrix operations
00345 
00346 extern OCTAVE_API Matrix operator * (const ColumnVector& a, const RowVector& b);
00347 
00348 // Other functions.
00349 
00350 extern OCTAVE_API Matrix Givens (double, double);
00351 
00352 extern OCTAVE_API Matrix Sylvester (const Matrix&, const Matrix&, const Matrix&);
00353 
00354 extern OCTAVE_API Matrix xgemm (const Matrix& a, const Matrix& b,
00355                                 blas_trans_type transa = blas_no_trans,
00356                                 blas_trans_type transb = blas_no_trans);
00357 
00358 extern OCTAVE_API Matrix operator * (const Matrix& a, const Matrix& b);
00359 
00360 extern OCTAVE_API Matrix min (double d, const Matrix& m);
00361 extern OCTAVE_API Matrix min (const Matrix& m, double d);
00362 extern OCTAVE_API Matrix min (const Matrix& a, const Matrix& b);
00363 
00364 extern OCTAVE_API Matrix max (double d, const Matrix& m);
00365 extern OCTAVE_API Matrix max (const Matrix& m, double d);
00366 extern OCTAVE_API Matrix max (const Matrix& a, const Matrix& b);
00367 
00368 extern OCTAVE_API Matrix linspace (const ColumnVector& x1,
00369                                    const ColumnVector& x2,
00370                                    octave_idx_type n);
00371 
00372 MS_CMP_OP_DECLS (Matrix, double, OCTAVE_API)
00373 MS_BOOL_OP_DECLS (Matrix, double, OCTAVE_API)
00374 
00375 SM_CMP_OP_DECLS (double, Matrix, OCTAVE_API)
00376 SM_BOOL_OP_DECLS (double, Matrix, OCTAVE_API)
00377 
00378 MM_CMP_OP_DECLS (Matrix, Matrix, OCTAVE_API)
00379 MM_BOOL_OP_DECLS (Matrix, Matrix, OCTAVE_API)
00380 
00381 MARRAY_FORWARD_DEFS (MArray, Matrix, double)
00382 
00383 template <class T>
00384 void read_int (std::istream& is, bool swap_bytes, T& val);
00385 
00386 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines