Changeset 42 for trunk/src/matrix.h


Ignore:
Timestamp:
Feb 26, 2004, 4:06:20 PM (19 years ago)
Author:
Jari Häkkinen
Message:

Made a major revision of matrix and vector classes. Everything compiles
but the binaries have not been tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.h

    r40 r42  
    1 #ifndef _thep_gsl_api_matrix_
    2 #define _thep_gsl_api_matrix_
     1// $Id$
     2
     3#ifndef _theplu_gslapi_matrix_
     4#define _theplu_gslapi_matrix_
    35
    46#include <iostream>
    5 #include <fstream>
    6 #include <iomanip>
    7 #include <cmath>
    8 #include <cstdlib>
    9 #include <cassert>
    10 
    11 #include <gsl/gsl_math.h>
     7
    128#include <gsl/gsl_matrix.h>
    13 #include <gsl/gsl_linalg.h>
    14 #include <gsl/gsl_blas.h>
    15 
    16 #include "vector.h"
    17 
    18 namespace thep_gsl_api
    19 {
    20 
    21   /**
    22      This is a matrix interface to GSL for c++.
    23      Note that the namespace is "thep_gsl_api".
    24   */
     9
     10namespace theplu {
     11namespace gslapi {
     12
     13  ///
     14  /// This is the C++ tools interface to GSL matrix.
     15  ///
     16
     17  class vector;
    2518
    2619  class matrix
    2720  {
    2821  public:
    29     typedef double Type;
    30  
    31     /**
    32        Default constructor
    33        Does not allocate any memory
    34     */
    35     matrix();
    36 
    37     /**
    38        Constructor taking three argumens:
    39        N = Number of rows
    40        M = Number of columns
    41        init_to_zero, if true matrix will be initialized with zeros.
    42     */
    43     matrix( const size_t& rows, const size_t& cols,
    44       bool init_to_zero = true );
    45 
    46 
    47 
    48 
     22
     23    ///
     24    /// The default constructor.
     25    ///
     26    matrix(void);
     27
     28    ///
     29    /// Constructor. Allocates memory space for \a r times \a c
     30    /// elements, and sets all elements to \a init_value.
     31    ///
     32    matrix(const size_t& r, const size_t& c, const double init_value=0);
    4933 
    50     /**
    51   Copy constructor
    52     */
    53     matrix( const matrix& other );
    54 
    55 
    56     /**
    57        Constructor taking as only argument a matrix of gsl type.
    58        Be causious here! Do not delete this matrix outside of
    59        class: NO COPY IS MADE!!!
    60     */
    61     matrix( gsl_matrix* );
     34    ///
     35    /// The copy constructor.
     36    ///
     37    matrix(const matrix&);
     38
     39    ///
     40    /// The istream constructor.
     41    ///
     42    /// Matrix rows are sepearated with the new line character, and
     43    /// column elements in a row must be separated with white space
     44    /// characters except the new line (\\n) character. Rows, and
     45    /// columns, are read consequently and only rectangular matrices
     46    /// are supported. End of input is at end of file marker.
     47    ///
     48    explicit matrix(std::istream &);
     49
     50    ///
     51    /// The destructor.
     52    ///
     53    ~matrix(void);
     54
     55    ///
     56    /// @return The number of columns in the matrix.
     57    ///
     58    inline size_t columns(void) const { return m_->size2; }
     59
     60    ///
     61    /// Swap column \a i with \j.
     62    ///
     63    inline void column_swap(const size_t& i,const size_t& j)
     64      { gsl_matrix_swap_columns(m_,i,j); }
     65
     66    ///
     67    /// Create a new copy of the internal GSL matrix.
     68    ///
     69    /// Necessary memory for the new GSL matrix is allocated and the
     70    /// caller is responsible for freeing the allocated memory.
     71    ///
     72    /// @return A pointer to a copy of the internal GSL matrix.
     73    ///
     74    gsl_matrix* gsl_matrix_copy(void) const;
     75
     76    ///
     77    /// @return A const pointer to the internal GSL matrix.
     78    ///
     79    inline const gsl_matrix* gsl_matrix_pointer(void) const { return m_; }
     80
     81    ///
     82    /// @return A pointer to the internal GSL matrix.
     83    ///
     84    inline gsl_matrix* gsl_matrix_pointer(void) { return m_; };
     85
     86    ///
     87    /// This function multiplies all elements between two matrices and
     88    /// returns a third matrix containing the result, \f$a_{ij} =
     89    /// b_{ij} * c_{ij} \; \forall i,j\f$.
     90    ///
     91    /// @return The result matrix.
     92    ///
     93    matrix mul_elements(const matrix&);
     94
     95    ///
     96    /// Swap row \a i with \a j.
     97    ///
     98    inline void row_swap( const size_t& i, const size_t& j)
     99      { gsl_matrix_swap_rows(m_,i,j); }
     100
     101    ///
     102    /// @return The number of rows in the matrix.
     103    ///
     104    inline size_t rows(void) const { return m_->size1; }
     105
     106    ///
     107    /// Set all elements to \a value.
     108    ///
     109    inline void set_all(const double value) { gsl_matrix_set_all(m_,value); }
     110
     111    ///
     112    /// Transpose the matrix, the object is changed.
     113    ///
     114    /// @return Transpose of the matrix.
     115    ///
     116    matrix transpose(void) const;
    62117   
    63 
    64     /**
    65        Constructor taking as only argument a an istream that should
    66        contain the matrix where columns are separated with whitespace
    67        (not '\\n') and rows by '\\n'.
    68     */
    69     matrix(std::istream &);
    70 
    71     /**
    72        Destructor will clear matrix's allocated memory
    73     */
    74     ~matrix();
    75 
    76 
    77     /**
    78        The most important function when adding new classes to
    79        c++_tools. This function will return a pointer to the
    80        inner data of the class. Should be forbidden in a strictly
    81        object-orientated environment.
    82        Motivation: Effeciency! ( CAN IT BE MADE CONST? )
    83        To be used when writting new classes using gsl-functions.
    84     */
    85     inline gsl_matrix* get_gsl_matrix() const { return m_; } 
    86 
    87 
    88     /**
    89        Returns the number of rows in the matrix.
    90     */
    91     size_t rows() const { return m_->size1; }
    92 
    93 
    94     /**
    95        Returns the number of rows in the matrix.
    96     */
    97     size_t cols() const { return m_->size2; }
    98 
    99     /**
    100        get( i, j ) will return the Aij element in matrix A
    101        where i = row number and j = column number
    102     */
    103     inline Type get( const size_t& i, const size_t& j ) const
    104       { return gsl_matrix_get(m_,i_row,j_col); }
    105 
    106 
    107     /**
    108        set( i, j, val ) will make Aij = val in matrix A
    109        where i = row number and j = column number
    110     */
    111     inline void set( const size_t& i, const size_t& j, const Type& val )
    112       { gsl_matrix_set( m_, i_row, j_col, val ); }
    113 
    114     /**
    115        set_all( val ) will make Aij = val in matrix A
    116        for all "i"
    117     */
    118     inline void set_all( const Type& val ) { gsl_matrix_set_all(m_,val); }
    119 
    120 
    121 
    122     /**
    123        Assignment-operator. No requirements on dimensions,
    124        i.e. if A = B, then A will be a copy of B with B:s
    125        dimensions.
    126      */
    127     matrix& operator=( const matrix& other );
    128    
    129     /**
    130        operators for comparison are:
    131        == equal
    132        != not equal
    133     */
     118    ///
     119    /// @return Reference to the element position (\a row, \a column).
     120    ///
     121    inline double& operator()(size_t row,size_t column)
     122      { return (*gsl_matrix_ptr(m_,row,column)); }
     123
     124    ///
     125    /// @return Const reference to the element position (\a row, \a
     126    /// column).
     127    ///
     128    inline const double& operator()(size_t row,size_t column) const
     129      { return (*gsl_matrix_const_ptr(m_,row,column)); }
     130
     131    ///
     132    /// Jari, To be implemented. Should return a reference to a vector
     133    /// object. Underlying GSL supports only GSL_vector views, thus
     134    /// some redesign of the vector class is needed.
     135    ///
     136    /// inline vector& operator[](size_t i);
     137    /// So for now, stupid copying is done, and actually a matrix
     138    /// row is returned using this function.
     139    vector operator[](size_t row) const;
     140
     141    ///
     142    /// Jari, to be properly implemented. Should return a reference to
     143    /// a vector object (matrix row). Underlying GSL supports only GSL_vector
     144    /// views, thus some redesign of the vector class is needed.
     145    ///
     146    /// inline const vector& operator[](size_t i) const;
     147    ///
     148    /// So for now, stupid copying is done, and actually a matrix
     149    /// column is returned using this function.
     150    vector TEMP_col_return(size_t column) const;
     151
     152    ///
     153    /// Matrix multiplication.
     154    ///
     155    /// @return The resulting matrix.
     156    ///
     157    matrix operator*(const matrix&) const;
     158
     159    ///
     160    /// Matrix-vector multiplication.
     161    ///
     162    /// @return The resulting vector.
     163    ///
     164    vector operator*(const vector&) const;
     165
     166    ///
     167    /// Matrix addition.
     168    ///
     169    /// @return The resulting matrix.
     170    ///
     171    matrix operator+(const matrix&) const;
     172
     173    ///
     174    /// Matrix subtraction.
     175    ///
     176    /// @return The resulting matrix.
     177    ///
     178    matrix operator-(const matrix&) const;
     179
     180    ///
     181    /// Comparison operator.
     182    ///
     183    /// @return True if ... (to be defined) ... otherwise false.
     184    ///
    134185    bool operator==( const matrix &other ) const;
     186
     187    ///
     188    /// Comparison operator.
     189    ///
     190    /// @return True if ... (to be defined) ... otherwise false.
     191    ///
    135192    bool operator!=( const matrix &other ) const;
    136193
    137     /**
    138        operators for basic arithmetics are: \n
    139        \f$+\f$ matrix-matrix addition \n
    140        \f$-\f$ matrix-matrix subtraction \n
    141        \f$*\f$ matrix-matrix multiplication
    142     */
    143     matrix operator+( const matrix &other ) const;
    144     matrix operator-( const matrix &other ) const;
    145     matrix operator*( const matrix &other ) const;
    146     vector operator*( const vector &other ) const;
    147 
    148     /**
    149   multiplying each element of two matrices
    150   dimension MUST AGREE
    151     */
    152     matrix mul_elements( const matrix& a, const matrix& b );
    153 
    154     /**
    155        Method for transposing a matrix
    156      */
    157     matrix transpose() const;
    158    
    159     /**
    160        Method for calculating the n:th sqrt of the sum of
    161        the n:th power of all elements
    162     */
    163     double norm( double n ) const;
    164 
    165     /**
    166        Calculates sum of all elements in matrix
    167     */   
    168     double matrix::sum() const;
    169      
    170     /**
    171        Exchange row i with row j (vice versa)
    172      */
    173     void swap_rows( const size_t& i, const size_t& j );
    174 
    175     /**
    176        Exchange column i with column j (vice versa)
    177      */
    178     void swap_cols( const size_t& i, const size_t& j );
    179 
    180     /**
    181        Returns row i in matrix
    182     */   
    183     matrix row( const size_t& i ) const;
    184 
    185 
    186     /**
    187        Returns row i in matrix (vector form)
    188     */
    189     vector row_vector( const size_t& i ) const;
    190 
    191 
    192     /**
    193        Returns col i in matrix
    194     */   
    195     matrix col( const size_t& i ) const;
    196 
    197 
    198     /**
    199        Returns col i in matrix (vector form)
    200     */
    201     vector col_vector( const size_t& i ) const;
    202 
    203 
    204     /**
    205        Method for calculating the row sum
    206     */   
    207     vector row_sum() const;
    208 
    209     /**
    210        Method for calculating the mean row sum
    211     */   
    212     vector mean_row_sum() const;
    213 
    214     /**
    215   standard output operator is defined
    216     */
    217     friend std::ostream& operator<< ( std::ostream& s_out, const matrix& );
    218 
    219     /**
    220        Getting vector operator
    221     */
    222     vector operator[]( const size_t& i ) const;
    223  
     194    ///
     195    /// The assignment operator. There is no requirements on
     196    /// dimensions.
     197    ///
     198    matrix& operator=(const matrix& other);
     199
     200    ///
     201    /// Multiply and assign operator.
     202    ///
     203    inline matrix& operator*=(const double d)
     204      { gsl_matrix_scale(m_,d); return *this; }
     205
     206
    224207  private:
    225     gsl_matrix* new_copy( const gsl_matrix* );
    226208
    227209    gsl_matrix* m_;
    228   };
    229 
    230 
    231 };
    232 
    233 
    234 
     210
     211  };
     212
     213  ///
     214  /// The output operator for the vector class.
     215  ///
     216  std::ostream& operator<< (std::ostream& s, const matrix&);
     217
     218
     219
     220}} // of namespace gslapi and namespace thep
    235221
    236222#endif
Note: See TracChangeset for help on using the changeset viewer.