ArrayWrapper.h revision 810535bb0c575a003b32076e5352ab8fd3f23a1c
1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// This file is part of Eigen, a lightweight C++ template library
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// for linear algebra.
38cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com//
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com//
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// This Source Code Form is subject to the terms of the Mozilla
78cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com// Public License v. 2.0. If a copy of the MPL was not distributed
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
98cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
10cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#ifndef EIGEN_ARRAYWRAPPER_H
118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#define EIGEN_ARRAYWRAPPER_H
128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace Eigen {
148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com/** \class ArrayWrapper
168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \ingroup Core_Module
178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \brief Expression of a mathematical vector or matrix as an array object
198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * This class is the return type of MatrixBase::array(), and most of the time
218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * this is the only way it is use.
228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \sa MatrixBase::array(), class MatrixWrapper
248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  */
258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace internal {
278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comtemplate<typename ExpressionType>
288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comstruct traits<ArrayWrapper<ExpressionType> >
298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  : public traits<typename remove_all<typename ExpressionType::Nested>::type >
308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com{
318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  typedef ArrayXpr XprKind;
328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comtemplate<typename ExpressionType>
368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comclass ArrayWrapper : public ArrayBase<ArrayWrapper<ExpressionType> >
378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com{
388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  public:
398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    typedef ArrayBase<ArrayWrapper> Base;
408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    EIGEN_DENSE_PUBLIC_INTERFACE(ArrayWrapper)
418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ArrayWrapper)
428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    typedef typename internal::conditional<
448cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                       internal::is_lvalue<ExpressionType>::value,
458cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                       Scalar,
468cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                       const Scalar
478cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com                     >::type ScalarWithConstIfNotLvalue;
488cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
498cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    typedef typename internal::nested<ExpressionType>::type NestedExpressionType;
508cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
518cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline ArrayWrapper(ExpressionType& matrix) : m_expression(matrix) {}
528cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
538cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Index rows() const { return m_expression.rows(); }
548cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Index cols() const { return m_expression.cols(); }
558cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Index outerStride() const { return m_expression.outerStride(); }
568cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Index innerStride() const { return m_expression.innerStride(); }
578cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
588cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline ScalarWithConstIfNotLvalue* data() { return m_expression.data(); }
598cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline const Scalar* data() const { return m_expression.data(); }
608cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
618cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline CoeffReturnType coeff(Index row, Index col) const
628cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
638cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.coeff(row, col);
648cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
658cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
668cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Scalar& coeffRef(Index row, Index col)
678cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
688cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.const_cast_derived().coeffRef(row, col);
698cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
708cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
718cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline const Scalar& coeffRef(Index row, Index col) const
728cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
738cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.const_cast_derived().coeffRef(row, col);
748cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
758cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
768cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline CoeffReturnType coeff(Index index) const
778cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
788cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.coeff(index);
798cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
808cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
818cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline Scalar& coeffRef(Index index)
828cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
838cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.const_cast_derived().coeffRef(index);
848cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
858cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
868cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline const Scalar& coeffRef(Index index) const
878cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
888cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.const_cast_derived().coeffRef(index);
898cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
908cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
918cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    template<int LoadMode>
928cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline const PacketScalar packet(Index row, Index col) const
938cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
948cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.template packet<LoadMode>(row, col);
958cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
968cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
978cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    template<int LoadMode>
988cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline void writePacket(Index row, Index col, const PacketScalar& x)
998cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
1008cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      m_expression.const_cast_derived().template writePacket<LoadMode>(row, col, x);
1018cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
1028cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1038cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    template<int LoadMode>
1048cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline const PacketScalar packet(Index index) const
1058cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
1068cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression.template packet<LoadMode>(index);
1078cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
1088cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1098cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    template<int LoadMode>
1108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline void writePacket(Index index, const PacketScalar& x)
1118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
1128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      m_expression.const_cast_derived().template writePacket<LoadMode>(index, x);
1138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
1148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    template<typename Dest>
1168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    inline void evalTo(Dest& dst) const { dst = m_expression; }
1178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    const typename internal::remove_all<NestedExpressionType>::type&
1198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    nestedExpression() const
1208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    {
1218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      return m_expression;
1228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    }
1238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  protected:
1258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com    NestedExpressionType m_expression;
1268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com};
1278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com/** \class MatrixWrapper
1298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \ingroup Core_Module
1308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
1318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \brief Expression of an array as a mathematical vector or matrix
1328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
1338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * This class is the return type of ArrayBase::matrix(), and most of the time
1348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * this is the only way it is use.
1358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  *
1368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  * \sa MatrixBase::matrix(), class ArrayWrapper
1378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com  */
1388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
1398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comnamespace internal {
1408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comtemplate<typename ExpressionType>
1418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comstruct traits<MatrixWrapper<ExpressionType> >
142 : public traits<typename remove_all<typename ExpressionType::Nested>::type >
143{
144  typedef MatrixXpr XprKind;
145};
146}
147
148template<typename ExpressionType>
149class MatrixWrapper : public MatrixBase<MatrixWrapper<ExpressionType> >
150{
151  public:
152    typedef MatrixBase<MatrixWrapper<ExpressionType> > Base;
153    EIGEN_DENSE_PUBLIC_INTERFACE(MatrixWrapper)
154    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(MatrixWrapper)
155
156    typedef typename internal::conditional<
157                       internal::is_lvalue<ExpressionType>::value,
158                       Scalar,
159                       const Scalar
160                     >::type ScalarWithConstIfNotLvalue;
161
162    typedef typename internal::nested<ExpressionType>::type NestedExpressionType;
163
164    inline MatrixWrapper(ExpressionType& matrix) : m_expression(matrix) {}
165
166    inline Index rows() const { return m_expression.rows(); }
167    inline Index cols() const { return m_expression.cols(); }
168    inline Index outerStride() const { return m_expression.outerStride(); }
169    inline Index innerStride() const { return m_expression.innerStride(); }
170
171    inline ScalarWithConstIfNotLvalue* data() { return m_expression.const_cast_derived().data(); }
172    inline const Scalar* data() const { return m_expression.data(); }
173
174    inline CoeffReturnType coeff(Index row, Index col) const
175    {
176      return m_expression.coeff(row, col);
177    }
178
179    inline Scalar& coeffRef(Index row, Index col)
180    {
181      return m_expression.const_cast_derived().coeffRef(row, col);
182    }
183
184    inline const Scalar& coeffRef(Index row, Index col) const
185    {
186      return m_expression.derived().coeffRef(row, col);
187    }
188
189    inline CoeffReturnType coeff(Index index) const
190    {
191      return m_expression.coeff(index);
192    }
193
194    inline Scalar& coeffRef(Index index)
195    {
196      return m_expression.const_cast_derived().coeffRef(index);
197    }
198
199    inline const Scalar& coeffRef(Index index) const
200    {
201      return m_expression.const_cast_derived().coeffRef(index);
202    }
203
204    template<int LoadMode>
205    inline const PacketScalar packet(Index row, Index col) const
206    {
207      return m_expression.template packet<LoadMode>(row, col);
208    }
209
210    template<int LoadMode>
211    inline void writePacket(Index row, Index col, const PacketScalar& x)
212    {
213      m_expression.const_cast_derived().template writePacket<LoadMode>(row, col, x);
214    }
215
216    template<int LoadMode>
217    inline const PacketScalar packet(Index index) const
218    {
219      return m_expression.template packet<LoadMode>(index);
220    }
221
222    template<int LoadMode>
223    inline void writePacket(Index index, const PacketScalar& x)
224    {
225      m_expression.const_cast_derived().template writePacket<LoadMode>(index, x);
226    }
227
228    const typename internal::remove_all<NestedExpressionType>::type&
229    nestedExpression() const
230    {
231      return m_expression;
232    }
233
234  protected:
235    NestedExpressionType m_expression;
236};
237
238} // end namespace Eigen
239
240#endif // EIGEN_ARRAYWRAPPER_H
241