source: trunk/src/vector.cc @ 63

Last change on this file since 63 was 63, checked in by Peter, 19 years ago

minmax function added

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