gsl_matrix_double.h

Go to the documentation of this file.
00001 
00002 
00003 //
00013 
00014 //  This matrix class has been adapted from the gslwrap project.
00015 //  It is a C++ wrapper for the GNU Scientific Library
00016 //  Copyright (C) 2001 Ramin Nakisa
00017 
00018 //  This program is free software; you can redistribute it and/or modify
00019 //  it under the terms of the GNU General Public License as published by
00020 //  the Free Software Foundation; either version 2 of the License, or
00021 //  (at your option) any later version.
00022 
00023 //  This program is distributed in the hope that it will be useful,
00024 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 //  GNU General Public License for more details.
00027 
00028 //  You should have received a copy of the GNU General Public License
00029 //  along with this program; if not, write to the Free Software
00030 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00031 
00032 #ifndef _GSL_MATRIX_DOUBLE_H
00033 #define _GSL_MATRIX_DOUBLE_H
00034 
00035 #include <iostream>
00036 #include <fstream>
00037 #include <iomanip>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <assert.h> 
00041 #include <gsl/gsl_math.h>
00042 #include <gsl/gsl_matrix.h>
00043 #include <gsl/gsl_linalg.h>
00044 #include "gsl_permutation.h"
00045 #include "gsl_vector_double.h"
00046 
00047 #define type_is_int
00048 #ifdef  type_is
00049 #define type_is_double
00050 #endif
00051 
00053 //
00054 
00055 class matrix 
00056 {
00057 
00058  friend class matrix_int;
00059 
00060  private: 
00061  
00063 //
00064 
00065  gsl_matrix *m;
00066  
00067  public:
00068  
00069  typedef double value_type;
00070  typedef vector vector_type;
00071 
00072 //---------------------------------------------------------------------------
00073 
00075 //
00077 //
00078 
00079   matrix();
00080   
00082 //
00090 //
00091   
00092   matrix( size_t new_rows, size_t new_cols, bool clear = true);
00093   
00095 //
00097 //
00098 
00099   ~matrix();
00100 
00102 //
00108 //
00109         
00110   void set_element( size_t row, size_t col, const double &v );
00111   
00113 //
00118 //
00119         
00120   double get_element( size_t row, size_t col ) const;
00121   
00123 //
00126 //
00127         
00128   void dimensions( size_t *num_rows, size_t *num_cols ) const;
00129   
00131 //
00137 //
00138         
00139   void set_dimensions( size_t new_rows, size_t new_cols );
00140 
00142 //
00147 //
00148         
00149   const double &operator()( size_t row, size_t col ) const;
00150   
00152 //
00157 //
00158         
00159   double &operator()( size_t row, size_t col );
00160   
00162 //
00167 //
00168 
00169   template<class oclass>
00170   void copy(const oclass &other)
00171   {
00172                 if ( static_cast<const void *>( this ) == static_cast<const void *>( &other ) )
00173                         return;
00174 
00175                 set_dimensions( other.get_rows(), other.get_cols() );
00176                 for ( size_t i = 0; i < get_rows(); i++ ) 
00177                 {
00178                         for ( size_t j = 0; j < get_cols(); j++ ) 
00179                         {
00180                                 gsl_matrix_set( m, i, j, (double)other(i,j));
00181                         }
00182                 }
00183    }
00184   
00186 //
00187         
00188    matrix( const matrix &other ):m(NULL) {copy(other);}
00189         
00191 //
00192    template<class oclass>
00193    matrix( const oclass &other ):m(NULL) {copy(other);}
00194 
00196 //
00204 //
00205 
00206         matrix submatrix(size_t row_min, size_t row_max, size_t col_min, size_t col_max) const;         
00207 
00209 // 
00210 
00211     void set_zero();
00212 
00214 //              
00215 
00216         size_t get_rows() const; 
00217 
00219 //
00220 
00221         size_t get_cols() const; 
00222 
00224 //      
00225         size_t size1() const; 
00226         
00228 //
00229         size_t size2() const; 
00230 
00232 //      
00233 
00234         int fwrite (FILE * stream) const;
00235 
00237 // 
00238 
00239         int fread (FILE * stream);
00240 
00242 // 
00245 //
00246 
00247         int fprintf(FILE * stream, const char * format) const;
00248 
00250 // 
00252 //
00253 
00254         int fscanf (FILE * stream);
00255         
00257 //
00258 
00259         vector_view       row( size_t rowindex );
00260         
00262 //
00263 
00264         const vector_view row( size_t rowindex ) const ;
00265 
00267 //
00268 
00269         vector_view       column( size_t colindex );
00270 
00272 //
00273 
00274         const vector_view column( size_t colindex ) const;
00275 
00277 //
00278 
00279         vector_view       diagonal();
00280 
00282 //
00283 
00284         const vector_view diagonal() const;
00285 
00287 //
00289 //
00290 
00291         matrix &operator=( const matrix& other );
00292 
00294 //
00296 //
00297         
00298         template<class omatrix>
00299         matrix &operator=( const omatrix& other )
00300         {
00301                         copy(other);
00302                         return *this;
00303         }
00304         
00306 //
00308 //      
00309         
00310         bool operator==( const matrix &other ) const;
00311 
00313 //
00315 //
00316         
00317         bool operator!=( const matrix &other ) const {return !((*this)==other);}
00318         
00320 //
00323 //
00324         
00325         matrix operator+( const matrix &other ) const;
00326         
00328 //
00330 //
00331 
00332     matrix operator+( const double &f ) const;
00333     
00335 //
00337     
00338     friend matrix operator+( const double &f, const matrix &other );
00339 
00341 //
00344 
00345         matrix &operator+=( const double &f );
00346 
00348 //
00351 
00352         matrix &operator+=( const matrix &other );
00353 
00355 //
00357 //
00358 
00359         matrix operator-( const matrix &other ) const;
00360 
00362 //
00364 //
00365         
00366         matrix operator-( const double &f ) const;
00367 
00369 //
00371 //
00372 
00373         friend matrix operator-( const double &f, const matrix &other );
00374 
00376 //
00379 //
00380 
00381         matrix &operator-=( const double &f );
00382 
00384 //
00387 //
00388 
00389         matrix &operator-=( const matrix &other );
00390         
00392 //
00395 //
00396 
00397         matrix operator*( const matrix &other ) const;
00398 
00400 //
00403 //
00404 
00405         matrix operator*( const double &f ) const;
00406         
00408 //
00410 //
00411         
00412         friend matrix operator*( const double &f, const matrix &other );
00413 
00415 //
00418 //
00419 
00420         matrix &operator*=( const double &f );
00421 
00423 //
00426 //
00427 
00428         matrix &operator*=( const matrix &other );
00429 
00431 //
00434 //
00435         
00436         matrix operator/( const double &) const;
00437 
00439 //
00442 //
00443         
00444         matrix &operator/=( const double &);
00445         
00447 //
00449 //      
00450         
00451         void diag(const vector& v);
00452         
00454 //
00462 //
00463 
00464         void set_tridiag(const vector& v1, const vector& v2, const vector& v3);
00465 
00467 //
00468         
00469         matrix transpose() const;
00470         
00472 //
00473 
00474         matrix LU_decomp(permutation *perm=NULL,int *psign=NULL) const;
00475 
00477 //
00478         
00479         matrix LU_invert() const;
00480 
00482 //
00483         
00484         double sum() const;
00485 
00487 //
00489 //
00490 
00491         double row_sum( size_t rowindex ) const;
00492 
00494 //
00496 //
00497         
00498         double col_sum( size_t colindex ) const;
00499 
00501 //
00502         
00503         double LU_lndet() const;
00504 
00506 //
00507         
00508         int cholesky_decomp( matrix &a ) const;
00509          
00511 //      
00512 
00513         gsl_matrix       *gslobj()       {assert(m);return m;} 
00514         
00516 //
00517 
00518         const gsl_matrix *gslobj() const {assert(m);return m;}
00519         
00520 };
00521 
00522 #endif // GSL_MATRIX_DOUBLE_H
00523 
All Classes Files Functions Variables Typedefs Friends Defines