diff options
author | Gael Guennebaud <g.gael@free.fr> | 2010-06-19 23:36:38 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2010-06-19 23:36:38 +0200 |
commit | 7fd8418b199cda8b7facb37fccbe871375c5b6d2 (patch) | |
tree | 215d97917637683c78efb23dfa604687fb037f5e /Eigen/src/Core/Replicate.h | |
parent | 575ac5409c6f6a9af219cdff3374c40e8a2a120d (diff) |
finish to merge Array into Core:
- mv Array/* into Core/
- merge Functors.h files, and move Norms.h into Dot.h
Diffstat (limited to 'Eigen/src/Core/Replicate.h')
-rw-r--r-- | Eigen/src/Core/Replicate.h | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/Eigen/src/Core/Replicate.h b/Eigen/src/Core/Replicate.h new file mode 100644 index 000000000..c60d9903b --- /dev/null +++ b/Eigen/src/Core/Replicate.h @@ -0,0 +1,175 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2009-2010 Gael Guennebaud <g.gael@free.fr> +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_REPLICATE_H +#define EIGEN_REPLICATE_H + +/** \nonstableyet + * \class Replicate + * + * \brief Expression of the multiple replication of a matrix or vector + * + * \param MatrixType the type of the object we are replicating + * + * This class represents an expression of the multiple replication of a matrix or vector. + * It is the return type of DenseBase::replicate() and most of the time + * this is the only way it is used. + * + * \sa DenseBase::replicate() + */ +template<typename MatrixType,int RowFactor,int ColFactor> +struct ei_traits<Replicate<MatrixType,RowFactor,ColFactor> > + : ei_traits<MatrixType> +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename ei_traits<MatrixType>::StorageKind StorageKind; + typedef typename ei_traits<MatrixType>::XprKind XprKind; + typedef typename ei_nested<MatrixType>::type MatrixTypeNested; + typedef typename ei_unref<MatrixTypeNested>::type _MatrixTypeNested; + enum { + RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic + ? Dynamic + : RowFactor * MatrixType::RowsAtCompileTime, + ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic + ? Dynamic + : ColFactor * MatrixType::ColsAtCompileTime, + //FIXME we don't propagate the max sizes !!! + MaxRowsAtCompileTime = RowsAtCompileTime, + MaxColsAtCompileTime = ColsAtCompileTime, + IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1 + : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0 + : (MatrixType::Flags & RowMajorBit) ? 1 : 0, + Flags = (_MatrixTypeNested::Flags & HereditaryBits & ~RowMajorBit) | (IsRowMajor ? RowMajorBit : 0), + CoeffReadCost = _MatrixTypeNested::CoeffReadCost + }; +}; + +template<typename MatrixType,int RowFactor,int ColFactor> class Replicate + : public ei_dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type +{ + public: + + typedef typename ei_dense_xpr_base<Replicate>::type Base; + EIGEN_DENSE_PUBLIC_INTERFACE(Replicate) + + template<typename OriginalMatrixType> + inline explicit Replicate(const OriginalMatrixType& matrix) + : m_matrix(matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor) + { + EIGEN_STATIC_ASSERT((ei_is_same_type<MatrixType,OriginalMatrixType>::ret), + THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE) + ei_assert(RowFactor!=Dynamic && ColFactor!=Dynamic); + } + + template<typename OriginalMatrixType> + inline Replicate(const OriginalMatrixType& matrix, int rowFactor, int colFactor) + : m_matrix(matrix), m_rowFactor(rowFactor), m_colFactor(colFactor) + { + EIGEN_STATIC_ASSERT((ei_is_same_type<MatrixType,OriginalMatrixType>::ret), + THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE) + } + + inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); } + inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); } + + inline Scalar coeff(Index row, Index col) const + { + // try to avoid using modulo; this is a pure optimization strategy + const Index actual_row = ei_traits<MatrixType>::RowsAtCompileTime==1 ? 0 + : RowFactor==1 ? row + : row%m_matrix.rows(); + const Index actual_col = ei_traits<MatrixType>::ColsAtCompileTime==1 ? 0 + : ColFactor==1 ? col + : col%m_matrix.cols(); + + return m_matrix.coeff(actual_row, actual_col); + } + template<int LoadMode> + inline PacketScalar packet(Index row, Index col) const + { + const Index actual_row = ei_traits<MatrixType>::RowsAtCompileTime==1 ? 0 + : RowFactor==1 ? row + : row%m_matrix.rows(); + const Index actual_col = ei_traits<MatrixType>::ColsAtCompileTime==1 ? 0 + : ColFactor==1 ? col + : col%m_matrix.cols(); + + return m_matrix.template packet<LoadMode>(actual_row, actual_col); + } + + + protected: + const typename MatrixType::Nested m_matrix; + const ei_variable_if_dynamic<Index, RowFactor> m_rowFactor; + const ei_variable_if_dynamic<Index, ColFactor> m_colFactor; +}; + +/** \nonstableyet + * \return an expression of the replication of \c *this + * + * Example: \include MatrixBase_replicate.cpp + * Output: \verbinclude MatrixBase_replicate.out + * + * \sa VectorwiseOp::replicate(), DenseBase::replicate(Index,Index), class Replicate + */ +template<typename Derived> +template<int RowFactor, int ColFactor> +inline const Replicate<Derived,RowFactor,ColFactor> +DenseBase<Derived>::replicate() const +{ + return Replicate<Derived,RowFactor,ColFactor>(derived()); +} + +/** \nonstableyet + * \return an expression of the replication of \c *this + * + * Example: \include MatrixBase_replicate_int_int.cpp + * Output: \verbinclude MatrixBase_replicate_int_int.out + * + * \sa VectorwiseOp::replicate(), DenseBase::replicate<int,int>(), class Replicate + */ +template<typename Derived> +inline const Replicate<Derived,Dynamic,Dynamic> +DenseBase<Derived>::replicate(Index rowFactor,Index colFactor) const +{ + return Replicate<Derived,Dynamic,Dynamic>(derived(),rowFactor,colFactor); +} + +/** \nonstableyet + * \return an expression of the replication of each column (or row) of \c *this + * + * Example: \include DirectionWise_replicate_int.cpp + * Output: \verbinclude DirectionWise_replicate_int.out + * + * \sa VectorwiseOp::replicate(), DenseBase::replicate(), class Replicate + */ +template<typename ExpressionType, int Direction> +const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType +VectorwiseOp<ExpressionType,Direction>::replicate(Index factor) const +{ + return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType + (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1); +} + +#endif // EIGEN_REPLICATE_H |