1 | // $Id: vector.h 63 2004-04-19 11:21:28Z 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. |
---|
50 | /// |
---|
51 | vector(const vector&); |
---|
52 | |
---|
53 | /// |
---|
54 | /// Constructor that imports a GSL vector. The GSL object is owned |
---|
55 | /// by the created object. |
---|
56 | /// |
---|
57 | vector(gsl_vector*); |
---|
58 | |
---|
59 | /// |
---|
60 | /// The istream constructor. |
---|
61 | /// |
---|
62 | /// Elements should be separated with white space characters, the |
---|
63 | /// end of input to the vector is at end of file marker. |
---|
64 | /// |
---|
65 | explicit vector(std::istream &); |
---|
66 | |
---|
67 | /// |
---|
68 | /// The destructor. |
---|
69 | /// |
---|
70 | ~vector(void); |
---|
71 | |
---|
72 | /// |
---|
73 | /// Create a new copy of the internal GSL vector. |
---|
74 | /// |
---|
75 | /// Necessary memory for the new GSL vector is allocated and the |
---|
76 | /// caller is responsible for freeing the allocated memory. |
---|
77 | /// |
---|
78 | /// @return A pointer to a copy of the internal GSL vector. |
---|
79 | /// |
---|
80 | gsl_vector* gsl_vector_copy(void) const; |
---|
81 | |
---|
82 | /// |
---|
83 | /// @return A const pointer to the internal GSL vector, |
---|
84 | /// |
---|
85 | inline const gsl_vector* gsl_vector_pointer(void) const { return v_; } |
---|
86 | |
---|
87 | /// |
---|
88 | /// @return A pointer to the internal GSL vector, |
---|
89 | /// |
---|
90 | inline gsl_vector* gsl_vector_pointer(void) { return v_; } |
---|
91 | |
---|
92 | ///This function returns the indices of the minimum and maximum values in |
---|
93 | ///the sub-vector (defined by subset), storing them in imin and imax. When |
---|
94 | ///there are several equal minimum or maximum elements then the lowest |
---|
95 | ///indices are returned. The returned index is the index from the complete vector (not the sub-vector) @return Index corresponding to the smallest |
---|
96 | ///and largest value. |
---|
97 | /// |
---|
98 | std::pair<u_int,u_int> vector::minmax_index(const std::vector<u_int>& subset ) const; |
---|
99 | |
---|
100 | /// |
---|
101 | /// This function multiplies all elements between two vectors and |
---|
102 | /// returns a third vector containing the result, \f$a_i = b_i * |
---|
103 | /// c_i \; \forall i\f$. |
---|
104 | /// |
---|
105 | /// @return The result vector. |
---|
106 | /// |
---|
107 | vector vector::mul_elements( const vector& other ) const; |
---|
108 | |
---|
109 | /// |
---|
110 | /// Set all elements to \a value. |
---|
111 | /// |
---|
112 | inline void set_all(const double& value) { gsl_vector_set_all(v_,value); } |
---|
113 | |
---|
114 | /// |
---|
115 | /// @return the number of elements in the vector. |
---|
116 | /// |
---|
117 | inline size_t size(void) const { return v_->size; } |
---|
118 | |
---|
119 | /// |
---|
120 | /// Calculate the sum of all vector elements. |
---|
121 | /// |
---|
122 | /// @return The sum. |
---|
123 | /// |
---|
124 | double vector::sum(void) const; |
---|
125 | |
---|
126 | /// |
---|
127 | /// Element access operator. |
---|
128 | /// |
---|
129 | /// @return Reference to element \a i. |
---|
130 | /// |
---|
131 | inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); } |
---|
132 | |
---|
133 | /// |
---|
134 | /// Const element access operator. |
---|
135 | /// |
---|
136 | /// @return The value of element \a i. |
---|
137 | /// |
---|
138 | inline const double& operator()(size_t i) const |
---|
139 | { return *gsl_vector_ptr(v_,i); } |
---|
140 | |
---|
141 | /// |
---|
142 | /// Element access operator. |
---|
143 | /// |
---|
144 | /// @return Reference to element \a i. |
---|
145 | /// |
---|
146 | inline double& operator[](size_t i) { return (*this)(i); } |
---|
147 | |
---|
148 | /// |
---|
149 | /// Const element access operator. |
---|
150 | /// |
---|
151 | /// @return The value of element \a i. |
---|
152 | /// |
---|
153 | inline const double& operator[](size_t i) const { return (*this)(i); } |
---|
154 | |
---|
155 | /// |
---|
156 | /// @return The negation of the vector. |
---|
157 | /// |
---|
158 | vector operator-(void) const; |
---|
159 | |
---|
160 | /// |
---|
161 | /// @return The dot product. |
---|
162 | /// |
---|
163 | double operator*( const vector &other ) const; |
---|
164 | |
---|
165 | /// |
---|
166 | /// Vector addition. |
---|
167 | /// |
---|
168 | /// @return The resulting vector. |
---|
169 | /// |
---|
170 | vector operator+( const vector& other ) const; |
---|
171 | |
---|
172 | /// |
---|
173 | /// Vector subtraction. |
---|
174 | /// |
---|
175 | /// @return The resulting vector. |
---|
176 | /// |
---|
177 | vector operator-( const vector& other ) const; |
---|
178 | |
---|
179 | /// |
---|
180 | /// The assignment operator. There is no requirements on |
---|
181 | /// dimensions. |
---|
182 | /// |
---|
183 | vector& operator=(const vector&); |
---|
184 | |
---|
185 | /// |
---|
186 | /// Multiply and assign operator. |
---|
187 | /// |
---|
188 | vector& operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
189 | |
---|
190 | /// |
---|
191 | /// Multiply operator. |
---|
192 | /// |
---|
193 | vector& operator*(const double d) { gsl_vector_scale(v_,d); return *this; } |
---|
194 | |
---|
195 | |
---|
196 | private: |
---|
197 | |
---|
198 | gsl_vector* v_; |
---|
199 | |
---|
200 | }; |
---|
201 | |
---|
202 | /// |
---|
203 | /// The output operator for the vector class. |
---|
204 | /// |
---|
205 | std::ostream& operator<< ( std::ostream&, const vector& ); |
---|
206 | |
---|
207 | |
---|
208 | }} // of namespace gslapi and namespace theplu |
---|
209 | |
---|
210 | #endif |
---|