source: trunk/yat/utility/vector.cc @ 703

Last change on this file since 703 was 703, checked in by Jari Häkkinen, 16 years ago

Addresses #65 and #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1// $Id: vector.cc 703 2006-12-18 00:47:44Z jari $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2006 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 "vector.h"
28#include "matrix.h"
29#include "stl_utility.h"
30#include "utility.h"
31
32#include <iostream>
33#include <sstream>
34#include <vector>
35#include <utility>
36
37namespace theplu {
38namespace yat {
39namespace utility {
40
41
42  vector::vector(void)
43    : v_(NULL), v_const_(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)
44  {
45  }
46
47
48  vector::vector(size_t n, double init_value)
49    : v_(gsl_vector_alloc(n)), v_const_(NULL), view_(NULL), view_const_(NULL),
50      proxy_v_(v_)
51  {
52    set_all(init_value);
53  }
54
55
56  vector::vector(const vector& other)
57    : v_(other.create_gsl_vector_copy()), v_const_(NULL), view_(NULL),
58      view_const_(NULL), proxy_v_(v_)
59  {
60  }
61
62
63  vector::vector(vector& v, size_t offset, size_t n, size_t stride)
64    : v_const_(NULL), view_const_(NULL)
65  {
66    // Jari, exception handling needed here. Failure in setting up a
67    // proper gsl_vector_view is communicated by NULL pointer in the
68    // view structure (cf. GSL manual). How about GSL error state?
69    view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset,
70                                                                 stride,n));
71    proxy_v_ = v_ = &(view_->vector);
72  }
73
74
75  vector::vector(const vector& v, size_t offset, size_t n, size_t stride)
76    : v_(NULL), view_(NULL)
77  {
78    // Jari, exception handling needed here. Failure in setting up a
79    // proper gsl_vector_view is communicated by NULL pointer in the
80    // view structure (cf. GSL manual). How about GSL error state?
81    view_const_ = new gsl_vector_const_view(
82                   gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n));
83    proxy_v_ = v_const_ = &(view_const_->vector);
84  }
85
86
87  vector::vector(matrix& m, size_t i, bool row)
88    : v_const_(NULL), view_const_(NULL)
89  {
90    view_=new gsl_vector_view(row ?
91                              gsl_matrix_row   (m.gsl_matrix_p(),i) :
92                              gsl_matrix_column(m.gsl_matrix_p(),i)  );
93    proxy_v_ = v_ = &(view_->vector);
94  }
95
96
97  vector::vector(const matrix& m, size_t i, bool row)
98    : v_(NULL), view_(NULL)
99  {
100    view_const_ = new gsl_vector_const_view(row ?
101                                   gsl_matrix_const_row   (m.gsl_matrix_p(),i) :
102                                   gsl_matrix_const_column(m.gsl_matrix_p(),i) );
103    proxy_v_ = v_const_ = &(view_const_->vector);
104  }
105
106
107  vector::vector(std::istream& is, char sep)
108    throw (utility::IO_error, std::exception)
109    : v_const_(NULL), view_(NULL), view_const_(NULL)
110  {
111    // read the data file and store in stl vectors (dynamically
112    // expandable)
113    std::vector<std::vector<double> > data_matrix;
114    u_int nof_columns=0;
115    u_int nof_rows=0;
116    std::string line;
117    while(getline(is, line, '\n')) {
118      // Empty lines
119      if (!line.size())
120          continue;
121      nof_rows++;
122
123      std::vector<double> v;
124      std::string element;
125      std::stringstream ss(line);
126      bool ok=true;
127      while(ok) {
128        if(sep=='\0')
129          ok=(ss>>element);
130        else 
131          ok=getline(ss, element, sep);
132        if(!ok)
133          break;       
134
135        if(utility::is_double(element)) {
136          v.push_back(atof(element.c_str()));
137        }
138        else if (!element.size() || utility::is_nan(element)) {
139          v.push_back(std::numeric_limits<double>::quiet_NaN());
140        }
141        else {
142          // Jari, this should be communicated with as an exception.
143          // std::cerr << "Warning: '" << element
144          //           << "' is not an integer." << std::endl;
145        }
146      } 
147      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
148          v.push_back(std::numeric_limits<double>::quiet_NaN());
149      if (!nof_columns)
150        nof_columns=v.size();
151      else if (nof_rows && (nof_columns>1)) {
152        std::ostringstream s;
153        s << "vector::vector(std::istream&) data file error:\n"
154          << "    File has inconsistent number of rows (" << nof_rows
155          << ") and columns (" << nof_columns
156          << ").\n    Expected a row or a column vector.";
157        throw utility::IO_error(s.str());
158      }
159      else if (v.size()!=nof_columns) {
160        std::ostringstream s;
161        s << "vector::vector(std::istream&) data file error:\n"
162          << "    Line " << nof_rows << " has " << v.size()
163          << " columns; expected " << nof_columns << " column.";
164        throw utility::IO_error(s.str());
165      }
166      data_matrix.push_back(v);
167    }
168
169    // manipulate the state of the stream to be good
170    is.clear(std::ios::goodbit);
171    // convert the data to a gsl vector
172    proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns);
173    size_t n=0;
174    for (size_t i=0; i<nof_rows; i++)
175      for (size_t j=0; j<nof_columns; j++) 
176        gsl_vector_set( v_, n++, data_matrix[i][j] );
177  }
178
179
180  vector::~vector(void)
181  {
182    if (view_)
183      delete view_;
184    else if (view_const_)
185      delete view_const_;
186    else if (v_)
187      gsl_vector_free(v_);
188  }
189
190
191  gsl_vector* vector::create_gsl_vector_copy(void) const
192  {
193    gsl_vector* vec = gsl_vector_alloc(size());
194    gsl_vector_memcpy(vec, proxy_v_);
195    return vec;
196  }
197
198
199  std::pair<double,double> vector::minmax(void) const
200  {
201    double min, max;
202    gsl_vector_minmax(proxy_v_, &min, &max);
203    return std::pair<double,double>(min,max);
204  }
205
206
207  std::pair<size_t,size_t> vector::minmax_index(void) const
208  {
209    size_t min_index, max_index;
210    gsl_vector_minmax_index(proxy_v_, &min_index, &max_index);
211    return std::pair<size_t,size_t>(min_index,max_index);
212  }
213
214
215  double vector::sum(void) const
216  {
217    double sum = 0;
218    for (size_t i=0; i<size(); i++)
219      sum += (*this)(i);
220    return( sum );
221  }
222
223
224  bool vector::operator==(const vector& a) const
225  {
226    if (size()!=a.size())
227      return false;
228    for (size_t i=0; i<size(); ++i)
229      if (gsl_vector_get(proxy_v_,i)!=a(i))
230        return false;
231    return true;
232  }
233
234
235  double vector::operator*( const vector &other ) const
236  {
237    double res = 0.0;;
238    for ( size_t i = 0; i < size(); ++i ) 
239      res += other(i) * (*this)(i);
240    return res;
241  }
242
243
244  const vector& vector::operator=( const vector& other )
245  {
246    if( this != &other ) {
247      if (view_)
248        delete view_;
249      else if (view_const_)
250        delete view_const_;
251      else if ( v_ )
252        gsl_vector_free( v_ );
253      v_const_=NULL;
254      proxy_v_ = v_ = other.create_gsl_vector_copy();
255    }
256    return *this;
257  } 
258
259
260  const vector& vector::operator+=(const vector& other)
261  {
262    gsl_vector_add(v_,other.v_);
263    return *this;
264  }
265
266
267  const vector& vector::operator-=(const vector& other)
268  {
269    gsl_vector_sub(v_,other.v_);
270    return *this;
271  }
272
273
274  const vector& vector::operator*=(const double d)
275  {
276    gsl_vector_scale(v_,d);
277    return *this;
278  }
279
280
281  std::ostream& operator<<(std::ostream& s, const vector& a)
282  {
283    s.setf(std::ios::dec);
284    s.precision(12);
285    for (size_t j = 0; j < a.size(); ++j) {
286      s << a[j];
287      if ( (j+1)<a.size() )
288        s << s.fill();
289    }
290    return s;
291  }
292
293}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.