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