source: trunk/src/vector.h @ 142

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

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1// $Id: vector.h 142 2004-09-04 22:04:29Z 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.
31  ///
32
33  class vector
34  {
35  public:
36
37    ///
38    /// The default constructor.
39    ///
40    vector(void);
41
42    ///
43    /// Constructor. Allocates memory space for \a n elements, and
44    /// sets all elements to \a init_value.
45    ///
46    vector(const size_t n, const double init_value=0);
47
48    ///
49    /// The copy constructor, creating a new sub-vector defined by \a
50    /// index (default is to copy the whole vector).
51    ///
52    vector(const vector&, 
53           const std::vector<size_t>& index = std::vector<size_t>());
54
55    ///
56    /// Constructor that imports a GSL vector. The GSL object is owned
57    /// by the created object.
58    ///
59    vector(gsl_vector*);
60
61    ///
62    /// The istream constructor.
63    ///
64    /// Elements should be separated with white space characters, the
65    /// end of input to the vector is at end of file marker.
66    ///
67    explicit vector(std::istream &);
68
69    ///
70    /// The destructor.
71    ///
72    ~vector(void);
73
74    ///
75    /// Create a new copy of the internal GSL vector.
76    ///
77    /// Necessary memory for the new GSL vector is allocated and the
78    /// caller is responsible for freeing the allocated memory.
79    ///
80    /// @return A pointer to a copy of the internal GSL vector.
81    ///
82    gsl_vector* gsl_vector_copy(void) const;
83
84    ///
85    /// @return A const pointer to the internal GSL vector,
86    ///
87    inline const gsl_vector* gsl_vector_pointer(void) const { return v_; }
88
89    ///
90    /// @return A pointer to the internal GSL vector,
91    ///
92    inline gsl_vector* gsl_vector_pointer(void) { return v_; }
93
94    ///This function returns the indices of the minimum and maximum values in
95    ///the vector, storing them in imin and imax. When
96    ///there are several equal minimum or maximum elements then the lowest
97    ///indices are returned. @return Index corresponding to the smallest
98    /// and largest value.
99    ///
100    std::pair<size_t,size_t> vector::minmax_index(void) const;
101
102    ///This function returns the indices of the minimum and maximum
103    ///values in the sub-vector (defined by \a subset), storing them
104    ///in imin and imax. When there are several equal minimum or
105    ///maximum elements then the lowest indices are returned. The
106    ///returned index is the index from the complete vector (not the
107    ///sub-vector)
108    ///
109    /// @return Index corresponding to the smallest and largest value.
110    ///
111    std::pair<size_t,size_t> 
112    vector::minmax_index(const std::vector<size_t>& subset ) const;
113
114    ///
115    /// This function multiplies all elements between two vectors and
116    /// returns a third vector containing the result, \f$a_i = b_i *
117    /// c_i \; \forall i\f$.
118    ///
119    /// @return The result vector.
120    ///
121    vector vector::mul_elements( const vector& other ) const;
122
123    ///
124    /// Set all elements to \a value.
125    ///
126    inline void set_all(const double& value) { gsl_vector_set_all(v_,value); }
127
128    ///
129    /// Makes a basis vector by setting all elements to
130    /// zero except the \a i-th element which is set to
131    /// one.
132    ///
133    inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); }
134   
135    ///
136    /// Set all elements to zero.
137    ///
138    inline void set_zero(void) { gsl_vector_set_zero(v_); }
139
140    ///
141    /// Randomly shuffles the elements in vector @return resulting
142    /// vector
143    ///
144    void vector::shuffle(void) const; 
145
146    ///
147    /// @return the number of elements in the vector.
148    ///
149    inline size_t size(void) const { return v_->size; }
150
151    ///
152    /// Calculate the sum of all vector elements.
153    ///
154    /// @return The sum.
155    ///
156    double vector::sum(void) const;
157
158    ///
159    /// Element access operator.
160    ///
161    /// @return Reference to element \a i.
162    ///
163    inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); }
164
165    ///
166    /// Const element access operator.
167    ///
168    /// @return The value of element \a i.
169    ///
170    inline const double& operator()(size_t i) const
171      { return *gsl_vector_const_ptr(v_,i); }
172   
173    ///
174    /// Element access operator.
175    ///
176    /// @return Reference to element \a i.
177    ///
178    inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); }
179
180    ///
181    /// Const element access operator.
182    ///
183    /// @return The value of element \a i.
184    ///
185    inline const double& operator[](size_t i) const
186      { return *gsl_vector_const_ptr(v_,i); }
187   
188    ///
189    /// @return The negation of the vector.
190    ///
191    vector operator-(void) const;
192
193    ///
194    /// @return The dot product.
195    ///
196    double operator*( const vector &other ) const;
197
198    ///
199    /// Vector addition.
200    ///
201    /// @return The resulting vector.
202    ///
203    vector operator+( const vector& other ) const;
204
205    ///
206    /// Vector subtraction.
207    ///
208    /// @return The resulting vector.
209    ///
210    vector operator-( const vector& other ) const;
211
212    ///
213    /// The assignment operator. There is no requirements on
214    /// dimensions.
215    ///
216    vector& operator=(const vector&);
217
218    ///
219    /// Multiply and assign operator.
220    ///
221    vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
222
223    ///
224    /// Multiply operator.
225    ///
226    vector& operator*(const double d) { gsl_vector_scale(v_,d); return *this; }
227
228
229  private:
230
231    gsl_vector* v_;
232
233  };
234
235  ///
236  /// The output operator for the vector class.
237  ///
238  std::ostream& operator<< ( std::ostream&, const vector& );
239
240
241}} // of namespace gslapi and namespace theplu
242
243#endif
Note: See TracBrowser for help on using the repository browser.