source: trunk/yat/utility/VectorMutable.h @ 1041

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

generalized StrideIterator? to work with Iterators as well - not only pointers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.h 1041 2008-02-06 17:36:54Z 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 "VectorBase.h"
33#include "Exception.h"
34#include "StrideIterator.h"
35
36#include <iostream>
37#include <vector>
38#include <utility>
39
40#include <gsl/gsl_vector.h>
41#include <gsl/gsl_sort_vector.h>
42
43namespace theplu {
44namespace yat {
45namespace utility {
46
47  class matrix;
48  class vector;
49
50  /**
51     @brief This is the yat interface to GSL vector.
52
53     For time being 'double' is the only type supported.
54
55     \par File streams:
56     Reading and writing vectors to file streams are of course
57     supported. These are implemented without using GSL functionality,
58     and thus binary read and write to streams are not supported.
59
60     \par Vector views:
61     GSL vector views are supported and these are disguised as
62     ordinary utility::vectors. A support function is added,
63     utility::vector::isview(), that can be used to check if a vector
64     object is a view. Note that view vectors do not own the
65     underlying data, and a view is not valid if the vector/matrix
66     owing the data is deallocated.
67
68     \par
69     Currently there is no restriction on how a vector is used when
70     the vector is a const view into another vector or matrix. To
71     avoid unexpected runtime errors, the programmer must declare
72     const view vectors as 'const' in order to get compile time
73     diagnostics about improper use of a const view vector object. If
74     'const' is not used and the const view vector is used erroneously
75     (such as on the left hand side in assignments), the compiler will
76     not catch the error and runtime error will occur. assert(3) is
77     used to catch the runtime error during development. Example on
78     bad and proper use of const view vectors:
79     @code
80  const vector vm(13,1.0);
81  vector v1(vm,2,4);       // bad code! not const!
82  v1(0)=-123;              // accepted by compiler, runtime abort will occur
83                           // or catched by assert depending on compiler flags
84  const vector v2(vm,2,4); // proper code
85  v2(0)=-123;              // not acceptable for the compiler
86     @endcode
87  */
88
89  class VectorMutable : public VectorBase
90  {
91  public:
92    /**
93       \brief mutable iterator
94    */
95    typedef StrideIterator<double*> iterator;
96
97    /**
98       \brief default constructor
99    */
100    VectorMutable(void);
101
102    /**
103       \brief Constructor.
104    */
105    VectorMutable(gsl_vector*);
106
107    /**
108       \brief Constructor.
109    */
110    VectorMutable(const gsl_vector*);
111
112    ///
113    /// The destructor.
114    ///
115    virtual ~VectorMutable(void);
116
117    ///
118    /// Set all elements to \a value.
119    ///
120    void all(double value);
121
122    /**
123       \return mutable iterator to start of VectorBase
124     */
125    iterator begin(void);
126
127    // to allow overload from base class
128    using VectorBase::begin;
129
130    /**
131       \brief This function performs element-wise division, \f$
132       this_i = this_i / other_i \; \forall i \f$.
133
134       \throw GSL_error if dimensions mis-match.
135    */
136    void div(const VectorBase& other);
137
138    /**
139       \return mutable iterator to end of VectorMutable
140     */
141    iterator end(void);
142
143    // to allow overload from base class
144    using VectorBase::end;
145
146    /**
147       @return A pointer to the internal GSL vector,
148    */
149    gsl_vector* gsl_vector_p(void);
150
151    using VectorBase::gsl_vector_p;
152
153    /**
154       Check if the vector object is a view (sub-vector) to another
155       vector.
156   
157       \return True if the object is a view, false othwerwise.
158   
159     */
160    virtual bool isview(void) const=0; 
161
162    /**
163       \brief This function performs element-wise multiplication, \f$
164       this_i = this_i * other_i \; \forall i \f$.
165
166       \throw GSL_error if dimensions mis-match.
167    */
168    void mul(const VectorBase& other);
169
170    /**
171       \brief Reverse the order of elements in the VectorMutable.
172    */
173    void reverse(void);
174
175    /**
176       \brief Exchange elements \a i and \a j.
177    */
178    void swap(size_t i, size_t j);
179
180    /**
181       \brief Element access operator.
182
183       \return Reference to element \a i.
184
185       \throw If GSL range checks are enabled in the underlying GSL
186       library a GSL_error exception is thrown if either index is out
187       of range.
188    */
189    double& operator()(size_t i);
190    // Peter, remove this one
191    double& operator[](size_t i);
192    // to allow overload from base class
193    using VectorBase::operator();
194    using VectorBase::operator[];
195
196    /**
197       \brief Addition and assign operator. VectorBase addition, \f$
198       this_i = this_i + other_i \; \forall i \f$.
199
200       \return A const reference to the resulting VectorBase.
201
202       \throw GSL_error if dimensions mis-match.
203    */
204    const VectorMutable& operator+=(const VectorBase&);
205
206    /**
207       \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;
208       \forall i \f$.
209
210       \return A const reference to the resulting VectorBase.
211    */
212    const VectorMutable& operator+=(double d);
213
214    /**
215       \brief Subtract and assign operator. VectorBase subtraction, \f$
216       this_i = this_i - other_i \; \forall i \f$.
217
218       \return A const reference to the resulting VectorBase.
219
220       \throw GSL_error if dimensions mis-match.
221    */
222    const VectorMutable& operator-=(const VectorBase&);
223
224    /**
225       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
226       \; \forall i \f$.
227
228       \return A const reference to the resulting VectorBase.
229    */
230    const VectorMutable& operator-=(double d);
231
232    /**
233       \brief Multiply with scalar and assign operator, \f$ this_i =
234       this_i * d \; \forall i \f$.
235
236       \return A const reference to the resulting VectorBase.
237    */
238    const VectorMutable& operator*=(double d);
239
240  protected:
241    gsl_vector* vec_;
242
243    // Peter document
244    struct proxy
245    { 
246      gsl_vector* vec_;
247    };
248
249  private:
250    // copy assignment no allowed
251    const VectorMutable& operator=(const VectorMutable&);
252  public:
253    /**
254     */
255    operator proxy();
256
257  };
258
259  /**
260     Randomly shuffles the elements in VectorBase \a invec
261  */
262  void shuffle(VectorMutable& invec); 
263
264  /**
265     Sort the elements in the VectorBase.
266  */
267  void sort(VectorMutable&);
268
269}}} // of namespace utility, yat, and theplu
270
271#endif
Note: See TracBrowser for help on using the repository browser.