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

Last change on this file since 1125 was 1125, checked in by Peter, 14 years ago

fixing Doxygen parsing

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