1 | #ifndef _theplu_yat_utility_vector_base_ |
---|
2 | #define _theplu_yat_utility_vector_base_ |
---|
3 | |
---|
4 | // $Id: VectorBase.h 1120 2008-02-21 23:18:41Z 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, Markus Ringnér, Peter Johansson |
---|
10 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér |
---|
11 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
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 | // Peter, remove this one |
---|
121 | const double& operator[](size_t i) const; |
---|
122 | |
---|
123 | /** |
---|
124 | \brief Comparison operator. Takes linear time. |
---|
125 | |
---|
126 | Checks are performed with exact matching, i.e., rounding off |
---|
127 | effects may destroy comparison. Use the equal function for |
---|
128 | comparing elements within a user defined precision. |
---|
129 | |
---|
130 | \return True if all elements are equal otherwise false. |
---|
131 | |
---|
132 | \see equal(const VectorBase&, const double precision=0) |
---|
133 | */ |
---|
134 | bool operator==(const VectorBase&) const; |
---|
135 | |
---|
136 | /** |
---|
137 | \brief Comparison operator. Takes linear time. |
---|
138 | |
---|
139 | Checks are performed with exact matching, i.e., rounding off |
---|
140 | effects may destroy comparison. Use the equal function for |
---|
141 | comparing elements within a user defined precision. |
---|
142 | |
---|
143 | \return False if all elements are equal otherwise true. |
---|
144 | |
---|
145 | \see equal(const VectorBase&, const double precision=0) |
---|
146 | */ |
---|
147 | bool operator!=(const VectorBase&) const; |
---|
148 | |
---|
149 | /// |
---|
150 | /// @return The dot product. |
---|
151 | /// |
---|
152 | double operator*(const VectorBase&) const; |
---|
153 | |
---|
154 | protected: |
---|
155 | const gsl_vector* const_vec_; |
---|
156 | |
---|
157 | private: |
---|
158 | // copy assignment no allowed |
---|
159 | const VectorBase& operator=(const VectorBase&); |
---|
160 | }; |
---|
161 | |
---|
162 | /** |
---|
163 | \brief Check if all elements of the VectorBase are zero. |
---|
164 | |
---|
165 | \return True if all elements in the VectorBase is zero, false |
---|
166 | othwerwise. |
---|
167 | */ |
---|
168 | bool isnull(const VectorBase&); |
---|
169 | |
---|
170 | /** |
---|
171 | \brief Get the maximum value of the VectorBase. |
---|
172 | |
---|
173 | \return The maximum value of the VectorBase. |
---|
174 | */ |
---|
175 | double max(const VectorBase&); |
---|
176 | |
---|
177 | /** |
---|
178 | \brief Locate the maximum value in the VectorBase. |
---|
179 | |
---|
180 | \return The index to the maximum value of the VectorBase. |
---|
181 | |
---|
182 | \note Lower index has precedence. |
---|
183 | */ |
---|
184 | size_t max_index(const VectorBase&); |
---|
185 | |
---|
186 | /** |
---|
187 | \brief Get the minimum value of the VectorBase. |
---|
188 | |
---|
189 | \return The minimum value of the VectorBase. |
---|
190 | */ |
---|
191 | double min(const VectorBase&); |
---|
192 | |
---|
193 | /** |
---|
194 | \brief Locate the minimum value in the VectorBase. |
---|
195 | |
---|
196 | \return The index to the minimum value of the VectorBase. |
---|
197 | |
---|
198 | \note Lower index has precedence. |
---|
199 | */ |
---|
200 | size_t min_index(const VectorBase&); |
---|
201 | |
---|
202 | /** |
---|
203 | \brief Create a VectorBase \a flag indicating NaN's in another VectorBase |
---|
204 | \a templat. |
---|
205 | |
---|
206 | The \a flag VectorBase is changed to contain 1's and 0's only. A 1 |
---|
207 | means that the corresponding element in the \a templat VectorBase is |
---|
208 | valid and a zero means that the corresponding element is a NaN. |
---|
209 | |
---|
210 | \note Space for vector \a flag is reallocated to fit the size of |
---|
211 | VectorBase \a templat if sizes mismatch. |
---|
212 | |
---|
213 | \return True if the \a templat VectorBase contains at least one NaN. |
---|
214 | */ |
---|
215 | bool nan(const VectorBase& templat, Vector& flag); |
---|
216 | |
---|
217 | /** |
---|
218 | Create a vector \a sort_index containing the indeces of |
---|
219 | elements in a another VectorBase \a invec. The elements of \a |
---|
220 | sort_index give the index of the VectorBase element which would |
---|
221 | have been stored in that position if the VectorBase had been sorted |
---|
222 | in place. The first element of \a sort_index gives the index of the least |
---|
223 | element in \a invec, and the last element of \a sort_index gives the |
---|
224 | index of the greatest element in \a invec . The VectorBase \a invec |
---|
225 | is not changed. |
---|
226 | */ |
---|
227 | void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec); |
---|
228 | |
---|
229 | /** |
---|
230 | Similar to sort_index but creates a VectorBase with indices to |
---|
231 | the \a k smallest elements in \a invec. |
---|
232 | */ |
---|
233 | void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, |
---|
234 | const VectorBase& invec); |
---|
235 | |
---|
236 | /** |
---|
237 | Similar to sort_index but creates a VectorBase with indices to |
---|
238 | the \a k largest elements in \a invec. |
---|
239 | */ |
---|
240 | void sort_largest_index(std::vector<size_t>& sort_index, size_t k, |
---|
241 | const VectorBase& invec); |
---|
242 | |
---|
243 | /** |
---|
244 | \brief Calculate the sum of all VectorBase elements. |
---|
245 | |
---|
246 | \return The sum. |
---|
247 | */ |
---|
248 | double sum(const VectorBase&); |
---|
249 | |
---|
250 | /** |
---|
251 | \brief The output operator for the VectorBase class. |
---|
252 | */ |
---|
253 | std::ostream& operator<<(std::ostream&, const VectorBase&); |
---|
254 | |
---|
255 | }}} // of namespace utility, yat, and theplu |
---|
256 | |
---|
257 | #endif |
---|