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

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

some dox - refs #256

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.h 1046 2008-02-06 23:56:23Z 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    double& operator[](size_t i);
163    // to allow overload from base class
164    using VectorBase::operator();
165    using VectorBase::operator[];
166
167    /**
168       \brief Addition and assign operator. VectorBase addition, \f$
169       this_i = this_i + other_i \; \forall i \f$.
170
171       \return A const reference to the resulting VectorBase.
172
173       \throw GSL_error if dimensions mis-match.
174    */
175    const VectorMutable& operator+=(const VectorBase&);
176
177    /**
178       \brief Add a constant to a VectorBase, \f$ this_i = this_i + d \;
179       \forall i \f$.
180
181       \return A const reference to the resulting VectorBase.
182    */
183    const VectorMutable& operator+=(double d);
184
185    /**
186       \brief Subtract and assign operator. VectorBase subtraction, \f$
187       this_i = this_i - other_i \; \forall i \f$.
188
189       \return A const reference to the resulting VectorBase.
190
191       \throw GSL_error if dimensions mis-match.
192    */
193    const VectorMutable& operator-=(const VectorBase&);
194
195    /**
196       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
197       \; \forall i \f$.
198
199       \return A const reference to the resulting VectorBase.
200    */
201    const VectorMutable& operator-=(double d);
202
203    /**
204       \brief Multiply with scalar and assign operator, \f$ this_i =
205       this_i * d \; \forall i \f$.
206
207       \return A const reference to the resulting VectorBase.
208    */
209    const VectorMutable& operator*=(double d);
210
211  protected:
212    gsl_vector* vec_;
213
214    /**
215       Proxy class used to allow copy and assignment of VectorView. By
216       design vectors and matrices are passed as non-const references
217       in all constructors of VectorView. Because the standard does
218       not allow temporary objects to be bound to non-const
219       references, it is not possible to directly construct a
220       VectorView from a temporary VectorView returned from a
221       function. Instead this proxy class is created from the
222       temporary object and then a VectorView can be created from thsi
223       proxy.
224
225       \see VectorView
226     */
227    struct proxy
228    { 
229      gsl_vector* vec_;
230    };
231
232  private:
233    // copy assignment no allowed
234    const VectorMutable& operator=(const VectorMutable&);
235  public:
236    /**
237       conversion operator to protected proxy class.
238     */
239    operator proxy();
240
241  };
242
243  /**
244     Randomly shuffles the elements in VectorMutable \a invec
245  */
246  void shuffle(VectorMutable& invec); 
247
248  /**
249     Sort the elements in the VectorMutable.
250  */
251  void sort(VectorMutable&);
252
253}}} // of namespace utility, yat, and theplu
254
255#endif
Note: See TracBrowser for help on using the repository browser.