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