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

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 13 years ago

Addresses #436.

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