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

Last change on this file since 357 was 357, checked in by Peter, 18 years ago

updated doc and set precision to 12 for vector operator <<

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