source: trunk/yat/utility/Vector.cc

Last change on this file was 4089, checked in by Peter, 2 weeks ago

updating copyright years

  • 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 4089 2021-09-07 00:56:40Z 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, 2014, 2017, 2018, 2020, 2021 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(detail::create_gsl_vector(n, init_value))
53  {
54    assert(n==0 || vec_);
55    assert(n==0 || const_vec_);
56  }
57
58
59  Vector::Vector(const Vector& other)
60    : VectorMutable(create_gsl_vector_copy(other))
61  {
62  }
63
64
65  Vector::Vector(Vector&& other) noexcept
66    : VectorMutable(other.vec_)
67  {
68    other.vec_ = NULL;
69    other.const_vec_ = NULL;
70  }
71
72
73  Vector::Vector(const VectorBase& other)
74    : VectorMutable(create_gsl_vector_copy(other))
75  {
76  }
77
78
79  Vector::Vector(std::istream& is, char sep)
80    : VectorMutable()
81  {
82    if (!is.good())
83      throw utility::IO_error("Vector: istream is not good");
84
85    // read the data file and store in stl vectors (dynamically
86    // expandable)
87    std::vector<std::vector<double> > data_matrix;
88    try {
89      load(is, data_matrix, sep, '\n', true);
90    }
91    catch (utility::IO_error& e) {
92      std::stringstream ss(e.what());
93      ss << "\nVector(std::istream&): invalid dimensions\n";
94      std::throw_with_nested(IO_error(ss.str()));
95    }
96    catch (runtime_error& e) {
97      std::stringstream ss(e.what());
98      ss << "\nVector(std::istream&): invalid vector element\n";
99      std::throw_with_nested(IO_error(ss.str()));
100    }
101
102    unsigned int nof_rows = data_matrix.size();
103    // if stream was empty, create nothing
104    if (!nof_rows)
105      return;
106
107    unsigned int nof_columns=data_matrix[0].size();
108
109    if (!(nof_rows==1 || nof_columns==1)) {
110      std::stringstream ss;
111      ss << "\nVector(std::istream&) data file error:\n"
112         << "    File has inconsistent number of rows (" << nof_rows
113         << ") and columns (" << nof_columns
114         << ").\n    Expected a row or column Vector.";
115      throw IO_error(ss.str());
116    }
117
118    // convert the data to a gsl vector
119    vec_ = detail::create_gsl_vector(nof_rows*nof_columns);
120    size_t n=0;
121    // if gsl error handler disabled, out of bounds index will not
122    // abort the program.
123    for (size_t i=0; i<nof_rows; i++)
124      for (size_t j=0; j<nof_columns; j++)
125        gsl_vector_set( vec_, n++, data_matrix[i][j] );
126    const_vec_ = vec_;
127  }
128
129
130  Vector::~Vector(void)
131  {
132    delete_allocated_memory();
133  }
134
135
136  const Vector& Vector::assign(const gsl_vector* other)
137  {
138    // empty rhs
139    if (other == NULL) {
140      delete_allocated_memory();
141      return *this;
142    }
143
144    if (size() != other->size) {
145      gsl_vector* tmp = detail::create_gsl_vector_copy(other);
146      // delete memory once we know created copy did not fail
147      delete_allocated_memory();
148      vec_ = tmp;
149    }
150    else { // same size, no allocation needed
151      if (gsl_vector_memcpy(vec_, other))
152        throw utility::GSL_error("Vector::assign memcpy failed");
153    }
154
155    const_vec_ = vec_;
156    return *this;
157  }
158
159
160  const Vector& Vector::assign(const VectorBase& other)
161  {
162    // avoid self assignment
163    if (this == &other)
164      return *this;
165    return assign(other.gsl_vector_p());
166  }
167
168
169  gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const
170  {
171    return detail::create_gsl_vector_copy(other.gsl_vector_p());
172  }
173
174
175  void Vector::delete_allocated_memory(void)
176  {
177    gsl_vector_free(vec_);
178    const_vec_ = vec_ = NULL;
179  }
180
181
182  bool Vector::isview(void) const
183  {
184    return false;
185  }
186
187
188  void Vector::resize(size_t n, double init_value)
189  {
190    if (size() == n) {
191      all(init_value);
192      return;
193    }
194    if (!n) {
195      delete_allocated_memory();
196      return;
197    }
198    gsl_vector* tmp = detail::create_gsl_vector(n, init_value);
199    delete_allocated_memory();
200    const_vec_ = vec_ = tmp;
201  }
202
203
204  void swap(Vector& v, Vector& w)
205  {
206    assert(v.gsl_vector_p()); assert(w.gsl_vector_p());
207    int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());
208    if (status)
209      throw utility::GSL_error("swap(Vector&,Vector&)",status);
210  }
211
212
213  const Vector& Vector::operator=( const VectorBase& other )
214  {
215    return assign(other);
216  }
217
218
219  const Vector& Vector::operator=( const Vector& other )
220  {
221    return assign(other);
222  }
223
224
225  Vector& Vector::operator=(Vector&& other )
226  {
227    std::swap(vec_, other.vec_);
228    std::swap(const_vec_, other.const_vec_);
229    return *this;
230  }
231
232
233}}} // of namespace utility, yat, and thep
Note: See TracBrowser for help on using the repository browser.