11#ifndef EIGEN_CONCAT_OP_H
12#define EIGEN_CONCAT_OP_H
15#include "./InternalHeaderCheck.h"
21template <
int Direction,
typename LhsType,
typename RhsType>
22struct traits<Concat<Direction, LhsType, RhsType>> : traits<LhsType> {
23 using Scalar =
typename LhsType::Scalar;
24 using StorageKind =
typename traits<LhsType>::StorageKind;
25 using XprKind =
typename traits<LhsType>::XprKind;
26 using LhsTypeNested =
typename ref_selector<LhsType>::type;
27 using RhsTypeNested =
typename ref_selector<RhsType>::type;
28 using LhsTypeNested_ = std::remove_reference_t<LhsTypeNested>;
29 using RhsTypeNested_ = std::remove_reference_t<RhsTypeNested>;
33 LhsRows = int(LhsType::RowsAtCompileTime),
34 RhsRows = int(RhsType::RowsAtCompileTime),
35 LhsCols = int(LhsType::ColsAtCompileTime),
36 RhsCols = int(RhsType::ColsAtCompileTime),
38 RowsAtCompileTime = Direction ==
Vertical
39 ? (LhsRows == Dynamic || RhsRows == Dynamic ? int(Dynamic) : LhsRows + RhsRows)
40 : size_prefer_fixed(LhsRows, RhsRows),
42 ? (LhsCols == Dynamic || RhsCols == Dynamic ? int(Dynamic) : LhsCols + RhsCols)
43 : size_prefer_fixed(LhsCols, RhsCols),
45 LhsMaxRows = int(LhsType::MaxRowsAtCompileTime),
46 RhsMaxRows = int(RhsType::MaxRowsAtCompileTime),
47 LhsMaxCols = int(LhsType::MaxColsAtCompileTime),
48 RhsMaxCols = int(RhsType::MaxColsAtCompileTime),
50 MaxRowsAtCompileTime =
52 ? (LhsMaxRows == Dynamic || RhsMaxRows == Dynamic ? int(Dynamic) : LhsMaxRows + RhsMaxRows)
53 : max_size_prefer_dynamic(LhsMaxRows, RhsMaxRows),
54 MaxColsAtCompileTime =
56 ? (LhsMaxCols == Dynamic || RhsMaxCols == Dynamic ? int(Dynamic) : LhsMaxCols + RhsMaxCols)
57 : max_size_prefer_dynamic(LhsMaxCols, RhsMaxCols),
59 IsRowMajor = MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1 ? 1
60 : MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1 ? 0
86template <
int Direction,
typename LhsType,
typename RhsType>
87class Concat :
public internal::dense_xpr_base<Concat<Direction, LhsType, RhsType>>::type {
88 using LhsTypeNested =
typename internal::traits<Concat>::LhsTypeNested;
89 using RhsTypeNested =
typename internal::traits<Concat>::RhsTypeNested;
90 using LhsTypeNested_ =
typename internal::traits<Concat>::LhsTypeNested_;
91 using RhsTypeNested_ =
typename internal::traits<Concat>::RhsTypeNested_;
94 using Base =
typename internal::dense_xpr_base<Concat>::type;
95 EIGEN_DENSE_PUBLIC_INTERFACE(Concat)
96 using LhsNestedExpression = internal::remove_all_t<LhsType>;
97 using RhsNestedExpression = internal::remove_all_t<RhsType>;
99 template <
typename OriginalLhsType,
typename OriginalRhsType>
100 EIGEN_DEVICE_FUNC
constexpr inline Concat(
const OriginalLhsType& lhs,
const OriginalRhsType& rhs)
101 : m_lhs(lhs), m_rhs(rhs) {
102 EIGEN_STATIC_ASSERT((std::is_same<std::remove_const_t<LhsType>, OriginalLhsType>::value),
103 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
104 EIGEN_STATIC_ASSERT((std::is_same<std::remove_const_t<RhsType>, OriginalRhsType>::value),
105 THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE)
107 (std::is_same<typename LhsType::Scalar, typename RhsType::Scalar>::value),
108 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
109 EIGEN_STATIC_ASSERT_SAME_XPR_KIND(LhsType, RhsType)
112 EIGEN_INTERNAL_CHECK_SHAPE(
113 (Direction ==
Vertical ? lhs.cols() == rhs.cols() : lhs.rows() == rhs.rows()) &&
114 (Direction ==
Vertical ?
"vcat: number of columns must match" :
"hcat: number of rows must match"),
115 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES,
116 std::conditional_t<Direction ==
Vertical, internal::matching_cols<LhsType, RhsType>,
117 internal::matching_rows<LhsType, RhsType>>);
120 EIGEN_DEVICE_FUNC
constexpr Index rows()
const {
121 return Direction ==
Vertical ? m_lhs.rows() + m_rhs.rows() : m_lhs.rows();
123 EIGEN_DEVICE_FUNC
constexpr Index cols()
const {
124 return Direction ==
Horizontal ? m_lhs.cols() + m_rhs.cols() : m_lhs.cols();
127 EIGEN_DEVICE_FUNC
constexpr const LhsTypeNested_& lhs()
const {
return m_lhs; }
128 EIGEN_DEVICE_FUNC
constexpr const RhsTypeNested_& rhs()
const {
return m_rhs; }
138template <
int Direction,
typename LhsType,
typename RhsType>
139struct evaluator<
Concat<Direction, LhsType, RhsType>> : evaluator_base<Concat<Direction, LhsType, RhsType>> {
141 using CoeffReturnType =
typename XprType::CoeffReturnType;
143 using LhsNested =
typename nested_eval<LhsType, 1>::type;
144 using RhsNested =
typename nested_eval<RhsType, 1>::type;
145 using LhsNestedCleaned = remove_all_t<LhsNested>;
146 using RhsNestedCleaned = remove_all_t<RhsNested>;
149 CoeffReadCost = plain_enum_max(evaluator<LhsNestedCleaned>::CoeffReadCost,
150 evaluator<RhsNestedCleaned>::CoeffReadCost) +
151 NumTraits<typename XprType::Scalar>::AddCost,
152 LhsFlags = evaluator<LhsNestedCleaned>::Flags,
153 RhsFlags = evaluator<RhsNestedCleaned>::Flags,
154 IsRowMajor = int(traits<XprType>::Flags) &
RowMajorBit,
155 IsVectorAtCompileTime = XprType::IsVectorAtCompileTime,
160 LhsOrderAgrees = (bool(
int(LhsFlags) &
RowMajorBit) == bool(IsRowMajor)) || LhsNestedCleaned::IsVectorAtCompileTime,
161 RhsOrderAgrees = (
bool(
int(RhsFlags) &
RowMajorBit) ==
bool(IsRowMajor)) || RhsNestedCleaned::IsVectorAtCompileTime,
162 BothHavePacketAccess = (int(LhsFlags) & int(RhsFlags) &
PacketAccessBit) && LhsOrderAgrees && RhsOrderAgrees,
163 BothHaveLinearAccess =
bool(
int(LhsFlags) &
int(RhsFlags) &
LinearAccessBit),
169 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE
explicit evaluator(
const XprType& xpr)
174 m_lhsRows(xpr.lhs().rows()),
175 m_lhsCols(xpr.lhs().cols()) {}
177 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col)
const {
178 EIGEN_IF_CONSTEXPR (Direction == Vertical) {
179 if (row < m_lhsRows.value())
180 return m_lhsImpl.coeff(row, col);
182 return m_rhsImpl.coeff(row - m_lhsRows.value(), col);
184 if (col < m_lhsCols.value())
185 return m_lhsImpl.coeff(row, col);
187 return m_rhsImpl.coeff(row, col - m_lhsCols.value());
191 EIGEN_DEVICE_FUNC
constexpr EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
192 const Index boundary = m_lhsRows.value() * m_lhsCols.value();
193 if (index < boundary)
194 return m_lhsImpl.coeff(index);
196 return m_rhsImpl.coeff(index - boundary);
199 template <
int LoadMode,
typename PacketType>
200 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index row, Index col)
const {
201 constexpr int packetSize = unpacket_traits<PacketType>::size;
202 EIGEN_IF_CONSTEXPR (Direction == Vertical) {
203 const Index boundary = m_lhsRows.value();
204 if (row >= boundary)
return m_rhsImpl.template packet<LoadMode, PacketType>(row - boundary, col);
207 EIGEN_IF_CONSTEXPR (!IsRowMajor) {
208 if (row + packetSize > boundary)
return packetBoundary<LoadMode, PacketType>(row, col);
210 return m_lhsImpl.template packet<LoadMode, PacketType>(row, col);
212 const Index boundary = m_lhsCols.value();
213 if (col >= boundary)
return m_rhsImpl.template packet<LoadMode, PacketType>(row, col - boundary);
216 EIGEN_IF_CONSTEXPR (IsRowMajor) {
217 if (col + packetSize > boundary)
return packetBoundary<LoadMode, PacketType>(row, col);
219 return m_lhsImpl.template packet<LoadMode, PacketType>(row, col);
223 template <
int LoadMode,
typename PacketType>
224 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index index)
const {
225 constexpr int packetSize = unpacket_traits<PacketType>::size;
226 const Index boundary = m_lhsRows.value() * m_lhsCols.value();
227 if (index >= boundary)
return m_rhsImpl.template packet<LoadMode, PacketType>(index - boundary);
228 if (index + packetSize > boundary)
return packetBoundaryLinear<LoadMode, PacketType>(index);
229 return m_lhsImpl.template packet<LoadMode, PacketType>(index);
232 template <
int LoadMode,
typename PacketType>
233 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index row, Index col, Index begin, Index count)
const {
234 EIGEN_IF_CONSTEXPR (Direction == Vertical) {
235 const Index boundary = m_lhsRows.value();
237 return m_rhsImpl.template packetSegment<LoadMode, PacketType>(row - boundary, col, begin, count);
238 EIGEN_IF_CONSTEXPR (!IsRowMajor) {
239 if (row + begin + count > boundary)
return packetSegmentBoundary<LoadMode, PacketType>(row, col, begin, count);
241 return m_lhsImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count);
243 const Index boundary = m_lhsCols.value();
245 return m_rhsImpl.template packetSegment<LoadMode, PacketType>(row, col - boundary, begin, count);
246 EIGEN_IF_CONSTEXPR (IsRowMajor) {
247 if (col + begin + count > boundary)
return packetSegmentBoundary<LoadMode, PacketType>(row, col, begin, count);
249 return m_lhsImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count);
253 template <
int LoadMode,
typename PacketType>
254 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegment(Index index, Index begin, Index count)
const {
255 const Index boundary = m_lhsRows.value() * m_lhsCols.value();
256 if (index >= boundary)
257 return m_rhsImpl.template packetSegment<LoadMode, PacketType>(index - boundary, begin, count);
258 if (index + begin + count > boundary)
return packetSegmentBoundaryLinear<LoadMode, PacketType>(index, begin, count);
259 return m_lhsImpl.template packetSegment<LoadMode, PacketType>(index, begin, count);
263 using Scalar =
typename XprType::Scalar;
265 template <
int LoadMode,
typename PacketType>
266 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetBoundary(Index row, Index col)
const {
267 constexpr int packetSize = unpacket_traits<PacketType>::size;
268 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<PacketType>::alignment) Scalar tmp[packetSize];
269 for (
int i = 0; i < packetSize; ++i)
270 tmp[i] = coeff(row + (Direction == Vertical ? i : 0), col + (Direction == Horizontal ? i : 0));
271 return pload<PacketType>(tmp);
274 template <
int LoadMode,
typename PacketType>
275 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetBoundaryLinear(Index index)
const {
276 constexpr int packetSize = unpacket_traits<PacketType>::size;
277 EIGEN_ALIGN_TO_BOUNDARY(unpacket_traits<PacketType>::alignment) Scalar tmp[packetSize];
278 for (
int i = 0; i < packetSize; ++i) tmp[i] = coeff(index + i);
279 return pload<PacketType>(tmp);
282 template <
int LoadMode,
typename PacketType>
283 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegmentBoundary(Index row, Index col, Index begin,
285 constexpr int packetSize = unpacket_traits<PacketType>::size;
286 EIGEN_ALIGN_MAX Scalar tmp[packetSize];
287 for (Index i = begin; i < begin + count; ++i)
288 tmp[i] = coeff(row + (Direction == Vertical ? i : 0), col + (Direction == Horizontal ? i : 0));
289 return ploadSegment<PacketType>(tmp, begin, count);
292 template <
int LoadMode,
typename PacketType>
293 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegmentBoundaryLinear(Index index, Index begin,
295 constexpr int packetSize = unpacket_traits<PacketType>::size;
296 EIGEN_ALIGN_MAX Scalar tmp[packetSize];
297 for (Index i = begin; i < begin + count; ++i) tmp[i] = coeff(index + i);
298 return ploadSegment<PacketType>(tmp, begin, count);
303 evaluator<LhsNestedCleaned> m_lhsImpl;
304 evaluator<RhsNestedCleaned> m_rhsImpl;
305 const variable_if_dynamic<Index, LhsType::RowsAtCompileTime> m_lhsRows;
306 const variable_if_dynamic<Index, LhsType::ColsAtCompileTime> m_lhsCols;
325template <
typename Lhs,
typename Rhs>
327 return Concat<Horizontal, Lhs, Rhs>(lhs.derived(), rhs.derived());
344template <
typename Lhs,
typename Rhs>
346 return Concat<Vertical, Lhs, Rhs>(lhs.derived(), rhs.derived());
Expression of the concatenation of two dense expressions.
Definition ConcatOp.h:87
const Concat< Vertical, Lhs, Rhs > vcat(const DenseBase< Lhs > &lhs, const DenseBase< Rhs > &rhs)
Definition ConcatOp.h:345
const Concat< Horizontal, Lhs, Rhs > hcat(const DenseBase< Lhs > &lhs, const DenseBase< Rhs > &rhs)
Definition ConcatOp.h:326
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
@ Horizontal
Definition Constants.h:270
@ Vertical
Definition Constants.h:267
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int RowMajorBit
Definition Constants.h:71