source: trunk/c++_tools/utility/vector.cc @ 675

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

References #83. Changing project name to yat. Compilation will fail in this revision.

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