1 | // $Id: Vector.cc 3743 2018-07-12 00:43:25Z 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 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 | |
---|
40 | namespace theplu { |
---|
41 | namespace yat { |
---|
42 | namespace 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 | #ifdef YAT_HAVE_RVALUE |
---|
66 | Vector::Vector(Vector&& other) noexcept |
---|
67 | : VectorMutable(other.vec_) |
---|
68 | { |
---|
69 | other.vec_ = NULL; |
---|
70 | other.const_vec_ = NULL; |
---|
71 | } |
---|
72 | #endif |
---|
73 | |
---|
74 | |
---|
75 | Vector::Vector(const VectorBase& other) |
---|
76 | : VectorMutable(create_gsl_vector_copy(other)) |
---|
77 | { |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | Vector::Vector(std::istream& is, char sep) |
---|
82 | throw (utility::IO_error, std::exception) |
---|
83 | : VectorMutable() |
---|
84 | { |
---|
85 | if (!is.good()) |
---|
86 | throw utility::IO_error("Vector: istream is not good"); |
---|
87 | |
---|
88 | // read the data file and store in stl vectors (dynamically |
---|
89 | // expandable) |
---|
90 | std::vector<std::vector<double> > data_matrix; |
---|
91 | try { |
---|
92 | load(is, data_matrix, sep, '\n', true); |
---|
93 | } |
---|
94 | catch (utility::IO_error& e) { |
---|
95 | std::stringstream ss(e.what()); |
---|
96 | ss << "\nVector(std::istream&): invalid dimensions\n"; |
---|
97 | throw IO_error(ss.str()); |
---|
98 | } |
---|
99 | catch (runtime_error& e) { |
---|
100 | std::stringstream ss(e.what()); |
---|
101 | ss << "\nVector(std::istream&): invalid vector element\n"; |
---|
102 | throw IO_error(ss.str()); |
---|
103 | } |
---|
104 | |
---|
105 | unsigned int nof_rows = data_matrix.size(); |
---|
106 | // if stream was empty, create nothing |
---|
107 | if (!nof_rows) |
---|
108 | return; |
---|
109 | |
---|
110 | unsigned int nof_columns=data_matrix[0].size(); |
---|
111 | |
---|
112 | if (!(nof_rows==1 || nof_columns==1)) { |
---|
113 | std::stringstream ss; |
---|
114 | ss << "\nVector(std::istream&) data file error:\n" |
---|
115 | << " File has inconsistent number of rows (" << nof_rows |
---|
116 | << ") and columns (" << nof_columns |
---|
117 | << ").\n Expected a row or column Vector."; |
---|
118 | throw IO_error(ss.str()); |
---|
119 | } |
---|
120 | |
---|
121 | // convert the data to a gsl vector |
---|
122 | vec_ = detail::create_gsl_vector(nof_rows*nof_columns); |
---|
123 | size_t n=0; |
---|
124 | // if gsl error handler disabled, out of bounds index will not |
---|
125 | // abort the program. |
---|
126 | for (size_t i=0; i<nof_rows; i++) |
---|
127 | for (size_t j=0; j<nof_columns; j++) |
---|
128 | gsl_vector_set( vec_, n++, data_matrix[i][j] ); |
---|
129 | const_vec_ = vec_; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | Vector::~Vector(void) |
---|
134 | { |
---|
135 | delete_allocated_memory(); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | const Vector& Vector::assign(const gsl_vector* other) |
---|
140 | { |
---|
141 | // empty rhs |
---|
142 | if (other == NULL) { |
---|
143 | delete_allocated_memory(); |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | if (size() != other->size) { |
---|
148 | gsl_vector* tmp = detail::create_gsl_vector_copy(other); |
---|
149 | // delete memory once we know created copy did not fail |
---|
150 | delete_allocated_memory(); |
---|
151 | vec_ = tmp; |
---|
152 | } |
---|
153 | else { // same size, no allocation needed |
---|
154 | if (gsl_vector_memcpy(vec_, other)) |
---|
155 | throw utility::GSL_error("Vector::assign memcpy failed"); |
---|
156 | } |
---|
157 | |
---|
158 | const_vec_ = vec_; |
---|
159 | return *this; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | const Vector& Vector::assign(const VectorBase& other) |
---|
164 | { |
---|
165 | // avoid self assignment |
---|
166 | if (this == &other) |
---|
167 | return *this; |
---|
168 | return assign(other.gsl_vector_p()); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const |
---|
173 | { |
---|
174 | return detail::create_gsl_vector_copy(other.gsl_vector_p()); |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | void Vector::delete_allocated_memory(void) |
---|
179 | { |
---|
180 | if (vec_) |
---|
181 | gsl_vector_free(vec_); |
---|
182 | const_vec_ = vec_ = NULL; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | bool Vector::isview(void) const |
---|
187 | { |
---|
188 | return false; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | void Vector::resize(size_t n, double init_value) |
---|
193 | { |
---|
194 | if (size() == n) { |
---|
195 | all(init_value); |
---|
196 | return; |
---|
197 | } |
---|
198 | if (!n) { |
---|
199 | delete_allocated_memory(); |
---|
200 | return; |
---|
201 | } |
---|
202 | gsl_vector* tmp = detail::create_gsl_vector(n, init_value); |
---|
203 | delete_allocated_memory(); |
---|
204 | const_vec_ = vec_ = tmp; |
---|
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("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 | #ifdef YAT_HAVE_RVALUE |
---|
230 | Vector& Vector::operator=(Vector&& other ) |
---|
231 | { |
---|
232 | std::swap(vec_, other.vec_); |
---|
233 | std::swap(const_vec_, other.const_vec_); |
---|
234 | return *this; |
---|
235 | } |
---|
236 | #endif |
---|
237 | |
---|
238 | |
---|
239 | }}} // of namespace utility, yat, and thep |
---|