source: branches/peter-dev/yat/utility/vectorBase.h @ 995

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

Splitting vector class into three different classes for vector, view and const_view, and adding abstract classes appropriately. NOTE, the tests are not going through, not even compiling; sorry about that

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1#ifndef _theplu_yat_utility_vector_base_
2#define _theplu_yat_utility_vector_base_
3
4// $Id: vectorBase.h 995 2007-11-30 22:55: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, 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
13  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
14
15  The yat library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License as
17  published by the Free Software Foundation; either version 2 of the
18  License, or (at your option) any later version.
19
20  The yat library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  General Public License for more details.
24
25  You should have received a copy of the GNU General Public License
26  along with this program; if not, write to the Free Software
27  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28  02111-1307, USA.
29*/
30
31#include "Exception.h"
32#include "Iterator.h"
33
34#include <iostream>
35#include <vector>
36#include <utility>
37
38#include <gsl/gsl_vector.h>
39#include <gsl/gsl_sort_vector.h>
40
41namespace theplu {
42namespace yat {
43namespace utility {
44
45  class matrix;
46  class vector;
47
48  /**
49     @brief This is the yat interface to GSL vector.
50
51     For time being 'double' is the only type supported.
52
53     \par File streams:
54     Reading and writing vectors to file streams are of course
55     supported. These are implemented without using GSL functionality,
56     and thus binary read and write to streams are not supported.
57
58     \par Vector views:
59     GSL vector views are supported and these are disguised as
60     ordinary utility::vectors. A support function is added,
61     utility::vector::isview(), that can be used to check if a vector
62     object is a view. Note that view vectors do not own the
63     underlying data, and a view is not valid if the vector/matrix
64     owing the data is deallocated.
65
66     \par
67     Currently there is no restriction on how a vector is used when
68     the vector is a const view into another vector or matrix. To
69     avoid unexpected runtime errors, the programmer must declare
70     const view vectors as 'const' in order to get compile time
71     diagnostics about improper use of a const view vector object. If
72     'const' is not used and the const view vector is used erroneously
73     (such as on the left hand side in assignments), the compiler will
74     not catch the error and runtime error will occur. assert(3) is
75     used to catch the runtime error during development. Example on
76     bad and proper use of const view vectors:
77     @code
78  const vector vm(13,1.0);
79  vector v1(vm,2,4);       // bad code! not const!
80  v1(0)=-123;              // accepted by compiler, runtime abort will occur
81                           // or catched by assert depending on compiler flags
82  const vector v2(vm,2,4); // proper code
83  v2(0)=-123;              // not acceptable for the compiler
84     @endcode
85  */
86
87  class vectorBase
88  {
89  public:
90
91    /// \brief vectorBase::const_iterator
92    typedef Iterator<const double, const vectorBase> const_iterator;
93
94    /**
95       \brief Constructor.
96    */
97    vectorBase(const gsl_vector*);
98
99    ///
100    /// The destructor.
101    ///
102    virtual ~vectorBase(void);
103
104    /**
105       \return read-only iterator to start of vectorBase
106     */
107    const_iterator begin(void) const;
108
109    /**
110       \return read-only iterator to end of vectorBase
111     */
112    const_iterator end(void) const;
113
114    /**
115       \brief Check whether vectorBases are equal within a user defined
116       precision, set by \a precision.
117
118       \return True if each element deviates less or equal than \a
119       d. If any vectorBase contain a NaN, false is always returned.
120
121       \see operator== and operator!=
122    */
123    bool equal(const vectorBase&, const double precision=0) const;
124
125    ///
126    /// @return A const pointer to the internal GSL vector,
127    ///
128    const gsl_vector* gsl_vector_p(void) const;
129
130    ///
131    /// @return the number of elements in the vectorBase.
132    ///
133    size_t size(void) const;
134
135    /**
136       \brief Element access operator.
137
138       \return Const reference to element \a i.
139
140       \throw If GSL range checks are enabled in the underlying GSL
141       library a GSL_error exception is thrown if either index is out
142       of range.
143    */
144    const double& operator()(size_t i) const;
145    const double& operator[](size_t i) const;
146
147    /**
148       \brief Comparison operator. Takes linear time.
149
150       Checks are performed with exact matching, i.e., rounding off
151       effects may destroy comparison. Use the equal function for
152       comparing elements within a user defined precision.
153
154       \return True if all elements are equal otherwise false.
155
156       \see equal(const vectorBase&, const double precision=0)
157    */
158    bool operator==(const vectorBase&) const;
159
160    /**
161       \brief Comparison operator. Takes linear time.
162
163       Checks are performed with exact matching, i.e., rounding off
164       effects may destroy comparison. Use the equal function for
165       comparing elements within a user defined precision.
166
167       \return False if all elements are equal otherwise true.
168
169       \see equal(const vectorBase&, const double precision=0)
170    */
171    bool operator!=(const vectorBase&) const;
172
173    ///
174    /// @return The dot product.
175    ///
176    double operator*(const vectorBase&) const;
177
178  protected:
179    const gsl_vector* const_vec_;
180  };
181
182  /**
183     \brief Check if all elements of the vectorBase are zero.
184
185     \return True if all elements in the vectorBase is zero, false
186     othwerwise.
187  */
188  bool isnull(const vectorBase&);
189
190  /**
191     \brief Get the maximum value of the vectorBase.
192
193     \return The maximum value of the vectorBase.
194  */
195  double max(const vectorBase&);
196
197  /**
198     \brief Locate the maximum value in the vectorBase.
199
200     \return The index to the maximum value of the vectorBase.
201
202     \note Lower index has precedence.
203  */
204  size_t max_index(const vectorBase&);
205
206  /**
207     \brief Get the minimum value of the vectorBase.
208
209     \return The minimum value of the vectorBase.
210  */
211  double min(const vectorBase&);
212
213  /**
214     \brief Locate the minimum value in the vectorBase.
215
216     \return The index to the minimum value of the vectorBase.
217
218     \note Lower index has precedence.
219  */
220  size_t min_index(const vectorBase&);
221
222  /**
223     \brief Create a vectorBase \a flag indicating NaN's in another vectorBase
224     \a templat.
225
226     The \a flag vectorBase is changed to contain 1's and 0's only. A 1
227     means that the corresponding element in the \a templat vectorBase is
228     valid and a zero means that the corresponding element is a NaN.
229
230     \note Space for vectorBase \a flag is reallocated to fit the size of
231     vectorBase \a templat if sizes mismatch.
232
233     \return True if the \a templat vectorBase contains at least one NaN.
234  */
235  bool nan(const vectorBase& templat, vector& flag);
236
237  /**
238     Create a vectorBase \a sort_index containing the indeces of
239     elements in a another vectorBase \a invec.  The elements of \a
240     sort_index give the index of the vectorBase element which would
241     have been stored in that position if the vectorBase had been sorted
242     in place. The first element of \a sort_index gives the index of the least
243     element in \a invec, and the last element of \a sort_index gives the
244     index of the greatest element in \a invec . The vectorBase \a invec
245     is not changed.
246  */
247  void sort_index(std::vector<size_t>& sort_index, const vectorBase& invec);
248
249  /**
250      Similar to sort_index but creates a vectorBase with indices to the \a k
251  smallest elements in \a invec. 
252  */
253  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const
254  vectorBase& invec);
255
256  /**
257      Similar to sort_index but creates a vectorBase with indices to the \a k
258  largest elements in \a invec. 
259  */
260  void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const
261  vectorBase& invec);
262
263 
264
265  /**
266     \brief Calculate the sum of all vectorBase elements.
267
268     \return The sum.
269  */
270  double sum(const vectorBase&);
271
272  /**
273     \brief The output operator for the vectorBase class.
274  */
275  std::ostream& operator<<(std::ostream&, const vectorBase&);
276
277}}} // of namespace utility, yat, and theplu
278
279#endif
Note: See TracBrowser for help on using the repository browser.