1 | #ifndef _theplu_yat_utility_vector_base_ |
---|
2 | #define _theplu_yat_utility_vector_base_ |
---|
3 | |
---|
4 | // $Id: VectorBase.h 1551 2008-10-06 14:01:53Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2003 Daniel Dalevi, Peter Johansson |
---|
8 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
9 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
10 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér |
---|
11 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
12 | Copyright (C) 2008 Peter Johansson |
---|
13 | |
---|
14 | This file is part of the yat library, http://dev.thep.lu.se/trac/yat |
---|
15 | |
---|
16 | The yat library is free software; you can redistribute it and/or |
---|
17 | modify it under the terms of the GNU General Public License as |
---|
18 | published by the Free Software Foundation; either version 3 of the |
---|
19 | License, or (at your option) any later version. |
---|
20 | |
---|
21 | The yat library is distributed in the hope that it will be useful, |
---|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
24 | General Public License for more details. |
---|
25 | |
---|
26 | You should have received a copy of the GNU General Public License |
---|
27 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "Exception.h" |
---|
31 | #include "StrideIterator.h" |
---|
32 | |
---|
33 | #include <iostream> |
---|
34 | #include <vector> |
---|
35 | #include <utility> |
---|
36 | |
---|
37 | #include <gsl/gsl_vector.h> |
---|
38 | #include <gsl/gsl_sort_vector.h> |
---|
39 | |
---|
40 | namespace theplu { |
---|
41 | namespace yat { |
---|
42 | namespace utility { |
---|
43 | |
---|
44 | class matrix; |
---|
45 | class Vector; |
---|
46 | |
---|
47 | /** |
---|
48 | @brief This is the yat interface to GSL vector. |
---|
49 | |
---|
50 | This is an interface class for vectors containing the const |
---|
51 | interface. For mutable functionality see VectorMutable. |
---|
52 | */ |
---|
53 | class VectorBase |
---|
54 | { |
---|
55 | public: |
---|
56 | /** |
---|
57 | value_type is double |
---|
58 | |
---|
59 | \since New in yat 0.5 |
---|
60 | */ |
---|
61 | typedef double value_type; |
---|
62 | |
---|
63 | /** |
---|
64 | const_reference type is const double& |
---|
65 | |
---|
66 | \since New in yat 0.5 |
---|
67 | */ |
---|
68 | typedef const double& const_reference; |
---|
69 | |
---|
70 | /// \brief VectorBase::const_iterator |
---|
71 | typedef StrideIterator<const double*> const_iterator; |
---|
72 | |
---|
73 | /** |
---|
74 | \brief Constructor. |
---|
75 | */ |
---|
76 | VectorBase(const gsl_vector* v=NULL); |
---|
77 | |
---|
78 | /// |
---|
79 | /// The destructor. |
---|
80 | /// |
---|
81 | virtual ~VectorBase(void); |
---|
82 | |
---|
83 | /** |
---|
84 | \return read-only iterator to start of VectorBase |
---|
85 | */ |
---|
86 | const_iterator begin(void) const; |
---|
87 | |
---|
88 | /** |
---|
89 | \return read-only iterator to end of VectorBase |
---|
90 | */ |
---|
91 | const_iterator end(void) const; |
---|
92 | |
---|
93 | /** |
---|
94 | \brief Check whether VectorBases are equal within a user defined |
---|
95 | precision, set by \a precision. |
---|
96 | |
---|
97 | \return True if each element deviates less or equal than \a |
---|
98 | d. If any VectorBase contain a NaN, false is always returned. |
---|
99 | |
---|
100 | \see operator== and operator!= |
---|
101 | */ |
---|
102 | bool equal(const VectorBase&, const double precision=0) const; |
---|
103 | |
---|
104 | /** |
---|
105 | \return A const pointer to the internal GSL vector, |
---|
106 | */ |
---|
107 | const gsl_vector* gsl_vector_p(void) const; |
---|
108 | |
---|
109 | /** |
---|
110 | Check if the vector object is a view (sub-vector) to another |
---|
111 | vector. |
---|
112 | |
---|
113 | \return True if the object is a view, false othwerwise. |
---|
114 | */ |
---|
115 | virtual bool isview(void) const=0; |
---|
116 | |
---|
117 | /** |
---|
118 | \return number of elements in the VectorBase. |
---|
119 | */ |
---|
120 | size_t size(void) const; |
---|
121 | |
---|
122 | /** |
---|
123 | \brief Element access operator. |
---|
124 | |
---|
125 | \return Const reference to element \a i. |
---|
126 | |
---|
127 | \throw If GSL range checks are enabled in the underlying GSL |
---|
128 | library a GSL_error exception is thrown if either index is out |
---|
129 | of range. |
---|
130 | */ |
---|
131 | const double& operator()(size_t i) const; |
---|
132 | |
---|
133 | /** |
---|
134 | \brief Comparison operator. Takes linear time. |
---|
135 | |
---|
136 | Checks are performed with exact matching, i.e., rounding off |
---|
137 | effects may destroy comparison. Use the equal function for |
---|
138 | comparing elements within a user defined precision. |
---|
139 | |
---|
140 | \return True if all elements are equal otherwise false. |
---|
141 | |
---|
142 | \see equal(const VectorBase&, const double precision=0) |
---|
143 | */ |
---|
144 | bool operator==(const VectorBase&) const; |
---|
145 | |
---|
146 | /** |
---|
147 | \brief Comparison operator. Takes linear time. |
---|
148 | |
---|
149 | Checks are performed with exact matching, i.e., rounding off |
---|
150 | effects may destroy comparison. Use the equal function for |
---|
151 | comparing elements within a user defined precision. |
---|
152 | |
---|
153 | \return False if all elements are equal otherwise true. |
---|
154 | |
---|
155 | \see equal(const VectorBase&, const double precision=0) |
---|
156 | */ |
---|
157 | bool operator!=(const VectorBase&) const; |
---|
158 | |
---|
159 | /// |
---|
160 | /// @return The dot product. |
---|
161 | /// |
---|
162 | double operator*(const VectorBase&) const; |
---|
163 | |
---|
164 | protected: |
---|
165 | /// pointer to underlying GSL vector |
---|
166 | const gsl_vector* const_vec_; |
---|
167 | |
---|
168 | private: |
---|
169 | // copy assignment not allowed |
---|
170 | const VectorBase& operator=(const VectorBase&); |
---|
171 | }; |
---|
172 | |
---|
173 | /** |
---|
174 | \brief Check if all elements of the VectorBase are zero. |
---|
175 | |
---|
176 | \return True if all elements in the VectorBase is zero, false |
---|
177 | othwerwise. |
---|
178 | */ |
---|
179 | bool isnull(const VectorBase&); |
---|
180 | |
---|
181 | /** |
---|
182 | \brief Get the maximum value of the VectorBase. |
---|
183 | |
---|
184 | \return The maximum value of the VectorBase. |
---|
185 | */ |
---|
186 | double max(const VectorBase&); |
---|
187 | |
---|
188 | /** |
---|
189 | \brief Locate the maximum value in the VectorBase. |
---|
190 | |
---|
191 | \return The index to the maximum value of the VectorBase. |
---|
192 | |
---|
193 | \note Lower index has precedence. |
---|
194 | */ |
---|
195 | size_t max_index(const VectorBase&); |
---|
196 | |
---|
197 | /** |
---|
198 | \brief Get the minimum value of the VectorBase. |
---|
199 | |
---|
200 | \return The minimum value of the VectorBase. |
---|
201 | */ |
---|
202 | double min(const VectorBase&); |
---|
203 | |
---|
204 | /** |
---|
205 | \brief Locate the minimum value in the VectorBase. |
---|
206 | |
---|
207 | \return The index to the minimum value of the VectorBase. |
---|
208 | |
---|
209 | \note Lower index has precedence. |
---|
210 | */ |
---|
211 | size_t min_index(const VectorBase&); |
---|
212 | |
---|
213 | /** |
---|
214 | \brief Create a VectorBase \a flag indicating NaN's in another VectorBase |
---|
215 | \a templat. |
---|
216 | |
---|
217 | The \a flag VectorBase is changed to contain 1's and 0's only. A 1 |
---|
218 | means that the corresponding element in the \a templat VectorBase is |
---|
219 | valid and a zero means that the corresponding element is a NaN. |
---|
220 | |
---|
221 | \note Space for vector \a flag is reallocated to fit the size of |
---|
222 | VectorBase \a templat if sizes mismatch. |
---|
223 | |
---|
224 | \return True if the \a templat VectorBase contains at least one NaN. |
---|
225 | */ |
---|
226 | bool nan(const VectorBase& templat, Vector& flag); |
---|
227 | |
---|
228 | /** |
---|
229 | Create a vector \a sort_index containing the indeces of |
---|
230 | elements in a another VectorBase \a invec. The elements of \a |
---|
231 | sort_index give the index of the VectorBase element which would |
---|
232 | have been stored in that position if the VectorBase had been sorted |
---|
233 | in place. The first element of \a sort_index gives the index of the least |
---|
234 | element in \a invec, and the last element of \a sort_index gives the |
---|
235 | index of the greatest element in \a invec . The VectorBase \a invec |
---|
236 | is not changed. |
---|
237 | */ |
---|
238 | void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec); |
---|
239 | |
---|
240 | /** |
---|
241 | Similar to sort_index but creates a VectorBase with indices to |
---|
242 | the \a k smallest elements in \a invec. |
---|
243 | */ |
---|
244 | void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, |
---|
245 | const VectorBase& invec); |
---|
246 | |
---|
247 | /** |
---|
248 | Similar to sort_index but creates a VectorBase with indices to |
---|
249 | the \a k largest elements in \a invec. |
---|
250 | */ |
---|
251 | void sort_largest_index(std::vector<size_t>& sort_index, size_t k, |
---|
252 | const VectorBase& invec); |
---|
253 | |
---|
254 | /** |
---|
255 | \brief Calculate the sum of all VectorBase elements. |
---|
256 | |
---|
257 | \return The sum. |
---|
258 | */ |
---|
259 | double sum(const VectorBase&); |
---|
260 | |
---|
261 | /** |
---|
262 | \brief The output operator for the VectorBase class. |
---|
263 | |
---|
264 | Elements in VectorBase \a v are sent to ostream \a s and |
---|
265 | separated with the fill character of stream \a s, s.fill(). If |
---|
266 | you, for example, want to print the VectorBase \a v with the |
---|
267 | elements separated by a ':', you do so by: |
---|
268 | \verbatim |
---|
269 | s << setfill(':') << v; |
---|
270 | \endverbatim |
---|
271 | */ |
---|
272 | std::ostream& operator<<(std::ostream& s, const VectorBase& v); |
---|
273 | |
---|
274 | }}} // of namespace utility, yat, and theplu |
---|
275 | |
---|
276 | #endif |
---|