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