Ignore:
Timestamp:
Feb 22, 2008, 4:29:56 PM (14 years ago)
Author:
Peter
Message:

fixes #308

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Matrix.cc

    r1120 r1121  
    2525*/
    2626
    27 #include "yat/utility/matrix.h"
    28 #include "yat/utility/Vector.h"
     27#include "Matrix.h"
     28#include "Vector.h"
    2929#include "VectorBase.h"
    3030#include "VectorConstView.h"
     
    4444
    4545
    46   matrix::matrix(void)
     46  Matrix::Matrix(void)
    4747    : blas_result_(NULL), m_(NULL)
    4848  {
     
    5050
    5151
    52   matrix::matrix(const size_t& r, const size_t& c, double init_value)
     52  Matrix::Matrix(const size_t& r, const size_t& c, double init_value)
    5353    : blas_result_(NULL), m_(gsl_matrix_alloc(r,c))
    5454  {
    5555    if (!m_)
    56       throw utility::GSL_error("matrix::matrix failed to allocate memory");
     56      throw utility::GSL_error("Matrix::Matrix failed to allocate memory");
    5757    all(init_value);
    5858  }
    5959
    6060
    61   matrix::matrix(const matrix& o)
     61  Matrix::Matrix(const Matrix& o)
    6262    : blas_result_(NULL), m_(o.create_gsl_matrix_copy())
    6363  {
     
    6666
    6767  // Constructor that gets data from istream
    68   matrix::matrix(std::istream& is, char sep)
     68  Matrix::Matrix(std::istream& is, char sep)
    6969    throw (utility::IO_error,std::exception)
    7070    : blas_result_(NULL)
     
    113113      else if (v.size()!=nof_columns) {
    114114        std::ostringstream s;
    115         s << "matrix::matrix(std::istream&, char) data file error: "
     115        s << "Matrix::Matrix(std::istream&, char) data file error: "
    116116          << "line " << nof_rows << " has " << v.size()
    117117          << " columns; expected " << nof_columns << " columns.";
     
    126126    m_ = gsl_matrix_alloc ( nof_rows, nof_columns );
    127127    if (!m_)
    128       throw utility::GSL_error("matrix::matrix failed to allocate memory");
     128      throw utility::GSL_error("Matrix::Matrix failed to allocate memory");
    129129
    130130    // if gsl error handler disabled, out of bounds index will not
     
    136136
    137137
    138   matrix::~matrix(void)
     138  Matrix::~Matrix(void)
    139139  {
    140140    delete_allocated_memory();
     
    145145
    146146
    147   void matrix::all(const double value)
     147  void Matrix::all(const double value)
    148148  {
    149149    assert(m_);
     
    152152
    153153
    154   matrix::iterator matrix::begin(void)
     154  Matrix::iterator Matrix::begin(void)
    155155  {
    156156    return iterator(&(*this)(0,0), 1);
     
    158158
    159159
    160   matrix::const_iterator matrix::begin(void) const
     160  Matrix::const_iterator Matrix::begin(void) const
    161161  {
    162162    return const_iterator(&(*this)(0,0), 1);
     
    164164
    165165
    166   matrix::column_iterator matrix::begin_column(size_t i)
     166  Matrix::column_iterator Matrix::begin_column(size_t i)
    167167  {
    168168    return iterator(&(*this)(0,i), this->columns());
     
    170170
    171171
    172   matrix::const_column_iterator matrix::begin_column(size_t i) const
     172  Matrix::const_column_iterator Matrix::begin_column(size_t i) const
    173173  {
    174174    return const_iterator(&(*this)(0,i), this->columns());
     
    176176
    177177
    178   matrix::row_iterator matrix::begin_row(size_t i)
     178  Matrix::row_iterator Matrix::begin_row(size_t i)
    179179  {
    180180    return iterator(&(*this)(i,0), 1);
     
    182182
    183183
    184   matrix::const_row_iterator matrix::begin_row(size_t i) const
     184  Matrix::const_row_iterator Matrix::begin_row(size_t i) const
    185185  {
    186186    return const_iterator(&(*this)(i,0), 1);
     
    188188
    189189
    190   VectorView matrix::column_view(size_t col)
     190  VectorView Matrix::column_view(size_t col)
    191191  {
    192192    VectorView res(*this, col, false);
     
    195195
    196196
    197   const VectorConstView matrix::column_const_view(size_t col) const
     197  const VectorConstView Matrix::column_const_view(size_t col) const
    198198  {
    199199    return VectorConstView(*this, col, false);
     
    201201
    202202
    203   size_t matrix::columns(void) const
     203  size_t Matrix::columns(void) const
    204204  {
    205205    return (m_ ? m_->size2 : 0);
     
    207207
    208208
    209   gsl_matrix* matrix::create_gsl_matrix_copy(void) const
     209  gsl_matrix* Matrix::create_gsl_matrix_copy(void) const
    210210  {
    211211    gsl_matrix* m = gsl_matrix_alloc(rows(),columns());
    212212    if (!m)
    213       throw utility::GSL_error("matrix::create_gsl_matrix_copy failed to allocate memory");
     213      throw utility::GSL_error("Matrix::create_gsl_matrix_copy failed to allocate memory");
    214214    if (gsl_matrix_memcpy(m,m_))
    215       throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
     215      throw utility::GSL_error("Matrix::create_gsl_matrix_copy dimension mis-match");
    216216    return m;
    217217  }
    218218
    219219
    220   void matrix::delete_allocated_memory(void)
     220  void Matrix::delete_allocated_memory(void)
    221221  {
    222222    if (m_)
     
    226226
    227227
    228   void matrix::div(const matrix& other)
     228  void Matrix::div(const Matrix& other)
    229229  {
    230230    assert(m_);
     
    235235
    236236
    237   matrix::iterator matrix::end(void)
     237  Matrix::iterator Matrix::end(void)
    238238  {
    239239    return iterator(&(*this)(0,0)+rows()*columns(), 1);
     
    241241
    242242
    243   matrix::const_iterator matrix::end(void) const
     243  Matrix::const_iterator Matrix::end(void) const
    244244  {
    245245    return const_iterator(&(*this)(0,0)+rows()*columns(), 1);
     
    247247
    248248
    249   matrix::column_iterator matrix::end_column(size_t i)
     249  Matrix::column_iterator Matrix::end_column(size_t i)
    250250  {
    251251    return column_iterator(&(*this)(0,i)+rows()*columns(), this->columns());
     
    253253
    254254
    255   matrix::const_column_iterator matrix::end_column(size_t i) const
     255  Matrix::const_column_iterator Matrix::end_column(size_t i) const
    256256  {
    257257    return const_column_iterator(&(*this)(0,i)+rows()*columns(),this->columns());
     
    259259
    260260
    261   matrix::row_iterator matrix::end_row(size_t i)
     261  Matrix::row_iterator Matrix::end_row(size_t i)
    262262  {
    263263    return row_iterator(&(*this)(i,0)+columns(), 1);
     
    265265
    266266
    267   matrix::const_row_iterator matrix::end_row(size_t i) const
     267  Matrix::const_row_iterator Matrix::end_row(size_t i) const
    268268  {
    269269    return const_row_iterator(&(*this)(i,0)+columns(), 1);
     
    271271
    272272
    273   bool matrix::equal(const matrix& other, const double d) const
     273  bool Matrix::equal(const Matrix& other, const double d) const
    274274  {
    275275    if (this==&other)
     
    287287
    288288
    289   const gsl_matrix* matrix::gsl_matrix_p(void) const
     289  const gsl_matrix* Matrix::gsl_matrix_p(void) const
    290290  {
    291291    return m_;
     
    293293
    294294
    295   gsl_matrix* matrix::gsl_matrix_p(void)
     295  gsl_matrix* Matrix::gsl_matrix_p(void)
    296296  {
    297297    return m_;
     
    299299
    300300
    301   void matrix::mul(const matrix& other)
     301  void Matrix::mul(const Matrix& other)
    302302  {
    303303    assert(m_);
    304304    int status=gsl_matrix_mul_elements(m_, other.gsl_matrix_p());
    305305    if (status)
    306       throw utility::GSL_error(std::string("matrix::mul_elements",status));
    307   }
    308 
    309 
    310   void matrix::resize(size_t r, size_t c, double init_value)
     306      throw utility::GSL_error(std::string("Matrix::mul_elements",status));
     307  }
     308
     309
     310  void Matrix::resize(size_t r, size_t c, double init_value)
    311311  {
    312312    delete_allocated_memory();
     
    314314    m_ = gsl_matrix_alloc(r,c);
    315315    if (!m_)
    316       throw utility::GSL_error("matrix::matrix failed to allocate memory");
     316      throw utility::GSL_error("Matrix::Matrix failed to allocate memory");
    317317    all(init_value);
    318318
     
    326326
    327327
    328   size_t matrix::rows(void) const
     328  size_t Matrix::rows(void) const
    329329  {
    330330    return (m_ ? m_->size1 : 0);
     
    332332
    333333
    334   const VectorConstView matrix::row_const_view(size_t col) const
     334  const VectorConstView Matrix::row_const_view(size_t col) const
    335335  {
    336336    return VectorConstView(*this, col, true);
     
    338338
    339339
    340   VectorView matrix::row_view(size_t row)
     340  VectorView Matrix::row_view(size_t row)
    341341  {
    342342    VectorView res(*this, row, true);
     
    345345
    346346
    347   void matrix::swap_columns(const size_t i, const size_t j)
     347  void Matrix::swap_columns(const size_t i, const size_t j)
    348348  {
    349349    assert(m_);
    350350    int status=gsl_matrix_swap_columns(m_, i, j);
    351351    if (status)
    352       throw utility::GSL_error(std::string("matrix::swap_columns",status));
    353   }
    354 
    355 
    356   void matrix::swap_rowcol(const size_t i, const size_t j)
     352      throw utility::GSL_error(std::string("Matrix::swap_columns",status));
     353  }
     354
     355
     356  void Matrix::swap_rowcol(const size_t i, const size_t j)
    357357  {
    358358    assert(m_);
    359359    int status=gsl_matrix_swap_rowcol(m_, i, j);
    360360    if (status)
    361       throw utility::GSL_error(std::string("matrix::swap_rowcol",status));
    362   }
    363 
    364 
    365   void matrix::swap_rows(const size_t i, const size_t j)
     361      throw utility::GSL_error(std::string("Matrix::swap_rowcol",status));
     362  }
     363
     364
     365  void Matrix::swap_rows(const size_t i, const size_t j)
    366366  {
    367367    assert(m_);
    368368    int status=gsl_matrix_swap_rows(m_, i, j);
    369369    if (status)
    370       throw utility::GSL_error(std::string("matrix::swap_rows",status));
    371   }
    372 
    373 
    374   void matrix::transpose(void)
     370      throw utility::GSL_error(std::string("Matrix::swap_rows",status));
     371  }
     372
     373
     374  void Matrix::transpose(void)
    375375  {
    376376    assert(m_);
     
    380380      gsl_matrix* transposed = gsl_matrix_alloc(columns(),rows());
    381381      if (!transposed)
    382         throw utility::GSL_error("matrix::transpose failed to allocate memory");
     382        throw utility::GSL_error("Matrix::transpose failed to allocate memory");
    383383      // next line never fails if allocation above succeeded.
    384384      gsl_matrix_transpose_memcpy(transposed,m_);
     
    393393
    394394
    395   double& matrix::operator()(size_t row, size_t column)
     395  double& Matrix::operator()(size_t row, size_t column)
    396396  {
    397397    assert(m_);
     
    400400    double* d=gsl_matrix_ptr(m_, row, column);
    401401    if (!d)
    402       throw utility::GSL_error("matrix::operator()",GSL_EINVAL);
     402      throw utility::GSL_error("Matrix::operator()",GSL_EINVAL);
    403403    return *d;
    404404  }
    405405
    406406
    407   const double& matrix::operator()(size_t row, size_t column) const
     407  const double& Matrix::operator()(size_t row, size_t column) const
    408408  {
    409409    assert(row<rows());
     
    411411    const double* d=gsl_matrix_const_ptr(m_, row, column);
    412412    if (!d)
    413       throw utility::GSL_error("matrix::operator()",GSL_EINVAL);
     413      throw utility::GSL_error("Matrix::operator()",GSL_EINVAL);
    414414    return *d;
    415415  }
    416416
    417417
    418   bool matrix::operator==(const matrix& other) const
     418  bool Matrix::operator==(const Matrix& other) const
    419419  {
    420420    return equal(other);
     
    422422
    423423
    424   bool matrix::operator!=(const matrix& other) const
     424  bool Matrix::operator!=(const Matrix& other) const
    425425  {
    426426    return !equal(other);
     
    428428
    429429
    430   const matrix& matrix::operator=( const matrix& other )
     430  const Matrix& Matrix::operator=( const Matrix& other )
    431431  {
    432432    assert(other.m_);
     
    435435        resize(other.m_->size1,other.m_->size2);
    436436      if (gsl_matrix_memcpy(m_, other.gsl_matrix_p()))
    437         throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
     437        throw utility::GSL_error("Matrix::create_gsl_matrix_copy dimension mis-match");
    438438    }
    439439    return *this;
     
    441441
    442442
    443   const matrix& matrix::operator+=(const matrix& other)
     443  const Matrix& Matrix::operator+=(const Matrix& other)
    444444  {
    445445    assert(m_);
    446446    int status=gsl_matrix_add(m_, other.m_);
    447447    if (status)
    448       throw utility::GSL_error(std::string("matrix::operator+=", status));
    449     return *this;
    450   }
    451 
    452 
    453   const matrix& matrix::operator+=(const double d)
     448      throw utility::GSL_error(std::string("Matrix::operator+=", status));
     449    return *this;
     450  }
     451
     452
     453  const Matrix& Matrix::operator+=(const double d)
    454454  {
    455455    assert(m_);
     
    459459
    460460
    461   const matrix& matrix::operator-=(const matrix& other)
     461  const Matrix& Matrix::operator-=(const Matrix& other)
    462462  {
    463463    assert(m_);
    464464    int status=gsl_matrix_sub(m_, other.m_);
    465465    if (status)
    466       throw utility::GSL_error(std::string("matrix::operator-=", status));
    467     return *this;
    468   }
    469 
    470 
    471   const matrix& matrix::operator-=(const double d)
     466      throw utility::GSL_error(std::string("Matrix::operator-=", status));
     467    return *this;
     468  }
     469
     470
     471  const Matrix& Matrix::operator-=(const double d)
    472472  {
    473473    assert(m_);
     
    477477
    478478
    479   const matrix& matrix::operator*=(const matrix& other)
     479  const Matrix& Matrix::operator*=(const Matrix& other)
    480480  {
    481481    assert(m_);
     
    488488      blas_result_ = gsl_matrix_alloc(rows(),other.columns());
    489489      if (!blas_result_)
    490         throw utility::GSL_error("matrix::operator*= failed to allocate memory");
     490        throw utility::GSL_error("Matrix::operator*= failed to allocate memory");
    491491    }
    492492    gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, m_, other.m_, 0.0, blas_result_);
     
    498498
    499499
    500   const matrix& matrix::operator*=(const double d)
     500  const Matrix& Matrix::operator*=(const double d)
    501501  {
    502502    assert(m_);
     
    506506
    507507
    508   bool isnull(const matrix& other)
     508  bool isnull(const Matrix& other)
    509509  {
    510510    return gsl_matrix_isnull(other.gsl_matrix_p());
     
    512512
    513513
    514   double max(const matrix& other)
     514  double max(const Matrix& other)
    515515  {
    516516    return gsl_matrix_max(other.gsl_matrix_p());
     
    518518
    519519
    520   double min(const matrix& other)
     520  double min(const Matrix& other)
    521521  {
    522522    return gsl_matrix_min(other.gsl_matrix_p());
     
    524524
    525525
    526   void minmax_index(const matrix& other,
     526  void minmax_index(const Matrix& other,
    527527                    std::pair<size_t,size_t>& min, std::pair<size_t,size_t>& max)
    528528  {
     
    532532
    533533
    534   bool nan(const matrix& templat, matrix& flag)
     534  bool nan(const Matrix& templat, Matrix& flag)
    535535  {
    536536    size_t rows=templat.rows();
     
    551551
    552552
    553   void swap(matrix& a, matrix& b)
     553  void swap(Matrix& a, Matrix& b)
    554554  {
    555555    assert(a.gsl_matrix_p()); assert(b.gsl_matrix_p());
    556556    int status=gsl_matrix_swap(a.gsl_matrix_p(), b.gsl_matrix_p());
    557557    if (status)
    558       throw utility::GSL_error(std::string("swap(matrix&,matrix&)",status));
    559   }
    560 
    561 
    562   std::ostream& operator<<(std::ostream& s, const matrix& m)
     558      throw utility::GSL_error(std::string("swap(Matrix&,Matrix&)",status));
     559  }
     560
     561
     562  std::ostream& operator<<(std::ostream& s, const Matrix& m)
    563563  {
    564564    s.setf(std::ios::dec);
     
    576576
    577577
    578   Vector operator*(const matrix& m, const VectorBase& v)
     578  Vector operator*(const Matrix& m, const VectorBase& v)
    579579  {
    580580    utility::Vector res(m.rows());
     
    585585
    586586
    587   Vector operator*(const VectorBase& v, const matrix& m)
     587  Vector operator*(const VectorBase& v, const Matrix& m)
    588588  {
    589589    utility::Vector res(m.columns());
Note: See TracChangeset for help on using the changeset viewer.