source: trunk/src/vector.h @ 227

Last change on this file since 227 was 227, checked in by Jari Häkkinen, 17 years ago

Started reimplementation of the vector class.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1// $Id: vector.h 227 2005-02-01 00:52:27Z 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///
17namespace 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///
27namespace 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    /// Constructor that imports a GSL vector. The GSL object is owned
71    /// by the created object.
72    ///
73    vector(gsl_vector*);
74
75    ///
76    /// The istream constructor.
77    ///
78    /// Elements should be separated with white space characters, the
79    /// end of input to the vector is at end of file marker.
80    ///
81    explicit vector(std::istream &);
82
83    ///
84    /// The destructor.
85    ///
86    ~vector(void);
87
88    ///
89    /// Vector addition, \f$this_i = this_i + other_i \; \forall i\f$.
90    ///
91    /// @return .GSL_SUCCESS on normal exit.
92    ///
93    // Jari, doxygen group as Vector operators
94    inline int add(const vector& other) { return gsl_vector_add(v_,other.v_); }
95
96    ///
97    /// Add a constant to a vector, \f$this_i = this_i + term \;
98    /// \forall i\f$.
99    ///
100    /// @return .GSL_SUCCESS on normal exit.
101    ///
102    // Jari, doxygen group as Vector operators
103    inline int
104    add_constant(double term) { return gsl_vector_add_constant(v_,term); }
105
106    ///
107    /// This function performs element-wise division, \f$this_i =
108    /// this_i/other_i \; \forall i\f$.
109    ///
110    /// @return .GSL_SUCCESS on normal exit.
111    ///
112    // Jari, doxygen group as Vector operators
113    inline int div(const vector& other) { return gsl_vector_div(v_,other.v_); }
114
115    ///
116    /// @return True if all elements in the vector is zero, false
117    /// othwerwise;
118    ///
119    inline bool isnull(void) const { return gsl_vector_isnull(v_); }
120
121    ///
122    /// Check if the vector object is a view (sub vector) to another
123    /// vector.
124    ///
125    /// @return True if the object is a view, false othwerwise;
126    ///
127    inline bool isview(void) const { return view_; }
128
129    ///
130    /// @return A const pointer to the internal GSL vector,
131    ///
132    inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
133
134    ///
135    /// @return A pointer to the internal GSL vector,
136    ///
137    inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; }
138
139    ///
140    /// @return The maximum value of the vector.
141    ///
142    // Jari, doxygen group as Finding maximum and minimum elements
143    inline double max(void) const { return gsl_vector_max(v_); }
144
145    ///
146    /// @return The element index to the maximum value of the
147    /// vector. The lowest index has precedence.
148    ///
149    // Jari, doxygen group as Finding maximum and minimum elements
150    inline size_t max_index(void) const { return gsl_vector_max_index(v_); }
151
152    ///
153    /// @return The minimum value of the vector.
154    ///
155    // Jari, doxygen group as Finding maximum and minimum elements
156    inline double min(void) const { return gsl_vector_min(v_); }
157
158    ///
159    /// @return The element index to the minimum value of the
160    /// vector. The lowest index has precedence.
161    ///
162    // Jari, doxygen group as Finding maximum and minimum elements
163    inline size_t min_index(void) const { return gsl_vector_min_index(v_); }
164
165    ///
166    /// @return The minimum and maximum values of the vector, as the
167    /// \a first and \a second member of the returned \a pair,
168    /// respectively.
169    ///
170    // Jari, doxygen group as Finding maximum and minimum elements
171    std::pair<double,double> vector::minmax(void) const;
172
173    ///
174    /// @return The indecies to the minimum and maximum values of the
175    /// vector, as the \a first and \a second member of the returned
176    /// \a pair, respectively. The lowest index has precedence.
177    ///
178    // Jari, doxygen group as Finding maximum and minimum elements
179    std::pair<size_t,size_t> vector::minmax_index(void) const;
180
181    ///This function returns the indices of the minimum and maximum
182    ///values in the sub-vector (defined by \a subset), storing them
183    ///in imin and imax. When there are several equal minimum or
184    ///maximum elements then the lowest indices are returned. The
185    ///returned index is the index from the complete vector (not the
186    ///sub-vector)
187    ///
188    /// @return Index corresponding to the smallest and largest value.
189    ///
190    std::pair<size_t,size_t> 
191    vector::TEMP_minmax_index(const std::vector<size_t>& subset ) const;
192
193    ///
194    /// This function performs element-wise multiplication, \f$this_i =
195    /// this_i * other_i \; \forall i\f$.
196    ///
197    /// @return .GSL_SUCCESS on normal exit.
198    ///
199    // Jari, doxygen group as Vector operators
200    inline int mul(const vector& other) { return gsl_vector_mul(v_,other.v_); }
201
202    ///
203    /// Reverse the order of elements in the vector.
204    ///
205    /// @return .GSL_SUCCESS on normal exit.
206    ///
207    // Jari, doxygen group as Exchanging elements
208    inline int reverse(void) { return gsl_vector_reverse(v_);}
209
210    ///
211    /// Rescale vector, \f$this_i = this_i * factor \; \forall i\f$.
212    ///
213    /// @return .GSL_SUCCESS on normal exit.
214    ///
215    // Jari, doxygen group as Vector operators
216    inline int scale(double factor) { return gsl_vector_scale(v_,factor); }
217
218    ///
219    /// Set all elements to \a value.
220    ///
221    // Jari, doxygen group as Initializing vector elements
222    inline void set_all(const double& value) { gsl_vector_set_all(v_,value); }
223
224    ///
225    /// Makes a basis vector by setting all elements to
226    /// zero except the \a i-th element which is set to
227    /// one.
228    ///
229    // Jari, doxygen group as Initializing vector elements
230    inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); }
231   
232    ///
233    /// Set all elements to zero.
234    ///
235    // Jari, doxygen group as Initializing vector elements
236    inline void set_zero(void) { gsl_vector_set_zero(v_); }
237
238    ///
239    /// Randomly shuffles the elements in vector @return resulting
240    /// vector
241    ///
242    void vector::shuffle(void) const; 
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
Note: See TracBrowser for help on using the repository browser.