1 | #ifndef _theplu_yat_utility_vector_base_ |
---|
2 | #define _theplu_yat_utility_vector_base_ |
---|
3 | |
---|
4 | // $Id: VectorBase.h 1369 2008-07-11 16:10:03Z 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://trac.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 2 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 this program; if not, write to the Free Software |
---|
28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
29 | 02111-1307, USA. |
---|
30 | */ |
---|
31 | |
---|
32 | #include "Exception.h" |
---|
33 | #include "StrideIterator.h" |
---|
34 | |
---|
35 | #include <iostream> |
---|
36 | #include <vector> |
---|
37 | #include <utility> |
---|
38 | |
---|
39 | #include <gsl/gsl_vector.h> |
---|
40 | #include <gsl/gsl_sort_vector.h> |
---|
41 | |
---|
42 | namespace theplu { |
---|
43 | namespace yat { |
---|
44 | namespace utility { |
---|
45 | |
---|
46 | class matrix; |
---|
47 | class Vector; |
---|
48 | |
---|
49 | /** |
---|
50 | @brief This is the yat interface to GSL vector. |
---|
51 | |
---|
52 | This is an interface class for vectors containing the const |
---|
53 | interface. For mutable functionality see VectorMutable. |
---|
54 | */ |
---|
55 | class VectorBase |
---|
56 | { |
---|
57 | public: |
---|
58 | /// \brief VectorBase::const_iterator |
---|
59 | typedef StrideIterator<const double*> const_iterator; |
---|
60 | |
---|
61 | /** |
---|
62 | \brief Constructor. |
---|
63 | */ |
---|
64 | VectorBase(const gsl_vector* v=NULL); |
---|
65 | |
---|
66 | /// |
---|
67 | /// The destructor. |
---|
68 | /// |
---|
69 | virtual ~VectorBase(void); |
---|
70 | |
---|
71 | /** |
---|
72 | \return read-only iterator to start of VectorBase |
---|
73 | */ |
---|
74 | const_iterator begin(void) const; |
---|
75 | |
---|
76 | /** |
---|
77 | \return read-only iterator to end of VectorBase |
---|
78 | */ |
---|
79 | const_iterator end(void) const; |
---|
80 | |
---|
81 | /** |
---|
82 | \brief Check whether VectorBases are equal within a user defined |
---|
83 | precision, set by \a precision. |
---|
84 | |
---|
85 | \return True if each element deviates less or equal than \a |
---|
86 | d. If any VectorBase contain a NaN, false is always returned. |
---|
87 | |
---|
88 | \see operator== and operator!= |
---|
89 | */ |
---|
90 | bool equal(const VectorBase&, const double precision=0) const; |
---|
91 | |
---|
92 | /** |
---|
93 | \return A const pointer to the internal GSL vector, |
---|
94 | */ |
---|
95 | const gsl_vector* gsl_vector_p(void) const; |
---|
96 | |
---|
97 | /** |
---|
98 | Check if the vector object is a view (sub-vector) to another |
---|
99 | vector. |
---|
100 | |
---|
101 | \return True if the object is a view, false othwerwise. |
---|
102 | */ |
---|
103 | virtual bool isview(void) const=0; |
---|
104 | |
---|
105 | /** |
---|
106 | \return number of elements in the VectorBase. |
---|
107 | */ |
---|
108 | size_t size(void) const; |
---|
109 | |
---|
110 | /** |
---|
111 | \brief Element access operator. |
---|
112 | |
---|
113 | \return Const reference to element \a i. |
---|
114 | |
---|
115 | \throw If GSL range checks are enabled in the underlying GSL |
---|
116 | library a GSL_error exception is thrown if either index is out |
---|
117 | of range. |
---|
118 | */ |
---|
119 | const double& operator()(size_t i) const; |
---|
120 | |
---|
121 | /** |
---|
122 | \brief Comparison operator. Takes linear time. |
---|
123 | |
---|
124 | Checks are performed with exact matching, i.e., rounding off |
---|
125 | effects may destroy comparison. Use the equal function for |
---|
126 | comparing elements within a user defined precision. |
---|
127 | |
---|
128 | \return True if all elements are equal otherwise false. |
---|
129 | |
---|
130 | \see equal(const VectorBase&, const double precision=0) |
---|
131 | */ |
---|
132 | bool operator==(const VectorBase&) const; |
---|
133 | |
---|
134 | /** |
---|
135 | \brief Comparison operator. Takes linear time. |
---|
136 | |
---|
137 | Checks are performed with exact matching, i.e., rounding off |
---|
138 | effects may destroy comparison. Use the equal function for |
---|
139 | comparing elements within a user defined precision. |
---|
140 | |
---|
141 | \return False if all elements are equal otherwise true. |
---|
142 | |
---|
143 | \see equal(const VectorBase&, const double precision=0) |
---|
144 | */ |
---|
145 | bool operator!=(const VectorBase&) const; |
---|
146 | |
---|
147 | /// |
---|
148 | /// @return The dot product. |
---|
149 | /// |
---|
150 | double operator*(const VectorBase&) const; |
---|
151 | |
---|
152 | protected: |
---|
153 | /// pointer to underlying GSL vector |
---|
154 | const gsl_vector* const_vec_; |
---|
155 | |
---|
156 | private: |
---|
157 | // copy assignment not allowed |
---|
158 | const VectorBase& operator=(const VectorBase&); |
---|
159 | }; |
---|
160 | |
---|
161 | /** |
---|
162 | \brief Check if all elements of the VectorBase are zero. |
---|
163 | |
---|
164 | \return True if all elements in the VectorBase is zero, false |
---|
165 | othwerwise. |
---|
166 | */ |
---|
167 | bool isnull(const VectorBase&); |
---|
168 | |
---|
169 | /** |
---|
170 | \brief Get the maximum value of the VectorBase. |
---|
171 | |
---|
172 | \return The maximum value of the VectorBase. |
---|
173 | */ |
---|
174 | double max(const VectorBase&); |
---|
175 | |
---|
176 | /** |
---|
177 | \brief Locate the maximum value in the VectorBase. |
---|
178 | |
---|
179 | \return The index to the maximum value of the VectorBase. |
---|
180 | |
---|
181 | \note Lower index has precedence. |
---|
182 | */ |
---|
183 | size_t max_index(const VectorBase&); |
---|
184 | |
---|
185 | /** |
---|
186 | \brief Get the minimum value of the VectorBase. |
---|
187 | |
---|
188 | \return The minimum value of the VectorBase. |
---|
189 | */ |
---|
190 | double min(const VectorBase&); |
---|
191 | |
---|
192 | /** |
---|
193 | \brief Locate the minimum value in the VectorBase. |
---|
194 | |
---|
195 | \return The index to the minimum value of the VectorBase. |
---|
196 | |
---|
197 | \note Lower index has precedence. |
---|
198 | */ |
---|
199 | size_t min_index(const VectorBase&); |
---|
200 | |
---|
201 | /** |
---|
202 | \brief Create a VectorBase \a flag indicating NaN's in another VectorBase |
---|
203 | \a templat. |
---|
204 | |
---|
205 | The \a flag VectorBase is changed to contain 1's and 0's only. A 1 |
---|
206 | means that the corresponding element in the \a templat VectorBase is |
---|
207 | valid and a zero means that the corresponding element is a NaN. |
---|
208 | |
---|
209 | \note Space for vector \a flag is reallocated to fit the size of |
---|
210 | VectorBase \a templat if sizes mismatch. |
---|
211 | |
---|
212 | \return True if the \a templat VectorBase contains at least one NaN. |
---|
213 | */ |
---|
214 | bool nan(const VectorBase& templat, Vector& flag); |
---|
215 | |
---|
216 | /** |
---|
217 | Create a vector \a sort_index containing the indeces of |
---|
218 | elements in a another VectorBase \a invec. The elements of \a |
---|
219 | sort_index give the index of the VectorBase element which would |
---|
220 | have been stored in that position if the VectorBase had been sorted |
---|
221 | in place. The first element of \a sort_index gives the index of the least |
---|
222 | element in \a invec, and the last element of \a sort_index gives the |
---|
223 | index of the greatest element in \a invec . The VectorBase \a invec |
---|
224 | is not changed. |
---|
225 | */ |
---|
226 | void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec); |
---|
227 | |
---|
228 | /** |
---|
229 | Similar to sort_index but creates a VectorBase with indices to |
---|
230 | the \a k smallest elements in \a invec. |
---|
231 | */ |
---|
232 | void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, |
---|
233 | const VectorBase& invec); |
---|
234 | |
---|
235 | /** |
---|
236 | Similar to sort_index but creates a VectorBase with indices to |
---|
237 | the \a k largest elements in \a invec. |
---|
238 | */ |
---|
239 | void sort_largest_index(std::vector<size_t>& sort_index, size_t k, |
---|
240 | const VectorBase& invec); |
---|
241 | |
---|
242 | /** |
---|
243 | \brief Calculate the sum of all VectorBase elements. |
---|
244 | |
---|
245 | \return The sum. |
---|
246 | */ |
---|
247 | double sum(const VectorBase&); |
---|
248 | |
---|
249 | /** |
---|
250 | \brief The output operator for the VectorBase class. |
---|
251 | |
---|
252 | Elements in VectorBase \a v are sent to ostream \a s and |
---|
253 | separated with the fill character of stream \a s, s.fill(). If |
---|
254 | you, for example, want to print the VectorBase \a v with the |
---|
255 | elements separated by a ':', you do so by: |
---|
256 | \verbatim |
---|
257 | s << setfill(':') << v; |
---|
258 | \endverbatim |
---|
259 | */ |
---|
260 | std::ostream& operator<<(std::ostream& s, const VectorBase& v); |
---|
261 | |
---|
262 | }}} // of namespace utility, yat, and theplu |
---|
263 | |
---|
264 | #endif |
---|