mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add reshaped<>() shortcuts when returning vectors and remove the reshaping version of operator()(all)
This commit is contained in:
@@ -103,7 +103,8 @@
|
||||
STORAGE_KIND_MUST_MATCH=1,
|
||||
STORAGE_INDEX_MUST_MATCH=1,
|
||||
CHOLMOD_SUPPORTS_DOUBLE_PRECISION_ONLY=1,
|
||||
SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY=1
|
||||
SELFADJOINTVIEW_ACCEPTS_UPPER_AND_LOWER_MODE_ONLY=1,
|
||||
INVALID_TEMPLATE_PARAMETER=1
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/// \param nRows the number of rows in the reshaped expression, specified at either run-time or compile-time, or AutoSize
|
||||
/// \param nCols the number of columns in the reshaped expression, specified at either run-time or compile-time, or AutoSize
|
||||
/// \tparam Order specifies whether the coefficients should be processed in column-major-order (ColMajor), in row-major-order (RowMajor),
|
||||
/// or follows the \em natural order of the nested expression (AutoOrder). The default is ColMajor.
|
||||
/// or follows the \em natural order of the nested expression (AutoOrder). The default is ColMajor.
|
||||
/// \tparam NRowsType the type of the value handling the number of rows, typically Index.
|
||||
/// \tparam NColsType the type of the value handling the number of columns, typically Index.
|
||||
///
|
||||
@@ -38,6 +38,36 @@ EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,...>
|
||||
reshaped(NRowsType nRows, NColsType nCols) const;
|
||||
|
||||
/// \returns an expression of \c *this with columns (or rows) stacked to a linear column (or row) vector
|
||||
///
|
||||
/// \tparam Order specifies whether to glue columns or rows, and returns a column or row vector.
|
||||
/// Possible values are ColMajor, RowMajor or AutoOrder. The default is ColMajor.
|
||||
///
|
||||
/// If Order==ColMajor (the default), then it returns a column vector from the stacked columns of \c *this.
|
||||
/// This is equivalent to \code A(all) \endcode and \code A.reshaped<RowMajor>(fix<1>,AutoSize) \endcode.
|
||||
///
|
||||
/// If Order==RowMajor, then it returns a row vector from the glued rows of \c *this.
|
||||
/// This is equivalent to \code A.reshaped<RowMajor>(fix<1>,AutoSize) \endcode.
|
||||
///
|
||||
/// If Order=AutoOrder, the it returns the same expression as \code A.reshaped<storage_order_of_A>() \endcode
|
||||
///
|
||||
/// If you want more control, you can still fall back to reshaped(NRowsType,NColsType).
|
||||
/// For instance, to return a column vector with element stacked following the storage order,
|
||||
/// you can do: \code A.reshaped<AutoOrder>(AutoSize,fix<1>) \endcode
|
||||
///
|
||||
/// \sa operator()(all), reshaped(NRowsType,NColsType), class Reshaped
|
||||
///
|
||||
template<int Order = ColMajor>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline Reshaped<Derived,...>
|
||||
reshaped();
|
||||
|
||||
/** This is the const version of reshaped(). */
|
||||
template<int Order = ColMajor>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,...>
|
||||
reshaped() const;
|
||||
|
||||
/// \returns as expression of \c *this with columns stacked to a linear column vector
|
||||
///
|
||||
/// This overload is essentially a shortcut for
|
||||
@@ -104,11 +134,29 @@ reshaped(NRowsType nRows, NColsType nCols) EIGEN_RESHAPED_METHOD_CONST
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>
|
||||
operator()(const Eigen::internal::all_t&) EIGEN_RESHAPED_METHOD_CONST
|
||||
reshaped() EIGEN_RESHAPED_METHOD_CONST
|
||||
{
|
||||
return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>(derived(),size(),1);
|
||||
}
|
||||
|
||||
template<int Order>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
Order==RowMajor ? 1 : SizeAtCompileTime,
|
||||
Order==RowMajor ? SizeAtCompileTime : 1,
|
||||
Order==AutoOrder?Flags&RowMajorBit:Order>
|
||||
reshaped() EIGEN_RESHAPED_METHOD_CONST
|
||||
{
|
||||
EIGEN_STATIC_ASSERT(Order==RowMajor || Order==ColMajor, INVALID_TEMPLATE_PARAMETER);
|
||||
return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
Order==RowMajor ? 1 : SizeAtCompileTime,
|
||||
Order==RowMajor ? SizeAtCompileTime : 1,
|
||||
Order==AutoOrder?Flags&RowMajorBit:Order>
|
||||
(derived(),
|
||||
Order==RowMajor ? 1 : size(),
|
||||
Order==RowMajor ? size() : 1);
|
||||
}
|
||||
|
||||
#undef EIGEN_RESHAPED_METHOD_CONST
|
||||
|
||||
#ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
|
||||
@@ -149,18 +149,22 @@ void reshape4x4(MatType m)
|
||||
MatrixXi m28r2 = m.transpose().template reshaped<ColMajor>(8,2).transpose();
|
||||
VERIFY_IS_EQUAL( m28r1, m28r2);
|
||||
|
||||
VERIFY(is_same_eq(m.reshaped(v16,fix<1>), m(all)));
|
||||
VERIFY_IS_EQUAL(m.reshaped(16,1), m(all));
|
||||
VERIFY_IS_EQUAL(m.reshaped(1,16), m(all).transpose());
|
||||
VERIFY_IS_EQUAL(m(all).reshaped(2,8), m.reshaped(2,8));
|
||||
VERIFY_IS_EQUAL(m(all).reshaped(4,4), m.reshaped(4,4));
|
||||
VERIFY_IS_EQUAL(m(all).reshaped(8,2), m.reshaped(8,2));
|
||||
VERIFY(is_same_eq(m.reshaped(v16,fix<1>), m.reshaped()));
|
||||
VERIFY_IS_EQUAL(m.reshaped(16,1), m.reshaped());
|
||||
VERIFY_IS_EQUAL(m.reshaped(1,16), m.reshaped().transpose());
|
||||
VERIFY_IS_EQUAL(m.reshaped().reshaped(2,8), m.reshaped(2,8));
|
||||
VERIFY_IS_EQUAL(m.reshaped().reshaped(4,4), m.reshaped(4,4));
|
||||
VERIFY_IS_EQUAL(m.reshaped().reshaped(8,2), m.reshaped(8,2));
|
||||
|
||||
VERIFY(is_same_eq(m.reshaped(AutoSize,fix<1>), m(all)));
|
||||
VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>,AutoSize), m.transpose()(all).transpose());
|
||||
VERIFY_IS_EQUAL(m.reshaped(), m.template reshaped<ColMajor>());
|
||||
VERIFY_IS_EQUAL(m.transpose().reshaped().transpose(), m.template reshaped<RowMajor>());
|
||||
VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>, AutoSize), m.template reshaped<RowMajor>());
|
||||
|
||||
VERIFY(is_same_eq(m.reshaped(AutoSize,fix<1>), m.reshaped()));
|
||||
VERIFY_IS_EQUAL(m.template reshaped<RowMajor>(fix<1>,AutoSize), m.transpose().reshaped().transpose());
|
||||
}
|
||||
|
||||
void test_reshape()
|
||||
EIGEN_DECLARE_TEST(reshape)
|
||||
{
|
||||
typedef Matrix<int,Dynamic,Dynamic> RowMatrixXi;
|
||||
typedef Matrix<int,4,4> RowMatrix4i;
|
||||
|
||||
Reference in New Issue
Block a user