mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add support for automatic-size deduction in reshaped, e.g.:
mat.reshaped(4,AutoSize); <-> mat.reshaped(4,mat.size()/4);
This commit is contained in:
45
Eigen/src/Core/util/ReshapedHelper.h
Normal file
45
Eigen/src/Core/util/ReshapedHelper.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
|
||||
//
|
||||
// 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/.
|
||||
|
||||
|
||||
#ifndef EIGEN_RESHAPED_HELPER_H
|
||||
#define EIGEN_RESHAPED_HELPER_H
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
enum AutoSize_t { AutoSize };
|
||||
|
||||
namespace internal {
|
||||
|
||||
template<typename SizeType,typename OtherSize, int TotalSize>
|
||||
struct get_compiletime_reshape_size {
|
||||
enum { value = get_fixed_value<SizeType>::value };
|
||||
};
|
||||
|
||||
template<typename SizeType>
|
||||
Index get_runtime_reshape_size(SizeType size, Index /*other*/, Index /*total*/) {
|
||||
return internal::get_runtime_value(size);
|
||||
}
|
||||
|
||||
template<typename OtherSize, int TotalSize>
|
||||
struct get_compiletime_reshape_size<AutoSize_t,OtherSize,TotalSize> {
|
||||
enum {
|
||||
other_size = get_fixed_value<OtherSize>::value,
|
||||
value = (TotalSize==Dynamic || other_size==Dynamic) ? Dynamic : TotalSize / other_size };
|
||||
};
|
||||
|
||||
Index get_runtime_reshape_size(AutoSize_t /*size*/, Index other, Index total) {
|
||||
return total/other;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // end namespace Eigen
|
||||
|
||||
#endif // EIGEN_RESHAPED_HELPER_H
|
||||
@@ -28,62 +28,70 @@ template<typename NRowsType, typename NColsType, typename OrderType>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,...>
|
||||
reshaped(NRowsType nRows, NColsType nCols, OrderType = ColOrder) const;
|
||||
|
||||
#else
|
||||
|
||||
// This file is automatically included twice to generate const and non-const versions
|
||||
|
||||
#ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
#define EIGEN_RESHAPED_METHOD_CONST const
|
||||
#else
|
||||
#define EIGEN_RESHAPED_METHOD_CONST
|
||||
#endif
|
||||
|
||||
#ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
|
||||
// This part is included once
|
||||
|
||||
#endif
|
||||
|
||||
template<typename NRowsType, typename NColsType>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline Reshaped<Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>
|
||||
reshaped(NRowsType nRows, NColsType nCols)
|
||||
inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
|
||||
internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value>
|
||||
reshaped(NRowsType nRows, NColsType nCols) EIGEN_RESHAPED_METHOD_CONST
|
||||
{
|
||||
return Reshaped<Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>(
|
||||
derived(), internal::get_runtime_value(nRows), internal::get_runtime_value(nCols));
|
||||
return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
|
||||
internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value>
|
||||
(derived(),
|
||||
internal::get_runtime_reshape_size(nRows,internal::get_runtime_value(nCols),size()),
|
||||
internal::get_runtime_reshape_size(nCols,internal::get_runtime_value(nRows),size()));
|
||||
}
|
||||
|
||||
template<typename NRowsType, typename NColsType, typename OrderType>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline Reshaped<Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value,
|
||||
inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
|
||||
internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value,
|
||||
OrderType::value==AutoOrderValue?Flags&RowMajorBit:OrderType::value>
|
||||
reshaped(NRowsType nRows, NColsType nCols, OrderType)
|
||||
reshaped(NRowsType nRows, NColsType nCols, OrderType) EIGEN_RESHAPED_METHOD_CONST
|
||||
{
|
||||
return Reshaped<Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value,
|
||||
OrderType::value==AutoOrderValue?Flags&RowMajorBit:OrderType::value>(
|
||||
derived(), internal::get_runtime_value(nRows), internal::get_runtime_value(nCols));
|
||||
}
|
||||
|
||||
|
||||
template<typename NRowsType, typename NColsType>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>
|
||||
reshaped(NRowsType nRows, NColsType nCols) const
|
||||
{
|
||||
return Reshaped<const Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value>(
|
||||
derived(), internal::get_runtime_value(nRows), internal::get_runtime_value(nCols));
|
||||
}
|
||||
|
||||
template<typename NRowsType, typename NColsType, typename OrderType>
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value,
|
||||
OrderType::value==AutoOrderValue?Flags&RowMajorBit:OrderType::value>
|
||||
reshaped(NRowsType nRows, NColsType nCols, OrderType) const
|
||||
{
|
||||
return Reshaped<const Derived,internal::get_fixed_value<NRowsType>::value,internal::get_fixed_value<NColsType>::value,
|
||||
OrderType::value==AutoOrderValue?Flags&RowMajorBit:OrderType::value>(
|
||||
derived(), internal::get_runtime_value(nRows), internal::get_runtime_value(nCols));
|
||||
return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,
|
||||
internal::get_compiletime_reshape_size<NRowsType,NColsType,SizeAtCompileTime>::value,
|
||||
internal::get_compiletime_reshape_size<NColsType,NRowsType,SizeAtCompileTime>::value,
|
||||
OrderType::value==AutoOrderValue?Flags&RowMajorBit:OrderType::value>
|
||||
(derived(),
|
||||
internal::get_runtime_reshape_size(nRows,internal::get_runtime_value(nCols),size()),
|
||||
internal::get_runtime_reshape_size(nCols,internal::get_runtime_value(nRows),size()));
|
||||
}
|
||||
|
||||
// Views as linear vectors
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<Derived,SizeAtCompileTime,1>
|
||||
operator()(const Eigen::internal::all_t&)
|
||||
inline Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>
|
||||
operator()(const Eigen::internal::all_t&) EIGEN_RESHAPED_METHOD_CONST
|
||||
{
|
||||
return Reshaped<Derived,SizeAtCompileTime,1>(derived(),size(),1);
|
||||
return Reshaped<EIGEN_RESHAPED_METHOD_CONST Derived,SizeAtCompileTime,1>(derived(),size(),1);
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
inline const Reshaped<const Derived,SizeAtCompileTime,1>
|
||||
operator()(const Eigen::internal::all_t&) const
|
||||
{
|
||||
return Reshaped<const Derived,SizeAtCompileTime,1>(derived(),size(),1);
|
||||
}
|
||||
#undef EIGEN_RESHAPED_METHOD_CONST
|
||||
|
||||
#ifndef EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
#define EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
#include "ReshapedMethods.h"
|
||||
#undef EIGEN_RESHAPED_METHOD_2ND_PASS
|
||||
#endif
|
||||
|
||||
#endif // EIGEN_PARSED_BY_DOXYGEN
|
||||
|
||||
Reference in New Issue
Block a user