source: trunk/lib/gslapi/vector.cc @ 535

Last change on this file since 535 was 535, checked in by Peter, 17 years ago

changed minor stupid stuff in gslapi

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