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

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

fixing Doxygen parsing

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