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

Last change on this file since 3169 was 3169, checked in by Peter, 8 years ago

use private function create_gsl_vector rather than calling gsl function and repeating code for error detection

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1// $Id: Vector.cc 3169 2014-01-22 07:41:42Z 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_ = create_gsl_vector(nof_rows*nof_columns);
115    size_t n=0;
116    // if gsl error handler disabled, out of bounds index will not
117    // abort the program.
118    for (size_t i=0; i<nof_rows; i++)
119      for (size_t j=0; j<nof_columns; j++)
120        gsl_vector_set( vec_, n++, data_matrix[i][j] );
121    const_vec_ = vec_;
122  }
123
124
125  Vector::~Vector(void)
126  {
127    delete_allocated_memory();
128  }
129
130
131  const Vector& Vector::assign(const VectorBase& other)
132  {
133    // avoid self assignment
134    if (this == &other)
135      return *this;
136    if (!other.size()){
137      delete_allocated_memory();
138      return *this;
139    }
140    // indirect self assignment if begin <= other.begin() < end
141    if (begin() <= other.begin() && other.begin()<end()) {
142      if (size() != other.size()) {
143        gsl_vector* tmp = create_gsl_vector_copy(other);
144        delete_allocated_memory();
145        const_vec_ = vec_ = tmp;
146      }
147      return *this;
148    }
149    if (size()==other.size()){
150      if (gsl_vector_memcpy(vec_, other.gsl_vector_p()))
151        throw utility::GSL_error("Vector::assign memcpy failed");
152    }
153    else {
154      delete_allocated_memory();
155      vec_ = create_gsl_vector_copy(other);
156    }
157    const_vec_ = vec_;
158    return *this;
159  }
160
161
162  gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const
163  {
164    if (!other.size())
165      return NULL;
166    gsl_vector* vec = create_gsl_vector(other.size());
167    if (gsl_vector_memcpy(vec, other.gsl_vector_p()))
168      throw utility::GSL_error("Vector::create_gsl_vector_copy memcpy failed");
169    return vec;
170  }
171
172
173  gsl_vector* Vector::create_gsl_vector(size_t n) const
174  {
175    if (!n)
176      return NULL;
177    gsl_vector* vec = gsl_vector_alloc(n);
178    if (!vec)
179      throw utility::GSL_error("Vector: failed to allocate memory");
180    return vec;
181  }
182
183
184  void Vector::delete_allocated_memory(void)
185  {
186    if (vec_)
187      gsl_vector_free(vec_);
188    const_vec_ = vec_ = NULL;
189  }
190
191
192  bool Vector::isview(void) const
193  {
194    return false;
195  }
196
197
198  void Vector::resize(size_t n, double init_value)
199  {
200    delete_allocated_memory();
201    if (!n)
202      return;
203    const_vec_ = vec_ = create_gsl_vector(n);
204    all(init_value);
205  }
206
207
208  void swap(Vector& v, Vector& w)
209  {
210    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
211    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
212    if (status)
213      throw utility::GSL_error(std::string("swap(Vector&,Vector&)",status));
214  }
215
216
217  const Vector& Vector::operator=( const VectorBase& other )
218  {
219    return assign(other);
220  }
221
222
223  const Vector& Vector::operator=( const Vector& other )
224  {
225    return assign(other);
226  }
227
228
229}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.