source: trunk/yat/utility/VectorBase.h @ 1887

Last change on this file since 1887 was 1887, checked in by Peter, 15 years ago

closes #512. added relates tag in namespace utility.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_base_
2#define _theplu_yat_utility_vector_base_
3
4// $Id: VectorBase.h 1887 2009-03-31 17:38:16Z 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 Jari Häkkinen, Peter Johansson
13  Copyright (C) 2009 Peter Johansson
14
15  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
16
17  The yat library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU General Public License as
19  published by the Free Software Foundation; either version 3 of the
20  License, or (at your option) any later version.
21
22  The yat library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  General Public License for more details.
26
27  You should have received a copy of the GNU General Public License
28  along with yat. If not, see <http://www.gnu.org/licenses/>.
29*/
30
31#include "Exception.h"
32#include "StrideIterator.h"
33
34#include <iosfwd>
35#include <vector>
36#include <cstddef> // size_t
37
38#include <gsl/gsl_vector.h>
39
40namespace theplu {
41namespace yat {
42namespace 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     \relates VectorBase
180  */
181  bool isnull(const VectorBase&);
182
183  /**
184     \brief Get the maximum value of the VectorBase.
185
186     \return The maximum value of the VectorBase.
187
188     \relates VectorBase
189  */
190  double max(const VectorBase&);
191
192  /**
193     \brief Locate the maximum value in the VectorBase.
194
195     \return The index to the maximum value of the VectorBase.
196
197     \note Lower index has precedence.
198
199     \relates VectorBase
200  */
201  size_t max_index(const VectorBase&);
202
203  /**
204     \brief Get the minimum value of the VectorBase.
205
206     \return The minimum value of the VectorBase.
207
208     \relates VectorBase
209  */
210  double min(const VectorBase&);
211
212  /**
213     \brief Locate the minimum value in the VectorBase.
214
215     \return The index to the minimum value of the VectorBase.
216
217     \note Lower index has precedence.
218
219     \relates VectorBase
220  */
221  size_t min_index(const VectorBase&);
222
223  /**
224     \brief Create a VectorBase \a flag indicating NaN's in another VectorBase
225     \a templat.
226
227     The \a flag VectorBase is changed to contain 1's and 0's only. A 1
228     means that the corresponding element in the \a templat VectorBase is
229     valid and a zero means that the corresponding element is a NaN.
230
231     \note Space for vector \a flag is reallocated to fit the size of
232     VectorBase \a templat if sizes mismatch.
233
234     \return True if the \a templat VectorBase contains at least one NaN.
235
236     \relates VectorBase
237  */
238  bool nan(const VectorBase& templat, Vector& flag);
239
240  /**
241     Create a vector \a sort_index containing the indeces of
242     elements in a another VectorBase \a invec.  The elements of \a
243     sort_index give the index of the VectorBase element which would
244     have been stored in that position if the VectorBase had been sorted
245     in place. The first element of \a sort_index gives the index of the least
246     element in \a invec, and the last element of \a sort_index gives the
247     index of the greatest element in \a invec . The VectorBase \a invec
248     is not changed.
249
250     \relatesalso VectorBase
251  */
252  void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec);
253
254  /**
255      Similar to sort_index but creates a VectorBase with indices to
256      the \a k smallest elements in \a invec.
257
258     \relatesalso VectorBase
259  */
260  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, 
261                           const VectorBase& invec);
262
263  /**
264      Similar to sort_index but creates a VectorBase with indices to
265      the \a k largest elements in \a invec.
266
267     \relatesalso VectorBase
268  */
269  void sort_largest_index(std::vector<size_t>& sort_index, size_t k, 
270                          const VectorBase& invec);
271
272  /**
273     \brief Calculate the sum of all VectorBase elements.
274
275     \return The sum.
276
277     \relates VectorBase
278  */
279  double sum(const VectorBase&);
280
281  /**
282     \brief The output operator for the VectorBase class.
283
284     Elements in VectorBase \a v are sent to ostream \a s and
285     separated with the fill character of stream \a s, s.fill(). If
286     you, for example, want to print the VectorBase \a v with the
287     elements separated by a ':', you do so by:
288     \verbatim
289     s << setfill(':') << v;
290     \endverbatim
291
292     \relates VectorBase
293  */
294  std::ostream& operator<<(std::ostream& s, const VectorBase& v);
295
296}}} // of namespace utility, yat, and theplu
297
298#endif
Note: See TracBrowser for help on using the repository browser.