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

Last change on this file since 2881 was 2881, checked in by Peter, 9 years ago

Define PP variables in config.h rather than in CPPFLAGS. Include
config.h into all source files. Only ammend CXXFLAGS with '-Wall
-pedantic' when --enable-debug. In default mode we respect CXXFLAGS
value set by user, or set to default value '-O3'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1// $Id: Vector.cc 2881 2012-11-18 01:28:05Z peter $
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  Copyright (C) 2009, 2010, 2012 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "Vector.h"
29#include "utility.h"
30
31#include <algorithm>
32#include <cassert>
33#include <cmath>
34#include <iostream>
35#include <string>
36#include <sstream>
37#include <utility>
38#include <vector>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44
45  Vector::Vector(void)
46    : VectorMutable()
47  {
48  }
49
50
51  Vector::Vector(size_t n, double init_value)
52    : VectorMutable(create_gsl_vector(n))
53  {
54    if (n)
55      all(init_value);
56    assert(n==0 || vec_);
57    assert(n==0 || const_vec_);
58  }
59
60
61  Vector::Vector(const Vector& other)
62    : VectorMutable(create_gsl_vector_copy(other))
63  {
64  }
65
66
67  Vector::Vector(const VectorBase& other)
68    : VectorMutable(create_gsl_vector_copy(other))
69  {
70  }
71
72
73  Vector::Vector(std::istream& is, char sep)
74    throw (utility::IO_error, std::exception)
75    : VectorMutable()
76  {
77    if (!is.good())
78      throw utility::IO_error("Vector: istream is not good");
79
80    // read the data file and store in stl vectors (dynamically
81    // expandable)
82    std::vector<std::vector<double> > data_matrix;
83    try {
84      load(is, data_matrix, sep, '\n', true);
85    }
86    catch (utility::IO_error& e) {
87      std::stringstream ss(e.what());
88      ss << "\nVector(std::istream&): invalid dimensions\n";
89      throw IO_error(ss.str());
90    }
91    catch (runtime_error& e) {
92      std::stringstream ss(e.what());
93      ss << "\nVector(std::istream&): invalid vector element\n";
94      throw IO_error(ss.str());
95    }
96
97    unsigned int nof_rows = data_matrix.size();
98    // if stream was empty, create nothing
99    if (!nof_rows)
100      return;
101
102    unsigned int nof_columns=data_matrix[0].size();
103
104    if (!(nof_rows==1 || nof_columns==1)) {
105      std::stringstream ss;
106      ss << "\nVector(std::istream&) data file error:\n"
107         << "    File has inconsistent number of rows (" << nof_rows
108         << ") and columns (" << nof_columns
109         << ").\n    Expected a row or column Vector.";
110      throw IO_error(ss.str());
111    }
112
113    // convert the data to a gsl vector
114    vec_ = gsl_vector_alloc(nof_rows*nof_columns);
115    if (!vec_)
116      throw utility::GSL_error("Vector::Vector failed to allocate memory");
117    size_t n=0;
118    // if gsl error handler disabled, out of bounds index will not
119    // abort the program.
120    for (size_t i=0; i<nof_rows; i++)
121      for (size_t j=0; j<nof_columns; j++)
122        gsl_vector_set( vec_, n++, data_matrix[i][j] );
123    const_vec_ = vec_;
124  }
125
126
127  Vector::~Vector(void)
128  {
129    delete_allocated_memory();
130  }
131
132
133  const Vector& Vector::assign(const VectorBase& other)
134  {
135    // avoid self assignment
136    if (this == &other)
137      return *this;
138    if (!other.size()){
139      delete_allocated_memory();
140      return *this;
141    }
142    // indirect self assignment if begin <= other.begin() < end
143    if (begin() <= other.begin() && other.begin()<end()) {
144      if (size() != other.size()) {
145        gsl_vector* tmp = create_gsl_vector_copy(other);
146        delete_allocated_memory();
147        const_vec_ = vec_ = tmp;
148      }
149      return *this;
150    }
151    if (size()==other.size()){
152      if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
153        throw utility::GSL_error("Vector::assign memcpy failed");
154    }
155    else {
156      delete_allocated_memory();
157      vec_ = create_gsl_vector_copy(other);
158    }
159    const_vec_ = vec_;
160    return *this;
161  }
162
163
164  gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const
165  {
166    if (!other.size())
167      return NULL;
168    gsl_vector* vec = gsl_vector_alloc(other.size());
169    if (!vec)
170      throw utility::GSL_error("Vector::create_gsl_vector_copy failed to allocate memory");
171    if (gsl_vector_memcpy(vec, other.gsl_vector_p()))
172      throw utility::GSL_error("Vector::create_gsl_vector_copy memcpy failed");
173    return vec;
174  }
175
176
177  gsl_vector* Vector::create_gsl_vector(size_t n) const
178  {
179    if (!n)
180      return NULL;
181    gsl_vector* vec = gsl_vector_alloc(n);
182    if (!vec)
183      throw utility::GSL_error("Vector::create_gsl_vector failed to allocate memory");
184    return vec;
185  }
186
187
188  void Vector::delete_allocated_memory(void)
189  {
190    if (vec_)
191      gsl_vector_free(vec_);
192    const_vec_ = vec_ = NULL;
193  }
194
195
196  bool Vector::isview(void) const
197  {
198    return false;
199  }
200
201
202  void Vector::resize(size_t n, double init_value)
203  {
204    delete_allocated_memory();
205    if (!n)
206      return;
207    const_vec_ = vec_ = gsl_vector_alloc(n);
208    if (!vec_)
209      throw utility::GSL_error("Vector::resize failed to allocate memory");
210    all(init_value);
211  }
212
213
214  void swap(Vector& v, Vector& w)
215  {
216    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
217    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
218    if (status)
219      throw utility::GSL_error(std::string("swap(Vector&,Vector&)",status));
220  }
221
222
223  const Vector& Vector::operator=( const VectorBase& other )
224  {
225    return assign(other);
226  }
227
228
229  const Vector& Vector::operator=( const Vector& other )
230  {
231    return assign(other);
232  }
233
234
235}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.