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