22 |
05 Aug 03 |
peter |
// $Id$ |
12 |
19 Jun 03 |
daniel |
2 |
|
570 |
05 Apr 06 |
jari |
3 |
/* |
570 |
05 Apr 06 |
jari |
Copyright (C) 2003 Daniel Dalevi, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012, 2014, 2017, 2021, 2023 Peter Johansson |
570 |
05 Apr 06 |
jari |
9 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
570 |
05 Apr 06 |
jari |
11 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
570 |
05 Apr 06 |
jari |
16 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
570 |
05 Apr 06 |
jari |
General Public License for more details. |
570 |
05 Apr 06 |
jari |
21 |
|
570 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
570 |
05 Apr 06 |
jari |
24 |
*/ |
570 |
05 Apr 06 |
jari |
25 |
|
2881 |
18 Nov 12 |
peter |
26 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
27 |
|
1120 |
21 Feb 08 |
peter |
28 |
#include "Vector.h" |
680 |
11 Oct 06 |
jari |
29 |
#include "utility.h" |
125 |
02 Aug 04 |
peter |
30 |
|
899 |
26 Sep 07 |
peter |
31 |
#include <algorithm> |
783 |
05 Mar 07 |
jari |
32 |
#include <cassert> |
789 |
10 Mar 07 |
jari |
33 |
#include <cmath> |
527 |
01 Mar 06 |
peter |
34 |
#include <iostream> |
1680 |
29 Dec 08 |
peter |
35 |
#include <string> |
341 |
07 Jun 05 |
jari |
36 |
#include <sstream> |
783 |
05 Mar 07 |
jari |
37 |
#include <utility> |
22 |
05 Aug 03 |
peter |
38 |
#include <vector> |
42 |
26 Feb 04 |
jari |
39 |
|
42 |
26 Feb 04 |
jari |
40 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
41 |
namespace yat { |
616 |
31 Aug 06 |
jari |
42 |
namespace utility { |
12 |
19 Jun 03 |
daniel |
43 |
|
12 |
19 Jun 03 |
daniel |
44 |
|
1120 |
21 Feb 08 |
peter |
45 |
Vector::Vector(void) |
1027 |
02 Feb 08 |
peter |
46 |
: VectorMutable() |
686 |
16 Oct 06 |
jari |
47 |
{ |
686 |
16 Oct 06 |
jari |
48 |
} |
12 |
19 Jun 03 |
daniel |
49 |
|
686 |
16 Oct 06 |
jari |
50 |
|
1120 |
21 Feb 08 |
peter |
51 |
Vector::Vector(size_t n, double init_value) |
3623 |
09 Feb 17 |
peter |
52 |
: VectorMutable(detail::create_gsl_vector(n, init_value)) |
686 |
16 Oct 06 |
jari |
53 |
{ |
1506 |
16 Sep 08 |
peter |
54 |
assert(n==0 || vec_); |
1506 |
16 Sep 08 |
peter |
55 |
assert(n==0 || const_vec_); |
686 |
16 Oct 06 |
jari |
56 |
} |
686 |
16 Oct 06 |
jari |
57 |
|
686 |
16 Oct 06 |
jari |
58 |
|
1120 |
21 Feb 08 |
peter |
59 |
Vector::Vector(const Vector& other) |
1027 |
02 Feb 08 |
peter |
60 |
: VectorMutable(create_gsl_vector_copy(other)) |
686 |
16 Oct 06 |
jari |
61 |
{ |
686 |
16 Oct 06 |
jari |
62 |
} |
686 |
16 Oct 06 |
jari |
63 |
|
686 |
16 Oct 06 |
jari |
64 |
|
3691 |
14 Sep 17 |
peter |
65 |
Vector::Vector(Vector&& other) noexcept |
3587 |
19 Jan 17 |
peter |
66 |
: VectorMutable(other.vec_) |
3587 |
19 Jan 17 |
peter |
67 |
{ |
3587 |
19 Jan 17 |
peter |
68 |
other.vec_ = NULL; |
3587 |
19 Jan 17 |
peter |
69 |
other.const_vec_ = NULL; |
3587 |
19 Jan 17 |
peter |
70 |
} |
3587 |
19 Jan 17 |
peter |
71 |
|
3587 |
19 Jan 17 |
peter |
72 |
|
1120 |
21 Feb 08 |
peter |
73 |
Vector::Vector(const VectorBase& other) |
1027 |
02 Feb 08 |
peter |
74 |
: VectorMutable(create_gsl_vector_copy(other)) |
42 |
26 Feb 04 |
jari |
75 |
{ |
42 |
26 Feb 04 |
jari |
76 |
} |
12 |
19 Jun 03 |
daniel |
77 |
|
12 |
19 Jun 03 |
daniel |
78 |
|
1120 |
21 Feb 08 |
peter |
79 |
Vector::Vector(std::istream& is, char sep) |
1027 |
02 Feb 08 |
peter |
80 |
: VectorMutable() |
42 |
26 Feb 04 |
jari |
81 |
{ |
1147 |
25 Feb 08 |
peter |
82 |
if (!is.good()) |
1147 |
25 Feb 08 |
peter |
83 |
throw utility::IO_error("Vector: istream is not good"); |
1147 |
25 Feb 08 |
peter |
84 |
|
328 |
30 May 05 |
jari |
// read the data file and store in stl vectors (dynamically |
328 |
30 May 05 |
jari |
// expandable) |
328 |
30 May 05 |
jari |
87 |
std::vector<std::vector<double> > data_matrix; |
1929 |
30 Apr 09 |
peter |
88 |
try { |
1929 |
30 Apr 09 |
peter |
89 |
load(is, data_matrix, sep, '\n', true); |
1929 |
30 Apr 09 |
peter |
90 |
} |
1929 |
30 Apr 09 |
peter |
91 |
catch (utility::IO_error& e) { |
1929 |
30 Apr 09 |
peter |
92 |
std::stringstream ss(e.what()); |
1929 |
30 Apr 09 |
peter |
93 |
ss << "\nVector(std::istream&): invalid dimensions\n"; |
4060 |
10 May 21 |
peter |
94 |
std::throw_with_nested(IO_error(ss.str())); |
1929 |
30 Apr 09 |
peter |
95 |
} |
2210 |
05 Mar 10 |
peter |
96 |
catch (runtime_error& e) { |
1929 |
30 Apr 09 |
peter |
97 |
std::stringstream ss(e.what()); |
1929 |
30 Apr 09 |
peter |
98 |
ss << "\nVector(std::istream&): invalid vector element\n"; |
4060 |
10 May 21 |
peter |
99 |
std::throw_with_nested(IO_error(ss.str())); |
1929 |
30 Apr 09 |
peter |
100 |
} |
686 |
16 Oct 06 |
jari |
101 |
|
1929 |
30 Apr 09 |
peter |
102 |
unsigned int nof_rows = data_matrix.size(); |
1929 |
30 Apr 09 |
peter |
// if stream was empty, create nothing |
1929 |
30 Apr 09 |
peter |
104 |
if (!nof_rows) |
1929 |
30 Apr 09 |
peter |
105 |
return; |
434 |
14 Dec 05 |
markus |
106 |
|
1929 |
30 Apr 09 |
peter |
107 |
unsigned int nof_columns=data_matrix[0].size(); |
1929 |
30 Apr 09 |
peter |
108 |
|
1929 |
30 Apr 09 |
peter |
109 |
if (!(nof_rows==1 || nof_columns==1)) { |
1929 |
30 Apr 09 |
peter |
110 |
std::stringstream ss; |
1929 |
30 Apr 09 |
peter |
111 |
ss << "\nVector(std::istream&) data file error:\n" |
1929 |
30 Apr 09 |
peter |
112 |
<< " File has inconsistent number of rows (" << nof_rows |
2687 |
27 Feb 12 |
peter |
113 |
<< ") and columns (" << nof_columns |
1929 |
30 Apr 09 |
peter |
114 |
<< ").\n Expected a row or column Vector."; |
1929 |
30 Apr 09 |
peter |
115 |
throw IO_error(ss.str()); |
42 |
26 Feb 04 |
jari |
116 |
} |
328 |
30 May 05 |
jari |
117 |
|
328 |
30 May 05 |
jari |
// convert the data to a gsl vector |
3623 |
09 Feb 17 |
peter |
119 |
vec_ = detail::create_gsl_vector(nof_rows*nof_columns); |
4294 |
04 Feb 23 |
peter |
120 |
|
4294 |
04 Feb 23 |
peter |
121 |
if (nof_rows == 1) |
4294 |
04 Feb 23 |
peter |
122 |
std::copy(data_matrix[0].begin(), data_matrix[0].end(), begin()); |
4294 |
04 Feb 23 |
peter |
123 |
else { |
4294 |
04 Feb 23 |
peter |
124 |
assert(nof_columns == 1); |
4294 |
04 Feb 23 |
peter |
125 |
for (size_t i=0; i<nof_rows; ++i) |
4294 |
04 Feb 23 |
peter |
126 |
gsl_vector_set(vec_, i, data_matrix[i][0]); |
4294 |
04 Feb 23 |
peter |
127 |
} |
1009 |
01 Feb 08 |
peter |
128 |
const_vec_ = vec_; |
42 |
26 Feb 04 |
jari |
129 |
} |
22 |
05 Aug 03 |
peter |
130 |
|
42 |
26 Feb 04 |
jari |
131 |
|
1120 |
21 Feb 08 |
peter |
132 |
Vector::~Vector(void) |
42 |
26 Feb 04 |
jari |
133 |
{ |
810 |
15 Mar 07 |
jari |
134 |
delete_allocated_memory(); |
42 |
26 Feb 04 |
jari |
135 |
} |
12 |
19 Jun 03 |
daniel |
136 |
|
12 |
19 Jun 03 |
daniel |
137 |
|
3605 |
27 Jan 17 |
peter |
138 |
const Vector& Vector::assign(const gsl_vector* other) |
1101 |
18 Feb 08 |
peter |
139 |
{ |
3605 |
27 Jan 17 |
peter |
// empty rhs |
3605 |
27 Jan 17 |
peter |
141 |
if (other == NULL) { |
1135 |
23 Feb 08 |
peter |
142 |
delete_allocated_memory(); |
1135 |
23 Feb 08 |
peter |
143 |
return *this; |
1135 |
23 Feb 08 |
peter |
144 |
} |
3605 |
27 Jan 17 |
peter |
145 |
|
3605 |
27 Jan 17 |
peter |
146 |
if (size() != other->size) { |
3623 |
09 Feb 17 |
peter |
147 |
gsl_vector* tmp = detail::create_gsl_vector_copy(other); |
3623 |
09 Feb 17 |
peter |
// delete memory once we know created copy did not fail |
3605 |
27 Jan 17 |
peter |
149 |
delete_allocated_memory(); |
3605 |
27 Jan 17 |
peter |
150 |
vec_ = tmp; |
2687 |
27 Feb 12 |
peter |
151 |
} |
3605 |
27 Jan 17 |
peter |
152 |
else { // same size, no allocation needed |
3605 |
27 Jan 17 |
peter |
153 |
if (gsl_vector_memcpy(vec_, other)) |
1136 |
23 Feb 08 |
peter |
154 |
throw utility::GSL_error("Vector::assign memcpy failed"); |
1136 |
23 Feb 08 |
peter |
155 |
} |
3605 |
27 Jan 17 |
peter |
156 |
|
1135 |
23 Feb 08 |
peter |
157 |
const_vec_ = vec_; |
1101 |
18 Feb 08 |
peter |
158 |
return *this; |
2687 |
27 Feb 12 |
peter |
159 |
} |
1101 |
18 Feb 08 |
peter |
160 |
|
1101 |
18 Feb 08 |
peter |
161 |
|
3605 |
27 Jan 17 |
peter |
162 |
const Vector& Vector::assign(const VectorBase& other) |
3605 |
27 Jan 17 |
peter |
163 |
{ |
3605 |
27 Jan 17 |
peter |
// avoid self assignment |
3605 |
27 Jan 17 |
peter |
165 |
if (this == &other) |
3605 |
27 Jan 17 |
peter |
166 |
return *this; |
3605 |
27 Jan 17 |
peter |
167 |
return assign(other.gsl_vector_p()); |
3605 |
27 Jan 17 |
peter |
168 |
} |
3605 |
27 Jan 17 |
peter |
169 |
|
3605 |
27 Jan 17 |
peter |
170 |
|
1120 |
21 Feb 08 |
peter |
171 |
gsl_vector* Vector::create_gsl_vector_copy(const VectorBase& other) const |
880 |
21 Sep 07 |
peter |
172 |
{ |
3623 |
09 Feb 17 |
peter |
173 |
return detail::create_gsl_vector_copy(other.gsl_vector_p()); |
3605 |
27 Jan 17 |
peter |
174 |
} |
3605 |
27 Jan 17 |
peter |
175 |
|
3605 |
27 Jan 17 |
peter |
176 |
|
2687 |
27 Feb 12 |
peter |
177 |
void Vector::delete_allocated_memory(void) |
810 |
15 Mar 07 |
jari |
178 |
{ |
3907 |
09 May 20 |
peter |
179 |
gsl_vector_free(vec_); |
1009 |
01 Feb 08 |
peter |
180 |
const_vec_ = vec_ = NULL; |
810 |
15 Mar 07 |
jari |
181 |
} |
810 |
15 Mar 07 |
jari |
182 |
|
810 |
15 Mar 07 |
jari |
183 |
|
1120 |
21 Feb 08 |
peter |
184 |
bool Vector::isview(void) const |
714 |
22 Dec 06 |
jari |
185 |
{ |
1009 |
01 Feb 08 |
peter |
186 |
return false; |
714 |
22 Dec 06 |
jari |
187 |
} |
714 |
22 Dec 06 |
jari |
188 |
|
714 |
22 Dec 06 |
jari |
189 |
|
1120 |
21 Feb 08 |
peter |
190 |
void Vector::resize(size_t n, double init_value) |
1099 |
18 Feb 08 |
jari |
191 |
{ |
3623 |
09 Feb 17 |
peter |
192 |
if (size() == n) { |
3623 |
09 Feb 17 |
peter |
193 |
all(init_value); |
3623 |
09 Feb 17 |
peter |
194 |
return; |
3623 |
09 Feb 17 |
peter |
195 |
} |
3623 |
09 Feb 17 |
peter |
196 |
if (!n) { |
3623 |
09 Feb 17 |
peter |
197 |
delete_allocated_memory(); |
3623 |
09 Feb 17 |
peter |
198 |
return; |
3623 |
09 Feb 17 |
peter |
199 |
} |
3623 |
09 Feb 17 |
peter |
200 |
gsl_vector* tmp = detail::create_gsl_vector(n, init_value); |
1099 |
18 Feb 08 |
jari |
201 |
delete_allocated_memory(); |
3623 |
09 Feb 17 |
peter |
202 |
const_vec_ = vec_ = tmp; |
1099 |
18 Feb 08 |
jari |
203 |
} |
1099 |
18 Feb 08 |
jari |
204 |
|
1099 |
18 Feb 08 |
jari |
205 |
|
1120 |
21 Feb 08 |
peter |
206 |
void swap(Vector& v, Vector& w) |
782 |
05 Mar 07 |
jari |
207 |
{ |
783 |
05 Mar 07 |
jari |
208 |
assert(v.gsl_vector_p()); assert(w.gsl_vector_p()); |
782 |
05 Mar 07 |
jari |
209 |
int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p()); |
782 |
05 Mar 07 |
jari |
210 |
if (status) |
3743 |
12 Jul 18 |
peter |
211 |
throw utility::GSL_error("swap(Vector&,Vector&)",status); |
782 |
05 Mar 07 |
jari |
212 |
} |
782 |
05 Mar 07 |
jari |
213 |
|
782 |
05 Mar 07 |
jari |
214 |
|
1120 |
21 Feb 08 |
peter |
215 |
const Vector& Vector::operator=( const VectorBase& other ) |
1101 |
18 Feb 08 |
peter |
216 |
{ |
1101 |
18 Feb 08 |
peter |
217 |
return assign(other); |
2687 |
27 Feb 12 |
peter |
218 |
} |
1101 |
18 Feb 08 |
peter |
219 |
|
1101 |
18 Feb 08 |
peter |
220 |
|
1120 |
21 Feb 08 |
peter |
221 |
const Vector& Vector::operator=( const Vector& other ) |
1101 |
18 Feb 08 |
peter |
222 |
{ |
1101 |
18 Feb 08 |
peter |
223 |
return assign(other); |
1101 |
18 Feb 08 |
peter |
224 |
} |
1101 |
18 Feb 08 |
peter |
225 |
|
1101 |
18 Feb 08 |
peter |
226 |
|
3589 |
19 Jan 17 |
peter |
227 |
Vector& Vector::operator=(Vector&& other ) |
3589 |
19 Jan 17 |
peter |
228 |
{ |
3589 |
19 Jan 17 |
peter |
229 |
std::swap(vec_, other.vec_); |
3589 |
19 Jan 17 |
peter |
230 |
std::swap(const_vec_, other.const_vec_); |
3589 |
19 Jan 17 |
peter |
231 |
return *this; |
3589 |
19 Jan 17 |
peter |
232 |
} |
3589 |
19 Jan 17 |
peter |
233 |
|
3589 |
19 Jan 17 |
peter |
234 |
|
686 |
16 Oct 06 |
jari |
235 |
}}} // of namespace utility, yat, and thep |