Changeset 407


Ignore:
Timestamp:
Nov 27, 2005, 11:40:18 PM (16 years ago)
Author:
Jari Häkkinen
Message:

Added documentation. Started with view implementation.

Location:
branches/better_matrix_class/lib/gslapi
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/better_matrix_class/lib/gslapi/matrix.cc

    r404 r407  
    77
    88#include <cmath>
    9 #include <iostream>
    109#include <sstream>
    11 #include <string>
    1210#include <vector>
    1311
     
    2220
    2321  matrix::matrix(void)
    24     : m_(NULL)
     22    : m_(NULL), view_(NULL)
    2523  {
    2624  }
     
    2927
    3028  matrix::matrix(const size_t& r, const size_t& c, double init_value)
     29    : view_(NULL)
    3130  {
    3231    m_ = gsl_matrix_alloc(r,c);
     
    3736
    3837  matrix::matrix(const matrix& other)
     38    : view_(NULL)
    3939  {
    4040    m_ = other.gsl_matrix_copy();
     
    4343
    4444
     45  matrix(matrix& m, size_t offset_row, size_t offset_column, size_t n_row,
     46         size_t n_column);
     47  {
     48    view_ = new gsl_matrix_view(gsl_matrix_submatrix(v.m_,offset_row,
     49                                                     offset_column,n_row,
     50                                                     n_column));
     51    v_ = &(view_->matrix);
     52  }
     53
     54
     55
    4556  // Constructor that gets data from istream
    46   matrix::matrix(std::istream& is)
     57  matrix::matrix(std::istream& is) throw (utility::IO_error,std::exception)
     58    : view_(NULL)
    4759  {
    4860    // read the data file and store in stl vectors (dynamically
     
    5365    std::vector<double> v;
    5466    for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) {
    55 
    5667      // Ignoring empty lines
    57       if(!v.size()){
     68      if (!v.size()) {
    5869        nof_rows--;
    5970        continue;
    6071      }
    61 
    62       if(nof_columns==0)
     72      if (!nof_columns)
    6373        nof_columns=v.size();
    64       else if(v.size()!=nof_columns) {
    65         std::cerr << "matrix::matrix(std::istream&) data file error: "
    66                   << "line" << nof_rows+1 << " has " << v.size()
    67                   << " columns; expected " << nof_columns
    68                   << " columns"
    69                   << std::endl;
    70         exit(1);
     74      else if (v.size()!=nof_columns) {
     75        std::ostringstream s;
     76        s << "matrix::matrix(std::istream&) data file error: "
     77          << "line" << nof_rows+1 << " has " << v.size()
     78          << " columns; expected " << nof_columns << " columns.";
     79        throw utility::IO_error(s.str());
    7180      }
    7281      data_matrix.push_back(v);
     
    8695  matrix::~matrix(void)
    8796  {
     97    if (view_)
     98      delete view_;
    8899    if (m_) {
    89100      gsl_matrix_free(m_);
     
    100111    for (size_t i=0; i<rows(); i++)
    101112      for (size_t j=0; j<columns(); j++)
    102         // two last cond. are to check for nans
     113        // The two last condition checks are needed for NaN detection
    103114        if (fabs( (*this)(i,j)-other(i,j) ) > d ||
    104115            (*this)(i,j)!=(*this)(i,j) || other(i,j)!=other(i,j))
    105116          return false;
    106 
    107117    return true;
    108118  }
     
    127137    return res;
    128138  }
    129   */
    130139
    131140
     
    151160
    152161
    153   /*
    154162  matrix matrix::operator*( const matrix &other ) const
    155163  {
  • branches/better_matrix_class/lib/gslapi/matrix.h

    r403 r407  
    55
    66#include <c++_tools/gslapi/vector.h>
     7#include <c++_tools/utility/Exception.h>
    78
    89#include <gsl/gsl_matrix.h>
    910#include <iostream>
    10 
    1111
    1212namespace theplu {
     
    2020  /// well in the future.
    2121  ///
     22  /// \par[File streams] Reading and writing vectors to file streams
     23  /// are of course supported. These are implemented without using GSL
     24  /// functionality, and thus binary read and write to streams are not
     25  /// supported.
     26  ///
     27  /// \par[Matrix views] GSL matrix views are supported and these are
     28  /// disguised as ordinary gslapi::matrix'es. A support function is
     29  /// added, gslapi::matrix::isview(), that can be used to check if a
     30  /// matrix object is a view. Note that view matricess do not own the
     31  /// undelying data, and a view is not valid if the matrix owning the
     32  /// data is deallocated.
     33  ///
    2234  /// @note All GSL matrix related functions are not implement but all
    2335  /// functionality, except binary file support, defined for GSL
     
    3143    /// The default constructor.
    3244    ///
     45    /// This contructor does not initialize underlying (essential)
     46    /// structures.
     47    ///
    3348    matrix(void);
    3449
     
    4661    ///
    4762    matrix(const matrix&);
     63
     64    ///
     65    /// The matrix view constructor.
     66    ///
     67    /// Create a view of matrix \a m, with starting row \a offset_row,
     68    /// starting column \a offset_column, row size \a n_row, and
     69    /// column size \a n_column.
     70    ///
     71    /// A matrix view can be used as any matrix with the difference
     72    /// that changes made to the view will also change the object that
     73    /// is viewed. Also, using the copy constructor will create a new
     74    /// matrix object that is a copy of whatever is viewed. If a copy
     75    /// of the view is needed then you should use this consturctor to
     76    /// get one.
     77    ///
     78    /// @note If the object viewed by the view goes out of scope or is
     79    /// deleted, the view becomes invalid and the result of further
     80    /// use is undefined.
     81    ///
     82    matrix(matrix& m, size_t offset_row, size_t offset_column, size_t n_row,
     83           size_t n_column);
    4884
    4985    ///
     
    80116    /// @return Whatever GSL returns.
    81117    ///
    82     inline int add_constant(const double d)
    83       { return gsl_matrix_add_constant(m_,d); }
     118    inline int
     119    add_constant(const double d) { return gsl_matrix_add_constant(m_,d); }
    84120
    85121    ///
     
    95131    /// @return Whatever GSL returns.
    96132    ///
    97     inline int div_elements(const matrix& b)
    98       { return gsl_matrix_div_elements(m_,b.m_); }
    99 
    100     ///
    101     /// @return true if each element deviates less or equal than \a
    102     /// d. If any matrix contain a nan false is always returned.
    103     ///
    104     bool equal(const matrix&, const double d=0) const;
     133    inline int
     134    div_elements(const matrix& b) { return gsl_matrix_div_elements(m_,b.m_); }
     135
     136    ///
     137    /// Check whether matrices are equal within a user defined
     138    /// precision, set by \a precision.
     139    ///
     140    /// @return True if each element deviates less or equal than \a
     141    /// d. If any matrix contain a NaN, false is always returned.
     142    ///
     143    bool equal(const matrix&, const double precision=0) const;
    105144
    106145    ///
     
    109148    ///
    110149    inline bool isnull(void) const { return gsl_matrix_isnull(m_); }
     150
     151    ///
     152    /// Check if the matrix object is a view (sub-matrix) to another
     153    /// matrix.
     154    ///
     155    /// @return True if the object is a view, false othwerwise.
     156    ///
     157    inline bool isview(void) const { return view_; }
    111158
    112159    ///
     
    158205    /// @return Whatever GSL returns.
    159206    ///
    160     inline int mul_elements(const matrix& b)
    161       { return gsl_matrix_mul_elements(m_,b.m_); }
     207    inline int
     208    mul_elements(const matrix& b) { return gsl_matrix_mul_elements(m_,b.m_); }
    162209
    163210    ///
     
    307354    /// Add and assign operator.
    308355    ///
    309     inline const matrix& operator+=(const double d)
    310       { add_constant(d); return *this; }
     356    inline const matrix&
     357    operator+=(const double d) { add_constant(d); return *this; }
    311358
    312359    ///
     
    329376
    330377  private:
     378
    331379    ///
    332380    /// Create a new copy of the internal GSL matrix.
     
    352400
    353401    gsl_matrix* m_;
    354 
     402    gsl_matrix_view* view_;
    355403  };
    356404
     
    360408  std::ostream& operator<< (std::ostream& s, const matrix&);
    361409
     410
    362411}} // of namespace gslapi and namespace theplu
    363412
Note: See TracChangeset for help on using the changeset viewer.