yat/utility/VectorView.h

Code
Comments
Other
Rev Date Author Line
995 30 Nov 07 peter 1 #ifndef _theplu_yat_utility_vector_view_
995 30 Nov 07 peter 2 #define _theplu_yat_utility_vector_view_
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
2119 12 Dec 09 peter 12   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 13   Copyright (C) 2017, 2021, 2022 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 "VectorMutable.h"
420 02 Dec 05 jari 32
12 19 Jun 03 daniel 33 #include <gsl/gsl_vector.h>
12 19 Jun 03 daniel 34
42 26 Feb 04 jari 35 namespace theplu {
680 11 Oct 06 jari 36 namespace yat {
616 31 Aug 06 jari 37 namespace utility {
420 02 Dec 05 jari 38
4119 07 Nov 21 peter 39   class MatrixMutable;
1120 21 Feb 08 peter 40   class Vector;
420 02 Dec 05 jari 41
686 16 Oct 06 jari 42   /**
2687 27 Feb 12 peter 43      @brief This is the yat interface to gsl_vector_view.
686 16 Oct 06 jari 44
1046 06 Feb 08 peter 45      This class can be used to create a vector view into a Matrix or a
1046 06 Feb 08 peter 46      VectorMutable. Modifying a view will also modify the underlying
1046 06 Feb 08 peter 47      data, i.e., the Matrix or VectorMutable that is viewed into. For
1046 06 Feb 08 peter 48      that reason all constructors are taking non-const references to
2687 27 Feb 12 peter 49      disallow mutable views into a const objects.
767 22 Feb 07 peter 50
1046 06 Feb 08 peter 51      The fact that there is no copy constructor with const argument,
1046 06 Feb 08 peter 52      but only a so-called move constructor (copying a non-const
1046 06 Feb 08 peter 53      reference), implies that temporary objects such as objects
1046 06 Feb 08 peter 54      returned from functions can not be copied directly. Instead the
1679 29 Dec 08 peter 55      copying is done via a proxy class. Also since we can not bind a
1679 29 Dec 08 peter 56      temporary to a non-const reference, a VectorView returned from a
1679 29 Dec 08 peter 57      function cannot be sent directly to a function.
1046 06 Feb 08 peter 58      For example
686 16 Oct 06 jari 59      @code
1046 06 Feb 08 peter 60      Matrix m(10,10);
1046 06 Feb 08 peter 61      sum(m.row_view(0));
686 16 Oct 06 jari 62      @endcode
1679 29 Dec 08 peter 63      but you need to create a dummie object
1046 06 Feb 08 peter 64      @code
1046 06 Feb 08 peter 65      Matrix m(10,10);
1046 06 Feb 08 peter 66      VectorView vv = m.row_view(0);
1046 06 Feb 08 peter 67      sum(vv);
1046 06 Feb 08 peter 68      @endcode
1046 06 Feb 08 peter 69      or since sum is a const function, you can use VectorConstView
1046 06 Feb 08 peter 70      @code
1046 06 Feb 08 peter 71      Matrix m(10,10);
1046 06 Feb 08 peter 72      sum(m.row_const_view(0));
1046 06 Feb 08 peter 73      @endcode
1046 06 Feb 08 peter 74
1046 06 Feb 08 peter 75      Note that VectorView does not own underlying data, and a
1046 06 Feb 08 peter 76      VectorView is not valid if Vector/Matrix owning the data is
1046 06 Feb 08 peter 77      deallocated.
686 16 Oct 06 jari 78   */
1027 02 Feb 08 peter 79   class VectorView : public VectorMutable
42 26 Feb 04 jari 80   {
42 26 Feb 04 jari 81   public:
754 17 Feb 07 jari 82     /**
995 30 Nov 07 peter 83        \brief Default constructor.
1046 06 Feb 08 peter 84
1130 23 Feb 08 peter 85        Creates a view into nothing and behaves like an empty Vector.
754 17 Feb 07 jari 86     */
1015 01 Feb 08 peter 87     VectorView(void);
12 19 Jun 03 daniel 88
754 17 Feb 07 jari 89     /**
754 17 Feb 07 jari 90        \brief The copy constructor.
1046 06 Feb 08 peter 91
1046 06 Feb 08 peter 92        Modifications to created VectorView will also modify \a
1046 06 Feb 08 peter 93        other. Created VectorView is not dependent on \a other, but if
1046 06 Feb 08 peter 94        underlying data (Vector or Matrix) is deallocated VectorView is
1046 06 Feb 08 peter 95        invalid.
2687 27 Feb 12 peter 96     */
1015 01 Feb 08 peter 97     VectorView(VectorView& other);
12 19 Jun 03 daniel 98
754 17 Feb 07 jari 99     /**
1046 06 Feb 08 peter 100        \brief copy another VectorMutable
1046 06 Feb 08 peter 101
1046 06 Feb 08 peter 102        \note If the object viewed by the view goes out of scope or is
1046 06 Feb 08 peter 103        deleted, the view becomes invalid and the result of further use
1046 06 Feb 08 peter 104        is undefined.
1046 06 Feb 08 peter 105     */
1651 15 Dec 08 peter 106     explicit VectorView(VectorMutable& other);
1046 06 Feb 08 peter 107
1046 06 Feb 08 peter 108     /**
995 30 Nov 07 peter 109        \brief VectorView constructor.
754 17 Feb 07 jari 110
1046 06 Feb 08 peter 111        Create a view of VectorMutable \a v, with starting index \a offset,
754 17 Feb 07 jari 112        size \a n, and an optional \a stride.
754 17 Feb 07 jari 113
754 17 Feb 07 jari 114        \note If the object viewed by the view goes out of scope or is
754 17 Feb 07 jari 115        deleted, the view becomes invalid and the result of further use
754 17 Feb 07 jari 116        is undefined.
754 17 Feb 07 jari 117     */
1027 02 Feb 08 peter 118     VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1);
257 04 Mar 05 jari 119
42 26 Feb 04 jari 120     ///
420 02 Dec 05 jari 121     /// Matrix row/column view constructor.
420 02 Dec 05 jari 122     ///
1015 01 Feb 08 peter 123     /// Create a row/column VectorView view of matrix \a m, pointing at
420 02 Dec 05 jari 124     /// row/column \a i. The parameter \a row is used to set whether
420 02 Dec 05 jari 125     /// the view should be a row or column view. If \a row is set to
420 02 Dec 05 jari 126     /// true, the view will be a row view (default behaviour), and,
420 02 Dec 05 jari 127     /// naturally, a column view otherwise.
420 02 Dec 05 jari 128     ///
1046 06 Feb 08 peter 129     /// A VectorView view can be used as any VectorMutable with the
1046 06 Feb 08 peter 130     /// difference that changes made to the view will also change the
2687 27 Feb 12 peter 131     /// object that is viewed.
420 02 Dec 05 jari 132     ///
420 02 Dec 05 jari 133     /// @note If the object viewed by the view goes out of scope or is
420 02 Dec 05 jari 134     /// deleted, the view becomes invalid and the result of further
420 02 Dec 05 jari 135     /// use is undefined.
420 02 Dec 05 jari 136     ///
4119 07 Nov 21 peter 137      VectorView(MatrixMutable& m, size_t i, bool row=true);
420 02 Dec 05 jari 138
4141 01 Feb 22 peter 139     /**
4141 01 Feb 22 peter 140        Create a view into \c v
4141 01 Feb 22 peter 141
4141 01 Feb 22 peter 142        \since New in yat 0.20
4141 01 Feb 22 peter 143      */
4141 01 Feb 22 peter 144     explicit VectorView(gsl_vector* v);
4141 01 Feb 22 peter 145
4141 01 Feb 22 peter 146     /**
4141 01 Feb 22 peter 147        Create a view with size \c size and stride \c stride such that
4141 01 Feb 22 peter 148        view(0) = v[0] and view(1) = v[stride].
4141 01 Feb 22 peter 149
4141 01 Feb 22 peter 150        \since New in yat 0.20
4141 01 Feb 22 peter 151      */
4141 01 Feb 22 peter 152     VectorView(double* v, size_t size, size_t stride=1);
4141 01 Feb 22 peter 153
475 22 Dec 05 peter 154     ///
42 26 Feb 04 jari 155     /// The destructor.
42 26 Feb 04 jari 156     ///
1015 01 Feb 08 peter 157     ~VectorView(void);
12 19 Jun 03 daniel 158
754 17 Feb 07 jari 159     /**
1008 01 Feb 08 peter 160        \return true
1008 01 Feb 08 peter 161     */
2687 27 Feb 12 peter 162     bool isview(void) const;
1008 01 Feb 08 peter 163
1008 01 Feb 08 peter 164     /**
754 17 Feb 07 jari 165        \brief The assignment operator.
754 17 Feb 07 jari 166
754 17 Feb 07 jari 167        \return A const reference to the resulting vector.
754 17 Feb 07 jari 168
1046 06 Feb 08 peter 169        \note modifies underlying data.
1046 06 Feb 08 peter 170
1130 23 Feb 08 peter 171        \throw GSL_error if dimensions mis-match or if assignment fails
754 17 Feb 07 jari 172     */
1026 01 Feb 08 peter 173     const VectorView& operator=(const VectorView&);
341 07 Jun 05 jari 174
1046 06 Feb 08 peter 175     /**
1046 06 Feb 08 peter 176        \brief The assignment operator.
1046 06 Feb 08 peter 177
1046 06 Feb 08 peter 178        \return A const reference to the resulting vector.
1046 06 Feb 08 peter 179
1046 06 Feb 08 peter 180        \note modifies underlying data.
1046 06 Feb 08 peter 181
1130 23 Feb 08 peter 182        \throw GSL_error if dimensions mis-match or if assignment fails
1046 06 Feb 08 peter 183     */
1046 06 Feb 08 peter 184     const VectorView& operator=(const VectorBase&);
1046 06 Feb 08 peter 185
3605 27 Jan 17 peter 186     /**
3605 27 Jan 17 peter 187        \brief assignment from vector expression
3605 27 Jan 17 peter 188      */
3605 27 Jan 17 peter 189     template<class T>
3605 27 Jan 17 peter 190     VectorView& operator=(const VectorExpression<T>& rhs)
3605 27 Jan 17 peter 191     {
3605 27 Jan 17 peter 192       assign(rhs.gsl_vector_p());
3605 27 Jan 17 peter 193       return *this;
3605 27 Jan 17 peter 194     }
3605 27 Jan 17 peter 195
420 02 Dec 05 jari 196   private:
1026 01 Feb 08 peter 197     const VectorView& assign(const VectorBase& other);
3605 27 Jan 17 peter 198     const VectorView& assign(const gsl_vector* rhs);
1118 21 Feb 08 peter 199     void copy(gsl_vector*);
1008 01 Feb 08 peter 200     void delete_allocated_memory(void);
12 19 Jun 03 daniel 201
227 01 Feb 05 jari 202     gsl_vector_view* view_;
1651 15 Dec 08 peter 203
1651 15 Dec 08 peter 204     /**
1651 15 Dec 08 peter 205        Proxy class used to allow copy and assignment of VectorView. By
1651 15 Dec 08 peter 206        design vectors and matrices are passed as non-const references
1651 15 Dec 08 peter 207        in all constructors of VectorView. Because the standard does
1651 15 Dec 08 peter 208        not allow temporary objects to be bound to non-const
1651 15 Dec 08 peter 209        references, it is not possible to directly construct a
1651 15 Dec 08 peter 210        VectorView from a temporary VectorView returned from a
1651 15 Dec 08 peter 211        function. Instead this proxy class is created from the
1651 15 Dec 08 peter 212        temporary object and then a VectorView can be created from this
1651 15 Dec 08 peter 213        proxy.
1651 15 Dec 08 peter 214      */
2687 27 Feb 12 peter 215     struct proxy
2687 27 Feb 12 peter 216     {
1651 15 Dec 08 peter 217       /// pointer to GSL vector keeping everything we need to create a
1651 15 Dec 08 peter 218       /// new VectorMutable from a proxy.
1651 15 Dec 08 peter 219       gsl_vector* vec_;
1651 15 Dec 08 peter 220     };
1651 15 Dec 08 peter 221
1651 15 Dec 08 peter 222   public:
1651 15 Dec 08 peter 223     /**
1651 15 Dec 08 peter 224        \brief create VectorView from proxy class
1651 15 Dec 08 peter 225      */
1651 15 Dec 08 peter 226     VectorView(proxy p);
1651 15 Dec 08 peter 227
1651 15 Dec 08 peter 228     /**
2687 27 Feb 12 peter 229        conversion operator to private proxy class.
1651 15 Dec 08 peter 230      */
1651 15 Dec 08 peter 231     operator proxy();
420 02 Dec 05 jari 232   };
12 19 Jun 03 daniel 233
686 16 Oct 06 jari 234 }}} // of namespace utility, yat, and theplu
12 19 Jun 03 daniel 235
420 02 Dec 05 jari 236 #endif