1 | // $Id: vector.h 304 2005-05-02 14:10:50Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_gslapi_vector_ |
---|
4 | #define _theplu_gslapi_vector_ |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <vector> |
---|
8 | #include <utility> |
---|
9 | |
---|
10 | #include <gsl/gsl_vector.h> |
---|
11 | |
---|
12 | namespace theplu { |
---|
13 | namespace gslapi { |
---|
14 | /// |
---|
15 | /// This is the C++ tools interface to GSL vector. 'double' is the |
---|
16 | /// only type supported, maybe we should add a 'complex' type as |
---|
17 | /// well in the future. |
---|
18 | /// |
---|
19 | /// \par[File streams] Reading and writing vectors to file streams |
---|
20 | /// are of course supported. These are implemented without using GSL |
---|
21 | /// functionality, and thus binary read and write to streams are not |
---|
22 | /// supported. |
---|
23 | /// |
---|
24 | /// \par[Vector views] GSL vector views are supported and these are |
---|
25 | /// disguised as ordinary gslapi::vectors. A support function is |
---|
26 | /// added, gslapi::vector::is_view(), that can be used to check if |
---|
27 | /// the vector object is a view. Note that view vectors do not own |
---|
28 | /// the undelying data, and is not valid if the original vector is |
---|
29 | /// deallocated. |
---|
30 | /// |
---|
31 | class vector |
---|
32 | { |
---|
33 | public: |
---|
34 | |
---|
35 | /// |
---|
36 | /// The default constructor. |
---|
37 | /// |
---|
38 | vector(void); |
---|
39 | |
---|
40 | /// |
---|
41 | /// Constructor. Allocates memory space for \a n elements, and |
---|
42 | /// sets all elements to \a init_value. |
---|
43 | /// |
---|
44 | vector(const size_t n, const double init_value=0); |
---|
45 | |
---|
46 | /// |
---|
47 | /// The copy constructor. |
---|
48 | /// |
---|
49 | /// @note If the object to be copied is a vector view, the values |
---|
50 | /// of the view will be copied, i.e. the view is not copied. |
---|
51 | /// |
---|
52 | vector(const vector&); |
---|
53 | |
---|
54 | /// |
---|
55 | /// The vector view constructor. |
---|
56 | /// |
---|
57 | /// Create a view of vector \a v, with starting index \a offset, |
---|
58 | /// size \a n, and an optional \a stride. |
---|
59 | /// |
---|
60 | /// A vector view can be used as any vector with the difference |
---|
61 | /// that changes made to the view will also change the object that |
---|
62 | /// is viewed. Also, using the copy constructor will create a new |
---|
63 | /// vector object that is a copy of whatever is viewed. If a copy |
---|
64 | /// of the view is needed the you should use this consturctor to |
---|
65 | /// get one. |
---|
66 | /// |
---|
67 | /// @note If the object viewed by the view goes out of scope or is |
---|
68 | /// deleted, the view becomes invalid. |
---|
69 | /// |
---|
70 | vector(vector& v, size_t offset, size_t n, size_t stride=1); |
---|
71 | |
---|
72 | /// |
---|
73 | /// Constructor that imports a GSL vector. The GSL object is owned |
---|
74 | /// by the created object. |
---|
75 | /// |
---|
76 | vector(gsl_vector*); |
---|
77 | |
---|
78 | /// |
---|
79 | /// The istream constructor. |
---|
80 | /// |
---|
81 | /// Elements should be separated with white space characters, the |
---|
82 | /// end of input to the vector is at end of file marker. |
---|
83 | /// |
---|
84 | explicit vector(std::istream &); |
---|
85 | |
---|
86 | /// |
---|
87 | /// The destructor. |
---|
88 | /// |
---|
89 | ~vector(void); |
---|
90 | |
---|
91 | /// |
---|
92 | /// Vector addition, \f$this_i = this_i + other_i \; \forall i\f$. |
---|
93 | /// |
---|
94 | /// @return GSL_SUCCESS on normal exit. |
---|
95 | /// |
---|
96 | // Jari, group as vector_operators |
---|
97 | inline int add(const vector& other) { return gsl_vector_add(v_,other.v_); } |
---|
98 | |
---|
99 | /// |
---|
100 | /// Add a constant to a vector, \f$this_i = this_i + term \; |
---|
101 | /// \forall i\f$. |
---|
102 | /// |
---|
103 | /// @return GSL_SUCCESS on normal exit. |
---|
104 | /// |
---|
105 | // Jari, group as vector_operators |
---|
106 | inline int |
---|
107 | add_constant(double term) { return gsl_vector_add_constant(v_,term); } |
---|
108 | |
---|
109 | /// |
---|
110 | /// This function performs element-wise division, \f$this_i = |
---|
111 | /// this_i/other_i \; \forall i\f$. |
---|
112 | /// |
---|
113 | /// @return GSL_SUCCESS on normal exit. |
---|
114 | /// |
---|
115 | // Jari, doxygen group as Vector operators |
---|
116 | inline int div(const vector& other) { return gsl_vector_div(v_,other.v_); } |
---|
117 | |
---|
118 | /// |
---|
119 | /// @return True if all elements in the vector is zero, false |
---|
120 | /// othwerwise; |
---|
121 | /// |
---|
122 | inline bool isnull(void) const { return gsl_vector_isnull(v_); } |
---|
123 | |
---|
124 | /// |
---|
125 | /// Check if the vector object is a view (sub vector) to another |
---|
126 | /// vector. |
---|
127 | /// |
---|
128 | /// @return True if the object is a view, false othwerwise; |
---|
129 | /// |
---|
130 | inline bool isview(void) const { return view_; } |
---|
131 | |
---|
132 | /// |
---|
133 | /// @return A const pointer to the internal GSL vector, |
---|
134 | /// |
---|
135 | inline const gsl_vector* gsl_vector_pointer(void) const { return v_; } |
---|
136 | |
---|
137 | /// |
---|
138 | /// @return A pointer to the internal GSL vector, |
---|
139 | /// |
---|
140 | inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; } |
---|
141 | |
---|
142 | /// |
---|
143 | /// @return The maximum value of the vector. |
---|
144 | /// |
---|
145 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
146 | inline double max(void) const { return gsl_vector_max(v_); } |
---|
147 | |
---|
148 | /// |
---|
149 | /// @return The element index to the maximum value of the |
---|
150 | /// vector. The lowest index has precedence. |
---|
151 | /// |
---|
152 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
153 | inline size_t max_index(void) const { return gsl_vector_max_index(v_); } |
---|
154 | |
---|
155 | /// |
---|
156 | /// @return The minimum value of the vector. |
---|
157 | /// |
---|
158 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
159 | inline double min(void) const { return gsl_vector_min(v_); } |
---|
160 | |
---|
161 | /// |
---|
162 | /// @return The element index to the minimum value of the |
---|
163 | /// vector. The lowest index has precedence. |
---|
164 | /// |
---|
165 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
166 | inline size_t min_index(void) const { return gsl_vector_min_index(v_); } |
---|
167 | |
---|
168 | /// |
---|
169 | /// @return The minimum and maximum values of the vector, as the |
---|
170 | /// \a first and \a second member of the returned \a pair, |
---|
171 | /// respectively. |
---|
172 | /// |
---|
173 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
174 | std::pair<double,double> vector::minmax(void) const; |
---|
175 | |
---|
176 | /// |
---|
177 | /// @return The indecies to the minimum and maximum values of the |
---|
178 | /// vector, as the \a first and \a second member of the returned |
---|
179 | /// \a pair, respectively. The lowest index has precedence. |
---|
180 | /// |
---|
181 | // Jari, doxygen group as Finding maximum and minimum elements |
---|
182 | std::pair<size_t,size_t> vector::minmax_index(void) const; |
---|
183 | |
---|
184 | /// |
---|
185 | /// This function performs element-wise multiplication, \f$this_i = |
---|
186 | /// this_i * other_i \; \forall i\f$. |
---|
187 | /// |
---|
188 | /// @return GSL_SUCCESS on normal exit. |
---|
189 | /// |
---|
190 | // Jari, doxygen group as Vector operators |
---|
191 | inline int mul(const vector& other) { return gsl_vector_mul(v_,other.v_); } |
---|
192 | |
---|
193 | /// |
---|
194 | /// Reverse the order of elements in the vector. |
---|
195 | /// |
---|
196 | /// @return GSL_SUCCESS on normal exit. |
---|
197 | /// |
---|
198 | // Jari, doxygen group as Exchanging elements |
---|
199 | inline int reverse(void) { return gsl_vector_reverse(v_);} |
---|
200 | |
---|
201 | /// |
---|
202 | /// Rescale vector, \f$this_i = this_i * factor \; \forall i\f$. |
---|
203 | /// |
---|
204 | /// @return GSL_SUCCESS on normal exit. |
---|
205 | /// |
---|
206 | // Jari, doxygen group as Vector operators |
---|
207 | inline int scale(double factor) { return gsl_vector_scale(v_,factor); } |
---|
208 | |
---|
209 | /// |
---|
210 | /// Set all elements to \a value. |
---|
211 | /// |
---|
212 | // Jari, doxygen group as Initializing vector elements |
---|
213 | inline void set_all(const double& value) { gsl_vector_set_all(v_,value); } |
---|
214 | |
---|
215 | /// |
---|
216 | /// Makes a basis vector by setting all elements to |
---|
217 | /// zero except the \a i-th element which is set to |
---|
218 | /// one. |
---|
219 | /// |
---|
220 | // Jari, doxygen group as Initializing vector elements |
---|
221 | inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); } |
---|
222 | |
---|
223 | /// |
---|
224 | /// Set all elements to zero. |
---|
225 | /// |
---|
226 | // Jari, doxygen group as Initializing vector elements |
---|
227 | inline void set_zero(void) { gsl_vector_set_zero(v_); } |
---|
228 | |
---|
229 | /// |
---|
230 | /// @return the number of elements in the vector. |
---|
231 | /// |
---|
232 | inline size_t size(void) const { return v_->size; } |
---|
233 | |
---|
234 | /// |
---|
235 | /// Vector subtraction, \f$this_i = this_i - other_i \; \forall i\f$. |
---|
236 | /// |
---|
237 | /// @return GSL_SUCCESS on normal exit. |
---|
238 | /// |
---|
239 | // Jari, doxygen group as Vector operators |
---|
240 | inline int sub(const vector& other) { return gsl_vector_sub(v_,other.v_); } |
---|
241 | |
---|
242 | /// |
---|
243 | /// Calculate the sum of all vector elements. |
---|
244 | /// |
---|
245 | /// @return The sum. |
---|
246 | /// |
---|
247 | // Jari, doxygen group as "Extends GSL". |
---|
248 | double vector::sum(void) const; |
---|
249 | |
---|
250 | /// |
---|
251 | /// Swap vector elements by copying. The two vectors must have the |
---|
252 | /// same length. |
---|
253 | /// |
---|
254 | /// @return GSL_SUCCESS on normal exit. |
---|
255 | /// |
---|
256 | inline int swap(vector& other) { return gsl_vector_swap(v_,other.v_); } |
---|
257 | |
---|
258 | /// |
---|
259 | /// Exchange elements \a i and \a j. |
---|
260 | /// |
---|
261 | /// @return GSL_SUCCESS on normal exit. |
---|
262 | /// |
---|
263 | // Jari, doxygen group as Exchanging elements |
---|
264 | inline int |
---|
265 | swap_elements(size_t i,size_t j) { return gsl_vector_swap_elements(v_,i,j);} |
---|
266 | |
---|
267 | /// |
---|
268 | /// Element access operator. |
---|
269 | /// |
---|
270 | /// @return Reference to element \a i. |
---|
271 | /// |
---|
272 | // Jari, doxygen group as Accessing vector elements |
---|
273 | inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
274 | |
---|
275 | /// |
---|
276 | /// Const element access operator. |
---|
277 | /// |
---|
278 | /// @return The value of element \a i. |
---|
279 | /// |
---|
280 | // Jari, doxygen group as Accessing vector elements |
---|
281 | inline const double& operator()(size_t i) const |
---|
282 | { return *gsl_vector_const_ptr(v_,i); } |
---|
283 | |
---|
284 | /// |
---|
285 | /// Element access operator. |
---|
286 | /// |
---|
287 | /// @return Reference to element \a i. |
---|
288 | /// |
---|
289 | // Jari, doxygen group as Accessing vector elements |
---|
290 | inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
291 | |
---|
292 | /// |
---|
293 | /// Const element access operator. |
---|
294 | /// |
---|
295 | /// @return The value of element \a i. |
---|
296 | /// |
---|
297 | // Jari, doxygen group as Accessing vector elements |
---|
298 | inline const double& operator[](size_t i) const |
---|
299 | { return *gsl_vector_const_ptr(v_,i); } |
---|
300 | |
---|
301 | /// |
---|
302 | /// @return The negation of the vector. |
---|
303 | /// |
---|
304 | vector operator-(void) const; |
---|
305 | |
---|
306 | /// |
---|
307 | /// @return The dot product. |
---|
308 | /// |
---|
309 | double operator*( const vector &other ) const; |
---|
310 | |
---|
311 | /// |
---|
312 | /// Vector addition. |
---|
313 | /// |
---|
314 | /// @return The resulting vector. |
---|
315 | /// |
---|
316 | vector operator+( const vector& other ) const; |
---|
317 | |
---|
318 | /// |
---|
319 | /// Addition and assign operator. |
---|
320 | /// |
---|
321 | /// @return The resulting vector. |
---|
322 | /// |
---|
323 | inline vector operator+=( const vector& other ) |
---|
324 | { gsl_vector_add(v_,other.v_); return *this; } |
---|
325 | |
---|
326 | |
---|
327 | /// |
---|
328 | /// Vector subtraction. |
---|
329 | /// |
---|
330 | /// @return The resulting vector. |
---|
331 | /// |
---|
332 | vector operator-( const vector& other ) const; |
---|
333 | |
---|
334 | /// |
---|
335 | /// Subtract and assign operator. |
---|
336 | /// |
---|
337 | /// @return The resulting vector. |
---|
338 | /// |
---|
339 | inline vector operator-=( const vector& other ) |
---|
340 | { gsl_vector_sub(v_,other.v_); return *this; } |
---|
341 | |
---|
342 | /// |
---|
343 | /// The assignment operator. There is no requirements on |
---|
344 | /// dimensions. |
---|
345 | /// |
---|
346 | vector& operator=(const vector&); |
---|
347 | |
---|
348 | /// |
---|
349 | /// Multiply with scalar and assign operator. |
---|
350 | /// |
---|
351 | vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
352 | |
---|
353 | /// |
---|
354 | /// Vector with scalar multiplication. |
---|
355 | /// |
---|
356 | /// \note This operator is not implemented! |
---|
357 | /// |
---|
358 | vector operator*(const double d) const; |
---|
359 | |
---|
360 | |
---|
361 | private: |
---|
362 | |
---|
363 | /// |
---|
364 | /// Create a new copy of the internal GSL vector. |
---|
365 | /// |
---|
366 | /// Necessary memory for the new GSL vector is allocated and the |
---|
367 | /// caller is responsible for freeing the allocated memory. |
---|
368 | /// |
---|
369 | /// @return A pointer to a copy of the internal GSL vector. |
---|
370 | /// |
---|
371 | gsl_vector* TEMP_gsl_vector_copy(void) const; |
---|
372 | |
---|
373 | gsl_vector* v_; |
---|
374 | gsl_vector_view* view_; |
---|
375 | }; |
---|
376 | |
---|
377 | /// |
---|
378 | /// The output operator for the vector class. |
---|
379 | /// |
---|
380 | std::ostream& operator<<(std::ostream&, const vector& ); |
---|
381 | |
---|
382 | |
---|
383 | }} // of namespace gslapi and namespace theplu |
---|
384 | |
---|
385 | #endif |
---|