Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Replicate.h Source File

Replicate.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_REPLICATE_H
00011 #define EIGEN_REPLICATE_H
00012 
00013 namespace Eigen { 
00014 
00015 /**
00016   * \class Replicate
00017   * \ingroup Core_Module
00018   *
00019   * \brief Expression of the multiple replication of a matrix or vector
00020   *
00021   * \param MatrixType the type of the object we are replicating
00022   *
00023   * This class represents an expression of the multiple replication of a matrix or vector.
00024   * It is the return type of DenseBase::replicate() and most of the time
00025   * this is the only way it is used.
00026   *
00027   * \sa DenseBase::replicate()
00028   */
00029 
00030 namespace internal {
00031 template<typename MatrixType,int RowFactor,int ColFactor>
00032 struct traits<Replicate<MatrixType,RowFactor,ColFactor> >
00033  : traits<MatrixType>
00034 {
00035   typedef typename MatrixType::Scalar Scalar;
00036   typedef typename traits<MatrixType>::StorageKind StorageKind;
00037   typedef typename traits<MatrixType>::XprKind XprKind;
00038   enum {
00039     Factor = (RowFactor==Dynamic || ColFactor==Dynamic) ? Dynamic : RowFactor*ColFactor
00040   };
00041   typedef typename nested<MatrixType,Factor>::type MatrixTypeNested;
00042   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00043   enum {
00044     RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
00045                       ? Dynamic
00046                       : RowFactor * MatrixType::RowsAtCompileTime,
00047     ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
00048                       ? Dynamic
00049                       : ColFactor * MatrixType::ColsAtCompileTime,
00050    //FIXME we don't propagate the max sizes !!!
00051     MaxRowsAtCompileTime = RowsAtCompileTime,
00052     MaxColsAtCompileTime = ColsAtCompileTime,
00053     IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
00054                : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
00055                : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
00056     Flags = (_MatrixTypeNested::Flags & HereditaryBits & ~RowMajorBit) | (IsRowMajor ? RowMajorBit : 0),
00057     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
00058   };
00059 };
00060 }
00061 
00062 template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
00063   : public internal::dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type
00064 {
00065     typedef typename internal::traits<Replicate>::MatrixTypeNested MatrixTypeNested;
00066     typedef typename internal::traits<Replicate>::_MatrixTypeNested _MatrixTypeNested;
00067   public:
00068 
00069     typedef typename internal::dense_xpr_base<Replicate>::type Base;
00070     EIGEN_DENSE_PUBLIC_INTERFACE(Replicate)
00071 
00072     template<typename OriginalMatrixType>
00073     inline explicit Replicate(const OriginalMatrixType& a_matrix)
00074       : m_matrix(a_matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor)
00075     {
00076       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00077                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00078       eigen_assert(RowFactor!=Dynamic && ColFactor!=Dynamic);
00079     }
00080 
00081     template<typename OriginalMatrixType>
00082     inline Replicate(const OriginalMatrixType& a_matrix, Index rowFactor, Index colFactor)
00083       : m_matrix(a_matrix), m_rowFactor(rowFactor), m_colFactor(colFactor)
00084     {
00085       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00086                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00087     }
00088 
00089     inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); }
00090     inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); }
00091 
00092     inline Scalar coeff(Index rowId, Index colId) const
00093     {
00094       // try to avoid using modulo; this is a pure optimization strategy
00095       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00096                             : RowFactor==1 ? rowId
00097                             : rowId%m_matrix.rows();
00098       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00099                             : ColFactor==1 ? colId
00100                             : colId%m_matrix.cols();
00101 
00102       return m_matrix.coeff(actual_row, actual_col);
00103     }
00104     template<int LoadMode>
00105     inline PacketScalar packet(Index rowId, Index colId) const
00106     {
00107       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00108                             : RowFactor==1 ? rowId
00109                             : rowId%m_matrix.rows();
00110       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00111                             : ColFactor==1 ? colId
00112                             : colId%m_matrix.cols();
00113 
00114       return m_matrix.template packet<LoadMode>(actual_row, actual_col);
00115     }
00116 
00117     const _MatrixTypeNested& nestedExpression() const
00118     { 
00119       return m_matrix; 
00120     }
00121 
00122   protected:
00123     MatrixTypeNested m_matrix;
00124     const internal::variable_if_dynamic<Index, RowFactor> m_rowFactor;
00125     const internal::variable_if_dynamic<Index, ColFactor> m_colFactor;
00126 };
00127 
00128 /**
00129   * \return an expression of the replication of \c *this
00130   *
00131   * Example: \include MatrixBase_replicate.cpp
00132   * Output: \verbinclude MatrixBase_replicate.out
00133   *
00134   * \sa VectorwiseOp::replicate(), DenseBase::replicate(Index,Index), class Replicate
00135   */
00136 template<typename Derived>
00137 template<int RowFactor, int ColFactor>
00138 const Replicate<Derived,RowFactor,ColFactor>
00139 DenseBase<Derived>::replicate() const
00140 {
00141   return Replicate<Derived,RowFactor,ColFactor>(derived());
00142 }
00143 
00144 /**
00145   * \return an expression of the replication of \c *this
00146   *
00147   * Example: \include MatrixBase_replicate_int_int.cpp
00148   * Output: \verbinclude MatrixBase_replicate_int_int.out
00149   *
00150   * \sa VectorwiseOp::replicate(), DenseBase::replicate<int,int>(), class Replicate
00151   */
00152 template<typename Derived>
00153 const typename DenseBase<Derived>::ReplicateReturnType
00154 DenseBase<Derived>::replicate(Index rowFactor,Index colFactor) const
00155 {
00156   return Replicate<Derived,Dynamic,Dynamic>(derived(),rowFactor,colFactor);
00157 }
00158 
00159 /**
00160   * \return an expression of the replication of each column (or row) of \c *this
00161   *
00162   * Example: \include DirectionWise_replicate_int.cpp
00163   * Output: \verbinclude DirectionWise_replicate_int.out
00164   *
00165   * \sa VectorwiseOp::replicate(), DenseBase::replicate(), class Replicate
00166   */
00167 template<typename ExpressionType, int Direction>
00168 const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00169 VectorwiseOp<ExpressionType,Direction>::replicate(Index factor) const
00170 {
00171   return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00172           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00173 }
00174 
00175 } // end namespace Eigen
00176 
00177 #endif // EIGEN_REPLICATE_H