1 | // $Id: vector.h 259 2005-03-04 01:09:55Z 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 | /// @return the number of elements in the vector. |
---|
246 | /// |
---|
247 | inline size_t size(void) const { return v_->size; } |
---|
248 | |
---|
249 | /// |
---|
250 | /// Vector subtraction, \f$this_i = this_i - other_i \; \forall i\f$. |
---|
251 | /// |
---|
252 | /// @return .GSL_SUCCESS on normal exit. |
---|
253 | /// |
---|
254 | // Jari, doxygen group as Vector operators |
---|
255 | inline int sub(const vector& other) { return gsl_vector_sub(v_,other.v_); } |
---|
256 | |
---|
257 | /// |
---|
258 | /// Calculate the sum of all vector elements. |
---|
259 | /// |
---|
260 | /// @return The sum. |
---|
261 | /// |
---|
262 | // Jari, doxygen group as "Extends GSL". |
---|
263 | double vector::sum(void) const; |
---|
264 | |
---|
265 | /// |
---|
266 | /// Swap vector elements by copying. The two vectors must have the |
---|
267 | /// same length. |
---|
268 | /// |
---|
269 | /// @return .GSL_SUCCESS on normal exit. |
---|
270 | /// |
---|
271 | inline int swap(vector& other) { return gsl_vector_swap(v_,other.v_); } |
---|
272 | |
---|
273 | /// |
---|
274 | /// Exchange elements \a i and \a j. |
---|
275 | /// |
---|
276 | /// @return .GSL_SUCCESS on normal exit. |
---|
277 | /// |
---|
278 | // Jari, doxygen group as Exchanging elements |
---|
279 | inline int |
---|
280 | swap_elements(size_t i,size_t j) { return gsl_vector_swap_elements(v_,i,j);} |
---|
281 | |
---|
282 | /// |
---|
283 | /// Element access operator. |
---|
284 | /// |
---|
285 | /// @return Reference to element \a i. |
---|
286 | /// |
---|
287 | // Jari, doxygen group as Accessing vector elements |
---|
288 | inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
289 | |
---|
290 | /// |
---|
291 | /// Const element access operator. |
---|
292 | /// |
---|
293 | /// @return The value of element \a i. |
---|
294 | /// |
---|
295 | // Jari, doxygen group as Accessing vector elements |
---|
296 | inline const double& operator()(size_t i) const |
---|
297 | { return *gsl_vector_const_ptr(v_,i); } |
---|
298 | |
---|
299 | /// |
---|
300 | /// Element access operator. |
---|
301 | /// |
---|
302 | /// @return Reference to element \a i. |
---|
303 | /// |
---|
304 | // Jari, doxygen group as Accessing vector elements |
---|
305 | inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
306 | |
---|
307 | /// |
---|
308 | /// Const element access operator. |
---|
309 | /// |
---|
310 | /// @return The value of element \a i. |
---|
311 | /// |
---|
312 | // Jari, doxygen group as Accessing vector elements |
---|
313 | inline const double& operator[](size_t i) const |
---|
314 | { return *gsl_vector_const_ptr(v_,i); } |
---|
315 | |
---|
316 | /// |
---|
317 | /// @return The negation of the vector. |
---|
318 | /// |
---|
319 | vector operator-(void) const; |
---|
320 | |
---|
321 | /// |
---|
322 | /// @return The dot product. |
---|
323 | /// |
---|
324 | double operator*( const vector &other ) const; |
---|
325 | |
---|
326 | /// |
---|
327 | /// Vector addition. |
---|
328 | /// |
---|
329 | /// @return The resulting vector. |
---|
330 | /// |
---|
331 | vector operator+( const vector& other ) const; |
---|
332 | |
---|
333 | /// |
---|
334 | /// Vector subtraction. |
---|
335 | /// |
---|
336 | /// @return The resulting vector. |
---|
337 | /// |
---|
338 | vector operator-( const vector& other ) const; |
---|
339 | |
---|
340 | /// |
---|
341 | /// The assignment operator. There is no requirements on |
---|
342 | /// dimensions. |
---|
343 | /// |
---|
344 | vector& operator=(const vector&); |
---|
345 | |
---|
346 | /// |
---|
347 | /// Multiply with scalar and assign operator. |
---|
348 | /// |
---|
349 | vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
350 | |
---|
351 | /// |
---|
352 | /// Vector with scalar multiplication. |
---|
353 | /// |
---|
354 | /// \note This operator is not implemented! |
---|
355 | /// |
---|
356 | vector operator*(const double d) const; |
---|
357 | |
---|
358 | |
---|
359 | private: |
---|
360 | |
---|
361 | /// |
---|
362 | /// Create a new copy of the internal GSL vector. |
---|
363 | /// |
---|
364 | /// Necessary memory for the new GSL vector is allocated and the |
---|
365 | /// caller is responsible for freeing the allocated memory. |
---|
366 | /// |
---|
367 | /// @return A pointer to a copy of the internal GSL vector. |
---|
368 | /// |
---|
369 | gsl_vector* TEMP_gsl_vector_copy(void) const; |
---|
370 | |
---|
371 | gsl_vector* v_; |
---|
372 | gsl_vector_view* view_; |
---|
373 | }; |
---|
374 | |
---|
375 | /// |
---|
376 | /// The output operator for the vector class. |
---|
377 | /// |
---|
378 | std::ostream& operator<< ( std::ostream&, const vector& ); |
---|
379 | |
---|
380 | |
---|
381 | }} // of namespace gslapi and namespace theplu |
---|
382 | |
---|
383 | #endif |
---|