source: trunk/src/vector.cc @ 257

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

Added vector views and made vector member functions to be non-member functions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// $Id: vector.cc 257 2005-03-04 00:53:34Z jari $
2
3#include "vector.h"
4#include "random_singleton.h"
5
6#include <iostream>
7#include <sstream>
8#include <string>
9#include <vector>
10#include <utility>
11
12
13
14
15
16namespace theplu {
17namespace gslapi {
18
19
20
21  vector::vector(void)
22    : v_(NULL), view_(NULL)
23  {
24  }
25
26
27
28  vector::vector(const size_t n,const double init_value)
29    : view_(NULL)
30  {
31    v_ = gsl_vector_alloc(n);
32    set_all(init_value);
33  }
34
35
36
37  vector::vector(const vector& other)
38    : view_(NULL)
39  {
40    v_ = other.TEMP_gsl_vector_copy();
41  }
42
43
44
45  vector::vector(vector& v, size_t offset, size_t n, size_t stride)
46  {
47    view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset,
48                                                                 stride,n));
49    v_ = &(view_->vector);
50  }
51
52
53
54  vector::vector(gsl_vector* vec)
55    : v_(vec), view_(NULL)
56  {
57  }
58
59
60
61  vector::vector(std::istream& is)
62    : view_(NULL)
63  {
64    using namespace std;
65 
66    // read the data file and store in stl vectors (dynamically expandable)
67    std::vector<std::vector<double> > data_matrix;
68    u_int nof_columns=0;
69    u_int nof_rows = 0;
70    string s;
71    for (nof_rows = 0; getline(is, s, '\n'); nof_rows++) {
72      istringstream line(s);
73      std::vector<double> v;
74      string tmp_string;
75      while (line >> tmp_string) {
76        if(!is.good()) {
77          // Jari. change to throw exception, remove unnecessary includes
78          cerr << "vector::vector(std::istream&): "
79               << "error reading data file!" << endl;
80          exit(1);
81        }
82        double t=atof(tmp_string.c_str());
83        // Here we should check that it actually was a double!!!!!
84        v.push_back(t);
85      }
86
87      // Ignoring empty lines
88      if(!v.size()){
89        nof_rows--;
90        continue;
91      }
92
93      if(nof_columns==0)
94        nof_columns=v.size();
95      else if(v.size()!=nof_columns) {
96        // Jari. change to throw exception, remove unnecessary includes
97        cerr << "vector::vector(std::istream&) data file error: "
98             << "line" << nof_rows+1 << " has " << v.size() 
99             << " columns; expected " << nof_columns
100             << " columns"
101             << endl; 
102        exit(1);
103      }
104      data_matrix.push_back(v);
105    }
106 
107    // manipulate the state of the stream to be good
108    is.clear(std::ios::goodbit);
109 
110    // convert the data to a gsl vector and check that data file is a
111    // column vector or a row vector
112    if(nof_columns==1) {
113      v_ = gsl_vector_alloc ( nof_rows );
114      for(u_int i=0;i<nof_rows;i++) 
115        gsl_vector_set( v_, i, data_matrix[i][0] );
116    }
117    else if(nof_rows==1){
118      v_ = gsl_vector_alloc ( nof_columns );
119      for(u_int i=0;i<nof_columns;i++) 
120        gsl_vector_set( v_, i, data_matrix[0][i] );
121    }
122    else {
123      // Jari. change to throw exception, remove unnecessary includes
124      cerr << "vector::vector(std::istream&) data file error: "
125           << "file has " << nof_rows << " rows and " << nof_columns
126           << " columns; expected a row vector or a column vector"
127           << endl;
128      exit(1);
129    }
130  }
131
132
133
134  vector::~vector(void)
135  {
136    if (view_)
137      delete view_;
138    else if (v_)
139      gsl_vector_free(v_);
140    v_=NULL;
141  }
142
143
144
145  gsl_vector* vector::TEMP_gsl_vector_copy(void) const
146  {
147    gsl_vector* vec = gsl_vector_alloc(size());
148    gsl_vector_memcpy(vec,v_);
149    return vec;
150  }
151
152
153
154  std::pair<double,double> vector::minmax(void) const
155  {
156    double min, max;
157    gsl_vector_minmax(v_,&min,&max);
158    return std::pair<double,double>(min,max);
159  }
160
161
162
163  std::pair<size_t,size_t> vector::minmax_index(void) const
164  {
165    size_t min_index, max_index;
166    gsl_vector_minmax_index(v_,&min_index,&max_index);
167    return std::pair<size_t,size_t>(min_index,max_index);
168  }
169
170
171
172  void vector::shuffle(void) const
173  {
174    vector vec(*this);
175    theplu::cpptools::random_singleton* 
176      rnd=theplu::cpptools::random_singleton::get_instance();
177    for (size_t i=0; i<vec.size(); i++){
178      size_t index = rnd->get_uniform_int(vec.size()-i);
179      gsl_vector_set(v_,i,vec(index));
180      vec(index)=vec(vec.size()-i-1);
181    }
182  }
183
184  double vector::sum(void) const
185  {
186    double sum = 0;
187    for (size_t i=0; i<size(); i++)
188      sum += gsl_vector_get( v_, i );
189    return( sum );
190  } 
191
192
193
194  vector vector::operator-(void) const
195  {
196    vector res( *this );
197    gsl_vector_scale (res.v_,-1.);
198    return res;
199  }
200
201
202
203  double vector::operator*( const vector &other ) const
204  {
205    // Jari, check for gsl_support
206    double res = 0.0;;
207    for ( size_t i = 0; i < size(); ++i ) 
208      res += other[i] * (*this)[i];
209    return res;
210  }
211
212
213
214  vector vector::operator+(const vector &other) const
215  {
216    vector res(*this);
217    gsl_vector_add(res.v_,other.v_);
218    return res;
219  }
220
221
222
223  vector vector::operator-( const vector &other ) const
224  {
225    vector res( *this );
226    gsl_vector_sub(res.v_,other.v_);
227    return res;
228  }
229
230
231
232  vector& vector::operator=( const vector& other )
233  {
234    if( this != &other ) {
235      if ( v_ )
236        gsl_vector_free( v_ );
237      v_ = other.TEMP_gsl_vector_copy();
238    }
239    return *this;
240  } 
241
242
243
244  std::ostream& theplu::gslapi::operator<<(std::ostream& s, const vector& a)
245  {
246    s.setf(std::ios::fixed);
247
248    for (size_t j = 0; j < a.size(); ++j) {
249      s << a[j];
250      if ( (j+1)<a.size() )
251        s << " ";
252    }
253
254    return s;
255  }
256
257
258}} // of namespace gslapi and namespace thep
Note: See TracBrowser for help on using the repository browser.