Replicate.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_REPLICATE_H
  10. #define EIGEN_REPLICATE_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename MatrixType,int RowFactor,int ColFactor>
  14. struct traits<Replicate<MatrixType,RowFactor,ColFactor> >
  15. : traits<MatrixType>
  16. {
  17. typedef typename MatrixType::Scalar Scalar;
  18. typedef typename traits<MatrixType>::StorageKind StorageKind;
  19. typedef typename traits<MatrixType>::XprKind XprKind;
  20. typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
  21. typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
  22. enum {
  23. RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
  24. ? Dynamic
  25. : RowFactor * MatrixType::RowsAtCompileTime,
  26. ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
  27. ? Dynamic
  28. : ColFactor * MatrixType::ColsAtCompileTime,
  29. //FIXME we don't propagate the max sizes !!!
  30. MaxRowsAtCompileTime = RowsAtCompileTime,
  31. MaxColsAtCompileTime = ColsAtCompileTime,
  32. IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
  33. : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
  34. : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
  35. // FIXME enable DirectAccess with negative strides?
  36. Flags = IsRowMajor ? RowMajorBit : 0
  37. };
  38. };
  39. }
  40. /**
  41. * \class Replicate
  42. * \ingroup Core_Module
  43. *
  44. * \brief Expression of the multiple replication of a matrix or vector
  45. *
  46. * \tparam MatrixType the type of the object we are replicating
  47. * \tparam RowFactor number of repetitions at compile time along the vertical direction, can be Dynamic.
  48. * \tparam ColFactor number of repetitions at compile time along the horizontal direction, can be Dynamic.
  49. *
  50. * This class represents an expression of the multiple replication of a matrix or vector.
  51. * It is the return type of DenseBase::replicate() and most of the time
  52. * this is the only way it is used.
  53. *
  54. * \sa DenseBase::replicate()
  55. */
  56. template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
  57. : public internal::dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type
  58. {
  59. typedef typename internal::traits<Replicate>::MatrixTypeNested MatrixTypeNested;
  60. typedef typename internal::traits<Replicate>::_MatrixTypeNested _MatrixTypeNested;
  61. public:
  62. typedef typename internal::dense_xpr_base<Replicate>::type Base;
  63. EIGEN_DENSE_PUBLIC_INTERFACE(Replicate)
  64. typedef typename internal::remove_all<MatrixType>::type NestedExpression;
  65. template<typename OriginalMatrixType>
  66. EIGEN_DEVICE_FUNC
  67. inline explicit Replicate(const OriginalMatrixType& matrix)
  68. : m_matrix(matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor)
  69. {
  70. EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
  71. THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
  72. eigen_assert(RowFactor!=Dynamic && ColFactor!=Dynamic);
  73. }
  74. template<typename OriginalMatrixType>
  75. EIGEN_DEVICE_FUNC
  76. inline Replicate(const OriginalMatrixType& matrix, Index rowFactor, Index colFactor)
  77. : m_matrix(matrix), m_rowFactor(rowFactor), m_colFactor(colFactor)
  78. {
  79. EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
  80. THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
  81. }
  82. EIGEN_DEVICE_FUNC
  83. inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); }
  84. EIGEN_DEVICE_FUNC
  85. inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); }
  86. EIGEN_DEVICE_FUNC
  87. const _MatrixTypeNested& nestedExpression() const
  88. {
  89. return m_matrix;
  90. }
  91. protected:
  92. MatrixTypeNested m_matrix;
  93. const internal::variable_if_dynamic<Index, RowFactor> m_rowFactor;
  94. const internal::variable_if_dynamic<Index, ColFactor> m_colFactor;
  95. };
  96. /**
  97. * \return an expression of the replication of \c *this
  98. *
  99. * Example: \include MatrixBase_replicate.cpp
  100. * Output: \verbinclude MatrixBase_replicate.out
  101. *
  102. * \sa VectorwiseOp::replicate(), DenseBase::replicate(Index,Index), class Replicate
  103. */
  104. template<typename Derived>
  105. template<int RowFactor, int ColFactor>
  106. const Replicate<Derived,RowFactor,ColFactor>
  107. DenseBase<Derived>::replicate() const
  108. {
  109. return Replicate<Derived,RowFactor,ColFactor>(derived());
  110. }
  111. /**
  112. * \return an expression of the replication of each column (or row) of \c *this
  113. *
  114. * Example: \include DirectionWise_replicate_int.cpp
  115. * Output: \verbinclude DirectionWise_replicate_int.out
  116. *
  117. * \sa VectorwiseOp::replicate(), DenseBase::replicate(), class Replicate
  118. */
  119. template<typename ExpressionType, int Direction>
  120. const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
  121. VectorwiseOp<ExpressionType,Direction>::replicate(Index factor) const
  122. {
  123. return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
  124. (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
  125. }
  126. } // end namespace Eigen
  127. #endif // EIGEN_REPLICATE_H