source: trunk/yat/utility/VectorMutable.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: 6.3 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.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 "VectorBase.h"
31#include "Exception.h"
32#include "StrideIterator.h"
33
34#include <iostream>
35#include <vector>
36#include <utility>
37
38#include <gsl/gsl_vector.h>
39
40namespace theplu {
41namespace yat {
42namespace utility {
43
44  class matrix;
45  class Vector;
46
47  /**
48     @brief This is the mutable interface to GSL vector.
49
50     This class contains the mutable interface to vector classes.
51
52     The non-mutable interface is inherited from VectorBase. When
53     dealing with const vectors, it is preferable to use the
54     VectorBase signature because this allows usage of VectorConstView
55     too.
56  */
57  class VectorMutable : public VectorBase
58  {
59  public:
60    /**
61       \brief mutable iterator
62    */
63    typedef StrideIterator<double*> iterator;
64
65    /**
66       \brief default constructor
67    */
68    VectorMutable(void);
69
70    /**
71       \brief Constructor.
72    */
73    VectorMutable(gsl_vector*);
74
75    /**
76       \brief Constructor.
77    */
78    VectorMutable(const gsl_vector*);
79
80    /**
81       The destructor.
82    */
83    virtual ~VectorMutable(void);
84
85    /**
86       Set all elements to \a value.
87    */
88    void all(double value);
89
90    /**
91       \return mutable iterator to start of VectorMutable
92     */
93    iterator begin(void);
94
95    // to allow overload from base class
96    using VectorBase::begin;
97
98    /**
99       \brief This function performs element-wise division, \f$
100       this_i = this_i / other_i \; \forall i \f$.
101
102       \throw GSL_error if dimensions mis-match.
103    */
104    void div(const VectorBase& other);
105
106    /**
107       \return mutable iterator to end of VectorMutable
108     */
109    iterator end(void);
110
111    // to allow overload from base class
112    using VectorBase::end;
113
114    /**
115       @return A pointer to the internal GSL vector,
116    */
117    gsl_vector* gsl_vector_p(void);
118
119    using VectorBase::gsl_vector_p;
120
121    /**
122       Check if the vector object is a view (sub-vector) to another
123       vector.
124   
125       \return True if the object is a view, false othwerwise.
126   
127     */
128    virtual bool isview(void) const=0; 
129
130    /**
131       \brief This function performs element-wise multiplication, \f$
132       this_i = this_i * other_i \; \forall i \f$.
133
134       \throw GSL_error if dimensions mis-match.
135    */
136    void mul(const VectorBase& other);
137
138    /**
139       \brief Reverse the order of elements in the VectorMutable.
140    */
141    void reverse(void);
142
143    /**
144       \brief Exchange elements \a i and \a j.
145    */
146    void swap(size_t i, size_t j);
147
148    /**
149       \brief Element access operator.
150
151       \return Reference to element \a i.
152
153       \throw If GSL range checks are enabled in the underlying GSL
154       library a GSL_error exception is thrown if either index is out
155       of range.
156    */
157    double& operator()(size_t i);
158
159    // to allow overload from base class
160    using VectorBase::operator();
161
162    /**
163       \brief Addition and assign operator. VectorBase addition, \f$
164       this_i = this_i + other_i \; \forall i \f$.
165
166       \return A const reference to the resulting VectorBase.
167
168       \throw GSL_error if dimensions mis-match.
169    */
170    const VectorMutable& operator+=(const VectorBase&);
171
172    /**
173       \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;
174       \forall i \f$.
175
176       \return A const reference to the resulting VectorBase.
177    */
178    const VectorMutable& operator+=(double d);
179
180    /**
181       \brief Subtract and assign operator. VectorBase subtraction, \f$
182       this_i = this_i - other_i \; \forall i \f$.
183
184       \return A const reference to the resulting VectorBase.
185
186       \throw GSL_error if dimensions mis-match.
187    */
188    const VectorMutable& operator-=(const VectorBase&);
189
190    /**
191       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
192       \; \forall i \f$.
193
194       \return A const reference to the resulting VectorBase.
195    */
196    const VectorMutable& operator-=(double d);
197
198    /**
199       \brief Multiply with scalar and assign operator, \f$ this_i =
200       this_i * d \; \forall i \f$.
201
202       \return A const reference to the resulting VectorBase.
203    */
204    const VectorMutable& operator*=(double d);
205
206  protected:
207    /**
208       pointer to underlying GSL vector. Should always point to same
209       gsl_vector as const_vec_ in base class does. This pointer should
210       by used for mutable operations. Do not use this in const
211       functions; use const_vec_ inherited from BaseVector.
212     */
213    gsl_vector* vec_;
214
215    /**
216       Proxy class used to allow copy and assignment of VectorView. By
217       design vectors and matrices are passed as non-const references
218       in all constructors of VectorView. Because the standard does
219       not allow temporary objects to be bound to non-const
220       references, it is not possible to directly construct a
221       VectorView from a temporary VectorView returned from a
222       function. Instead this proxy class is created from the
223       temporary object and then a VectorView can be created from this
224       proxy.
225
226       \see VectorView
227     */
228    struct proxy
229    { 
230      /// pointer to GSL vector keeping everything we need to create a
231      /// new VectorMutable from a proxy.
232      gsl_vector* vec_;
233    };
234
235  private:
236    // copy assignment not allowed
237    const VectorMutable& operator=(const VectorMutable&);
238  public:
239    /**
240       conversion operator to protected proxy class.
241     */
242    operator proxy();
243
244  };
245
246  /**
247     Randomly shuffles the elements in VectorMutable \a invec
248  */
249  void shuffle(VectorMutable& invec); 
250
251  /**
252     Sort the elements in the VectorMutable \a vec
253  */
254  void sort(VectorMutable& vec);
255
256}}} // of namespace utility, yat, and theplu
257
258#endif
Note: See TracBrowser for help on using the repository browser.