1 | // $Id: vector.h 126 2004-08-03 08:45:31Z 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 | /// |
---|
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 subset), storing them in |
---|
104 | ///imin and imax. When there are several equal minimum or maximum |
---|
105 | ///elements then the lowest indices are returned. The returned |
---|
106 | ///index is the index from the complete vector (not the |
---|
107 | ///sub-vector) |
---|
108 | ///@return Index corresponding to the smallest and largest value. |
---|
109 | /// |
---|
110 | std::pair<size_t,size_t> |
---|
111 | vector::minmax_index(const std::vector<size_t>& subset ) const; |
---|
112 | |
---|
113 | /// |
---|
114 | /// This function multiplies all elements between two vectors and |
---|
115 | /// returns a third vector containing the result, \f$a_i = b_i * |
---|
116 | /// c_i \; \forall i\f$. |
---|
117 | /// |
---|
118 | /// @return The result vector. |
---|
119 | /// |
---|
120 | vector vector::mul_elements( const vector& other ) const; |
---|
121 | |
---|
122 | /// |
---|
123 | /// Set all elements to \a value. |
---|
124 | /// |
---|
125 | inline void set_all(const double& value) { gsl_vector_set_all(v_,value); } |
---|
126 | |
---|
127 | /// |
---|
128 | /// Makes a basis vector by setting all elements to |
---|
129 | /// zero except the \a i-th element which is set to |
---|
130 | /// one. |
---|
131 | /// |
---|
132 | inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); } |
---|
133 | |
---|
134 | /// |
---|
135 | /// Set all elements to zero. |
---|
136 | /// |
---|
137 | inline void set_zero(void) { gsl_vector_set_zero(v_); } |
---|
138 | |
---|
139 | /// |
---|
140 | /// Randomly shuffles the elements in vector @return resulting |
---|
141 | /// vector |
---|
142 | /// |
---|
143 | void vector::shuffle(void) const; |
---|
144 | |
---|
145 | /// |
---|
146 | /// @return the number of elements in the vector. |
---|
147 | /// |
---|
148 | inline size_t size(void) const { return v_->size; } |
---|
149 | |
---|
150 | /// |
---|
151 | /// Calculate the sum of all vector elements. |
---|
152 | /// |
---|
153 | /// @return The sum. |
---|
154 | /// |
---|
155 | double vector::sum(void) const; |
---|
156 | |
---|
157 | /// |
---|
158 | /// Element access operator. |
---|
159 | /// |
---|
160 | /// @return Reference to element \a i. |
---|
161 | /// |
---|
162 | inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
163 | |
---|
164 | /// |
---|
165 | /// Const element access operator. |
---|
166 | /// |
---|
167 | /// @return The value of element \a i. |
---|
168 | /// |
---|
169 | inline const double& operator()(size_t i) const |
---|
170 | { return *gsl_vector_const_ptr(v_,i); } |
---|
171 | |
---|
172 | /// |
---|
173 | /// Element access operator. |
---|
174 | /// |
---|
175 | /// @return Reference to element \a i. |
---|
176 | /// |
---|
177 | inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
178 | |
---|
179 | /// |
---|
180 | /// Const element access operator. |
---|
181 | /// |
---|
182 | /// @return The value of element \a i. |
---|
183 | /// |
---|
184 | inline const double& operator[](size_t i) const |
---|
185 | { return *gsl_vector_const_ptr(v_,i); } |
---|
186 | |
---|
187 | /// |
---|
188 | /// @return The negation of the vector. |
---|
189 | /// |
---|
190 | vector operator-(void) const; |
---|
191 | |
---|
192 | /// |
---|
193 | /// @return The dot product. |
---|
194 | /// |
---|
195 | double operator*( const vector &other ) const; |
---|
196 | |
---|
197 | /// |
---|
198 | /// Vector addition. |
---|
199 | /// |
---|
200 | /// @return The resulting vector. |
---|
201 | /// |
---|
202 | vector operator+( const vector& other ) const; |
---|
203 | |
---|
204 | /// |
---|
205 | /// Vector subtraction. |
---|
206 | /// |
---|
207 | /// @return The resulting vector. |
---|
208 | /// |
---|
209 | vector operator-( const vector& other ) const; |
---|
210 | |
---|
211 | /// |
---|
212 | /// The assignment operator. There is no requirements on |
---|
213 | /// dimensions. |
---|
214 | /// |
---|
215 | vector& operator=(const vector&); |
---|
216 | |
---|
217 | /// |
---|
218 | /// Multiply and assign operator. |
---|
219 | /// |
---|
220 | vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
221 | |
---|
222 | /// |
---|
223 | /// Multiply operator. |
---|
224 | /// |
---|
225 | vector& operator*(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
226 | |
---|
227 | |
---|
228 | private: |
---|
229 | |
---|
230 | gsl_vector* v_; |
---|
231 | |
---|
232 | }; |
---|
233 | |
---|
234 | /// |
---|
235 | /// The output operator for the vector class. |
---|
236 | /// |
---|
237 | std::ostream& operator<< ( std::ostream&, const vector& ); |
---|
238 | |
---|
239 | |
---|
240 | }} // of namespace gslapi and namespace theplu |
---|
241 | |
---|
242 | #endif |
---|