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

Last change on this file since 1487 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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