source: trunk/yat/utility/VectorMutable.h

Last change on this file was 3661, checked in by Peter, 4 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svndigest:ignore set to 1026
File size: 6.6 KB
Line 
1#ifndef _theplu_yat_utility_vector_mutable_
2#define _theplu_yat_utility_vector_mutable_
3
4// $Id: VectorMutable.h 3661 2017-07-14 01:10:35Z 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, 2009 Jari Häkkinen, Peter Johansson
13  Copyright (C) 2012, 2016, 2017 Peter Johansson
14
15  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
16
17  The yat library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU General Public License as
19  published by the Free Software Foundation; either version 3 of the
20  License, or (at your option) any later version.
21
22  The yat library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  General Public License for more details.
26
27  You should have received a copy of the GNU General Public License
28  along with yat. If not, see <http://www.gnu.org/licenses/>.
29*/
30
31#include "VectorBase.h"
32#include "VectorExpression.h"
33#include "StrideIterator.h"
34
35#include <gsl/gsl_vector.h>
36
37#include <cstddef> // size_t
38
39namespace theplu {
40namespace yat {
41namespace utility {
42
43  class matrix;
44  class Vector;
45
46  /**
47     @brief This is the mutable interface to GSL vector.
48
49     This class contains the mutable interface to vector classes.
50
51     The non-mutable interface is inherited from VectorBase. When
52     dealing with const vectors, it is preferable to use the
53     VectorBase signature because this allows usage of VectorConstView
54     too.
55  */
56  class VectorMutable : public VectorBase
57  {
58  public:
59    /**
60       reference type is double&
61
62       \since New in yat 0.5
63     */
64    typedef double& reference;
65
66    /**
67       \brief mutable iterator
68    */
69    typedef StrideIterator<double*> iterator;
70
71    /**
72       \brief default constructor
73    */
74    VectorMutable(void);
75
76    /**
77       \brief Constructor.
78    */
79    VectorMutable(gsl_vector*);
80
81    /**
82       \brief Constructor.
83    */
84    VectorMutable(const gsl_vector*);
85
86    /**
87       The destructor.
88    */
89    virtual ~VectorMutable(void);
90
91    /**
92       Set all elements to \a value.
93    */
94    void all(double value);
95
96    /**
97       \return mutable iterator to start of VectorMutable
98     */
99    iterator begin(void);
100
101    // to allow overload from base class
102    using VectorBase::begin;
103
104    /**
105       \brief This function performs element-wise division, \f$
106       this_i = this_i / other_i \; \forall i \f$.
107
108       \throw GSL_error if dimensions mis-match.
109    */
110    void div(const VectorBase& other);
111
112    /**
113       \return mutable iterator to end of VectorMutable
114     */
115    iterator end(void);
116
117    // to allow overload from base class
118    using VectorBase::end;
119
120    /**
121       @return A pointer to the internal GSL vector,
122    */
123    gsl_vector* gsl_vector_p(void);
124
125    using VectorBase::gsl_vector_p;
126
127    /**
128       Check if the vector object is a view (sub-vector) to another
129       vector.
130
131       \return True if the object is a view, false othwerwise.
132
133     */
134    virtual bool isview(void) const=0;
135
136    /**
137       \brief This function performs element-wise multiplication, \f$
138       this_i = this_i * other_i \; \forall i \f$.
139
140       \throw GSL_error if dimensions mis-match.
141    */
142    void mul(const VectorBase& other);
143
144    /**
145       \brief Reverse the order of elements in the VectorMutable.
146    */
147    void reverse(void);
148
149    /**
150       \brief Exchange elements \a i and \a j.
151    */
152    void swap(size_t i, size_t j);
153
154    /**
155       \brief Element access operator.
156
157       \return Reference to element \a i.
158
159       \throw If GSL range checks are enabled in the underlying GSL
160       library a GSL_error exception is thrown if either index is out
161       of range.
162    */
163    double& operator()(size_t i);
164
165    // to allow overload from base class
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 VectorMutable.
173
174       \throw GSL_error if dimensions mis-match.
175    */
176    const VectorMutable& operator+=(const VectorBase&);
177
178    /**
179       \brief Addition and assign operator.
180
181       \return A reference to the resulting VectorMutable.
182    */
183    template<class T>
184    VectorMutable& operator+=(const VectorExpression<T>& rhs)
185    {
186      *this = *this + rhs;
187      return *this;
188    }
189
190    /**
191       \brief Add 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 Subtract and assign operator. VectorBase subtraction, \f$
200       this_i = this_i - other_i \; \forall i \f$.
201
202       \return A const reference to the resulting VectorBase.
203
204       \throw GSL_error if dimensions mis-match.
205    */
206    const VectorMutable& operator-=(const VectorBase&);
207
208    /**
209       \brief Subtraction and assign operator.
210
211       \return A reference to the resulting VectorMutable.
212    */
213    template<class T>
214    VectorMutable& operator-=(const VectorExpression<T>& rhs)
215    {
216      *this = *this - rhs;
217      return *this;
218    }
219
220    /**
221       \brief Subtract a constant to a VectorBase, \f$ this_i = this_i - d
222       \; \forall i \f$.
223
224       \return A const reference to the resulting VectorBase.
225    */
226    const VectorMutable& operator-=(double d);
227
228    /**
229       \brief Multiply with scalar and assign operator, \f$ this_i =
230       this_i * d \; \forall i \f$.
231
232       \return A const reference to the resulting VectorBase.
233    */
234    const VectorMutable& operator*=(double d);
235
236    /**
237       \brief assignment
238
239       \since New in yat 0.14
240     */
241    const VectorMutable& operator=(const VectorMutable&);
242
243    /**
244       \brief assignment
245
246       \since New in yat 0.14
247     */
248    const VectorMutable& operator=(const VectorBase&);
249
250    /**
251       \brief assignment from vector expression
252
253       \since New in yat 0.15
254     */
255    template<class T>
256    VectorMutable& operator=(const VectorExpression<T>& rhs)
257    {
258      assign(rhs.gsl_vector_p());
259      return *this;
260    }
261
262  protected:
263    /**
264       pointer to underlying GSL vector. Should always point to same
265       gsl_vector as const_vec_ in base class does. This pointer should
266       by used for mutable operations. Do not use this in const
267       functions; use const_vec_ inherited from BaseVector.
268     */
269    gsl_vector* vec_;
270
271  private:
272    virtual const VectorMutable& assign(const VectorBase& rhs)=0;
273    virtual const VectorMutable& assign(const gsl_vector* rhs)=0;
274  };
275
276  /**
277     Randomly shuffles the elements in VectorMutable \a invec
278  */
279  void shuffle(VectorMutable& invec);
280
281  /**
282     Sort the elements in the VectorMutable \a vec
283
284     NaNs are treated as infinity
285  */
286  void sort(VectorMutable& vec);
287
288}}} // of namespace utility, yat, and theplu
289
290#endif
Note: See TracBrowser for help on using the repository browser.