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

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

Addresses #436.

  • 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 1486 2008-09-09 21:17:19Z 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://dev.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 3 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 (!element.size())
103          v.push_back(std::numeric_limits<double>::quiet_NaN());
104        else {
105          try {
106            v.push_back(convert<double>(element));
107          }
108          catch (std::runtime_error& e) {
109            std::stringstream ss(e.what());
110            ss << "\nVector.cc: " << element
111               << " is not accepted as a Vector element\n";
112            throw IO_error(ss.str());
113          }
114        }
115      } 
116      if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator
117          v.push_back(std::numeric_limits<double>::quiet_NaN());
118      if (!nof_columns)
119        nof_columns=v.size();
120      else if (nof_rows && (nof_columns>1)) {
121        std::ostringstream s;
122        s << "Vector::Vector(std::istream&) data file error:\n"
123          << "    File has inconsistent number of rows (" << nof_rows
124          << ") and columns (" << nof_columns
125          << ").\n    Expected a row or a column Vector.";
126        throw utility::IO_error(s.str());
127      }
128      else if (v.size()!=nof_columns) {
129        std::ostringstream s;
130        s << "Vector::Vector(std::istream&) data file error:\n"
131          << "    Line " << nof_rows << " has " << v.size()
132          << " columns; expected " << nof_columns << " column.";
133        throw utility::IO_error(s.str());
134      }
135      data_matrix.push_back(v);
136    }
137
138    // manipulate the state of the stream to be good
139    is.clear(std::ios::goodbit);
140
141    if (!nof_columns || !nof_rows)
142      return;
143
144    // convert the data to a gsl vector
145    vec_ = gsl_vector_alloc(nof_rows*nof_columns);
146    if (!vec_)
147      throw utility::GSL_error("Vector::Vector failed to allocate memory");
148    size_t n=0;
149    // if gsl error handler disabled, out of bounds index will not
150    // abort the program.
151    for (size_t i=0; i<nof_rows; i++)
152      for (size_t j=0; j<nof_columns; j++) 
153        gsl_vector_set( vec_, n++, data_matrix[i][j] );
154    const_vec_ = vec_;
155  }
156
157
158  Vector::~Vector(void)
159  {
160    delete_allocated_memory();
161  }
162
163
164  const Vector& Vector::assign(const VectorBase& other)
165  {
166    // avoid self assignment
167    if (this == &other)
168      return *this;
169    if (!other.size()){
170      delete_allocated_memory();
171      return *this;
172    }
173    // indirect self assignment if begin <= other.begin() < end
174    if (begin() <= other.begin() && other.begin()<end()) {
175      if (size() != other.size()) {
176        gsl_vector* tmp = create_gsl_vector_copy(other);
177        delete_allocated_memory();
178        const_vec_ = vec_ = tmp;
179      }
180      return *this;
181    } 
182    if (size()==other.size()){
183      if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
184        throw utility::GSL_error("Vector::assign memcpy failed");
185    }
186    else {
187      delete_allocated_memory();
188      vec_ = create_gsl_vector_copy(other);
189    }
190    const_vec_ = vec_;
191    return *this;
192  } 
193
194
195  gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const
196  {
197    if (!other.size())
198      return NULL;
199    gsl_vector* vec = gsl_vector_alloc(other.size());
200    if (!vec)
201      throw utility::GSL_error("Vector::create_gsl_vector_copy failed to allocate memory");
202    if (gsl_vector_memcpy(vec, other.gsl_vector_p()))
203      throw utility::GSL_error("Vector::create_gsl_vector_copy memcpy failed");
204    return vec;
205  }
206
207
208  void Vector::delete_allocated_memory(void) 
209  {
210    if (vec_)
211      gsl_vector_free(vec_);
212    const_vec_ = vec_ = NULL;
213  }
214
215
216  bool Vector::isview(void) const
217  {
218    return false;
219  }
220
221
222  void Vector::resize(size_t n, double init_value)
223  {
224    delete_allocated_memory();
225    if (!n)
226      return;
227    const_vec_ = vec_ = gsl_vector_alloc(n);
228    if (!vec_)
229      throw utility::GSL_error("Vector::resize failed to allocate memory");
230    all(init_value);
231  }
232
233
234  void swap(Vector& v, Vector& w)
235  {
236    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
237    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
238    if (status)
239      throw utility::GSL_error(std::string("swap(Vector&,Vector&)",status));
240  }
241
242
243  const Vector& Vector::operator=( const VectorBase& other )
244  {
245    return assign(other);
246  } 
247
248
249  const Vector& Vector::operator=( const Vector& other )
250  {
251    return assign(other);
252  }
253
254
255}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.