ArrayWrapper.h 6.62 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_ARRAYWRAPPER_H
#define EIGEN_ARRAYWRAPPER_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen { 

LM's avatar
LM committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/** \class ArrayWrapper
  * \ingroup Core_Module
  *
  * \brief Expression of a mathematical vector or matrix as an array object
  *
  * This class is the return type of MatrixBase::array(), and most of the time
  * this is the only way it is use.
  *
  * \sa MatrixBase::array(), class MatrixWrapper
  */

namespace internal {
template<typename ExpressionType>
struct traits<ArrayWrapper<ExpressionType> >
  : public traits<typename remove_all<typename ExpressionType::Nested>::type >
{
  typedef ArrayXpr XprKind;
32 33 34
  // Let's remove NestByRefBit
  enum {
    Flags0 = traits<typename remove_all<typename ExpressionType::Nested>::type >::Flags,
35 36
    LvalueBitFlag = is_lvalue<ExpressionType>::value ? LvalueBit : 0,
    Flags = (Flags0 & ~(NestByRefBit | LvalueBit)) | LvalueBitFlag
37
  };
LM's avatar
LM committed
38 39 40 41 42 43 44 45 46 47
};
}

template<typename ExpressionType>
class ArrayWrapper : public ArrayBase<ArrayWrapper<ExpressionType> >
{
  public:
    typedef ArrayBase<ArrayWrapper> Base;
    EIGEN_DENSE_PUBLIC_INTERFACE(ArrayWrapper)
    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ArrayWrapper)
48
    typedef typename internal::remove_all<ExpressionType>::type NestedExpression;
LM's avatar
LM committed
49 50 51 52 53 54 55

    typedef typename internal::conditional<
                       internal::is_lvalue<ExpressionType>::value,
                       Scalar,
                       const Scalar
                     >::type ScalarWithConstIfNotLvalue;

56
    typedef typename internal::ref_selector<ExpressionType>::non_const_type NestedExpressionType;
LM's avatar
LM committed
57

58
    using Base::coeffRef;
LM's avatar
LM committed
59

60 61 62 63
    EIGEN_DEVICE_FUNC
    explicit EIGEN_STRONG_INLINE ArrayWrapper(ExpressionType& matrix) : m_expression(matrix) {}

    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
64
    inline Index rows() const { return m_expression.rows(); }
65
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
66
    inline Index cols() const { return m_expression.cols(); }
67
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
68
    inline Index outerStride() const { return m_expression.outerStride(); }
69
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
70 71
    inline Index innerStride() const { return m_expression.innerStride(); }

72 73 74
    EIGEN_DEVICE_FUNC
    inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
75 76
    inline const Scalar* data() const { return m_expression.data(); }

77
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
78
    inline const Scalar& coeffRef(Index rowId, Index colId) const
LM's avatar
LM committed
79
    {
80
      return m_expression.coeffRef(rowId, colId);
LM's avatar
LM committed
81 82
    }

83
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
84 85
    inline const Scalar& coeffRef(Index index) const
    {
86
      return m_expression.coeffRef(index);
LM's avatar
LM committed
87 88 89
    }

    template<typename Dest>
90
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
91 92
    inline void evalTo(Dest& dst) const { dst = m_expression; }

Don Gagne's avatar
Don Gagne committed
93
    const typename internal::remove_all<NestedExpressionType>::type& 
94
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
95 96 97 98 99 100 101
    nestedExpression() const 
    {
      return m_expression;
    }

    /** Forwards the resizing request to the nested expression
      * \sa DenseBase::resize(Index)  */
102 103
    EIGEN_DEVICE_FUNC
    void resize(Index newSize) { m_expression.resize(newSize); }
Don Gagne's avatar
Don Gagne committed
104 105
    /** Forwards the resizing request to the nested expression
      * \sa DenseBase::resize(Index,Index)*/
106 107
    EIGEN_DEVICE_FUNC
    void resize(Index rows, Index cols) { m_expression.resize(rows,cols); }
Don Gagne's avatar
Don Gagne committed
108

LM's avatar
LM committed
109
  protected:
Don Gagne's avatar
Don Gagne committed
110
    NestedExpressionType m_expression;
LM's avatar
LM committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
};

/** \class MatrixWrapper
  * \ingroup Core_Module
  *
  * \brief Expression of an array as a mathematical vector or matrix
  *
  * This class is the return type of ArrayBase::matrix(), and most of the time
  * this is the only way it is use.
  *
  * \sa MatrixBase::matrix(), class ArrayWrapper
  */

namespace internal {
template<typename ExpressionType>
struct traits<MatrixWrapper<ExpressionType> >
 : public traits<typename remove_all<typename ExpressionType::Nested>::type >
{
  typedef MatrixXpr XprKind;
130 131 132
  // Let's remove NestByRefBit
  enum {
    Flags0 = traits<typename remove_all<typename ExpressionType::Nested>::type >::Flags,
133 134
    LvalueBitFlag = is_lvalue<ExpressionType>::value ? LvalueBit : 0,
    Flags = (Flags0 & ~(NestByRefBit | LvalueBit)) | LvalueBitFlag
135
  };
LM's avatar
LM committed
136 137 138 139 140 141 142 143 144 145
};
}

template<typename ExpressionType>
class MatrixWrapper : public MatrixBase<MatrixWrapper<ExpressionType> >
{
  public:
    typedef MatrixBase<MatrixWrapper<ExpressionType> > Base;
    EIGEN_DENSE_PUBLIC_INTERFACE(MatrixWrapper)
    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixWrapper)
146
    typedef typename internal::remove_all<ExpressionType>::type NestedExpression;
LM's avatar
LM committed
147 148 149 150 151 152 153

    typedef typename internal::conditional<
                       internal::is_lvalue<ExpressionType>::value,
                       Scalar,
                       const Scalar
                     >::type ScalarWithConstIfNotLvalue;

154
    typedef typename internal::ref_selector<ExpressionType>::non_const_type NestedExpressionType;
LM's avatar
LM committed
155

156
    using Base::coeffRef;
LM's avatar
LM committed
157

158 159 160 161
    EIGEN_DEVICE_FUNC
    explicit inline MatrixWrapper(ExpressionType& matrix) : m_expression(matrix) {}

    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
162
    inline Index rows() const { return m_expression.rows(); }
163
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
164
    inline Index cols() const { return m_expression.cols(); }
165
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
166
    inline Index outerStride() const { return m_expression.outerStride(); }
167
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
168 169
    inline Index innerStride() const { return m_expression.innerStride(); }

170 171 172
    EIGEN_DEVICE_FUNC
    inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
173 174
    inline const Scalar* data() const { return m_expression.data(); }

175
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
176
    inline const Scalar& coeffRef(Index rowId, Index colId) const
LM's avatar
LM committed
177
    {
Don Gagne's avatar
Don Gagne committed
178
      return m_expression.derived().coeffRef(rowId, colId);
LM's avatar
LM committed
179 180
    }

181
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
182 183
    inline const Scalar& coeffRef(Index index) const
    {
184
      return m_expression.coeffRef(index);
Don Gagne's avatar
Don Gagne committed
185 186
    }

187
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
188 189
    const typename internal::remove_all<NestedExpressionType>::type& 
    nestedExpression() const 
LM's avatar
LM committed
190
    {
Don Gagne's avatar
Don Gagne committed
191
      return m_expression;
LM's avatar
LM committed
192 193
    }

Don Gagne's avatar
Don Gagne committed
194 195
    /** Forwards the resizing request to the nested expression
      * \sa DenseBase::resize(Index)  */
196 197
    EIGEN_DEVICE_FUNC
    void resize(Index newSize) { m_expression.resize(newSize); }
Don Gagne's avatar
Don Gagne committed
198 199
    /** Forwards the resizing request to the nested expression
      * \sa DenseBase::resize(Index,Index)*/
200 201
    EIGEN_DEVICE_FUNC
    void resize(Index rows, Index cols) { m_expression.resize(rows,cols); }
Don Gagne's avatar
Don Gagne committed
202

LM's avatar
LM committed
203
  protected:
Don Gagne's avatar
Don Gagne committed
204
    NestedExpressionType m_expression;
LM's avatar
LM committed
205 206
};

Don Gagne's avatar
Don Gagne committed
207 208
} // end namespace Eigen

LM's avatar
LM committed
209
#endif // EIGEN_ARRAYWRAPPER_H