Replicate.h
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
13namespace Eigen {
14
29
30namespace internal {
31template<typename MatrixType,int RowFactor,int ColFactor>
32struct traits<Replicate<MatrixType,RowFactor,ColFactor> >
33 : traits<MatrixType>
34{
35 typedef typename MatrixType::Scalar Scalar;
36 typedef typename traits<MatrixType>::StorageKind StorageKind;
37 typedef typename traits<MatrixType>::XprKind XprKind;
38 enum {
39 Factor = (RowFactor==Dynamic || ColFactor==Dynamic) ? Dynamic : RowFactor*ColFactor
40 };
41 typedef typename nested<MatrixType,Factor>::type MatrixTypeNested;
42 typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
43 enum {
44 RowsAtCompileTime = RowFactor==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
45 ? Dynamic
46 : RowFactor * MatrixType::RowsAtCompileTime,
47 ColsAtCompileTime = ColFactor==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
48 ? Dynamic
49 : ColFactor * MatrixType::ColsAtCompileTime,
50 //FIXME we don't propagate the max sizes !!!
51 MaxRowsAtCompileTime = RowsAtCompileTime,
52 MaxColsAtCompileTime = ColsAtCompileTime,
53 IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
54 : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
55 : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
56 Flags = (_MatrixTypeNested::Flags & HereditaryBits & ~RowMajorBit) | (IsRowMajor ? RowMajorBit : 0),
57 CoeffReadCost = _MatrixTypeNested::CoeffReadCost
58 };
59};
60}
61
62template<typename MatrixType,int RowFactor,int ColFactor> class Replicate
63 : public internal::dense_xpr_base< Replicate<MatrixType,RowFactor,ColFactor> >::type
64{
65 typedef typename internal::traits<Replicate>::MatrixTypeNested MatrixTypeNested;
66 typedef typename internal::traits<Replicate>::_MatrixTypeNested _MatrixTypeNested;
67 public:
68
69 typedef typename internal::dense_xpr_base<Replicate>::type Base;
70 EIGEN_DENSE_PUBLIC_INTERFACE(Replicate)
71
72 template<typename OriginalMatrixType>
73 inline explicit Replicate(const OriginalMatrixType& matrix)
74 : m_matrix(matrix), m_rowFactor(RowFactor), m_colFactor(ColFactor)
75 {
76 EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
77 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
78 eigen_assert(RowFactor!=Dynamic && ColFactor!=Dynamic);
79 }
80
81 template<typename OriginalMatrixType>
82 inline Replicate(const OriginalMatrixType& matrix, Index rowFactor, Index colFactor)
83 : m_matrix(matrix), m_rowFactor(rowFactor), m_colFactor(colFactor)
84 {
85 EIGEN_STATIC_ASSERT((internal::is_same<typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
86 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
87 }
88
89 inline Index rows() const { return m_matrix.rows() * m_rowFactor.value(); }
90 inline Index cols() const { return m_matrix.cols() * m_colFactor.value(); }
91
92 inline Scalar coeff(Index row, Index col) const
93 {
94 // try to avoid using modulo; this is a pure optimization strategy
95 const Index actual_row = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
96 : RowFactor==1 ? row
97 : row%m_matrix.rows();
98 const Index actual_col = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
99 : ColFactor==1 ? col
100 : col%m_matrix.cols();
101
102 return m_matrix.coeff(actual_row, actual_col);
103 }
104 template<int LoadMode>
105 inline PacketScalar packet(Index row, Index col) const
106 {
107 const Index actual_row = internal::traits<MatrixType>::RowsAtCompileTime==1 ? 0
108 : RowFactor==1 ? row
109 : row%m_matrix.rows();
110 const Index actual_col = internal::traits<MatrixType>::ColsAtCompileTime==1 ? 0
111 : ColFactor==1 ? col
112 : col%m_matrix.cols();
113
114 return m_matrix.template packet<LoadMode>(actual_row, actual_col);
115 }
116
117 const _MatrixTypeNested& nestedExpression() const
118 {
119 return m_matrix;
120 }
121
122 protected:
123 MatrixTypeNested m_matrix;
124 const internal::variable_if_dynamic<Index, RowFactor> m_rowFactor;
125 const internal::variable_if_dynamic<Index, ColFactor> m_colFactor;
126};
127
136template<typename Derived>
137template<int RowFactor, int ColFactor>
143
152template<typename Derived>
154DenseBase<Derived>::replicate(Index rowFactor,Index colFactor) const
155{
156 return Replicate<Derived,Dynamic,Dynamic>(derived(),rowFactor,colFactor);
157}
158
167template<typename ExpressionType, int Direction>
168const typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
170{
171 return typename VectorwiseOp<ExpressionType,Direction>::ReplicateReturnType
172 (_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
173}
174
175} // end namespace Eigen
176
177#endif // EIGEN_REPLICATE_H
const Replicate< Derived, RowFactor, ColFactor > replicate() const
Definition Replicate.h:139
internal::traits< Derived >::Index Index
The type of indices.
Definition DenseBase.h:51
Expression of the multiple replication of a matrix or vector.
Definition Replicate.h:64
const ReplicateReturnType replicate(Index factor) const
Definition Replicate.h:169
@ Vertical
Definition Constants.h:204
@ Horizontal
Definition Constants.h:207
const unsigned int RowMajorBit
Definition Constants.h:48
Definition LDLT.h:18