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 
00043 namespace internal {
00044 template<typename MatrixType,int RowFactor,int ColFactor>
00045 struct traits<Replicate<MatrixType,RowFactor,ColFactor> >
00046  : traits<MatrixType>
00047 {
00048   typedef typename MatrixType::Scalar Scalar;
00049   typedef typename traits<MatrixType>::StorageKind StorageKind;
00050   typedef typename traits<MatrixType>::XprKind XprKind;
00051   typedef typename nested<MatrixType>::type MatrixTypeNested;
00052   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00053   enum {
00054     RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
00055                       ? Dynamic
00056                       : RowFactor * MatrixType::RowsAtCompileTime,
00057     ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
00058                       ? Dynamic
00059                       : ColFactor * MatrixType::ColsAtCompileTime,
00060    //FIXME we don't propagate the max sizes !!!
00061     MaxRowsAtCompileTime = RowsAtCompileTime,
00062     MaxColsAtCompileTime = ColsAtCompileTime,
00063     IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
00064                : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
00065                : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
00066     Flags = (_MatrixTypeNested::Flags & HereditaryBits & ~RowMajorBit) | (IsRowMajor ? RowMajorBit : 0),
00067     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
00068   };
00069 };
00070 }
00071 
00072 template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
00073   : public internal::dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type
00074 {
00075   public:
00076 
00077     typedef typename internal::dense_xpr_base<Replicate>::type Base;
00078     EIGEN_DENSE_PUBLIC_INTERFACE(Replicate)
00079 
00080     template<typename OriginalMatrixType>
00081     inline explicit Replicate(const OriginalMatrixType& matrix)
00082       : m_matrix(matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor)
00083     {
00084       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00085                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00086       eigen_assert(RowFactor!=Dynamic && ColFactor!=Dynamic);
00087     }
00088 
00089     template<typename OriginalMatrixType>
00090     inline Replicate(const OriginalMatrixType& matrix, int rowFactor, int colFactor)
00091       : m_matrix(matrix), m_rowFactor(rowFactor), m_colFactor(colFactor)
00092     {
00093       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
00094                           THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
00095     }
00096 
00097     inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); }
00098     inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); }
00099 
00100     inline Scalar coeff(Index row, Index col) const
00101     {
00102       // try to avoid using modulo; this is a pure optimization strategy
00103       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00104                             : RowFactor==1 ? row
00105                             : row%m_matrix.rows();
00106       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00107                             : ColFactor==1 ? col
00108                             : col%m_matrix.cols();
00109 
00110       return m_matrix.coeff(actual_row, actual_col);
00111     }
00112     template<int LoadMode>
00113     inline PacketScalar packet(Index row, Index col) const
00114     {
00115       const Index actual_row  = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
00116                             : RowFactor==1 ? row
00117                             : row%m_matrix.rows();
00118       const Index actual_col  = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
00119                             : ColFactor==1 ? col
00120                             : col%m_matrix.cols();
00121 
00122       return m_matrix.template packet<LoadMode>(actual_row, actual_col);
00123     }
00124 
00125 
00126   protected:
00127     const typename MatrixType::Nested m_matrix;
00128     const internal::variable_if_dynamic<Index, RowFactor> m_rowFactor;
00129     const internal::variable_if_dynamic<Index, ColFactor> m_colFactor;
00130 };
00131 
00140 template<typename Derived>
00141 template<int RowFactor, int ColFactor>
00142 inline const Replicate<Derived,RowFactor,ColFactor>
00143 DenseBase<Derived>::replicate() const
00144 {
00145   return Replicate<Derived,RowFactor,ColFactor>(derived());
00146 }
00147 
00156 template<typename Derived>
00157 inline const Replicate<Derived,Dynamic,Dynamic>
00158 DenseBase<Derived>::replicate(Index rowFactor,Index colFactor) const
00159 {
00160   return Replicate<Derived,Dynamic,Dynamic>(derived(),rowFactor,colFactor);
00161 }
00162 
00171 template<typename ExpressionType, int Direction>
00172 const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00173 VectorwiseOp<ExpressionType,Direction>::replicate(Index factor) const
00174 {
00175   return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
00176           (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
00177 }
00178 
00179 #endif // EIGEN_REPLICATE_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:22