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

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
RevLine 
[995]1#ifndef _theplu_yat_utility_vector_base_
2#define _theplu_yat_utility_vector_base_
[616]3
[22]4// $Id: VectorBase.h 1437 2008-08-25 17:55:00Z peter $
5
[570]6/*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
[831]8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
[1275]9  Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
[831]10  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
[1275]11  Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
[1008]12  Copyright (C) 2008 Peter Johansson
[570]13
[1437]14  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
[570]15
[675]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.
[570]20
[675]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
[570]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
[680]32#include "Exception.h"
[1038]33#include "StrideIterator.h"
[420]34
[12]35#include <iostream>
[63]36#include <vector>
37#include <utility>
[12]38
39#include <gsl/gsl_vector.h>
[431]40#include <gsl/gsl_sort_vector.h>
[12]41
[42]42namespace theplu {
[680]43namespace yat {
[616]44namespace utility {
[1046]45 
[420]46  class matrix;
[1120]47  class Vector;
[420]48
[686]49  /**
[767]50     @brief This is the yat interface to GSL vector.
[686]51
[1046]52     This is an interface class for vectors containing the const
53     interface. For mutable functionality see VectorMutable.
[686]54  */
[1015]55  class VectorBase
[42]56  {
57  public:
[1015]58    /// \brief VectorBase::const_iterator
[1041]59    typedef StrideIterator<const double*> const_iterator;
[880]60
[754]61    /**
[995]62       \brief Constructor.
[754]63    */
[1027]64    VectorBase(const gsl_vector* v=NULL);
[1008]65
[42]66    ///
67    /// The destructor.
68    ///
[1015]69    virtual ~VectorBase(void);
[12]70
[754]71    /**
[1015]72       \return read-only iterator to start of VectorBase
[880]73     */
74    const_iterator begin(void) const;
75
76    /**
[1015]77       \return read-only iterator to end of VectorBase
[880]78     */
79    const_iterator end(void) const;
80
81    /**
[1015]82       \brief Check whether VectorBases are equal within a user defined
[789]83       precision, set by \a precision.
84
85       \return True if each element deviates less or equal than \a
[1015]86       d. If any VectorBase contain a NaN, false is always returned.
[789]87
88       \see operator== and operator!=
89    */
[1015]90    bool equal(const VectorBase&, const double precision=0) const;
[789]91
[1046]92    /**
93       \return A const pointer to the internal GSL vector,
94    */
[714]95    const gsl_vector* gsl_vector_p(void) const;
[227]96
[1008]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
[1046]105    /**
106       \return number of elements in the VectorBase.
107    */
[714]108    size_t size(void) const;
[12]109
[755]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    */
[714]119    const double& operator()(size_t i) const;
[420]120
[789]121    /**
122       \brief Comparison operator. Takes linear time.
123
124       Checks are performed with exact matching, i.e., rounding off
125       effects may destroy comparison. Use the equal function for
126       comparing elements within a user defined precision.
127
128       \return True if all elements are equal otherwise false.
129
[1015]130       \see equal(const VectorBase&, const double precision=0)
[789]131    */
[1015]132    bool operator==(const VectorBase&) const;
[276]133
[789]134    /**
135       \brief Comparison operator. Takes linear time.
136
137       Checks are performed with exact matching, i.e., rounding off
138       effects may destroy comparison. Use the equal function for
139       comparing elements within a user defined precision.
140
141       \return False if all elements are equal otherwise true.
142
[1015]143       \see equal(const VectorBase&, const double precision=0)
[789]144    */
[1015]145    bool operator!=(const VectorBase&) const;
[789]146
[341]147    ///
[714]148    /// @return The dot product.
149    ///
[1015]150    double operator*(const VectorBase&) const;
[714]151
[995]152  protected:
[1125]153    /// pointer to underlying GSL vector
[995]154    const gsl_vector* const_vec_;
[1008]155
156  private:
[1125]157    // copy assignment not allowed
[1015]158    const VectorBase& operator=(const VectorBase&);
[420]159  };
[12]160
[782]161  /**
[1015]162     \brief Check if all elements of the VectorBase are zero.
[12]163
[1015]164     \return True if all elements in the VectorBase is zero, false
[782]165     othwerwise.
166  */
[1015]167  bool isnull(const VectorBase&);
[12]168
[782]169  /**
[1015]170     \brief Get the maximum value of the VectorBase.
[782]171
[1015]172     \return The maximum value of the VectorBase.
[782]173  */
[1015]174  double max(const VectorBase&);
[782]175
176  /**
[1015]177     \brief Locate the maximum value in the VectorBase.
[782]178
[1015]179     \return The index to the maximum value of the VectorBase.
[782]180
181     \note Lower index has precedence.
182  */
[1015]183  size_t max_index(const VectorBase&);
[782]184
185  /**
[1015]186     \brief Get the minimum value of the VectorBase.
[782]187
[1015]188     \return The minimum value of the VectorBase.
[782]189  */
[1015]190  double min(const VectorBase&);
[782]191
192  /**
[1015]193     \brief Locate the minimum value in the VectorBase.
[782]194
[1015]195     \return The index to the minimum value of the VectorBase.
[782]196
197     \note Lower index has precedence.
198  */
[1015]199  size_t min_index(const VectorBase&);
[782]200
201  /**
[1015]202     \brief Create a VectorBase \a flag indicating NaN's in another VectorBase
[791]203     \a templat.
204
[1015]205     The \a flag VectorBase is changed to contain 1's and 0's only. A 1
206     means that the corresponding element in the \a templat VectorBase is
[791]207     valid and a zero means that the corresponding element is a NaN.
208
[1046]209     \note Space for vector \a flag is reallocated to fit the size of
[1015]210     VectorBase \a templat if sizes mismatch.
[791]211
[1015]212     \return True if the \a templat VectorBase contains at least one NaN.
[791]213  */
[1120]214  bool nan(const VectorBase& templat, Vector& flag);
[791]215
216  /**
[1008]217     Create a vector \a sort_index containing the indeces of
[1015]218     elements in a another VectorBase \a invec.  The elements of \a
219     sort_index give the index of the VectorBase element which would
220     have been stored in that position if the VectorBase had been sorted
[877]221     in place. The first element of \a sort_index gives the index of the least
[995]222     element in \a invec, and the last element of \a sort_index gives the
[1015]223     index of the greatest element in \a invec . The VectorBase \a invec
[995]224     is not changed.
[877]225  */
[1015]226  void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec);
[877]227
[995]228  /**
[1046]229      Similar to sort_index but creates a VectorBase with indices to
230      the \a k smallest elements in \a invec.
[879]231  */
[1008]232  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, 
[1015]233                           const VectorBase& invec);
[877]234
[995]235  /**
[1046]236      Similar to sort_index but creates a VectorBase with indices to
237      the \a k largest elements in \a invec.
[879]238  */
[1008]239  void sort_largest_index(std::vector<size_t>& sort_index, size_t k, 
[1015]240                          const VectorBase& invec);
[879]241
[877]242  /**
[1015]243     \brief Calculate the sum of all VectorBase elements.
[782]244
245     \return The sum.
246  */
[1015]247  double sum(const VectorBase&);
[782]248
249  /**
[1015]250     \brief The output operator for the VectorBase class.
[1437]251
252     Elements in VectorBase \a v are sent to ostream \a s and
253     separated with the fill character of stream \a s, s.fill(). If
254     you, for example, want to print the VectorBase \a v with the
255     elements separated by a ':', you do so by:
256     \verbatim
257     s << setfill(':') << v;
258     \endverbatim
[782]259  */
[1437]260  std::ostream& operator<<(std::ostream& s, const VectorBase& v);
[782]261
[686]262}}} // of namespace utility, yat, and theplu
[12]263
[420]264#endif
Note: See TracBrowser for help on using the repository browser.