26 template<
typename MatrixType,
int NumBlocks>
27 struct traits<BlockDiag<MatrixType,NumBlocks> >
30 typedef typename MatrixType::Scalar Scalar;
31 typedef typename traits<MatrixType>::StorageKind StorageKind;
32 typedef typename traits<MatrixType>::XprKind XprKind;
33 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
34 typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
36 RowsAtCompileTime = NumBlocks==Dynamic || int(MatrixType::RowsAtCompileTime)==Dynamic
38 : NumBlocks * MatrixType::RowsAtCompileTime,
39 ColsAtCompileTime = NumBlocks==Dynamic || int(MatrixType::ColsAtCompileTime)==Dynamic
41 : NumBlocks * MatrixType::ColsAtCompileTime,
43 MaxRowsAtCompileTime = RowsAtCompileTime,
44 MaxColsAtCompileTime = ColsAtCompileTime,
45 IsRowMajor = MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1 ? 1
46 : MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1 ? 0
47 : (MatrixType::Flags & RowMajorBit) ? 1 : 0,
50 Flags = IsRowMajor ? RowMajorBit : 0
55 template<
typename MatrixType,
int NumBlocks>
class BlockDiag
56 :
public internal::dense_xpr_base< BlockDiag<MatrixType,NumBlocks> >::type
58 typedef typename internal::traits<BlockDiag>::MatrixTypeNested MatrixTypeNested;
59 typedef typename internal::traits<BlockDiag>::_MatrixTypeNested _MatrixTypeNested;
62 typedef typename internal::dense_xpr_base<BlockDiag>::type Base;
64 typedef typename internal::remove_all<MatrixType>::type NestedExpression;
66 template<
typename OriginalMatrixType>
68 inline explicit BlockDiag(
const OriginalMatrixType& a_matrix)
69 : m_matrix(a_matrix), m_numBlocks(NumBlocks)
71 EIGEN_STATIC_ASSERT((internal::is_same<
typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
72 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
73 eigen_assert(NumBlocks!=Dynamic);
76 template<
typename OriginalMatrixType>
78 inline BlockDiag(
const OriginalMatrixType& a_matrix, Index numBlocks)
79 : m_matrix(a_matrix), m_numBlocks(numBlocks)
81 EIGEN_STATIC_ASSERT((internal::is_same<
typename internal::remove_const<MatrixType>::type,OriginalMatrixType>::value),
82 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
86 inline Index rows()
const {
return m_matrix.rows() * m_numBlocks.value(); }
89 inline Index cols()
const {
return m_matrix.cols() * m_numBlocks.value(); }
123 const _MatrixTypeNested& nestedExpression()
const
129 MatrixTypeNested m_matrix;
130 const internal::variable_if_dynamic<Index, NumBlocks> m_numBlocks;
134 template<
typename ArgType,
int NumBlocks>
135 struct unary_evaluator<
BlockDiag<ArgType, NumBlocks> >
136 : evaluator_base<BlockDiag<ArgType, NumBlocks> >
139 typedef typename XprType::CoeffReturnType CoeffReturnType;
141 Factor = (NumBlocks==Dynamic) ? Dynamic : NumBlocks*NumBlocks
143 typedef typename internal::nested_eval<ArgType,Factor>::type ArgTypeNested;
144 typedef typename internal::remove_all<ArgTypeNested>::type ArgTypeNestedCleaned;
147 CoeffReadCost = evaluator<ArgTypeNestedCleaned>::CoeffReadCost,
148 LinearAccessMask = XprType::IsVectorAtCompileTime ? LinearAccessBit : 0,
149 Flags = (evaluator<ArgTypeNestedCleaned>::Flags & (HereditaryBits|LinearAccessMask) & ~RowMajorBit) | (traits<XprType>::Flags & RowMajorBit),
151 Alignment = evaluator<ArgTypeNestedCleaned>::Alignment
154 EIGEN_DEVICE_FUNC
explicit unary_evaluator(
const XprType& xpr)
155 : m_arg(xpr.nestedExpression()),
157 m_rows(xpr.nestedExpression().rows()),
158 m_cols(xpr.nestedExpression().cols())
161 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
162 CoeffReturnType coeff(Index rowId, Index colId)
const
164 if ( rowId / m_rows.value() != colId / m_cols.value() )
165 return CoeffReturnType(0);
167 const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
168 : NumBlocks==1 ? rowId
169 : rowId%m_rows.value();
170 const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
171 : NumBlocks==1 ? colId
172 : colId%m_cols.value();
174 return m_argImpl.coeff(actual_row, actual_col);
177 template<
int LoadMode,
typename PacketType>
179 PacketType packet(Index rowId, Index colId)
const
181 assert( rowId / m_rows.value() != colId / m_cols.value() &&
184 const Index actual_row = internal::traits<XprType>::RowsAtCompileTime==1 ? 0
185 : NumBlocks==1 ? rowId
186 : rowId%m_rows.value();
187 const Index actual_col = internal::traits<XprType>::ColsAtCompileTime==1 ? 0
188 : NumBlocks==1 ? colId
189 : colId%m_cols.value();
191 return m_argImpl.template packet<LoadMode>(actual_row, actual_col);
196 evaluator<ArgTypeNestedCleaned> m_argImpl;
197 const variable_if_dynamic<Index, ArgType::RowsAtCompileTime> m_rows;
198 const variable_if_dynamic<Index, ArgType::ColsAtCompileTime> m_cols;
211 template<
typename Derived>
212 const typename MatrixBase<Derived>::BlockDiagReturnType
213 MatrixBase<Derived>::blockDiag(Index numBlocks)
const
215 return BlockDiag<Derived,Dynamic>(derived(),numBlocks);
Expression for block diagonal replication of a matrix or vector.
Definition: BlockDiag.h:55