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 |
2121 |
13 Dec 09 |
peter |
Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2010, 2012, 2016, 2017, 2020 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/trac/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 |
|
1027 |
02 Feb 08 |
peter |
28 |
#include "VectorMutable.h" |
3623 |
09 Feb 17 |
peter |
29 |
#include "Vector.h" |
2013 |
09 Jul 09 |
peter |
30 |
#include "Exception.h" |
1967 |
08 May 09 |
peter |
31 |
#include "stl_utility.h" |
825 |
19 Mar 07 |
peter |
32 |
#include "yat/random/random.h" |
125 |
02 Aug 04 |
peter |
33 |
|
899 |
26 Sep 07 |
peter |
34 |
#include <algorithm> |
783 |
05 Mar 07 |
jari |
35 |
#include <cassert> |
1954 |
07 May 09 |
jari |
36 |
#include <cmath> |
1954 |
07 May 09 |
jari |
37 |
#include <limits> |
1680 |
29 Dec 08 |
peter |
38 |
#include <stdexcept> |
1680 |
29 Dec 08 |
peter |
39 |
#include <string> |
42 |
26 Feb 04 |
jari |
40 |
|
42 |
26 Feb 04 |
jari |
41 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
42 |
namespace yat { |
616 |
31 Aug 06 |
jari |
43 |
namespace utility { |
12 |
19 Jun 03 |
daniel |
44 |
|
12 |
19 Jun 03 |
daniel |
45 |
|
1027 |
02 Feb 08 |
peter |
46 |
VectorMutable::VectorMutable(void) |
1027 |
02 Feb 08 |
peter |
47 |
: VectorBase(NULL), vec_(NULL) |
1008 |
01 Feb 08 |
peter |
48 |
{ |
1008 |
01 Feb 08 |
peter |
49 |
} |
1008 |
01 Feb 08 |
peter |
50 |
|
1008 |
01 Feb 08 |
peter |
51 |
|
1027 |
02 Feb 08 |
peter |
52 |
VectorMutable::VectorMutable(gsl_vector* v) |
1027 |
02 Feb 08 |
peter |
53 |
: VectorBase(v), vec_(v) |
1008 |
01 Feb 08 |
peter |
54 |
{ |
1008 |
01 Feb 08 |
peter |
55 |
} |
1008 |
01 Feb 08 |
peter |
56 |
|
1008 |
01 Feb 08 |
peter |
57 |
|
1027 |
02 Feb 08 |
peter |
58 |
VectorMutable::VectorMutable(const gsl_vector* v) |
1027 |
02 Feb 08 |
peter |
59 |
: VectorBase(v), vec_(NULL) |
686 |
16 Oct 06 |
jari |
60 |
{ |
686 |
16 Oct 06 |
jari |
61 |
} |
12 |
19 Jun 03 |
daniel |
62 |
|
686 |
16 Oct 06 |
jari |
63 |
|
1027 |
02 Feb 08 |
peter |
64 |
VectorMutable::~VectorMutable(void) |
686 |
16 Oct 06 |
jari |
65 |
{ |
686 |
16 Oct 06 |
jari |
66 |
} |
686 |
16 Oct 06 |
jari |
67 |
|
686 |
16 Oct 06 |
jari |
68 |
|
1027 |
02 Feb 08 |
peter |
69 |
void VectorMutable::all(double value) |
1008 |
01 Feb 08 |
peter |
70 |
{ |
3987 |
26 Aug 20 |
peter |
71 |
if (vec_) |
3987 |
26 Aug 20 |
peter |
72 |
gsl_vector_set_all(vec_,value); |
1008 |
01 Feb 08 |
peter |
73 |
} |
1008 |
01 Feb 08 |
peter |
74 |
|
1008 |
01 Feb 08 |
peter |
75 |
|
1027 |
02 Feb 08 |
peter |
76 |
VectorMutable::iterator VectorMutable::begin(void) |
686 |
16 Oct 06 |
jari |
77 |
{ |
1038 |
05 Feb 08 |
peter |
78 |
if (vec_) |
1038 |
05 Feb 08 |
peter |
79 |
return iterator(&(this->operator()(0)), vec_->stride); |
1038 |
05 Feb 08 |
peter |
80 |
return iterator(NULL, 1); |
1008 |
01 Feb 08 |
peter |
81 |
} |
1008 |
01 Feb 08 |
peter |
82 |
|
1008 |
01 Feb 08 |
peter |
83 |
|
1027 |
02 Feb 08 |
peter |
84 |
void VectorMutable::div(const VectorBase& other) |
1008 |
01 Feb 08 |
peter |
85 |
{ |
1008 |
01 Feb 08 |
peter |
86 |
assert(vec_); |
3623 |
09 Feb 17 |
peter |
87 |
if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) { |
3623 |
09 Feb 17 |
peter |
88 |
Vector tmp(other); |
3623 |
09 Feb 17 |
peter |
89 |
div(tmp); |
3623 |
09 Feb 17 |
peter |
90 |
return; |
3623 |
09 Feb 17 |
peter |
91 |
} |
1008 |
01 Feb 08 |
peter |
92 |
int status=gsl_vector_div(vec_,other.gsl_vector_p()); |
1008 |
01 Feb 08 |
peter |
93 |
if (status) |
3743 |
12 Jul 18 |
peter |
94 |
throw utility::GSL_error("VectorMutable::div",status); |
1008 |
01 Feb 08 |
peter |
95 |
} |
1008 |
01 Feb 08 |
peter |
96 |
|
1008 |
01 Feb 08 |
peter |
97 |
|
1027 |
02 Feb 08 |
peter |
98 |
VectorMutable::iterator VectorMutable::end(void) |
42 |
26 Feb 04 |
jari |
99 |
{ |
1038 |
05 Feb 08 |
peter |
100 |
if (vec_) |
2687 |
27 Feb 12 |
peter |
101 |
return iterator(&(this->operator()(0))+vec_->stride*size(), |
1038 |
05 Feb 08 |
peter |
102 |
const_vec_->stride); |
1038 |
05 Feb 08 |
peter |
103 |
return iterator(NULL, 1); |
1008 |
01 Feb 08 |
peter |
104 |
} |
1008 |
01 Feb 08 |
peter |
105 |
|
1008 |
01 Feb 08 |
peter |
106 |
|
1027 |
02 Feb 08 |
peter |
107 |
gsl_vector* VectorMutable::gsl_vector_p(void) |
42 |
26 Feb 04 |
jari |
108 |
{ |
1008 |
01 Feb 08 |
peter |
109 |
return vec_; |
1008 |
01 Feb 08 |
peter |
110 |
} |
1008 |
01 Feb 08 |
peter |
111 |
|
1008 |
01 Feb 08 |
peter |
112 |
|
1027 |
02 Feb 08 |
peter |
113 |
void VectorMutable::mul(const VectorBase& other) |
1008 |
01 Feb 08 |
peter |
114 |
{ |
1008 |
01 Feb 08 |
peter |
115 |
assert(vec_); |
3623 |
09 Feb 17 |
peter |
116 |
if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) { |
3623 |
09 Feb 17 |
peter |
117 |
Vector tmp(other); |
3623 |
09 Feb 17 |
peter |
118 |
mul(tmp); |
3623 |
09 Feb 17 |
peter |
119 |
return; |
3623 |
09 Feb 17 |
peter |
120 |
} |
1008 |
01 Feb 08 |
peter |
121 |
int status=gsl_vector_mul(vec_,other.gsl_vector_p()); |
1008 |
01 Feb 08 |
peter |
122 |
if (status) |
3743 |
12 Jul 18 |
peter |
123 |
throw utility::GSL_error("VectorMutable::div",status); |
1008 |
01 Feb 08 |
peter |
124 |
} |
1008 |
01 Feb 08 |
peter |
125 |
|
1008 |
01 Feb 08 |
peter |
126 |
|
1027 |
02 Feb 08 |
peter |
127 |
double& VectorMutable::operator()(size_t i) |
714 |
22 Dec 06 |
jari |
128 |
{ |
1008 |
01 Feb 08 |
peter |
129 |
double* d=gsl_vector_ptr(vec_, i); |
755 |
18 Feb 07 |
jari |
130 |
if (!d) |
1027 |
02 Feb 08 |
peter |
131 |
throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL); |
755 |
18 Feb 07 |
jari |
132 |
return *d; |
714 |
22 Dec 06 |
jari |
133 |
} |
714 |
22 Dec 06 |
jari |
134 |
|
714 |
22 Dec 06 |
jari |
135 |
|
1190 |
29 Feb 08 |
peter |
136 |
const VectorMutable& VectorMutable::operator+=(const VectorBase& other) |
1190 |
29 Feb 08 |
peter |
137 |
{ |
1190 |
29 Feb 08 |
peter |
138 |
if (size()!=other.size()) |
2210 |
05 Mar 10 |
peter |
139 |
throw runtime_error("VectorMutable::operator+= size must be same."); |
1190 |
29 Feb 08 |
peter |
140 |
assert(vec_); |
3623 |
09 Feb 17 |
peter |
141 |
|
3623 |
09 Feb 17 |
peter |
// if ranges overlap be a bit careful |
3623 |
09 Feb 17 |
peter |
143 |
if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) { |
3623 |
09 Feb 17 |
peter |
144 |
Vector tmp(other); |
3623 |
09 Feb 17 |
peter |
145 |
*this += tmp; |
3623 |
09 Feb 17 |
peter |
146 |
return *this; |
3623 |
09 Feb 17 |
peter |
147 |
} |
1190 |
29 Feb 08 |
peter |
148 |
int status=gsl_vector_add(vec_, other.gsl_vector_p()); |
1190 |
29 Feb 08 |
peter |
149 |
if (status) |
3743 |
12 Jul 18 |
peter |
150 |
throw utility::GSL_error("VectorMutable::add", status); |
1190 |
29 Feb 08 |
peter |
151 |
return *this; |
1190 |
29 Feb 08 |
peter |
152 |
} |
1190 |
29 Feb 08 |
peter |
153 |
|
1190 |
29 Feb 08 |
peter |
154 |
|
1027 |
02 Feb 08 |
peter |
155 |
const VectorMutable& VectorMutable::operator+=(double d) |
1008 |
01 Feb 08 |
peter |
156 |
{ |
1008 |
01 Feb 08 |
peter |
157 |
assert(vec_); |
1008 |
01 Feb 08 |
peter |
158 |
gsl_vector_add_constant(vec_, d); |
1008 |
01 Feb 08 |
peter |
159 |
return *this; |
1008 |
01 Feb 08 |
peter |
160 |
} |
1008 |
01 Feb 08 |
peter |
161 |
|
1008 |
01 Feb 08 |
peter |
162 |
|
1027 |
02 Feb 08 |
peter |
163 |
const VectorMutable& VectorMutable::operator-=(const VectorBase& other) |
1008 |
01 Feb 08 |
peter |
164 |
{ |
1190 |
29 Feb 08 |
peter |
165 |
if (size()!=other.size()) |
2210 |
05 Mar 10 |
peter |
166 |
throw runtime_error("VectorMutable::operator-= size must be same."); |
1008 |
01 Feb 08 |
peter |
167 |
assert(vec_); |
3623 |
09 Feb 17 |
peter |
168 |
if (detail::serial_overlap(const_vec_, other.gsl_vector_p())) { |
3623 |
09 Feb 17 |
peter |
169 |
Vector tmp(other); |
3623 |
09 Feb 17 |
peter |
170 |
*this -= tmp; |
3623 |
09 Feb 17 |
peter |
171 |
return *this; |
3623 |
09 Feb 17 |
peter |
172 |
} |
1008 |
01 Feb 08 |
peter |
173 |
int status=gsl_vector_sub(vec_, other.gsl_vector_p()); |
1008 |
01 Feb 08 |
peter |
174 |
if (status) |
3743 |
12 Jul 18 |
peter |
175 |
throw utility::GSL_error("VectorMutable::sub", status); |
1008 |
01 Feb 08 |
peter |
176 |
return *this; |
1008 |
01 Feb 08 |
peter |
177 |
} |
1008 |
01 Feb 08 |
peter |
178 |
|
1008 |
01 Feb 08 |
peter |
179 |
|
1027 |
02 Feb 08 |
peter |
180 |
const VectorMutable& VectorMutable::operator-=(const double d) |
1008 |
01 Feb 08 |
peter |
181 |
{ |
1008 |
01 Feb 08 |
peter |
182 |
assert(vec_); |
1008 |
01 Feb 08 |
peter |
183 |
gsl_vector_add_constant(vec_, -d); |
1008 |
01 Feb 08 |
peter |
184 |
return *this; |
1008 |
01 Feb 08 |
peter |
185 |
} |
1008 |
01 Feb 08 |
peter |
186 |
|
1008 |
01 Feb 08 |
peter |
187 |
|
1027 |
02 Feb 08 |
peter |
188 |
const VectorMutable& VectorMutable::operator*=(double d) |
1008 |
01 Feb 08 |
peter |
189 |
{ |
1008 |
01 Feb 08 |
peter |
190 |
assert(vec_); |
1008 |
01 Feb 08 |
peter |
191 |
gsl_vector_scale(vec_, d); |
1008 |
01 Feb 08 |
peter |
192 |
return *this; |
1008 |
01 Feb 08 |
peter |
193 |
} |
1008 |
01 Feb 08 |
peter |
194 |
|
1008 |
01 Feb 08 |
peter |
195 |
|
3467 |
10 Feb 16 |
peter |
196 |
const VectorMutable& VectorMutable::operator=(const VectorBase& rhs) |
3467 |
10 Feb 16 |
peter |
197 |
{ |
3467 |
10 Feb 16 |
peter |
198 |
return assign(rhs); |
3467 |
10 Feb 16 |
peter |
199 |
} |
3467 |
10 Feb 16 |
peter |
200 |
|
3467 |
10 Feb 16 |
peter |
201 |
|
3467 |
10 Feb 16 |
peter |
202 |
const VectorMutable& VectorMutable::operator=(const VectorMutable& rhs) |
3467 |
10 Feb 16 |
peter |
203 |
{ |
3467 |
10 Feb 16 |
peter |
204 |
return assign(rhs); |
3467 |
10 Feb 16 |
peter |
205 |
} |
3467 |
10 Feb 16 |
peter |
206 |
|
3467 |
10 Feb 16 |
peter |
207 |
|
1027 |
02 Feb 08 |
peter |
208 |
void shuffle(VectorMutable& invec) |
42 |
26 Feb 04 |
jari |
209 |
{ |
1027 |
02 Feb 08 |
peter |
210 |
random::random_shuffle(invec.begin(), invec.end()); |
782 |
05 Mar 07 |
jari |
211 |
} |
782 |
05 Mar 07 |
jari |
212 |
|
782 |
05 Mar 07 |
jari |
213 |
|
1027 |
02 Feb 08 |
peter |
214 |
void sort(VectorMutable& invec) |
782 |
05 Mar 07 |
jari |
215 |
{ |
1967 |
08 May 09 |
peter |
216 |
std::sort(invec.begin(), invec.end(), less_nan<double>()); |
782 |
05 Mar 07 |
jari |
217 |
} |
782 |
05 Mar 07 |
jari |
218 |
|
782 |
05 Mar 07 |
jari |
219 |
|
686 |
16 Oct 06 |
jari |
220 |
}}} // of namespace utility, yat, and thep |