source: trunk/yat/utility/Vector.cc @ 1275

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

Updating copyright statements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1// $Id: Vector.cc 1275 2008-04-11 06:10:12Z jari $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
5  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/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 "Vector.h"
28#include "utility.h"
29
30#include <algorithm>
31#include <cassert>
32#include <cmath>
33#include <iostream>
34#include <sstream>
35#include <utility>
36#include <vector>
37
38namespace theplu {
39namespace yat {
40namespace utility {
41
42
43  Vector::Vector(void)
44    : VectorMutable()
45  {
46  }
47
48
49  Vector::Vector(size_t n, double init_value)
50    : VectorMutable(gsl_vector_alloc(n))
51  {
52    if (!vec_)
53      throw utility::GSL_error("Vector::Vector failed to allocate memory");
54    assert(const_vec_);
55    all(init_value);
56  }
57
58
59  Vector::Vector(const Vector& other)
60    : VectorMutable(create_gsl_vector_copy(other))
61  {
62  }
63
64
65  Vector::Vector(const VectorBase& other)
66    : VectorMutable(create_gsl_vector_copy(other))
67  {
68  }
69
70
71  Vector::Vector(std::istream& is, char sep)
72    throw (utility::IO_error, std::exception)
73    : VectorMutable()
74  {
75    if (!is.good())
76      throw utility::IO_error("Vector: istream is not good");
77
78    // read the data file and store in stl vectors (dynamically
79    // expandable)
80    std::vector<std::vector<double> > data_matrix;
81    unsigned int nof_columns=0;
82    unsigned int nof_rows=0;
83    std::string line;
84    while(getline(is, line, '\n')) {
85      // Empty lines
86      if (!line.size())
87          continue;
88      nof_rows++;
89
90      std::vector<double> v;
91      std::string element;
92      std::stringstream ss(line);
93      bool ok=true;
94      while(ok) {
95        if(sep=='\0')
96          ok=(ss>>element);
97        else 
98          ok=getline(ss, element, sep);
99        if(!ok)
100          break;       
101
102        if(utility::is_double(element)) {
103          v.push_back(atof(element.c_str()));
104        }
105        else if (!element.size() || utility::is_nan(element)) {
106          v.push_back(std::numeric_limits<double>::quiet_NaN());
107        }
108        else {
109          std::stringstream ss("Warning: '");
110          ss << element << "' is not a double.";
111          throw IO_error(ss.str());
112        }
113      } 
114      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
115          v.push_back(std::numeric_limits<double>::quiet_NaN());
116      if (!nof_columns)
117        nof_columns=v.size();
118      else if (nof_rows && (nof_columns>1)) {
119        std::ostringstream s;
120        s << "Vector::Vector(std::istream&) data file error:\n"
121          << "    File has inconsistent number of rows (" << nof_rows
122          << ") and columns (" << nof_columns
123          << ").\n    Expected a row or a column Vector.";
124        throw utility::IO_error(s.str());
125      }
126      else if (v.size()!=nof_columns) {
127        std::ostringstream s;
128        s << "Vector::Vector(std::istream&) data file error:\n"
129          << "    Line " << nof_rows << " has " << v.size()
130          << " columns; expected " << nof_columns << " column.";
131        throw utility::IO_error(s.str());
132      }
133      data_matrix.push_back(v);
134    }
135
136    // manipulate the state of the stream to be good
137    is.clear(std::ios::goodbit);
138
139    if (!nof_columns || !nof_rows)
140      return;
141
142    // convert the data to a gsl vector
143    vec_ = gsl_vector_alloc(nof_rows*nof_columns);
144    if (!vec_)
145      throw utility::GSL_error("Vector::Vector failed to allocate memory");
146    size_t n=0;
147    // if gsl error handler disabled, out of bounds index will not
148    // abort the program.
149    for (size_t i=0; i<nof_rows; i++)
150      for (size_t j=0; j<nof_columns; j++) 
151        gsl_vector_set( vec_, n++, data_matrix[i][j] );
152    const_vec_ = vec_;
153  }
154
155
156  Vector::~Vector(void)
157  {
158    delete_allocated_memory();
159  }
160
161
162  const Vector& Vector::assign(const VectorBase& other)
163  {
164    // avoid self assignment
165    if (this == &other)
166      return *this;
167    if (!other.size()){
168      delete_allocated_memory();
169      return *this;
170    }
171    // indirect self assignment if begin <= other.begin() < end
172    if (begin() <= other.begin() && other.begin()<end()) {
173      if (size() != other.size()) {
174        gsl_vector* tmp = create_gsl_vector_copy(other);
175        delete_allocated_memory();
176        const_vec_ = vec_ = tmp;
177      }
178      return *this;
179    } 
180    if (size()==other.size()){
181      if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
182        throw utility::GSL_error("Vector::assign memcpy failed");
183    }
184    else {
185      delete_allocated_memory();
186      vec_ = create_gsl_vector_copy(other);
187    }
188    const_vec_ = vec_;
189    return *this;
190  } 
191
192
193  gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const
194  {
195    if (!other.size())
196      return NULL;
197    gsl_vector* vec = gsl_vector_alloc(other.size());
198    if (!vec)
199      throw utility::GSL_error("Vector::create_gsl_vector_copy failed to allocate memory");
200    if (gsl_vector_memcpy(vec, other.gsl_vector_p()))
201      throw utility::GSL_error("Vector::create_gsl_vector_copy memcpy failed");
202    return vec;
203  }
204
205
206  void Vector::delete_allocated_memory(void) 
207  {
208    if (vec_)
209      gsl_vector_free(vec_);
210    const_vec_ = vec_ = NULL;
211  }
212
213
214  bool Vector::isview(void) const
215  {
216    return false;
217  }
218
219
220  void Vector::resize(size_t n, double init_value)
221  {
222    delete_allocated_memory();
223    if (!n)
224      return;
225    const_vec_ = vec_ = gsl_vector_alloc(n);
226    if (!vec_)
227      throw utility::GSL_error("Vector::resize failed to allocate memory");
228    all(init_value);
229  }
230
231
232  void swap(Vector& v, Vector& w)
233  {
234    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
235    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
236    if (status)
237      throw utility::GSL_error(std::string("swap(Vector&,Vector&)",status));
238  }
239
240
241  const Vector& Vector::operator=( const VectorBase& other )
242  {
243    return assign(other);
244  } 
245
246
247  const Vector& Vector::operator=( const Vector& other )
248  {
249    return assign(other);
250  }
251
252
253}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.