Split StorageKind promotion into two helpers: one for products, and one for coefficient-wise operations.

This commit is contained in:
Gael Guennebaud 2014-07-01 17:51:53 +02:00
parent 3c63446507
commit 6f846ef9c6
8 changed files with 77 additions and 47 deletions

View File

@ -56,8 +56,9 @@ struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
typename Rhs::Scalar typename Rhs::Scalar
) )
>::type Scalar; >::type Scalar;
typedef typename promote_storage_type<typename traits<Lhs>::StorageKind, typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
typename traits<Rhs>::StorageKind>::ret StorageKind; typename traits<Rhs>::StorageKind,
BinaryOp>::ret StorageKind;
typedef typename promote_index_type<typename traits<Lhs>::Index, typedef typename promote_index_type<typename traits<Lhs>::Index,
typename traits<Rhs>::Index>::type Index; typename traits<Rhs>::Index>::type Index;
typedef typename Lhs::Nested LhsNested; typedef typename Lhs::Nested LhsNested;
@ -98,8 +99,9 @@ template<typename BinaryOp, typename LhsType, typename RhsType>
class CwiseBinaryOp : internal::no_assignment_operator, class CwiseBinaryOp : internal::no_assignment_operator,
public CwiseBinaryOpImpl< public CwiseBinaryOpImpl<
BinaryOp, LhsType, RhsType, BinaryOp, LhsType, RhsType,
typename internal::promote_storage_type<typename internal::traits<LhsType>::StorageKind, typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
typename internal::traits<RhsType>::StorageKind>::ret> typename internal::traits<RhsType>::StorageKind,
BinaryOp>::ret>
{ {
public: public:
@ -108,8 +110,9 @@ class CwiseBinaryOp : internal::no_assignment_operator,
typedef typename CwiseBinaryOpImpl< typedef typename CwiseBinaryOpImpl<
BinaryOp, LhsType, RhsType, BinaryOp, LhsType, RhsType,
typename internal::promote_storage_type<typename internal::traits<LhsType>::StorageKind, typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
typename internal::traits<Rhs>::StorageKind>::ret>::Base Base; typename internal::traits<Rhs>::StorageKind,
BinaryOp>::ret>::Base Base;
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp) EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
typedef typename internal::nested<LhsType>::type LhsNested; typedef typename internal::nested<LhsType>::type LhsNested;

View File

@ -465,22 +465,6 @@ class Map<PermutationMatrix<SizeAtCompileTime, MaxSizeAtCompileTime, IndexType>,
struct PermutationStorage {}; struct PermutationStorage {};
#ifdef EIGEN_TEST_EVALUATORS
// storage type of product of permutation wrapper with dense
namespace internal {
template<> struct promote_storage_type<Dense, PermutationStorage>
{ typedef Dense ret; };
template<> struct promote_storage_type<PermutationStorage, Dense>
{ typedef Dense ret; };
} // end namespace internal
#endif // EIGEN_TEST_EVALUATORS
template<typename _IndicesType> class TranspositionsWrapper; template<typename _IndicesType> class TranspositionsWrapper;
namespace internal { namespace internal {
template<typename _IndicesType> template<typename _IndicesType>

View File

@ -62,8 +62,9 @@ struct traits<Product<Lhs, Rhs, Option> >
typedef MatrixXpr XprKind; typedef MatrixXpr XprKind;
typedef typename product_result_scalar<LhsCleaned,RhsCleaned>::Scalar Scalar; typedef typename product_result_scalar<LhsCleaned,RhsCleaned>::Scalar Scalar;
typedef typename promote_storage_type<typename traits<LhsCleaned>::StorageKind, typedef typename product_promote_storage_type<typename traits<LhsCleaned>::StorageKind,
typename traits<RhsCleaned>::StorageKind>::ret StorageKind; typename traits<RhsCleaned>::StorageKind,
internal::product_type<Lhs,Rhs>::ret>::ret StorageKind;
typedef typename promote_index_type<typename traits<LhsCleaned>::Index, typedef typename promote_index_type<typename traits<LhsCleaned>::Index,
typename traits<RhsCleaned>::Index>::type Index; typename traits<RhsCleaned>::Index>::type Index;
@ -94,8 +95,9 @@ struct traits<Product<Lhs, Rhs, Option> >
template<typename _Lhs, typename _Rhs, int Option> template<typename _Lhs, typename _Rhs, int Option>
class Product : public ProductImpl<_Lhs,_Rhs,Option, class Product : public ProductImpl<_Lhs,_Rhs,Option,
typename internal::promote_storage_type<typename internal::traits<_Lhs>::StorageKind, typename internal::product_promote_storage_type<typename internal::traits<_Lhs>::StorageKind,
typename internal::traits<_Rhs>::StorageKind>::ret> typename internal::traits<_Rhs>::StorageKind,
internal::product_type<_Lhs,_Rhs>::ret>::ret>
{ {
public: public:
@ -104,8 +106,9 @@ class Product : public ProductImpl<_Lhs,_Rhs,Option,
typedef typename ProductImpl< typedef typename ProductImpl<
Lhs, Rhs, Option, Lhs, Rhs, Option,
typename internal::promote_storage_type<typename Lhs::StorageKind, typename internal::product_promote_storage_type<typename Lhs::StorageKind,
typename Rhs::StorageKind>::ret>::Base Base; typename Rhs::StorageKind,
internal::product_type<Lhs,Rhs>::ret>::ret>::Base Base;
EIGEN_GENERIC_PUBLIC_INTERFACE(Product) EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
typedef typename internal::nested<Lhs>::type LhsNested; typedef typename internal::nested<Lhs>::type LhsNested;

View File

@ -27,8 +27,9 @@ struct traits<ProductBase<Derived,_Lhs,_Rhs> >
typedef typename remove_all<_Lhs>::type Lhs; typedef typename remove_all<_Lhs>::type Lhs;
typedef typename remove_all<_Rhs>::type Rhs; typedef typename remove_all<_Rhs>::type Rhs;
typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar; typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
typedef typename promote_storage_type<typename traits<Lhs>::StorageKind, typedef typename product_promote_storage_type<typename traits<Lhs>::StorageKind,
typename traits<Rhs>::StorageKind>::ret StorageKind; typename traits<Rhs>::StorageKind,
0>::ret StorageKind;
typedef typename promote_index_type<typename traits<Lhs>::Index, typedef typename promote_index_type<typename traits<Lhs>::Index,
typename traits<Rhs>::Index>::type Index; typename traits<Rhs>::Index>::type Index;
enum { enum {

View File

@ -42,8 +42,9 @@ struct traits<CoeffBasedProduct<LhsNested,RhsNested,NestingFlags> >
typedef typename remove_all<LhsNested>::type _LhsNested; typedef typename remove_all<LhsNested>::type _LhsNested;
typedef typename remove_all<RhsNested>::type _RhsNested; typedef typename remove_all<RhsNested>::type _RhsNested;
typedef typename scalar_product_traits<typename _LhsNested::Scalar, typename _RhsNested::Scalar>::ReturnType Scalar; typedef typename scalar_product_traits<typename _LhsNested::Scalar, typename _RhsNested::Scalar>::ReturnType Scalar;
typedef typename promote_storage_type<typename traits<_LhsNested>::StorageKind, typedef typename product_promote_storage_type<typename traits<_LhsNested>::StorageKind,
typename traits<_RhsNested>::StorageKind>::ret StorageKind; typename traits<_RhsNested>::StorageKind,
0>::ret StorageKind;
typedef typename promote_index_type<typename traits<_LhsNested>::Index, typedef typename promote_index_type<typename traits<_LhsNested>::Index,
typename traits<_RhsNested>::Index>::type Index; typename traits<_RhsNested>::Index>::type Index;

View File

@ -449,8 +449,6 @@ struct MatrixXpr {};
/** The type used to identify an array expression */ /** The type used to identify an array expression */
struct ArrayXpr {}; struct ArrayXpr {};
#ifdef EIGEN_ENABLE_EVALUATORS
// An evaluator must define its shape. By default, it can be one of the following: // An evaluator must define its shape. By default, it can be one of the following:
struct DenseShape { static std::string debugName() { return "DenseShape"; } }; struct DenseShape { static std::string debugName() { return "DenseShape"; } };
struct DiagonalShape { static std::string debugName() { return "DiagonalShape"; } }; struct DiagonalShape { static std::string debugName() { return "DiagonalShape"; } };
@ -459,8 +457,6 @@ struct TriangularShape { static std::string debugName() { return "TriangularSha
struct SelfAdjointShape { static std::string debugName() { return "SelfAdjointShape"; } }; struct SelfAdjointShape { static std::string debugName() { return "SelfAdjointShape"; } };
struct PermutationShape { static std::string debugName() { return "PermutationShape"; } }; struct PermutationShape { static std::string debugName() { return "PermutationShape"; } };
struct SparseShape { static std::string debugName() { return "SparseShape"; } }; struct SparseShape { static std::string debugName() { return "SparseShape"; } };
#endif
} // end namespace Eigen } // end namespace Eigen

View File

@ -231,6 +231,10 @@ template<typename T> struct plain_matrix_type<T,Dense>
{ {
typedef typename plain_matrix_type_dense<T,typename traits<T>::XprKind>::type type; typedef typename plain_matrix_type_dense<T,typename traits<T>::XprKind>::type type;
}; };
template<typename T> struct plain_matrix_type<T,DiagonalShape>
{
typedef typename T::PlainObject type;
};
template<typename T> struct plain_matrix_type_dense<T,MatrixXpr> template<typename T> struct plain_matrix_type_dense<T,MatrixXpr>
{ {
@ -273,6 +277,11 @@ template<typename T> struct eval<T,Dense>
// > type; // > type;
}; };
template<typename T> struct eval<T,DiagonalShape>
{
typedef typename plain_matrix_type<T>::type type;
};
// for matrices, no need to evaluate, just use a const reference to avoid a useless copy // for matrices, no need to evaluate, just use a const reference to avoid a useless copy
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct eval<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense> struct eval<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, Dense>
@ -515,12 +524,51 @@ template<typename XprType, typename CastType> struct cast_return_type
const XprType&,CastType>::type type; const XprType&,CastType>::type type;
}; };
template <typename A, typename B> struct promote_storage_type; /** \internal Specify the "storage kind" of applying a coefficient-wise
* binary operations between two expressions of kinds A and B respectively.
* The template parameter Functor permits to specialize the resulting storage kind wrt to
* the functor.
* The default rules are as follows:
* \code
* A op A -> A
* A op dense -> dense
* dense op B -> dense
* A * dense -> A
* dense * B -> B
* \endcode
*/
template <typename A, typename B, typename Functor> struct cwise_promote_storage_type;
template <typename A> struct promote_storage_type<A,A> template <typename A, typename Functor> struct cwise_promote_storage_type<A,A,Functor> { typedef A ret; };
{ template <typename Functor> struct cwise_promote_storage_type<Dense,Dense,Functor> { typedef Dense ret; };
typedef A ret; template <typename ScalarA, typename ScalarB> struct cwise_promote_storage_type<Dense,Dense,scalar_product_op<ScalarA,ScalarB> > { typedef Dense ret; };
}; template <typename A, typename Functor> struct cwise_promote_storage_type<A,Dense,Functor> { typedef Dense ret; };
template <typename B, typename Functor> struct cwise_promote_storage_type<Dense,B,Functor> { typedef Dense ret; };
template <typename A, typename ScalarA, typename ScalarB> struct cwise_promote_storage_type<A,Dense,scalar_product_op<ScalarA,ScalarB> > { typedef A ret; };
template <typename B, typename ScalarA, typename ScalarB> struct cwise_promote_storage_type<Dense,B,scalar_product_op<ScalarA,ScalarB> > { typedef B ret; };
/** \internal Specify the "storage kind" of multiplying an expression of kind A with kind B.
* The template parameter ProductTag permits to specialize the resulting storage kind wrt to
* some compile-time properties of the product: GemmProduct, GemvProduct, OuterProduct, InnerProduct.
* The default rules are as follows:
* \code
* K * K -> K
* dense * K -> dense
* K * dense -> dense
* diag * K -> K
* K * diag -> K
* \endcode
*/
template <typename A, typename B, int ProductTag> struct product_promote_storage_type;
template <typename A, int ProductTag> struct product_promote_storage_type<A, A, ProductTag> { typedef A ret;};
template <int ProductTag> struct product_promote_storage_type<Dense, Dense, ProductTag> { typedef Dense ret;};
template <typename A, int ProductTag> struct product_promote_storage_type<A, Dense, ProductTag> { typedef Dense ret; };
template <typename B, int ProductTag> struct product_promote_storage_type<Dense, B, ProductTag> { typedef Dense ret; };
template <typename A, int ProductTag> struct product_promote_storage_type<A, DiagonalShape, ProductTag> { typedef A ret; };
template <typename B, int ProductTag> struct product_promote_storage_type<DiagonalShape,B, ProductTag> { typedef B ret; };
template <int ProductTag> struct product_promote_storage_type<Dense, DiagonalShape, ProductTag> { typedef Dense ret; };
template <int ProductTag> struct product_promote_storage_type<DiagonalShape,Dense, ProductTag> { typedef Dense ret; };
/** \internal gives the plain matrix or array type to store a row/column/diagonal of a matrix type. /** \internal gives the plain matrix or array type to store a row/column/diagonal of a matrix type.
* \param Scalar optional parameter allowing to pass a different scalar type than the one of the MatrixType. * \param Scalar optional parameter allowing to pass a different scalar type than the one of the MatrixType.

View File

@ -31,12 +31,6 @@ namespace Eigen {
namespace internal { namespace internal {
template<> struct promote_storage_type<Dense,Sparse>
{ typedef Sparse ret; };
template<> struct promote_storage_type<Sparse,Dense>
{ typedef Sparse ret; };
template<typename BinaryOp, typename Lhs, typename Rhs, typename Derived, template<typename BinaryOp, typename Lhs, typename Rhs, typename Derived,
typename _LhsStorageMode = typename traits<Lhs>::StorageKind, typename _LhsStorageMode = typename traits<Lhs>::StorageKind,
typename _RhsStorageMode = typename traits<Rhs>::StorageKind> typename _RhsStorageMode = typename traits<Rhs>::StorageKind>