flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
gsl_vector.cpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#include "gsl_vector.hpp"
20#include "error.hpp"
21
22#include <cmath>
23#include <iostream>
24#include <string>
25#include <utility>
26
27namespace flexiblesusy {
28
29GSL_vector::GSL_vector(std::size_t size)
30{
31 if (size == 0u) {
32 return;
33 }
34
35 vec = gsl_vector_calloc(size);
36
37 if (vec == nullptr) {
38 throw OutOfMemoryError(
39 "Allocation of GSL_vector of size " + std::to_string(size)
40 + " failed.");
41 }
42}
43
50GSL_vector::GSL_vector(const gsl_vector* v)
51{
52 assign(v);
53}
54
56{
57 vec = gsl_vector_alloc(other.size());
58
59 if (vec == nullptr) {
60 throw OutOfMemoryError(
61 "Allocation of GSL_vector of size " + std::to_string(other.size())
62 + " failed.");
63 }
64
65 gsl_vector_memcpy(vec, other.vec);
66}
67
69{
70 move_assign(std::move(other));
71}
72
73GSL_vector::GSL_vector(std::initializer_list<double> list)
74{
75 if (list.size() == 0) {
76 return;
77 }
78
79 vec = gsl_vector_alloc(list.size());
80
81 if (vec == nullptr) {
82 throw OutOfMemoryError(
83 "Allocation of GSL_vector of size " + std::to_string(list.size())
84 + " failed.");
85 }
86
87 std::copy(list.begin(), list.end(), gsl_vector_ptr(vec, 0));
88}
89
91{
92 gsl_vector_free(vec);
93}
94
100void GSL_vector::assign(const gsl_vector* other)
101{
102 if (other == nullptr) {
103 gsl_vector_free(vec);
104 vec = nullptr;
105 return;
106 }
107
108 // avoid free and alloc if other has same size
109 if (size() != other->size) {
110 gsl_vector_free(vec);
111 vec = gsl_vector_alloc(other->size);
112
113 if (vec == nullptr) {
114 throw OutOfMemoryError(
115 "Allocation of GSL_vector of size " + std::to_string(other->size)
116 + " failed.");
117 }
118 }
119
120 gsl_vector_memcpy(vec, other);
121}
122
123bool GSL_vector::empty() const noexcept
124{
125 return size() == 0;
126}
127
129{
130 if (this != &rhs) {
131 assign(rhs.vec);
132 }
133
134 return *this;
135}
136
138{
139 if (this != &rhs) {
140 gsl_vector_free(vec);
141 move_assign(std::move(rhs));
142 }
143
144 return *this;
145}
146
147double& GSL_vector::operator[](std::size_t n)
148{
149 return *gsl_vector_ptr(vec, n);
150}
151
152double GSL_vector::operator[](std::size_t n) const
153{
154 return gsl_vector_get(vec, n);
155}
156
157double& GSL_vector::operator()(std::size_t n)
158{
159 range_check(n);
160 return operator[](n);
161}
162
163double GSL_vector::operator()(std::size_t n) const
164{
165 range_check(n);
166 return operator[](n);
167}
168
169std::size_t GSL_vector::size() const noexcept
170{
171 if (vec == nullptr) {
172 return 0;
173 }
174 return vec->size;
175}
176
185{
186 gsl_vector* raw = vec;
187 vec = nullptr;
188 return raw;
189}
190
191const gsl_vector* GSL_vector::raw() const noexcept
192{
193 return vec;
194}
195
196gsl_vector* GSL_vector::raw() noexcept
197{
198 return vec;
199}
200
201void GSL_vector::set_all(double value) noexcept
202{
203 if (vec != nullptr) {
204 gsl_vector_set_all(vec, value);
205 }
206}
207
208std::ostream& operator<<(std::ostream& ostr, const GSL_vector& vec)
209{
210 ostr << "(";
211
212 for (std::size_t i = 0; i < vec.size(); i++) {
213 ostr << vec[i];
214 if (i < vec.size() - 1) {
215 ostr << ", ";
216 }
217 }
218
219 ostr << ")";
220
221 return ostr;
222}
223
225{
226 vec = other.vec;
227 other.vec = nullptr;
228}
229
230void GSL_vector::range_check(std::size_t n) const
231{
232 if (vec == nullptr) {
233 throw OutOfBoundsError(
234 "GSL_vector::operator[]: index " + std::to_string(n)
235 + " out of range for vector of size 0.");
236 }
237
238 if (n >= size()) {
239 throw OutOfBoundsError(
240 "GSL_vector::operator[]: index " + std::to_string(n)
241 + " out of range for vector of size " + std::to_string(size()) + ".");
242 }
243}
244
245double* begin(GSL_vector& v)
246{
247 if (!v.empty()) {
248 return gsl_vector_ptr(v.raw(), 0);
249 }
250
251 return nullptr;
252}
253
254double* end(GSL_vector& v)
255{
256 if (!v.empty()) {
257 double* last = gsl_vector_ptr(v.raw(), v.size() - 1);
258 return ++last;
259 }
260
261 return nullptr;
262}
263
264const double* cbegin(const GSL_vector& v)
265{
266 if (!v.empty()) {
267 return gsl_vector_const_ptr(v.raw(), 0);
268 }
269
270 return nullptr;
271}
272
273const double* cend(const GSL_vector& v)
274{
275 if (!v.empty()) {
276 const double* last = gsl_vector_const_ptr(v.raw(), v.size() - 1);
277 return ++last;
278 }
279
280 return nullptr;
281}
282
290bool is_finite(const GSL_vector& v)
291{
292 for (std::size_t i = 0; i < v.size(); i++) {
293 if (!std::isfinite(v[i])) {
294 return false;
295 }
296 }
297
298 return true;
299}
300
301} // namespace flexiblesusy
void assign(const gsl_vector *)
assign from gsl_vector
Definition: gsl_vector.cpp:100
void set_all(double) noexcept
set all elemets to same value
Definition: gsl_vector.cpp:201
double & operator[](std::size_t)
element read/write access
Definition: gsl_vector.cpp:147
const gsl_vector * raw() const noexcept
get raw pointer
Definition: gsl_vector.cpp:191
void range_check(std::size_t) const
Definition: gsl_vector.cpp:230
bool empty() const noexcept
check if empty
Definition: gsl_vector.cpp:123
gsl::owner< gsl_vector > * release() noexcept
release raw pointer from this object
Definition: gsl_vector.cpp:184
void move_assign(GSL_vector &&) noexcept
move assign
Definition: gsl_vector.cpp:224
double & operator()(std::size_t)
element read/write access w/ range check
Definition: gsl_vector.cpp:157
std::size_t size() const noexcept
number of elements
Definition: gsl_vector.cpp:169
GSL_vector & operator=(const GSL_vector &)
Definition: gsl_vector.cpp:128
gsl::owner< gsl_vector > * vec
raw gsl_vector
Definition: gsl_vector.hpp:55
Out of bounds access.
Definition: error.hpp:208
Not enough memory.
Definition: error.hpp:194
double * end(GSL_vector &v)
iterator to end of GSL_vector
Definition: gsl_vector.cpp:254
std::string to_string(char a)
double * begin(GSL_vector &v)
iterator to begin of GSL_vector
Definition: gsl_vector.cpp:245
bool is_finite(const gsl_vector *x)
Returns true if GSL vector contains only finite elements, false otherwise.
Definition: gsl_utils.cpp:32
const double * cbegin(const GSL_vector &v)
const iterator to begin of GSL_vector
Definition: gsl_vector.cpp:264
std::ostream & operator<<(std::ostream &ostr, const Dynamic_array_view< ElementType > &av)
Definition: array_view.hpp:143
const double * cend(const GSL_vector &v)
const iterator to end of GSL_vector
Definition: gsl_vector.cpp:273