Cleanup intitial reshape implementation:

- reshape -> reshaped
 - make it compatible with evaluators.
This commit is contained in:
Gael Guennebaud 2017-01-29 14:57:45 +01:00
parent 0e89baa5d8
commit 9036cda364
7 changed files with 310 additions and 254 deletions

View File

@ -471,9 +471,9 @@ using std::ptrdiff_t;
#include "src/Core/Map.h" #include "src/Core/Map.h"
#include "src/Core/Ref.h" #include "src/Core/Ref.h"
#include "src/Core/Block.h" #include "src/Core/Block.h"
#include "src/Core/Reshape.h"
#include "src/Core/VectorBlock.h" #include "src/Core/VectorBlock.h"
#include "src/Core/IndexedView.h" #include "src/Core/IndexedView.h"
#include "src/Core/Reshape.h"
#include "src/Core/Transpose.h" #include "src/Core/Transpose.h"
#include "src/Core/DiagonalMatrix.h" #include "src/Core/DiagonalMatrix.h"
#include "src/Core/Diagonal.h" #include "src/Core/Diagonal.h"

View File

@ -557,6 +557,20 @@ template<typename Derived> class DenseBase
} }
EIGEN_DEVICE_FUNC void reverseInPlace(); EIGEN_DEVICE_FUNC void reverseInPlace();
EIGEN_DEVICE_FUNC inline
Reshaped<Derived> reshaped(Index nRows, Index nCols);
EIGEN_DEVICE_FUNC inline
const Reshaped<const Derived> reshaped(Index nRows, Index nCols) const;
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline Reshaped<Derived, ReshapeRows, ReshapeCols> reshaped();
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline const Reshaped<const Derived, ReshapeRows, ReshapeCols> reshaped() const;
#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase
#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
#define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND) #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)

View File

@ -1,30 +1,30 @@
// This file is part of Eigen, a lightweight C++ template library // This file is part of Eigen, a lightweight C++ template library
// for linear algebra. // for linear algebra.
// //
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> // Copyright (C) 2008-2017 Gael Guennebaud <gael.guennebaud@inria.fr>
// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
// Copyright (C) 2014 yoco <peter.xiau@gmail.com> // Copyright (C) 2014 yoco <peter.xiau@gmail.com>
// //
// This Source Code Form is subject to the terms of the Mozilla // 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 // 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/. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef EIGEN_RESHAPE_H #ifndef EIGEN_RESHAPED_H
#define EIGEN_RESHAPE_H #define EIGEN_RESHAPED_H
namespace Eigen { namespace Eigen {
/** \class Reshape /** \class Reshapedd
* \ingroup Core_Module * \ingroup Core_Module
* *
* \brief Expression of a fixed-size or dynamic-size reshape * \brief Expression of a fixed-size or dynamic-size reshape
* *
* \param XprType the type of the expression in which we are taking a reshape * \tparam XprType the type of the expression in which we are taking a reshape
* \param ReshapeRows the number of rows of the reshape we are taking at compile time (optional) * \tparam Rows the number of rows of the reshape we are taking at compile time (optional)
* \param ReshapeCols the number of columns of the reshape we are taking at compile time (optional) * \tparam Cols the number of columns of the reshape we are taking at compile time (optional)
* \tparam Order
* *
* This class represents an expression of either a fixed-size or dynamic-size reshape. It is the return * This class represents an expression of either a fixed-size or dynamic-size reshape. It is the return
* type of DenseBase::reshape(Index,Index) and DenseBase::reshape<int,int>() and * type of DenseBase::reshaped(Index,Index) and DenseBase::reshape<int,int>() and
* most of the time this is the only way it is used. * most of the time this is the only way it is used.
* *
* However, if you want to directly maniputate reshape expressions, * However, if you want to directly maniputate reshape expressions,
@ -32,23 +32,23 @@ namespace Eigen {
* will need to use this class. * will need to use this class.
* *
* Here is an example illustrating the dynamic case: * Here is an example illustrating the dynamic case:
* \include class_Reshape.cpp * \include class_Reshaped.cpp
* Output: \verbinclude class_Reshape.out * Output: \verbinclude class_Reshaped.out
* *
* \note Even though this expression has dynamic size, in the case where \a XprType * \note Even though this expression has dynamic size, in the case where \a XprType
* has fixed size, this expression inherits a fixed maximal size which means that evaluating * has fixed size, this expression inherits a fixed maximal size which means that evaluating
* it does not cause a dynamic memory allocation. * it does not cause a dynamic memory allocation.
* *
* Here is an example illustrating the fixed-size case: * Here is an example illustrating the fixed-size case:
* \include class_FixedReshape.cpp * \include class_FixedReshaped.cpp
* Output: \verbinclude class_FixedReshape.out * Output: \verbinclude class_FixedReshaped.out
* *
* \sa DenseBase::reshape(Index,Index), DenseBase::reshape(), class VectorReshape * \sa DenseBase::reshaped(Index,Index), DenseBase::reshaped(), class VectorReshaped
*/ */
namespace internal { namespace internal {
template<typename XprType, int ReshapeRows, int ReshapeCols> template<typename XprType, int Rows, int Cols, int Order>
struct traits<Reshape<XprType, ReshapeRows, ReshapeCols> > : traits<XprType> struct traits<Reshaped<XprType, Rows, Cols, Order> > : traits<XprType>
{ {
typedef typename traits<XprType>::Scalar Scalar; typedef typename traits<XprType>::Scalar Scalar;
typedef typename traits<XprType>::StorageKind StorageKind; typedef typename traits<XprType>::StorageKind StorageKind;
@ -56,10 +56,10 @@ struct traits<Reshape<XprType, ReshapeRows, ReshapeCols> > : traits<XprType>
enum{ enum{
MatrixRows = traits<XprType>::RowsAtCompileTime, MatrixRows = traits<XprType>::RowsAtCompileTime,
MatrixCols = traits<XprType>::ColsAtCompileTime, MatrixCols = traits<XprType>::ColsAtCompileTime,
RowsAtCompileTime = ReshapeRows, RowsAtCompileTime = Rows,
ColsAtCompileTime = ReshapeCols, ColsAtCompileTime = Cols,
MaxRowsAtCompileTime = ReshapeRows, MaxRowsAtCompileTime = Rows,
MaxColsAtCompileTime = ReshapeCols, MaxColsAtCompileTime = Cols,
XprTypeIsRowMajor = (int(traits<XprType>::Flags) & RowMajorBit) != 0, XprTypeIsRowMajor = (int(traits<XprType>::Flags) & RowMajorBit) != 0,
IsRowMajor = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ? 1 IsRowMajor = (RowsAtCompileTime == 1 && ColsAtCompileTime != 1) ? 1
: (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ? 0 : (ColsAtCompileTime == 1 && RowsAtCompileTime != 1) ? 0
@ -75,50 +75,48 @@ struct traits<Reshape<XprType, ReshapeRows, ReshapeCols> > : traits<XprType>
MaskPacketAccessBit = (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0) MaskPacketAccessBit = (InnerSize == Dynamic || (InnerSize % packet_traits<Scalar>::size) == 0)
&& (InnerStrideAtCompileTime == 1) && (InnerStrideAtCompileTime == 1)
? PacketAccessBit : 0, ? PacketAccessBit : 0,
MaskAlignedBit = ((OuterStrideAtCompileTime!=Dynamic) && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % 16) == 0)) ? AlignedBit : 0, //MaskAlignedBit = ((OuterStrideAtCompileTime!=Dynamic) && (((OuterStrideAtCompileTime * int(sizeof(Scalar))) % 16) == 0)) ? AlignedBit : 0,
FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0, FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0, FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0, FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
Flags0 = traits<XprType>::Flags & ( (HereditaryBits & ~RowMajorBit) | Flags0 = traits<XprType>::Flags & ( (HereditaryBits & ~RowMajorBit) | MaskPacketAccessBit)
MaskPacketAccessBit |
MaskAlignedBit)
& ~DirectAccessBit, & ~DirectAccessBit,
Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit) Flags = (Flags0 | FlagsLinearAccessBit | FlagsLvalueBit | FlagsRowMajorBit)
}; };
}; };
template<typename XprType, int ReshapeRows=Dynamic, int ReshapeCols=Dynamic, template<typename XprType, int Rows=Dynamic, int Cols=Dynamic, int Order = 0,
bool HasDirectAccess = internal::has_direct_access<XprType>::ret> class ReshapeImpl_dense; bool HasDirectAccess = internal::has_direct_access<XprType>::ret> class ReshapedImpl_dense;
} // end namespace internal } // end namespace internal
template<typename XprType, int ReshapeRows, int ReshapeCols, typename StorageKind> class ReshapeImpl; template<typename XprType, int Rows, int Cols, int Order, typename StorageKind> class ReshapedImpl;
template<typename XprType, int ReshapeRows, int ReshapeCols> class Reshape template<typename XprType, int Rows, int Cols, int Order> class Reshaped
: public ReshapeImpl<XprType, ReshapeRows, ReshapeCols, typename internal::traits<XprType>::StorageKind> : public ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind>
{ {
typedef ReshapeImpl<XprType, ReshapeRows, ReshapeCols, typename internal::traits<XprType>::StorageKind> Impl; typedef ReshapedImpl<XprType, Rows, Cols, Order, typename internal::traits<XprType>::StorageKind> Impl;
public: public:
//typedef typename Impl::Base Base; //typedef typename Impl::Base Base;
typedef Impl Base; typedef Impl Base;
EIGEN_GENERIC_PUBLIC_INTERFACE(Reshape) EIGEN_GENERIC_PUBLIC_INTERFACE(Reshaped)
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reshape) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reshaped)
/** Fixed-size constructor /** Fixed-size constructor
*/ */
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
inline Reshape(XprType& xpr) inline Reshaped(XprType& xpr)
: Impl(xpr) : Impl(xpr)
{ {
EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE) EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
eigen_assert(ReshapeRows * ReshapeCols == xpr.rows() * xpr.cols()); eigen_assert(Rows * Cols == xpr.rows() * xpr.cols());
} }
/** Dynamic-size constructor /** Dynamic-size constructor
*/ */
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
inline Reshape(XprType& xpr, inline Reshaped(XprType& xpr,
Index reshapeRows, Index reshapeCols) Index reshapeRows, Index reshapeCols)
: Impl(xpr, reshapeRows, reshapeCols) : Impl(xpr, reshapeRows, reshapeCols)
{ {
@ -128,144 +126,56 @@ template<typename XprType, int ReshapeRows, int ReshapeCols> class Reshape
} }
}; };
// The generic default implementation for dense reshape simplu forward to the internal::ReshapeImpl_dense // The generic default implementation for dense reshape simplu forward to the internal::ReshapedImpl_dense
// that must be specialized for direct and non-direct access... // that must be specialized for direct and non-direct access...
template<typename XprType, int ReshapeRows, int ReshapeCols> template<typename XprType, int Rows, int Cols, int Order>
class ReshapeImpl<XprType, ReshapeRows, ReshapeCols, Dense> class ReshapedImpl<XprType, Rows, Cols, Order, Dense>
: public internal::ReshapeImpl_dense<XprType, ReshapeRows, ReshapeCols> : public internal::ReshapedImpl_dense<XprType, Rows, Cols, Order>
{ {
typedef internal::ReshapeImpl_dense<XprType, ReshapeRows, ReshapeCols> Impl; typedef internal::ReshapedImpl_dense<XprType, Rows, Cols, Order> Impl;
typedef typename XprType::Index Index;
public: public:
typedef Impl Base; typedef Impl Base;
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapeImpl) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl)
EIGEN_DEVICE_FUNC inline ReshapeImpl(XprType& xpr) : Impl(xpr) {} EIGEN_DEVICE_FUNC inline ReshapedImpl(XprType& xpr) : Impl(xpr) {}
EIGEN_DEVICE_FUNC inline ReshapeImpl(XprType& xpr, Index reshapeRows, Index reshapeCols) EIGEN_DEVICE_FUNC inline ReshapedImpl(XprType& xpr, Index reshapeRows, Index reshapeCols)
: Impl(xpr, reshapeRows, reshapeCols) {} : Impl(xpr, reshapeRows, reshapeCols) {}
}; };
namespace internal { namespace internal {
/** \internal Internal implementation of dense Reshapes in the general case. */ /** \internal Internal implementation of dense Reshapeds in the general case. */
template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAccess> class ReshapeImpl_dense template<typename XprType, int Rows, int Cols, int Order, bool HasDirectAccess> class ReshapedImpl_dense
: public internal::dense_xpr_base<Reshape<XprType, ReshapeRows, ReshapeCols> >::type : public internal::dense_xpr_base<Reshaped<XprType, Rows, Cols, Order> >::type
{ {
typedef Reshape<XprType, ReshapeRows, ReshapeCols> ReshapeType; typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
public: public:
typedef typename internal::dense_xpr_base<ReshapeType>::type Base; typedef typename internal::dense_xpr_base<ReshapedType>::type Base;
EIGEN_DENSE_PUBLIC_INTERFACE(ReshapeType) EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapeImpl_dense) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
typedef typename internal::remove_all<XprType>::type NestedExpression;
class InnerIterator; class InnerIterator;
/** Fixed-size constructor /** Fixed-size constructor
*/ */
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
inline ReshapeImpl_dense(XprType& xpr) inline ReshapedImpl_dense(XprType& xpr)
: m_xpr(xpr), m_reshapeRows(ReshapeRows), m_reshapeCols(ReshapeCols) : m_xpr(xpr), m_rows(Rows), m_cols(Cols)
{} {}
/** Dynamic-size constructor /** Dynamic-size constructor
*/ */
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
inline ReshapeImpl_dense(XprType& xpr, inline ReshapedImpl_dense(XprType& xpr,
Index reshapeRows, Index reshapeCols) Index nRows, Index nCols)
: m_xpr(xpr), m_reshapeRows(reshapeRows), m_reshapeCols(reshapeCols) : m_xpr(xpr), m_rows(nRows), m_cols(nCols)
{} {}
EIGEN_DEVICE_FUNC inline Index rows() const { return m_reshapeRows.value(); } EIGEN_DEVICE_FUNC Index rows() const { return m_rows; }
EIGEN_DEVICE_FUNC inline Index cols() const { return m_reshapeCols.value(); } EIGEN_DEVICE_FUNC Index cols() const { return m_cols; }
typedef std::pair<Index, Index> RowCol;
inline RowCol index_remap(Index rowId, Index colId) const {
const Index nth_elem_idx = colId * m_reshapeRows.value() + rowId;
const Index actual_col = nth_elem_idx / m_xpr.rows();
const Index actual_row = nth_elem_idx % m_xpr.rows();
return RowCol(actual_row, actual_col);
}
EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index rowId, Index colId)
{
EIGEN_STATIC_ASSERT_LVALUE(XprType)
const RowCol row_col = index_remap(rowId, colId);
return m_xpr.const_cast_derived().coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_xpr.derived().coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_xpr.coeff(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index index)
{
EIGEN_STATIC_ASSERT_LVALUE(XprType)
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_xpr.const_cast_derived().coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index index) const
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_xpr.const_cast_derived().coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const CoeffReturnType coeff(Index index) const
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_xpr.coeff(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
template<int LoadMode>
inline PacketScalar packet(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_xpr.template packet<Unaligned>(row_col.first, row_col.second);
}
template<int LoadMode>
inline void writePacket(Index rowId, Index colId, const PacketScalar& val)
{
const RowCol row_col = index_remap(rowId, colId);
m_xpr.const_cast_derived().template writePacket<Unaligned>
(row_col.first, row_col.second, val);
}
template<int LoadMode>
inline PacketScalar packet(Index index) const
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_xpr.template packet<Unaligned>(row_col.first, row_col.second);
}
template<int LoadMode>
inline void writePacket(Index index, const PacketScalar& val)
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_xpr.template packet<Unaligned>(row_col.first, row_col.second, val);
}
#ifdef EIGEN_PARSED_BY_DOXYGEN #ifdef EIGEN_PARSED_BY_DOXYGEN
/** \sa MapBase::data() */ /** \sa MapBase::data() */
@ -274,40 +184,168 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
EIGEN_DEVICE_FUNC inline Index outerStride() const; EIGEN_DEVICE_FUNC inline Index outerStride() const;
#endif #endif
/** \returns the nested expression */
EIGEN_DEVICE_FUNC EIGEN_DEVICE_FUNC
const typename internal::remove_all<typename XprType::Nested>::type& nestedExpression() const const typename internal::remove_all<XprType>::type&
{ nestedExpression() const { return m_xpr; }
return m_xpr;
} /** \returns the nested expression */
EIGEN_DEVICE_FUNC
typename internal::remove_reference<XprType>::type&
nestedExpression() { return m_xpr.const_cast_derived(); }
protected: protected:
const typename XprType::Nested m_xpr; MatrixTypeNested m_xpr;
const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_reshapeRows; const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_reshapeCols; const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
}; };
///** \internal Internal implementation of dense Reshapes in the direct access case.*/
//template<typename XprType, int ReshapeRows, int ReshapeCols> template<typename ArgType, int Rows, int Cols, int Order>
//class ReshapeImpl_dense<XprType,ReshapeRows,ReshapeCols, true> struct unary_evaluator<Reshaped<ArgType, Rows, Cols, Order>, IndexBased>
// : public MapBase<Reshape<XprType, ReshapeRows, ReshapeCols> > : evaluator_base<Reshaped<ArgType, Rows, Cols, Order> >
{
typedef Reshaped<ArgType, Rows, Cols, Order> XprType;
enum {
CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of index computations */,
Flags = (evaluator<ArgType>::Flags & (HereditaryBits /*| LinearAccessBit | DirectAccessBit*/)),
Alignment = 0
};
EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
{
EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
}
typedef typename XprType::Scalar Scalar;
typedef typename XprType::CoeffReturnType CoeffReturnType;
typedef std::pair<Index, Index> RowCol;
inline RowCol index_remap(Index rowId, Index colId) const {
const Index nth_elem_idx = colId * m_xpr.rows() + rowId;
const Index actual_col = nth_elem_idx / m_xpr.nestedExpression().rows();
const Index actual_row = nth_elem_idx % m_xpr.nestedExpression().rows();
return RowCol(actual_row, actual_col);
}
EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index rowId, Index colId)
{
EIGEN_STATIC_ASSERT_LVALUE(XprType)
const RowCol row_col = index_remap(rowId, colId);
return m_argImpl.coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_argImpl.coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_argImpl.coeff(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline Scalar& coeffRef(Index index)
{
EIGEN_STATIC_ASSERT_LVALUE(XprType)
const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
Rows == 1 ? index : 0);
return m_argImpl.coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const Scalar& coeffRef(Index index) const
{
const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
Rows == 1 ? index : 0);
return m_argImpl.coeffRef(row_col.first, row_col.second);
}
EIGEN_DEVICE_FUNC
inline const CoeffReturnType coeff(Index index) const
{
const RowCol row_col = index_remap(Rows == 1 ? 0 : index,
Rows == 1 ? index : 0);
return m_argImpl.coeff(row_col.first, row_col.second);
}
#if 0
EIGEN_DEVICE_FUNC
template<int LoadMode>
inline PacketScalar packet(Index rowId, Index colId) const
{
const RowCol row_col = index_remap(rowId, colId);
return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
}
template<int LoadMode>
EIGEN_DEVICE_FUNC
inline void writePacket(Index rowId, Index colId, const PacketScalar& val)
{
const RowCol row_col = index_remap(rowId, colId);
m_argImpl.const_cast_derived().template writePacket<Unaligned>
(row_col.first, row_col.second, val);
}
template<int LoadMode>
EIGEN_DEVICE_FUNC
inline PacketScalar packet(Index index) const
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second);
}
template<int LoadMode>
EIGEN_DEVICE_FUNC
inline void writePacket(Index index, const PacketScalar& val)
{
const RowCol row_col = index_remap(RowsAtCompileTime == 1 ? 0 : index,
RowsAtCompileTime == 1 ? index : 0);
return m_argImpl.template packet<Unaligned>(row_col.first, row_col.second, val);
}
#endif
protected:
evaluator<ArgType> m_argImpl;
const XprType& m_xpr;
};
///** \internal Internal implementation of dense Reshapeds in the direct access case.*/
//template<typename XprType, int Rows, int Cols, int Order>
//class ReshapedImpl_dense<XprType,ReshapedRows,ReshapedCols, true>
// : public MapBase<Reshaped<XprType, Rows, Cols, Order> >
//{ //{
// typedef Reshape<XprType, ReshapeRows, ReshapeCols> ReshapeType; // typedef Reshaped<XprType, Rows, Cols, Order> ReshapedType;
// public: // public:
// //
// typedef MapBase<ReshapeType> Base; // typedef MapBase<ReshapedType> Base;
// EIGEN_DENSE_PUBLIC_INTERFACE(ReshapeType) // EIGEN_DENSE_PUBLIC_INTERFACE(ReshapedType)
// EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapeImpl_dense) // EIGEN_INHERIT_ASSIGNMENT_OPERATORS(ReshapedImpl_dense)
// //
// /** Column or Row constructor // /** Column or Row constructor
// */ // */
// EIGEN_DEVICE_FUNC // EIGEN_DEVICE_FUNC
// inline ReshapeImpl_dense(XprType& xpr, Index i) // inline ReshapedImpl_dense(XprType& xpr, Index i)
// : Base(internal::const_cast_ptr(&xpr.coeffRef( // : Base(internal::const_cast_ptr(&xpr.coeffRef(
// (ReshapeRows==1) && (ReshapeCols==XprType::ColsAtCompileTime) ? i : 0, // (ReshapedRows==1) && (ReshapedCols==XprType::ColsAtCompileTime) ? i : 0,
// (ReshapeRows==XprType::RowsAtCompileTime) && (ReshapeCols==1) ? i : 0)), // (ReshapedRows==XprType::RowsAtCompileTime) && (ReshapedCols==1) ? i : 0)),
// ReshapeRows==1 ? 1 : xpr.rows(), // ReshapedRows==1 ? 1 : xpr.rows(),
// ReshapeCols==1 ? 1 : xpr.cols()), // ReshapedCols==1 ? 1 : xpr.cols()),
// m_xpr(xpr) // m_xpr(xpr)
// { // {
// init(); // init();
@ -316,7 +354,7 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
// /** Fixed-size constructor // /** Fixed-size constructor
// */ // */
// EIGEN_DEVICE_FUNC // EIGEN_DEVICE_FUNC
// inline ReshapeImpl_dense(XprType& xpr) // inline ReshapedImpl_dense(XprType& xpr)
// : Base(internal::const_cast_ptr(&xpr.coeffRef(0, 0))), m_xpr(xpr) // : Base(internal::const_cast_ptr(&xpr.coeffRef(0, 0))), m_xpr(xpr)
// { // {
// init(); // init();
@ -325,7 +363,7 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
// /** Dynamic-size constructor // /** Dynamic-size constructor
// */ // */
// EIGEN_DEVICE_FUNC // EIGEN_DEVICE_FUNC
// inline ReshapeImpl_dense(XprType& xpr, // inline ReshapedImpl_dense(XprType& xpr,
// Index reshapeRows, Index reshapeCols) // Index reshapeRows, Index reshapeCols)
// : Base(internal::const_cast_ptr(&xpr.coeffRef(0, 0)), reshapeRows, reshapeCols), // : Base(internal::const_cast_ptr(&xpr.coeffRef(0, 0)), reshapeRows, reshapeCols),
// m_xpr(xpr) // m_xpr(xpr)
@ -343,7 +381,7 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
// /** \sa MapBase::innerStride() */ // /** \sa MapBase::innerStride() */
// inline Index innerStride() const // inline Index innerStride() const
// { // {
// return internal::traits<ReshapeType>::HasSameStorageOrderAsXprType // return internal::traits<ReshapedType>::HasSameStorageOrderAsXprType
// ? m_xpr.innerStride() // ? m_xpr.innerStride()
// : m_xpr.outerStride(); // : m_xpr.outerStride();
// } // }
@ -364,7 +402,7 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
// #ifndef EIGEN_PARSED_BY_DOXYGEN // #ifndef EIGEN_PARSED_BY_DOXYGEN
// /** \internal used by allowAligned() */ // /** \internal used by allowAligned() */
// EIGEN_DEVICE_FUNC // EIGEN_DEVICE_FUNC
// inline ReshapeImpl_dense(XprType& xpr, const Scalar* data, Index reshapeRows, Index reshapeCols) // inline ReshapedImpl_dense(XprType& xpr, const Scalar* data, Index reshapeRows, Index reshapeCols)
// : Base(data, reshapeRows, reshapeCols), m_xpr(xpr) // : Base(data, reshapeRows, reshapeCols), m_xpr(xpr)
// { // {
// init(); // init();
@ -375,7 +413,7 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
// EIGEN_DEVICE_FUNC // EIGEN_DEVICE_FUNC
// void init() // void init()
// { // {
// m_outerStride = internal::traits<ReshapeType>::HasSameStorageOrderAsXprType // m_outerStride = internal::traits<ReshapedType>::HasSameStorageOrderAsXprType
// ? m_xpr.outerStride() // ? m_xpr.outerStride()
// : m_xpr.innerStride(); // : m_xpr.innerStride();
// } // }
@ -386,6 +424,65 @@ template<typename XprType, int ReshapeRows, int ReshapeCols, bool HasDirectAcces
} // end namespace internal } // end namespace internal
/** \returns a dynamic-size expression of a reshape in *this.
*
* \param reshapeRows the number of rows in the reshape
* \param reshapeCols the number of columns in the reshape
*
* Example: \include MatrixBase_reshape_int_int.cpp
* Output: \verbinclude MatrixBase_reshape_int_int.out
*
* \note Even though the returned expression has dynamic size, in the case
* when it is applied to a fixed-size matrix, it inherits a fixed maximal size,
* which means that evaluating it does not cause a dynamic memory allocation.
*
* \sa class Reshape, reshaped()
*/
template<typename Derived>
EIGEN_DEVICE_FUNC
inline Reshaped<Derived> DenseBase<Derived>::reshaped(Index reshapeRows, Index reshapeCols)
{
return Reshaped<Derived>(derived(), reshapeRows, reshapeCols);
}
/** This is the const version of reshaped(Index,Index). */
template<typename Derived>
EIGEN_DEVICE_FUNC
inline const Reshaped<const Derived> DenseBase<Derived>::reshaped(Index reshapeRows, Index reshapeCols) const
{
return Reshaped<const Derived>(derived(), reshapeRows, reshapeCols);
}
/** \returns a fixed-size expression of a reshape in *this.
*
* The template parameters \a ReshapeRows and \a ReshapeCols are the number of
* rows and columns in the reshape.
*
* Example: \include MatrixBase_reshape.cpp
* Output: \verbinclude MatrixBase_reshape.out
*
* \note since reshape is a templated member, the keyword template has to be used
* if the matrix type is also a template parameter: \code m.template reshape<3,3>(); \endcode
*
* \sa class Reshape, reshaped(Index,Index)
*/
template<typename Derived>
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline Reshaped<Derived, ReshapeRows, ReshapeCols> DenseBase<Derived>::reshaped()
{
return Reshaped<Derived, ReshapeRows, ReshapeCols>(derived());
}
/** This is the const version of reshape<>(Index, Index). */
template<typename Derived>
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline const Reshaped<const Derived, ReshapeRows, ReshapeCols> DenseBase<Derived>::reshaped() const
{
return Reshaped<const Derived, ReshapeRows, ReshapeCols>(derived());
}
} // end namespace Eigen } // end namespace Eigen
#endif // EIGEN_RESHAPE_H #endif // EIGEN_RESHAPED_H

View File

@ -84,7 +84,7 @@ template<typename ExpressionType> class SwapWrapper;
template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic, bool InnerPanel = false> class Block; template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic, bool InnerPanel = false> class Block;
template<typename XprType, typename RowIndices, typename ColIndices> class IndexedView; template<typename XprType, typename RowIndices, typename ColIndices> class IndexedView;
template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic> class Reshape; template<typename XprType, int Rows=Dynamic, int Cols=Dynamic, int Order=0> class Reshaped;
template<typename MatrixType, int Size=Dynamic> class VectorBlock; template<typename MatrixType, int Size=Dynamic> class VectorBlock;
template<typename MatrixType> class Transpose; template<typename MatrixType> class Transpose;

View File

@ -1354,59 +1354,3 @@ inline typename ConstFixedSegmentReturnType<N>::Type tail(Index n = N) const
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
return typename ConstFixedSegmentReturnType<N>::Type(derived(), size() - n); return typename ConstFixedSegmentReturnType<N>::Type(derived(), size() - n);
} }
/** \returns a dynamic-size expression of a reshape in *this.
*
* \param reshapeRows the number of rows in the reshape
* \param reshapeCols the number of columns in the reshape
*
* Example: \include MatrixBase_reshape_int_int.cpp
* Output: \verbinclude MatrixBase_reshape_int_int.out
*
* \note Even though the returned expression has dynamic size, in the case
* when it is applied to a fixed-size matrix, it inherits a fixed maximal size,
* which means that evaluating it does not cause a dynamic memory allocation.
*
* \sa class Reshape, reshape()
*/
EIGEN_DEVICE_FUNC
inline Reshape<Derived> reshape(Index reshapeRows, Index reshapeCols)
{
return Reshape<Derived>(derived(), reshapeRows, reshapeCols);
}
/** This is the const version of reshape(Index,Index). */
EIGEN_DEVICE_FUNC
inline const Reshape<const Derived> reshape(Index reshapeRows, Index reshapeCols) const
{
return Reshape<const Derived>(derived(), reshapeRows, reshapeCols);
}
/** \returns a fixed-size expression of a reshape in *this.
*
* The template parameters \a ReshapeRows and \a ReshapeCols are the number of
* rows and columns in the reshape.
*
* Example: \include MatrixBase_reshape.cpp
* Output: \verbinclude MatrixBase_reshape.out
*
* \note since reshape is a templated member, the keyword template has to be used
* if the matrix type is also a template parameter: \code m.template reshape<3,3>(); \endcode
*
* \sa class Reshape, reshape(Index,Index)
*/
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline Reshape<Derived, ReshapeRows, ReshapeCols> reshape()
{
return Reshape<Derived, ReshapeRows, ReshapeCols>(derived());
}
/** This is the const version of reshape<>(Index, Index). */
template<int ReshapeRows, int ReshapeCols>
EIGEN_DEVICE_FUNC
inline const Reshape<const Derived, ReshapeRows, ReshapeCols> reshape() const
{
return Reshape<const Derived, ReshapeRows, ReshapeCols>(derived());
}

View File

@ -160,10 +160,10 @@ endif()
ei_add_test(redux) ei_add_test(redux)
ei_add_test(visitor) ei_add_test(visitor)
ei_add_test(block) ei_add_test(block)
ei_add_test(reshape)
ei_add_test(corners) ei_add_test(corners)
ei_add_test(symbolic_index) ei_add_test(symbolic_index)
ei_add_test(indexed_view) ei_add_test(indexed_view)
ei_add_test(reshape)
ei_add_test(swap) ei_add_test(swap)
ei_add_test(resize) ei_add_test(resize)
ei_add_test(conservative_resize) ei_add_test(conservative_resize)

View File

@ -15,35 +15,36 @@ using Eigen::MatrixXi;
// just test a 4x4 matrix, enumerate all combination manually, // just test a 4x4 matrix, enumerate all combination manually,
// so I don't have to do template-meta-programming here. // so I don't have to do template-meta-programming here.
template <typename MatType> template <typename MatType>
void reshape_all_size(MatType m) { void reshape_all_size(MatType m)
{
typedef Eigen::Map<MatrixXi> MapMat; typedef Eigen::Map<MatrixXi> MapMat;
// dynamic // dynamic
VERIFY_IS_EQUAL((m.template reshape( 1, 16)), MapMat(m.data(), 1, 16)); VERIFY_IS_EQUAL((m.reshaped( 1, 16)), MapMat(m.data(), 1, 16));
VERIFY_IS_EQUAL((m.template reshape( 2, 8)), MapMat(m.data(), 2, 8)); VERIFY_IS_EQUAL((m.reshaped( 2, 8)), MapMat(m.data(), 2, 8));
VERIFY_IS_EQUAL((m.template reshape( 4, 4)), MapMat(m.data(), 4, 4)); VERIFY_IS_EQUAL((m.reshaped( 4, 4)), MapMat(m.data(), 4, 4));
VERIFY_IS_EQUAL((m.template reshape( 8, 2)), MapMat(m.data(), 8, 2)); VERIFY_IS_EQUAL((m.reshaped( 8, 2)), MapMat(m.data(), 8, 2));
VERIFY_IS_EQUAL((m.template reshape(16, 1)), MapMat(m.data(), 16, 1)); VERIFY_IS_EQUAL((m.reshaped(16, 1)), MapMat(m.data(), 16, 1));
// static // static
VERIFY_IS_EQUAL((m.template reshape< 1, 16>()), MapMat(m.data(), 1, 16)); VERIFY_IS_EQUAL((m.template reshaped< 1, 16>()), MapMat(m.data(), 1, 16));
VERIFY_IS_EQUAL((m.template reshape< 2, 8>()), MapMat(m.data(), 2, 8)); VERIFY_IS_EQUAL((m.template reshaped< 2, 8>()), MapMat(m.data(), 2, 8));
VERIFY_IS_EQUAL((m.template reshape< 4, 4>()), MapMat(m.data(), 4, 4)); VERIFY_IS_EQUAL((m.template reshaped< 4, 4>()), MapMat(m.data(), 4, 4));
VERIFY_IS_EQUAL((m.template reshape< 8, 2>()), MapMat(m.data(), 8, 2)); VERIFY_IS_EQUAL((m.template reshaped< 8, 2>()), MapMat(m.data(), 8, 2));
VERIFY_IS_EQUAL((m.template reshape<16, 1>()), MapMat(m.data(), 16, 1)); VERIFY_IS_EQUAL((m.template reshaped<16, 1>()), MapMat(m.data(), 16, 1));
// reshape chain // reshape chain
VERIFY_IS_EQUAL( VERIFY_IS_EQUAL(
(m (m
.template reshape( 1, 16) . reshaped( 1, 16)
.template reshape< 2, 8>() .template reshaped< 2, 8>()
.template reshape(16, 1) . reshaped(16, 1)
.template reshape< 8, 2>() .template reshaped< 8, 2>()
.template reshape( 2, 8) . reshaped( 2, 8)
.template reshape< 1, 16>() .template reshaped< 1, 16>()
.template reshape( 4, 4) . reshaped( 4, 4)
.template reshape<16, 1>() .template reshaped<16, 1>()
.template reshape( 8, 2) . reshaped( 8, 2)
.template reshape< 4, 4>() .template reshaped< 4, 4>()
), ),
MapMat(m.data(), 4, 4) MapMat(m.data(), 4, 4)
); );