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

Last change on this file since 1680 was 1680, checked in by Peter, 12 years ago

correcting includes

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