Replicate.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_REPLICATE_H
00026 #define EIGEN_REPLICATE_H
00027 
00028 namespace Eigen { 
00029 
00045 namespace internal {
00046 template<typename MatrixType,int RowFactor,int ColFactor>
00047 struct traits<Replicate<MatrixType,RowFactor,ColFactor> >
00048  : traits<MatrixType>
00049 {
00050   typedef typename MatrixType::Scalar Scalar;
00051   typedef typename traits<MatrixType>::StorageKind StorageKind;
00052   typedef typename traits<MatrixType>::XprKind XprKind;
00053   enum {
00054     Factor = (RowFactor==Dynamic || ColFactor==Dynamic) ? Dynamic : RowFactor*ColFactor
00055   };
00056   typedef typename nested<MatrixType,Factor>::type MatrixTypeNested;
00057   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00058   enum {
00059     RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
00060                       ? Dynamic
00061                       : RowFactor * MatrixType::RowsAtCompileTime,
00062     ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
00063                       ? Dynamic
00064                       : ColFactor * MatrixType::ColsAtCompileTime,
00065    //FIXME we don't propagate the max sizes !!!
00066     MaxRowsAtCompileTime = RowsAtCompileTime,
00067     MaxColsAtCompileTime = ColsAtCompileTime,
00068     IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
00069                : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
00070                : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
00071     Flags = (_MatrixTypeNested::Flags & HereditaryBits & ~RowMajorBit) | (IsRowMajor ? RowMajorBit : 0),
00072     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
00073   };
00074 };
00075 }
00076 
00077 template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
00078   : public internal::dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type
00079 {
00080     typedef typename internal::traits<Replicate>::MatrixTypeNested MatrixTypeNested;
00081     typedef typename internal::traits<Replicate>::_MatrixTypeNested _MatrixTypeNested;
00082   public:
00083 
00084     typedef typename internal::dense_xpr_base<Replicate>::type Base;
00085     EIGEN_DENSE_PUBLIC_INTERFACE(Replicate)
00086 
00087     template<typename OriginalMatrixType>
00088     inline explicit Replicate(const OriginalMatrixType& matrix)
00089       : m_matrix(matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor)
00090     {
00091       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00092                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00093       eigen_assert(RowFactor!=Dynamic && ColFactor!=Dynamic);
00094     }
00095 
00096     template<typename OriginalMatrixType>
00097     inline Replicate(const OriginalMatrixType& matrix, Index rowFactor, Index colFactor)
00098       : m_matrix(matrix), m_rowFactor(rowFactor), m_colFactor(colFactor)
00099     {
00100       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00101                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00102     }
00103 
00104     inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); }
00105     inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); }
00106 
00107     inline Scalar coeff(Index row, Index col) const
00108     {
00109       // try to avoid using modulo; this is a pure optimization strategy
00110       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00111                             : RowFactor==1 ? row
00112                             : row%m_matrix.rows();
00113       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00114                             : ColFactor==1 ? col
00115                             : col%m_matrix.cols();
00116 
00117       return m_matrix.coeff(actual_row, actual_col);
00118     }
00119     template<int LoadMode>
00120     inline PacketScalar packet(Index row, Index col) const
00121     {
00122       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00123                             : RowFactor==1 ? row
00124                             : row%m_matrix.rows();
00125       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00126                             : ColFactor==1 ? col
00127                             : col%m_matrix.cols();
00128 
00129       return m_matrix.template packet<LoadMode>(actual_row, actual_col);
00130     }
00131 
00132     const _MatrixTypeNested& nestedExpression() const
00133     { 
00134       return m_matrix; 
00135     }
00136 
00137   protected:
00138     MatrixTypeNested m_matrix;
00139     const internal::variable_if_dynamic<Index, RowFactor> m_rowFactor;
00140     const internal::variable_if_dynamic<Index, ColFactor> m_colFactor;
00141 };
00142 
00151 template<typename Derived>
00152 template<int RowFactor, int ColFactor>
00153 inline const Replicate<Derived,RowFactor,ColFactor>
00154 DenseBase<Derived>::replicate() const
00155 {
00156   return Replicate<Derived,RowFactor,ColFactor>(derived());
00157 }
00158 
00167 template<typename Derived>
00168 inline const Replicate<Derived,Dynamic,Dynamic>
00169 DenseBase<Derived>::replicate(Index rowFactor,Index colFactor) const
00170 {
00171   return Replicate<Derived,Dynamic,Dynamic>(derived(),rowFactor,colFactor);
00172 }
00173 
00182 template<typename ExpressionType, int Direction>
00183 const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00184 VectorwiseOp<ExpressionType,Direction>::replicate(Index factor) const
00185 {
00186   return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00187           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00188 }
00189 
00190 } // end namespace Eigen
00191 
00192 #endif // EIGEN_REPLICATE_H