yat/utility/VectorMutable.h

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