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

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

fixes #467 - implicit conversion between Vector and its Views are no longer allowed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.h 1651 2008-12-15 12:57:43Z 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, 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       reference type is double&
62
63       \since New in yat 0.5
64     */
65    typedef double& reference;
66
67    /**
68       \brief mutable iterator
69    */
70    typedef StrideIterator<double*> iterator;
71
72    /**
73       \brief default constructor
74    */
75    VectorMutable(void);
76
77    /**
78       \brief Constructor.
79    */
80    VectorMutable(gsl_vector*);
81
82    /**
83       \brief Constructor.
84    */
85    VectorMutable(const gsl_vector*);
86
87    /**
88       The destructor.
89    */
90    virtual ~VectorMutable(void);
91
92    /**
93       Set all elements to \a value.
94    */
95    void all(double value);
96
97    /**
98       \return mutable iterator to start of VectorMutable
99     */
100    iterator begin(void);
101
102    // to allow overload from base class
103    using VectorBase::begin;
104
105    /**
106       \brief This function performs element-wise division, \f$
107       this_i = this_i / other_i \; \forall i \f$.
108
109       \throw GSL_error if dimensions mis-match.
110    */
111    void div(const VectorBase& other);
112
113    /**
114       \return mutable iterator to end of VectorMutable
115     */
116    iterator end(void);
117
118    // to allow overload from base class
119    using VectorBase::end;
120
121    /**
122       @return A pointer to the internal GSL vector,
123    */
124    gsl_vector* gsl_vector_p(void);
125
126    using VectorBase::gsl_vector_p;
127
128    /**
129       Check if the vector object is a view (sub-vector) to another
130       vector.
131   
132       \return True if the object is a view, false othwerwise.
133   
134     */
135    virtual bool isview(void) const=0; 
136
137    /**
138       \brief This function performs element-wise multiplication, \f$
139       this_i = this_i * other_i \; \forall i \f$.
140
141       \throw GSL_error if dimensions mis-match.
142    */
143    void mul(const VectorBase& other);
144
145    /**
146       \brief Reverse the order of elements in the VectorMutable.
147    */
148    void reverse(void);
149
150    /**
151       \brief Exchange elements \a i and \a j.
152    */
153    void swap(size_t i, size_t j);
154
155    /**
156       \brief Element access operator.
157
158       \return Reference to element \a i.
159
160       \throw If GSL range checks are enabled in the underlying GSL
161       library a GSL_error exception is thrown if either index is out
162       of range.
163    */
164    double& operator()(size_t i);
165
166    // to allow overload from base class
167    using VectorBase::operator();
168
169    /**
170       \brief Addition and assign operator. VectorBase addition, \f$
171       this_i = this_i + other_i \; \forall i \f$.
172
173       \return A const reference to the resulting VectorBase.
174
175       \throw GSL_error if dimensions mis-match.
176    */
177    const VectorMutable& operator+=(const VectorBase&);
178
179    /**
180       \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;
181       \forall i \f$.
182
183       \return A const reference to the resulting VectorBase.
184    */
185    const VectorMutable& operator+=(double d);
186
187    /**
188       \brief Subtract and assign operator. VectorBase subtraction, \f$
189       this_i = this_i - other_i \; \forall i \f$.
190
191       \return A const reference to the resulting VectorBase.
192
193       \throw GSL_error if dimensions mis-match.
194    */
195    const VectorMutable& operator-=(const VectorBase&);
196
197    /**
198       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
199       \; \forall i \f$.
200
201       \return A const reference to the resulting VectorBase.
202    */
203    const VectorMutable& operator-=(double d);
204
205    /**
206       \brief Multiply with scalar and assign operator, \f$ this_i =
207       this_i * d \; \forall i \f$.
208
209       \return A const reference to the resulting VectorBase.
210    */
211    const VectorMutable& operator*=(double d);
212
213  protected:
214    /**
215       pointer to underlying GSL vector. Should always point to same
216       gsl_vector as const_vec_ in base class does. This pointer should
217       by used for mutable operations. Do not use this in const
218       functions; use const_vec_ inherited from BaseVector.
219     */
220    gsl_vector* vec_;
221
222  private:
223    // copy assignment not allowed
224    const VectorMutable& operator=(const VectorMutable&);
225
226  };
227
228  /**
229     Randomly shuffles the elements in VectorMutable \a invec
230  */
231  void shuffle(VectorMutable& invec); 
232
233  /**
234     Sort the elements in the VectorMutable \a vec
235  */
236  void sort(VectorMutable& vec);
237
238}}} // of namespace utility, yat, and theplu
239
240#endif
Note: See TracBrowser for help on using the repository browser.