source: trunk/yat/utility/matrix.cc @ 813

Last change on this file since 813 was 813, checked in by Peter, 16 years ago

Predict in NBC. Fixes #57

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1// $Id: matrix.cc 813 2007-03-16 19:30:02Z peter $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "matrix.h"
28#include "vector.h"
29#include "utility.h"
30
31#include <cassert>
32#include <cmath>
33#include <sstream>
34#include <vector>
35
36#include <gsl/gsl_blas.h>
37
38namespace theplu {
39namespace yat {
40namespace utility {
41
42
43  matrix::matrix(void)
44    : blas_result_(NULL), m_(NULL), view_(NULL), view_const_(NULL),
45      proxy_m_(NULL)
46  {
47  }
48
49
50  matrix::matrix(const size_t& r, const size_t& c, double init_value)
51    : blas_result_(NULL), m_(gsl_matrix_alloc(r,c)), view_(NULL),
52      view_const_(NULL), proxy_m_(m_)
53  {
54    if (!m_)
55      throw utility::GSL_error("matrix::matrix failed to allocate memory");
56    set_all(init_value);
57  }
58
59
60  matrix::matrix(const matrix& o)
61    : blas_result_(NULL), m_(o.create_gsl_matrix_copy()), view_(NULL),
62      view_const_(NULL), proxy_m_(m_)
63  {
64  }
65
66
67  matrix::matrix(matrix& m, size_t offset_row, size_t offset_column,
68                 size_t n_row, size_t n_column)
69    : blas_result_(NULL), view_const_(NULL)
70  {
71    view_ = new gsl_matrix_view(gsl_matrix_submatrix(m.m_,
72                                                     offset_row,offset_column,
73                                                     n_row,n_column));
74    if (!view_)
75      throw utility::GSL_error("matrix::matrix failed to setup view");
76    proxy_m_ = m_ = &(view_->matrix);
77  }
78
79
80  // Constructor that gets data from istream
81  matrix::matrix(std::istream& is, char sep) 
82    throw (utility::IO_error,std::exception)
83    : blas_result_(NULL), view_(NULL), view_const_(NULL)
84  {
85    // read the data file and store in stl vectors (dynamically
86    // expandable)
87    std::vector<std::vector<double> > data_matrix;
88    u_int nof_columns=0;
89    u_int nof_rows = 0;
90    std::string line;
91    while(getline(is, line, '\n')){
92      // Ignoring empty lines
93      if (!line.size()) {
94        continue;
95      }
96      nof_rows++;
97      std::vector<double> v;
98      std::string element;
99      std::stringstream ss(line);
100     
101      bool ok=true;
102      while(ok) {
103        if(sep=='\0')
104          ok=(ss>>element);
105        else
106          ok=getline(ss, element, sep);
107        if(!ok)
108          break;
109       
110        if(utility::is_double(element)) {
111          v.push_back(atof(element.c_str()));
112        }
113        else if (!element.size() || utility::is_nan(element)) {
114          v.push_back(std::numeric_limits<double>::quiet_NaN());
115        }
116        else {
117          std::stringstream ss("Warning: '");
118          ss << element << "' is not accepted as a matrix element.";
119          throw IO_error(ss.str());
120        }
121      }           
122      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
123          v.push_back(std::numeric_limits<double>::quiet_NaN());
124      if (!nof_columns)
125        nof_columns=v.size();
126      else if (v.size()!=nof_columns) {
127        std::ostringstream s;
128        s << "matrix::matrix(std::istream&, char) data file error: "
129          << "line " << nof_rows << " has " << v.size()
130          << " columns; expected " << nof_columns << " columns.";
131        throw utility::IO_error(s.str());
132      }
133      data_matrix.push_back(v);
134    }
135
136    // manipulate the state of the stream to be good
137    is.clear(std::ios::goodbit);
138    // convert the data to a gsl matrix
139    proxy_m_ = m_ = gsl_matrix_alloc ( nof_rows, nof_columns );
140    if (!m_)
141      throw utility::GSL_error("matrix::matrix failed to allocate memory");
142
143    // if gsl error handler disabled, out of bounds index will not
144    // abort the program.
145    for(u_int i=0;i<nof_rows;i++)
146      for(u_int j=0;j<nof_columns;j++)
147        gsl_matrix_set( m_, i, j, data_matrix[i][j] );
148  }
149
150
151  matrix::~matrix(void)
152  {
153    delete_allocated_memory();
154    if (blas_result_)
155      gsl_matrix_free(blas_result_);
156  }
157
158
159  const matrix& matrix::clone(const matrix& other)
160  {
161    if (this!=&other) {
162
163      delete_allocated_memory();
164
165      if (other.view_) {
166        view_ = new gsl_matrix_view(*other.view_);
167        proxy_m_ = m_ = &(view_->matrix);
168      }
169      else if (other.view_const_) {
170        view_const_ = new gsl_matrix_const_view(*other.view_const_);
171        proxy_m_ = &(view_const_->matrix);
172      }
173      else if (other.m_)
174        proxy_m_ = m_ = other.create_gsl_matrix_copy();
175
176      // no need to delete blas_result_ if the number of rows fit, it
177      // may be useful later.
178      if (blas_result_ && (blas_result_->size1!=rows())) {
179        gsl_matrix_free(blas_result_);
180        blas_result_=NULL;
181      }
182    }
183    return *this;
184  } 
185
186
187  size_t matrix::columns(void) const
188  {
189    if (!proxy_m_)
190      return 0;
191    return proxy_m_->size2;
192  }
193
194
195  gsl_matrix* matrix::create_gsl_matrix_copy(void) const
196  {
197    gsl_matrix* m = gsl_matrix_alloc(rows(),columns());
198    if (!m)
199      throw utility::GSL_error("matrix::create_gsl_matrix_copy failed to allocate memory");
200    if (gsl_matrix_memcpy(m,proxy_m_))
201      throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
202    return m;
203  }
204
205
206  void matrix::delete_allocated_memory(void)
207  {
208    if (view_)
209      delete view_;
210    else if (view_const_)
211      delete view_const_;
212    else if (m_)
213      gsl_matrix_free(m_);
214    blas_result_=NULL;
215    proxy_m_=m_=NULL;
216  }
217
218
219  void matrix::div(const matrix& other)
220  {
221    assert(m_);
222    int status=gsl_matrix_div_elements(m_, other.gsl_matrix_p());
223    if (status)
224      throw utility::GSL_error(std::string("matrix::div_elements",status));
225  }
226
227
228  bool matrix::equal(const matrix& other, const double d) const
229  {
230    if (this==&other)
231      return true;
232    if (columns()!=other.columns() || rows()!=other.rows())
233      return false;
234    for (size_t i=0; i<rows(); i++)
235      for (size_t j=0; j<columns(); j++)
236        // The two last condition checks are needed for NaN detection
237        if (fabs( (*this)(i,j)-other(i,j) ) > d ||
238            (*this)(i,j)!=(*this)(i,j) || other(i,j)!=other(i,j))
239          return false;
240    return true;
241  }
242
243
244  const gsl_matrix* matrix::gsl_matrix_p(void) const
245  {
246    return proxy_m_;
247  }
248
249
250  gsl_matrix* matrix::gsl_matrix_p(void)
251  {
252    return m_;
253  }
254
255
256  bool matrix::isview(void) const
257  {
258    return view_ || view_const_;
259  }
260
261
262  void matrix::mul(const matrix& other)
263  {
264    assert(m_);
265    int status=gsl_matrix_mul_elements(m_, other.gsl_matrix_p());
266    if (status)
267      throw utility::GSL_error(std::string("matrix::mul_elements",status));
268  }
269
270
271  void matrix::resize(size_t r, size_t c, double init_value)
272  {
273    delete_allocated_memory();
274
275    proxy_m_ = m_ = gsl_matrix_alloc(r,c);
276    if (!m_)
277      throw utility::GSL_error("matrix::matrix failed to allocate memory");
278    set_all(init_value);
279
280    // no need to delete blas_result_ if the number of rows fit, it
281    // may be useful later.
282    if (blas_result_ && (blas_result_->size1!=rows())) {
283      gsl_matrix_free(blas_result_);
284      blas_result_=NULL;
285    }
286  }
287
288
289  size_t matrix::rows(void) const
290  {
291    if (!proxy_m_)
292      return 0;
293    return proxy_m_->size1;
294  }
295
296
297  void matrix::set(const matrix& other)
298  {
299    assert(m_);
300    if (gsl_matrix_memcpy(m_, other.gsl_matrix_p()))
301      throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match");
302  }
303
304
305  void matrix::set_all(const double value)
306  {
307    assert(m_);
308    gsl_matrix_set_all(m_, value);
309  }
310
311
312  void matrix::set_column(const size_t column, const vector& vec)
313  {
314    assert(m_);
315    int status=gsl_matrix_set_col(m_, column, vec.gsl_vector_p());
316    if (status)
317      throw utility::GSL_error(std::string("matrix::set_column",status));
318  }
319
320
321  void matrix::set_row(const size_t row, const vector& vec)
322  {
323    assert(m_);
324    int status=gsl_matrix_set_row(m_, row, vec.gsl_vector_p());
325    if (status)
326      throw utility::GSL_error(std::string("matrix::set_row",status));
327  }
328
329
330  void matrix::swap_columns(const size_t i, const size_t j)
331  {
332    assert(m_);
333    int status=gsl_matrix_swap_columns(m_, i, j);
334    if (status)
335      throw utility::GSL_error(std::string("matrix::swap_columns",status));
336  }
337
338
339  void matrix::swap_rowcol(const size_t i, const size_t j)
340  {
341    assert(m_);
342    int status=gsl_matrix_swap_rowcol(m_, i, j);
343    if (status)
344      throw utility::GSL_error(std::string("matrix::swap_rowcol",status));
345  }
346
347
348  void matrix::swap_rows(const size_t i, const size_t j)
349  {
350    assert(m_);
351    int status=gsl_matrix_swap_rows(m_, i, j);
352    if (status)
353      throw utility::GSL_error(std::string("matrix::swap_rows",status));
354  }
355
356
357  void matrix::transpose(void)
358  {
359    assert(m_);
360    if (columns()==rows())
361      gsl_matrix_transpose(m_); // this never fails
362    else {
363      gsl_matrix* transposed = gsl_matrix_alloc(columns(),rows());
364      if (!transposed)
365        throw utility::GSL_error("matrix::transpose failed to allocate memory");
366      // next line never fails if allocation above succeeded.
367      gsl_matrix_transpose_memcpy(transposed,m_);
368      gsl_matrix_free(m_);
369      proxy_m_ = m_ = transposed;
370      if (blas_result_) {
371        gsl_matrix_free(blas_result_);
372        blas_result_=NULL;
373      }
374    }
375  }
376
377
378  double& matrix::operator()(size_t row, size_t column)
379  {
380    assert(m_);
381    assert(row<rows());
382    assert(column<columns());
383    double* d=gsl_matrix_ptr(m_, row, column);
384    if (!d)
385      throw utility::GSL_error("matrix::operator()",GSL_EINVAL);
386    return *d;
387  }
388
389
390  const double& matrix::operator()(size_t row, size_t column) const
391  {
392    assert(row<rows());
393    assert(column<columns());
394    const double* d=gsl_matrix_const_ptr(proxy_m_, row, column);
395    if (!d)
396      throw utility::GSL_error("matrix::operator()",GSL_EINVAL);
397    return *d;
398  }
399
400
401  bool matrix::operator==(const matrix& other) const
402  {
403    return equal(other);
404  }
405
406
407  bool matrix::operator!=(const matrix& other) const
408  {
409    return !equal(other);
410  }
411
412
413  const matrix& matrix::operator=( const matrix& other )
414  {
415    set(other);
416    return *this;
417  }
418
419
420  const matrix& matrix::operator+=(const matrix& other)
421  {
422    assert(m_);
423    int status=gsl_matrix_add(m_, other.proxy_m_);
424    if (status)
425      throw utility::GSL_error(std::string("matrix::operator+=", status));
426    return *this;
427  }
428
429
430  const matrix& matrix::operator+=(const double d)
431  {
432    assert(m_);
433    gsl_matrix_add_constant(m_, d);
434    return *this;
435  }
436
437
438  const matrix& matrix::operator-=(const matrix& other)
439  {
440    assert(m_);
441    int status=gsl_matrix_sub(m_, other.proxy_m_);
442    if (status)
443      throw utility::GSL_error(std::string("matrix::operator-=", status));
444    return *this;
445  }
446
447
448  const matrix& matrix::operator-=(const double d)
449  {
450    assert(m_);
451    gsl_matrix_add_constant(m_, -d);
452    return *this;
453  }
454
455
456  const matrix& matrix::operator*=(const matrix& other)
457  {
458    assert(m_);
459    if ( blas_result_ && ((blas_result_->size1!=rows()) ||
460                          (blas_result_->size2!=other.columns())) ) {
461      gsl_matrix_free(blas_result_);
462      blas_result_=NULL;
463    }
464    if (!blas_result_) {
465      blas_result_ = gsl_matrix_alloc(rows(),other.columns());
466      if (!blas_result_)
467        throw utility::GSL_error("matrix::operator*= failed to allocate memory");
468    }
469    gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, m_, other.proxy_m_, 0.0,
470                   blas_result_);
471    gsl_matrix* tmp=m_;
472    proxy_m_ = m_ = blas_result_;
473    blas_result_=tmp;
474    return *this;
475  }
476
477
478  const matrix& matrix::operator*=(const double d)
479  {
480    assert(m_);
481    gsl_matrix_scale(m_, d);
482    return *this;
483  }
484
485
486  bool isnull(const matrix& other)
487  {
488    return gsl_matrix_isnull(other.gsl_matrix_p());
489  }
490
491
492  double max(const matrix& other)
493  {
494    return gsl_matrix_max(other.gsl_matrix_p());
495  }
496
497
498  double min(const matrix& other)
499  {
500    return gsl_matrix_min(other.gsl_matrix_p());
501  }
502
503
504  void minmax_index(const matrix& other,
505                    std::pair<size_t,size_t>& min, std::pair<size_t,size_t>& max)
506  {
507    gsl_matrix_minmax_index(other.gsl_matrix_p(), &min.first, &min.second,
508                            &max.first, &max.second);
509  }
510
511
512  bool nan(const matrix& templat, matrix& flag)
513  {
514    size_t rows=templat.rows();
515    size_t columns=templat.columns();
516    if (rows!=flag.rows() && columns!=flag.columns())
517      flag.clone(matrix(rows,columns,1.0));
518    else
519      flag.set_all(1.0);
520    bool nan=false;
521    for (size_t i=0; i<rows; i++)
522      for (size_t j=0; j<columns; j++) 
523        if (std::isnan(templat(i,j))) {
524          flag(i,j)=0;
525          nan=true;
526        }
527    return nan;
528  }
529
530
531  void swap(matrix& a, matrix& b)
532  {
533    assert(a.gsl_matrix_p()); assert(b.gsl_matrix_p());
534    int status=gsl_matrix_swap(a.gsl_matrix_p(), b.gsl_matrix_p());
535    if (status)
536      throw utility::GSL_error(std::string("swap(matrix&,matrix&)",status));
537  }
538
539
540  std::ostream& operator<<(std::ostream& s, const matrix& m)
541  {
542    s.setf(std::ios::dec);
543    s.precision(12);
544    for(size_t i=0, j=0; i<m.rows(); i++)
545      for (j=0; j<m.columns(); j++) {
546        s << m(i,j);
547        if (j<m.columns()-1)
548          s << s.fill();
549        else if (i<m.rows()-1)
550          s << "\n";
551      }
552    return s;
553  }
554
555
556  vector operator*(const matrix& m, const vector& v)
557  {
558    utility::vector res(m.rows());
559    for (size_t i=0; i<res.size(); ++i)
560      res(i) = vector(m,i) * v;
561    return res;
562  }
563
564
565  vector operator*(const vector& v, const matrix& m)
566  {
567    utility::vector res(m.columns());
568    for (size_t i=0; i<res.size(); ++i)
569      res(i) = v * vector(m,i,false);
570    return res;
571  }
572
573}}} // of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.