plugins/base1/se.lu.thep.wenni/trunk/lib/c++_tools/gslapi/vector.h

Code
Comments
Other
Rev Date Author Line
597 27 Feb 08 jari 1 #ifndef _theplu_gslapi_vector_
597 27 Feb 08 jari 2 #define _theplu_gslapi_vector_
597 27 Feb 08 jari 3
69 11 Feb 06 jari 4 // $Id$
69 11 Feb 06 jari 5
95 05 Apr 06 jari 6 /*
95 05 Apr 06 jari 7   Copyright (C) 2003 Daniel Dalevi, Peter Johansson
95 05 Apr 06 jari 8   Copyright (C) 2004 Jari Häkkinen, Peter Johansson
95 05 Apr 06 jari 9   Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
597 27 Feb 08 jari 10   Copyright (C) 2006, 2008 Jari Häkkinen
95 05 Apr 06 jari 11
95 05 Apr 06 jari 12   This file is part of the thep c++ tools library,
95 05 Apr 06 jari 13                                 http://lev.thep.lu.se/trac/c++_tools
95 05 Apr 06 jari 14
95 05 Apr 06 jari 15   The c++ tools library is free software; you can redistribute it
95 05 Apr 06 jari 16   and/or modify it under the terms of the GNU General Public License
824 26 Nov 08 jari 17   as published by the Free Software Foundation; either version 3 of
95 05 Apr 06 jari 18   the License, or (at your option) any later version.
95 05 Apr 06 jari 19
95 05 Apr 06 jari 20   The c++ tools library is distributed in the hope that it will be
95 05 Apr 06 jari 21   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
95 05 Apr 06 jari 22   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
95 05 Apr 06 jari 23   General Public License for more details.
95 05 Apr 06 jari 24
95 05 Apr 06 jari 25   You should have received a copy of the GNU General Public License
824 26 Nov 08 jari 26   along with WeNNI. If not, see <http://www.gnu.org/licenses/>.
95 05 Apr 06 jari 27 */
95 05 Apr 06 jari 28
110 13 Jun 06 jari 29 #include <c++_tools/utility/Exception.h>
110 13 Jun 06 jari 30
69 11 Feb 06 jari 31 #include <iostream>
597 27 Feb 08 jari 32 #include <string>
597 27 Feb 08 jari 33 #include <utility>
69 11 Feb 06 jari 34 #include <vector>
69 11 Feb 06 jari 35
69 11 Feb 06 jari 36 #include <gsl/gsl_vector.h>
110 13 Jun 06 jari 37 #include <gsl/gsl_sort_vector.h>
69 11 Feb 06 jari 38
69 11 Feb 06 jari 39 namespace theplu {
69 11 Feb 06 jari 40 namespace gslapi {
110 13 Jun 06 jari 41
110 13 Jun 06 jari 42   class matrix;
110 13 Jun 06 jari 43
69 11 Feb 06 jari 44   ///
69 11 Feb 06 jari 45   /// This is the C++ tools interface to GSL vector. 'double' is the
69 11 Feb 06 jari 46   /// only type supported, maybe we should add a 'complex' type as
69 11 Feb 06 jari 47   /// well in the future.
69 11 Feb 06 jari 48   ///
69 11 Feb 06 jari 49   /// \par[File streams] Reading and writing vectors to file streams
69 11 Feb 06 jari 50   /// are of course supported. These are implemented without using GSL
69 11 Feb 06 jari 51   /// functionality, and thus binary read and write to streams are not
69 11 Feb 06 jari 52   /// supported.
69 11 Feb 06 jari 53   ///
69 11 Feb 06 jari 54   /// \par[Vector views] GSL vector views are supported and these are
69 11 Feb 06 jari 55   /// disguised as ordinary gslapi::vectors. A support function is
110 13 Jun 06 jari 56   /// added, gslapi::vector::isview(), that can be used to check if a
110 13 Jun 06 jari 57   /// vector object is a view. Note that view vectors do not own the
110 13 Jun 06 jari 58   /// undelying data, and a view is not valid if the vector owning the
110 13 Jun 06 jari 59   /// data is deallocated.
69 11 Feb 06 jari 60   ///
110 13 Jun 06 jari 61   /// @note Missing support to underlying GSL vector features can in
110 13 Jun 06 jari 62   /// principle be included to this class if requested by the user
110 13 Jun 06 jari 63   /// community.
110 13 Jun 06 jari 64   ///
69 11 Feb 06 jari 65   class vector
69 11 Feb 06 jari 66   {
69 11 Feb 06 jari 67   public:
69 11 Feb 06 jari 68
69 11 Feb 06 jari 69     ///
69 11 Feb 06 jari 70     /// The default constructor.
69 11 Feb 06 jari 71     ///
110 13 Jun 06 jari 72     inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {}
69 11 Feb 06 jari 73
69 11 Feb 06 jari 74     ///
69 11 Feb 06 jari 75     /// Constructor. Allocates memory space for \a n elements, and
69 11 Feb 06 jari 76     /// sets all elements to \a init_value.
69 11 Feb 06 jari 77     ///
110 13 Jun 06 jari 78     inline vector(const size_t n,const double init_value=0)
110 13 Jun 06 jari 79       : view_(NULL), const_view_(NULL)
110 13 Jun 06 jari 80       { v_ = gsl_vector_alloc(n); set_all(init_value); }
69 11 Feb 06 jari 81
69 11 Feb 06 jari 82     ///
69 11 Feb 06 jari 83     /// The copy constructor.
69 11 Feb 06 jari 84     ///
69 11 Feb 06 jari 85     /// @note If the object to be copied is a vector view, the values
69 11 Feb 06 jari 86     /// of the view will be copied, i.e. the view is not copied.
69 11 Feb 06 jari 87     ///
110 13 Jun 06 jari 88     inline vector(const vector& other) : view_(NULL), const_view_(NULL)
110 13 Jun 06 jari 89       { v_ = other.create_gsl_vector_copy(); }
69 11 Feb 06 jari 90
69 11 Feb 06 jari 91     ///
110 13 Jun 06 jari 92     /// Vector view constructor.
69 11 Feb 06 jari 93     ///
69 11 Feb 06 jari 94     /// Create a view of vector \a v, with starting index \a offset,
69 11 Feb 06 jari 95     /// size \a n, and an optional \a stride.
69 11 Feb 06 jari 96     ///
69 11 Feb 06 jari 97     /// A vector view can be used as any vector with the difference
69 11 Feb 06 jari 98     /// that changes made to the view will also change the object that
69 11 Feb 06 jari 99     /// is viewed. Also, using the copy constructor will create a new
69 11 Feb 06 jari 100     /// vector object that is a copy of whatever is viewed. If a copy
110 13 Jun 06 jari 101     /// of the view is needed then you should use this constructor to
110 13 Jun 06 jari 102     /// obtain a copy.
69 11 Feb 06 jari 103     ///
69 11 Feb 06 jari 104     /// @note If the object viewed by the view goes out of scope or is
110 13 Jun 06 jari 105     /// deleted, the view becomes invalid and the result of further
110 13 Jun 06 jari 106     /// use is undefined.
69 11 Feb 06 jari 107     ///
110 13 Jun 06 jari 108     vector(vector& v, size_t offset, size_t n, size_t stride=1);
69 11 Feb 06 jari 109
69 11 Feb 06 jari 110     ///
110 13 Jun 06 jari 111     /// Vector const view constructor.
69 11 Feb 06 jari 112     ///
110 13 Jun 06 jari 113     /// Create a view of vector \a v, with starting index \a offset,
110 13 Jun 06 jari 114     /// size \a n, and an optional \a stride.
110 13 Jun 06 jari 115     ///
110 13 Jun 06 jari 116     /// A vector view can be used as any const vector. Using the copy
110 13 Jun 06 jari 117     /// constructor will create a new vector object that is a copy of
110 13 Jun 06 jari 118     /// whatever is viewed. If a copy of the view is needed then you
110 13 Jun 06 jari 119     /// should use this constructor to obtain a copy.
110 13 Jun 06 jari 120     ///
110 13 Jun 06 jari 121     /// @note If the object viewed by the view goes out of scope or is
110 13 Jun 06 jari 122     /// deleted, the view becomes invalid and the result of further
110 13 Jun 06 jari 123     /// use is undefined.
110 13 Jun 06 jari 124     ///
110 13 Jun 06 jari 125     vector(const vector& v, size_t offset, size_t n, size_t stride=1);
69 11 Feb 06 jari 126
69 11 Feb 06 jari 127     ///
110 13 Jun 06 jari 128     /// Matrix row/column view constructor.
110 13 Jun 06 jari 129     ///
110 13 Jun 06 jari 130     /// Create a row/column vector view of matrix \a m, pointing at
110 13 Jun 06 jari 131     /// row/column \a i. The parameter \a row is used to set whether
110 13 Jun 06 jari 132     /// the view should be a row or column view. If \a row is set to
110 13 Jun 06 jari 133     /// true, the view will be a row view (default behaviour), and,
110 13 Jun 06 jari 134     /// naturally, a column view otherwise.
110 13 Jun 06 jari 135     ///
110 13 Jun 06 jari 136     /// A vector view can be used as any vector with the difference
110 13 Jun 06 jari 137     /// that changes made to the view will also change the object that
110 13 Jun 06 jari 138     /// is viewed. Also, using the copy constructor will create a new
110 13 Jun 06 jari 139     /// vector object that is a copy of whatever is viewed. If a copy
110 13 Jun 06 jari 140     /// of the view is needed then you should use the vector view
110 13 Jun 06 jari 141     /// constructor to obtain a copy.
110 13 Jun 06 jari 142     ///
110 13 Jun 06 jari 143     /// @note If the object viewed by the view goes out of scope or is
110 13 Jun 06 jari 144     /// deleted, the view becomes invalid and the result of further
110 13 Jun 06 jari 145     /// use is undefined.
110 13 Jun 06 jari 146     ///
110 13 Jun 06 jari 147      vector(matrix& m, size_t i, bool row=true);
110 13 Jun 06 jari 148
110 13 Jun 06 jari 149     ///
110 13 Jun 06 jari 150     /// Matrix row/column const view constructor.
110 13 Jun 06 jari 151     ///
110 13 Jun 06 jari 152     /// Create a row/column vector view of matrix \a m, pointing at
110 13 Jun 06 jari 153     /// row/column \a i. The parameter \a row is used to set whether
110 13 Jun 06 jari 154     /// the view should be a row or column view. If \a row is set to
110 13 Jun 06 jari 155     /// true, the view will be a row view (default behaviour), and,
110 13 Jun 06 jari 156     /// naturally, a column view otherwise.
110 13 Jun 06 jari 157     ///
110 13 Jun 06 jari 158     /// A const vector view can be used as any const vector. Using the
110 13 Jun 06 jari 159     /// copy constructor will create a new vector object that is a
110 13 Jun 06 jari 160     /// copy of whatever is viewed. If a copy of the view is needed
110 13 Jun 06 jari 161     /// then you should use the vector view constructor to obtain a
110 13 Jun 06 jari 162     /// copy.
110 13 Jun 06 jari 163     ///
110 13 Jun 06 jari 164     /// @note If the object viewed by the view goes out of scope or is
110 13 Jun 06 jari 165     /// deleted, the view becomes invalid and the result of further
110 13 Jun 06 jari 166     /// use is undefined.
110 13 Jun 06 jari 167     ///
110 13 Jun 06 jari 168      vector(const matrix& m, size_t i, bool row=true);
110 13 Jun 06 jari 169
110 13 Jun 06 jari 170     ///
69 11 Feb 06 jari 171     /// The istream constructor.
69 11 Feb 06 jari 172     ///
110 13 Jun 06 jari 173     /// Either elements should be separated 
110 13 Jun 06 jari 174     /// with white space characters (default), or elements should be separated
110 13 Jun 06 jari 175     /// by the delimiter \a sep. When delimiter \a sep is used empty elements
110 13 Jun 06 jari 176     /// are stored as NaN's (except that empty lines are ignored). The
69 11 Feb 06 jari 177     /// end of input to the vector is at end of file marker.
69 11 Feb 06 jari 178     ///
110 13 Jun 06 jari 179     explicit vector(std::istream &, char sep='\0') throw (utility::IO_error,std::exception);
69 11 Feb 06 jari 180
597 27 Feb 08 jari 181     ///
597 27 Feb 08 jari 182     /// The string constructor.
597 27 Feb 08 jari 183     ///
597 27 Feb 08 jari 184     /// Either elements should be separated 
597 27 Feb 08 jari 185     /// with white space characters (default), or elements should be separated
597 27 Feb 08 jari 186     /// by the delimiter \a sep. When delimiter \a sep is used empty elements
597 27 Feb 08 jari 187     /// are stored as NaN's (except that empty lines are ignored). The
597 27 Feb 08 jari 188     /// end of input to the vector is at end of file marker.
597 27 Feb 08 jari 189     ///
597 27 Feb 08 jari 190     explicit vector(std::string &, char sep='\0') throw (utility::IO_error,std::exception);
110 13 Jun 06 jari 191
597 27 Feb 08 jari 192
69 11 Feb 06 jari 193     ///
69 11 Feb 06 jari 194     /// The destructor.
69 11 Feb 06 jari 195     ///
69 11 Feb 06 jari 196     ~vector(void);
69 11 Feb 06 jari 197
69 11 Feb 06 jari 198     ///
69 11 Feb 06 jari 199     /// Vector addition, \f$this_i = this_i + other_i \; \forall i\f$.
69 11 Feb 06 jari 200     ///
69 11 Feb 06 jari 201     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 202     ///
69 11 Feb 06 jari 203     // Jari, group as vector_operators
69 11 Feb 06 jari 204     inline int add(const vector& other) { return gsl_vector_add(v_,other.v_); }
69 11 Feb 06 jari 205
69 11 Feb 06 jari 206     ///
69 11 Feb 06 jari 207     /// Add a constant to a vector, \f$this_i = this_i + term \;
69 11 Feb 06 jari 208     /// \forall i\f$.
69 11 Feb 06 jari 209     ///
69 11 Feb 06 jari 210     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 211     ///
69 11 Feb 06 jari 212     // Jari, group as vector_operators
69 11 Feb 06 jari 213     inline int add(double term) { return gsl_vector_add_constant(v_,term); }
69 11 Feb 06 jari 214
69 11 Feb 06 jari 215     ///
69 11 Feb 06 jari 216     /// This function performs element-wise division, \f$this_i =
69 11 Feb 06 jari 217     /// this_i/other_i \; \forall i\f$.
69 11 Feb 06 jari 218     ///
69 11 Feb 06 jari 219     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 220     ///
69 11 Feb 06 jari 221     // Jari, doxygen group as Vector operators
69 11 Feb 06 jari 222     inline int div(const vector& other) { return gsl_vector_div(v_,other.v_); }
69 11 Feb 06 jari 223
69 11 Feb 06 jari 224     ///
110 13 Jun 06 jari 225     /// @return A const pointer to the internal GSL vector,
69 11 Feb 06 jari 226     ///
110 13 Jun 06 jari 227      inline const gsl_vector* gsl_vector_p(void) const { return v_; }
69 11 Feb 06 jari 228
69 11 Feb 06 jari 229     ///
110 13 Jun 06 jari 230     /// @return A pointer to the internal GSL vector,
69 11 Feb 06 jari 231     ///
110 13 Jun 06 jari 232      inline gsl_vector* gsl_vector_p(void) { return v_; }
69 11 Feb 06 jari 233
69 11 Feb 06 jari 234     ///
110 13 Jun 06 jari 235     /// @return True if all elements in the vector is zero, false
110 13 Jun 06 jari 236     /// othwerwise;
69 11 Feb 06 jari 237     ///
110 13 Jun 06 jari 238     inline bool isnull(void) const { return gsl_vector_isnull(v_); }
69 11 Feb 06 jari 239
69 11 Feb 06 jari 240     ///
110 13 Jun 06 jari 241     /// Check if the vector object is a view (sub-vector) to another
110 13 Jun 06 jari 242     /// vector.
69 11 Feb 06 jari 243     ///
110 13 Jun 06 jari 244     /// @return True if the object is a view, false othwerwise.
110 13 Jun 06 jari 245     ///
110 13 Jun 06 jari 246     inline bool isview(void) const { return view_ || const_view_; }
69 11 Feb 06 jari 247
69 11 Feb 06 jari 248     ///
69 11 Feb 06 jari 249     /// @return The maximum value of the vector.
69 11 Feb 06 jari 250     ///
69 11 Feb 06 jari 251     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 252     inline double max(void) const { return gsl_vector_max(v_); }
69 11 Feb 06 jari 253
69 11 Feb 06 jari 254     ///
69 11 Feb 06 jari 255     /// @return The element index to the maximum value of the
69 11 Feb 06 jari 256     /// vector. The lowest index has precedence.
69 11 Feb 06 jari 257     ///
69 11 Feb 06 jari 258     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 259     inline size_t max_index(void) const { return gsl_vector_max_index(v_); }
69 11 Feb 06 jari 260
69 11 Feb 06 jari 261     ///
69 11 Feb 06 jari 262     /// @return The minimum value of the vector.
69 11 Feb 06 jari 263     ///
69 11 Feb 06 jari 264     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 265     inline double min(void) const { return gsl_vector_min(v_); }
69 11 Feb 06 jari 266
69 11 Feb 06 jari 267     ///
69 11 Feb 06 jari 268     /// @return The element index to the minimum value of the
69 11 Feb 06 jari 269     /// vector. The lowest index has precedence.
69 11 Feb 06 jari 270     ///
69 11 Feb 06 jari 271     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 272     inline size_t min_index(void) const { return gsl_vector_min_index(v_); }
69 11 Feb 06 jari 273
69 11 Feb 06 jari 274     ///
69 11 Feb 06 jari 275     /// @return The minimum and maximum values of the vector, as the
69 11 Feb 06 jari 276     /// \a first and \a second member of the returned \a pair,
69 11 Feb 06 jari 277     /// respectively.
69 11 Feb 06 jari 278     ///
69 11 Feb 06 jari 279     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 280     std::pair<double,double> minmax(void) const;
69 11 Feb 06 jari 281
69 11 Feb 06 jari 282     ///
69 11 Feb 06 jari 283     /// @return The indecies to the minimum and maximum values of the
69 11 Feb 06 jari 284     /// vector, as the \a first and \a second member of the returned
69 11 Feb 06 jari 285     /// \a pair, respectively. The lowest index has precedence.
69 11 Feb 06 jari 286     ///
69 11 Feb 06 jari 287     // Jari, doxygen group as Finding maximum and minimum elements
69 11 Feb 06 jari 288     std::pair<size_t,size_t> minmax_index(void) const;
69 11 Feb 06 jari 289
69 11 Feb 06 jari 290     ///
69 11 Feb 06 jari 291     /// This function performs element-wise multiplication, \f$this_i =
69 11 Feb 06 jari 292     /// this_i * other_i \; \forall i\f$.
69 11 Feb 06 jari 293     ///
69 11 Feb 06 jari 294     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 295     ///
69 11 Feb 06 jari 296     // Jari, doxygen group as Vector operators
69 11 Feb 06 jari 297     inline int mul(const vector& other) { return gsl_vector_mul(v_,other.v_); }
69 11 Feb 06 jari 298
69 11 Feb 06 jari 299     ///
69 11 Feb 06 jari 300     /// Reverse the order of elements in the vector.
69 11 Feb 06 jari 301     ///
69 11 Feb 06 jari 302     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 303     ///
69 11 Feb 06 jari 304     // Jari, doxygen group as Exchanging elements
69 11 Feb 06 jari 305     inline int reverse(void) { return gsl_vector_reverse(v_);}
69 11 Feb 06 jari 306
69 11 Feb 06 jari 307     ///
69 11 Feb 06 jari 308     /// Rescale vector, \f$this_i = this_i * factor \; \forall i\f$.
69 11 Feb 06 jari 309     ///
69 11 Feb 06 jari 310     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 311     ///
69 11 Feb 06 jari 312     // Jari, doxygen group as Vector operators
69 11 Feb 06 jari 313     inline int scale(double factor) { return gsl_vector_scale(v_,factor); }
69 11 Feb 06 jari 314
69 11 Feb 06 jari 315     ///
110 13 Jun 06 jari 316     /// Set element values to values in \a vec. This function is
110 13 Jun 06 jari 317     /// needed for assignment of viewed elements.
110 13 Jun 06 jari 318     ///
110 13 Jun 06 jari 319     /// @return Whatever GSL returns.
110 13 Jun 06 jari 320     ///
110 13 Jun 06 jari 321     /// @note No check on size is done.
110 13 Jun 06 jari 322     ///
110 13 Jun 06 jari 323     /// @see const vector& operator=(const vector&)
110 13 Jun 06 jari 324     ///
110 13 Jun 06 jari 325     inline int set(const vector& vec) { return gsl_vector_memcpy(v_,vec.v_); }
110 13 Jun 06 jari 326
110 13 Jun 06 jari 327     ///
69 11 Feb 06 jari 328     /// Set all elements to \a value.
69 11 Feb 06 jari 329     ///
69 11 Feb 06 jari 330     // Jari, doxygen group as Initializing vector elements
69 11 Feb 06 jari 331      inline void set_all(const double& value) { gsl_vector_set_all(v_,value); }
69 11 Feb 06 jari 332
69 11 Feb 06 jari 333     ///
69 11 Feb 06 jari 334     /// Makes a basis vector by setting all elements to
69 11 Feb 06 jari 335     /// zero except the \a i-th element which is set to
69 11 Feb 06 jari 336     /// one.
69 11 Feb 06 jari 337     ///
69 11 Feb 06 jari 338     // Jari, doxygen group as Initializing vector elements
69 11 Feb 06 jari 339     inline void set_basis(const size_t i) { gsl_vector_set_basis(v_,i); }
69 11 Feb 06 jari 340     
69 11 Feb 06 jari 341     ///
69 11 Feb 06 jari 342     /// Set all elements to zero.
69 11 Feb 06 jari 343     ///
69 11 Feb 06 jari 344     // Jari, doxygen group as Initializing vector elements
69 11 Feb 06 jari 345      inline void set_zero(void) { gsl_vector_set_zero(v_); }
69 11 Feb 06 jari 346
69 11 Feb 06 jari 347     ///
69 11 Feb 06 jari 348     /// @return the number of elements in the vector.
69 11 Feb 06 jari 349     ///
69 11 Feb 06 jari 350     inline size_t size(void) const { return v_->size; }
69 11 Feb 06 jari 351
69 11 Feb 06 jari 352     ///
110 13 Jun 06 jari 353     /// Sort the elements in the vector.
110 13 Jun 06 jari 354     ///
110 13 Jun 06 jari 355     /// Bug in gsl: if vector contains NaN an infinite loop is entered.
110 13 Jun 06 jari 356     ///
110 13 Jun 06 jari 357     // Markus to Jari, doxygen group as Exchanging elements ????
110 13 Jun 06 jari 358     inline void sort(void) { gsl_sort_vector(v_);}
110 13 Jun 06 jari 359
110 13 Jun 06 jari 360
110 13 Jun 06 jari 361     ///
69 11 Feb 06 jari 362     /// Vector subtraction, \f$this_i = this_i - other_i \; \forall i\f$.
69 11 Feb 06 jari 363     ///
69 11 Feb 06 jari 364     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 365     ///
69 11 Feb 06 jari 366     // Jari, doxygen group as Vector operators
69 11 Feb 06 jari 367     inline int sub(const vector& other) { return gsl_vector_sub(v_,other.v_); }
69 11 Feb 06 jari 368
69 11 Feb 06 jari 369     ///
69 11 Feb 06 jari 370     /// Calculate the sum of all vector elements.
69 11 Feb 06 jari 371     ///
69 11 Feb 06 jari 372     /// @return The sum.
69 11 Feb 06 jari 373     ///
69 11 Feb 06 jari 374     double sum(void) const;
69 11 Feb 06 jari 375
69 11 Feb 06 jari 376     ///
69 11 Feb 06 jari 377     /// Swap vector elements by copying. The two vectors must have the
69 11 Feb 06 jari 378     /// same length.
69 11 Feb 06 jari 379     ///
69 11 Feb 06 jari 380     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 381     ///
69 11 Feb 06 jari 382     inline int swap(vector& other) { return gsl_vector_swap(v_,other.v_); }
69 11 Feb 06 jari 383
69 11 Feb 06 jari 384     ///
69 11 Feb 06 jari 385     /// Exchange elements \a i and \a j.
69 11 Feb 06 jari 386     ///
69 11 Feb 06 jari 387     /// @return GSL_SUCCESS on normal exit.
69 11 Feb 06 jari 388     ///
69 11 Feb 06 jari 389     // Jari, doxygen group as Exchanging elements
69 11 Feb 06 jari 390     inline int
69 11 Feb 06 jari 391     swap_elements(size_t i,size_t j) { return gsl_vector_swap_elements(v_,i,j);}
69 11 Feb 06 jari 392
69 11 Feb 06 jari 393     ///
69 11 Feb 06 jari 394     /// Element access operator.
69 11 Feb 06 jari 395     ///
69 11 Feb 06 jari 396     /// @return Reference to element \a i.
69 11 Feb 06 jari 397     ///
69 11 Feb 06 jari 398     // Jari, doxygen group as Accessing vector elements
69 11 Feb 06 jari 399     inline double& operator()(size_t i) { return *gsl_vector_ptr(v_,i); }
69 11 Feb 06 jari 400
69 11 Feb 06 jari 401     ///
69 11 Feb 06 jari 402     /// Const element access operator.
69 11 Feb 06 jari 403     ///
69 11 Feb 06 jari 404     /// @return The value of element \a i.
69 11 Feb 06 jari 405     ///
69 11 Feb 06 jari 406     // Jari, doxygen group as Accessing vector elements
110 13 Jun 06 jari 407     inline const double&
110 13 Jun 06 jari 408     operator()(size_t i) const { return *gsl_vector_const_ptr(v_,i); }
110 13 Jun 06 jari 409
69 11 Feb 06 jari 410     ///
69 11 Feb 06 jari 411     /// Element access operator.
69 11 Feb 06 jari 412     ///
69 11 Feb 06 jari 413     /// @return Reference to element \a i.
69 11 Feb 06 jari 414     ///
69 11 Feb 06 jari 415     // Jari, doxygen group as Accessing vector elements
69 11 Feb 06 jari 416     inline double& operator[](size_t i) { return *gsl_vector_ptr(v_,i); }
69 11 Feb 06 jari 417
69 11 Feb 06 jari 418     ///
69 11 Feb 06 jari 419     /// Const element access operator.
69 11 Feb 06 jari 420     ///
69 11 Feb 06 jari 421     /// @return The value of element \a i.
69 11 Feb 06 jari 422     ///
69 11 Feb 06 jari 423     // Jari, doxygen group as Accessing vector elements
110 13 Jun 06 jari 424     inline const double&
110 13 Jun 06 jari 425     operator[](size_t i) const { return *gsl_vector_const_ptr(v_,i); }
69 11 Feb 06 jari 426
69 11 Feb 06 jari 427     ///
69 11 Feb 06 jari 428     /// Comparison operator. Takes linear time.
69 11 Feb 06 jari 429     ///
69 11 Feb 06 jari 430     /// @return True if the sequence of the elements in the vectors
69 11 Feb 06 jari 431     /// are element/wise equal.
69 11 Feb 06 jari 432     ///
69 11 Feb 06 jari 433     bool operator==(const vector&) const;
69 11 Feb 06 jari 434
69 11 Feb 06 jari 435     ///
69 11 Feb 06 jari 436     /// The assignment operator. There is no requirements on
110 13 Jun 06 jari 437     /// dimensions, i.e. the vector is remapped in memory if
110 13 Jun 06 jari 438     /// necessary. This implies that in general views cannot be
110 13 Jun 06 jari 439     /// assigned using this operator. Views will be mutated into
110 13 Jun 06 jari 440     /// normal vectors. The only exception to this behaviour on views
110 13 Jun 06 jari 441     /// is when self-assignemnt is done, since self-assignment is
110 13 Jun 06 jari 442     /// ignored.
69 11 Feb 06 jari 443     ///
69 11 Feb 06 jari 444     /// @return A const reference to the resulting vector.
69 11 Feb 06 jari 445     ///
110 13 Jun 06 jari 446     /// @see int set(const vector&)
110 13 Jun 06 jari 447     ///
69 11 Feb 06 jari 448     const vector& operator=(const vector&);
69 11 Feb 06 jari 449
69 11 Feb 06 jari 450     ///
110 13 Jun 06 jari 451     /// @return The dot product.
110 13 Jun 06 jari 452     ///
110 13 Jun 06 jari 453     double operator*( const vector &other ) const;
110 13 Jun 06 jari 454
110 13 Jun 06 jari 455     ///
69 11 Feb 06 jari 456     /// Addition and assign operator.
69 11 Feb 06 jari 457     ///
69 11 Feb 06 jari 458     /// @return A const reference to the resulting vector.
69 11 Feb 06 jari 459     ///
110 13 Jun 06 jari 460     inline const vector&
110 13 Jun 06 jari 461     operator+=(const vector& other) {  gsl_vector_add(v_,other.v_); return *this;}
69 11 Feb 06 jari 462
69 11 Feb 06 jari 463     ///
69 11 Feb 06 jari 464     /// Subtract and assign operator.
69 11 Feb 06 jari 465     ///
69 11 Feb 06 jari 466     /// @return A const reference to the resulting vector.
69 11 Feb 06 jari 467     ///
110 13 Jun 06 jari 468     inline const vector&
110 13 Jun 06 jari 469     operator-=(const vector& other) {  gsl_vector_sub(v_,other.v_); return *this;}
69 11 Feb 06 jari 470
69 11 Feb 06 jari 471     ///
69 11 Feb 06 jari 472     /// Multiply with scalar and assign operator.
69 11 Feb 06 jari 473     ///
69 11 Feb 06 jari 474     /// @return A const reference to the resulting vector.
69 11 Feb 06 jari 475     ///
110 13 Jun 06 jari 476     inline const vector&
110 13 Jun 06 jari 477     operator*=(const double d) { gsl_vector_scale(v_,d); return *this; }
69 11 Feb 06 jari 478
69 11 Feb 06 jari 479
110 13 Jun 06 jari 480   private:
69 11 Feb 06 jari 481
69 11 Feb 06 jari 482     ///
69 11 Feb 06 jari 483     /// Create a new copy of the internal GSL vector.
69 11 Feb 06 jari 484     ///
69 11 Feb 06 jari 485     /// Necessary memory for the new GSL vector is allocated and the
69 11 Feb 06 jari 486     /// caller is responsible for freeing the allocated memory.
69 11 Feb 06 jari 487     ///
69 11 Feb 06 jari 488     /// @return A pointer to a copy of the internal GSL vector.
69 11 Feb 06 jari 489     ///
110 13 Jun 06 jari 490     gsl_vector* create_gsl_vector_copy(void) const;
69 11 Feb 06 jari 491
110 13 Jun 06 jari 492     gsl_vector* v_;
69 11 Feb 06 jari 493     gsl_vector_view* view_;
110 13 Jun 06 jari 494     gsl_vector_const_view* const_view_;
110 13 Jun 06 jari 495   };
69 11 Feb 06 jari 496
69 11 Feb 06 jari 497   ///
69 11 Feb 06 jari 498   /// The output operator for the vector class.
110 13 Jun 06 jari 499   ///
69 11 Feb 06 jari 500   std::ostream& operator<<(std::ostream&, const vector& );
69 11 Feb 06 jari 501
69 11 Feb 06 jari 502
69 11 Feb 06 jari 503 }} // of namespace gslapi and namespace theplu
69 11 Feb 06 jari 504
110 13 Jun 06 jari 505 #endif