Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ConcatOp.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2026 Pavel Guzenfeld
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_CONCAT_OP_H
12#define EIGEN_CONCAT_OP_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
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>;
30 enum {
31 // For vertical concat (stacking rows): rows add up, cols must match
32 // For horizontal concat (stacking cols): cols add up, rows must match
33 LhsRows = int(LhsType::RowsAtCompileTime),
34 RhsRows = int(RhsType::RowsAtCompileTime),
35 LhsCols = int(LhsType::ColsAtCompileTime),
36 RhsCols = int(RhsType::ColsAtCompileTime),
37
38 RowsAtCompileTime = Direction == Vertical
39 ? (LhsRows == Dynamic || RhsRows == Dynamic ? int(Dynamic) : LhsRows + RhsRows)
40 : size_prefer_fixed(LhsRows, RhsRows),
41 ColsAtCompileTime = Direction == Horizontal
42 ? (LhsCols == Dynamic || RhsCols == Dynamic ? int(Dynamic) : LhsCols + RhsCols)
43 : size_prefer_fixed(LhsCols, RhsCols),
44
45 LhsMaxRows = int(LhsType::MaxRowsAtCompileTime),
46 RhsMaxRows = int(RhsType::MaxRowsAtCompileTime),
47 LhsMaxCols = int(LhsType::MaxColsAtCompileTime),
48 RhsMaxCols = int(RhsType::MaxColsAtCompileTime),
49
50 MaxRowsAtCompileTime =
51 Direction == Vertical
52 ? (LhsMaxRows == Dynamic || RhsMaxRows == Dynamic ? int(Dynamic) : LhsMaxRows + RhsMaxRows)
53 : max_size_prefer_dynamic(LhsMaxRows, RhsMaxRows),
54 MaxColsAtCompileTime =
55 Direction == Horizontal
56 ? (LhsMaxCols == Dynamic || RhsMaxCols == Dynamic ? int(Dynamic) : LhsMaxCols + RhsMaxCols)
57 : max_size_prefer_dynamic(LhsMaxCols, RhsMaxCols),
58
59 IsRowMajor = MaxRowsAtCompileTime == 1 && MaxColsAtCompileTime != 1 ? 1
60 : MaxColsAtCompileTime == 1 && MaxRowsAtCompileTime != 1 ? 0
61 : (int(LhsType::Flags) & RowMajorBit) ? 1
62 : 0,
63 Flags = IsRowMajor ? RowMajorBit : 0
64 };
65};
66
67} // namespace internal
68
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_;
92
93 public:
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>;
98
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)
106 EIGEN_STATIC_ASSERT(
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)
110 // Select the relation first: C++14 would instantiate static assertions in both branches of an ordinary if.
111 // TODO(C++17): use if constexpr with EIGEN_INTERNAL_ASSERT_SAME_COLS/EIGEN_INTERNAL_ASSERT_SAME_ROWS directly.
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>>);
118 }
119
120 EIGEN_DEVICE_FUNC constexpr Index rows() const {
121 return Direction == Vertical ? m_lhs.rows() + m_rhs.rows() : m_lhs.rows();
122 }
123 EIGEN_DEVICE_FUNC constexpr Index cols() const {
124 return Direction == Horizontal ? m_lhs.cols() + m_rhs.cols() : m_lhs.cols();
125 }
126
127 EIGEN_DEVICE_FUNC constexpr const LhsTypeNested_& lhs() const { return m_lhs; }
128 EIGEN_DEVICE_FUNC constexpr const RhsTypeNested_& rhs() const { return m_rhs; }
129
130 protected:
131 LhsTypeNested m_lhs;
132 RhsTypeNested m_rhs;
133};
134
135// Evaluator for Concat
136namespace internal {
137
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;
142
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>;
147
148 enum {
149 CoeffReadCost = plain_enum_max(evaluator<LhsNestedCleaned>::CoeffReadCost,
150 evaluator<RhsNestedCleaned>::CoeffReadCost) +
151 NumTraits<typename XprType::Scalar>::AddCost, // cost of the branch
152 LhsFlags = evaluator<LhsNestedCleaned>::Flags,
153 RhsFlags = evaluator<RhsNestedCleaned>::Flags,
154 IsRowMajor = int(traits<XprType>::Flags) & RowMajorBit,
155 IsVectorAtCompileTime = XprType::IsVectorAtCompileTime,
156 // Packets are loaded along the Concat's inner direction, so each operand must store its
157 // coefficients in that same order. Compile-time vectors are exempt: a vector evaluator with
158 // packet access is contiguous along its single extent, and requests along the other
159 // direction can only be single-lane.
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),
164 Flags = (traits<XprType>::Flags & RowMajorBit) | (BothHavePacketAccess ? PacketAccessBit : 0) |
165 (IsVectorAtCompileTime && BothHaveLinearAccess ? LinearAccessBit : 0),
166 Alignment = 0 // conservative: no alignment guarantees across boundary
167 };
168
169 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit evaluator(const XprType& xpr)
170 : m_lhs(xpr.lhs()),
171 m_rhs(xpr.rhs()),
172 m_lhsImpl(m_lhs),
173 m_rhsImpl(m_rhs),
174 m_lhsRows(xpr.lhs().rows()),
175 m_lhsCols(xpr.lhs().cols()) {}
176
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);
181 else
182 return m_rhsImpl.coeff(row - m_lhsRows.value(), col);
183 } else {
184 if (col < m_lhsCols.value())
185 return m_lhsImpl.coeff(row, col);
186 else
187 return m_rhsImpl.coeff(row, col - m_lhsCols.value());
188 }
189 }
190
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);
195 else
196 return m_rhsImpl.coeff(index - boundary);
197 }
198
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);
205 // Column-major: inner=rows, packet extends along rows and may straddle the row boundary.
206 // Row-major: inner=cols, packet extends along cols — never crosses the row boundary.
207 EIGEN_IF_CONSTEXPR (!IsRowMajor) {
208 if (row + packetSize > boundary) return packetBoundary<LoadMode, PacketType>(row, col);
209 }
210 return m_lhsImpl.template packet<LoadMode, PacketType>(row, col);
211 } else {
212 const Index boundary = m_lhsCols.value();
213 if (col >= boundary) return m_rhsImpl.template packet<LoadMode, PacketType>(row, col - boundary);
214 // Row-major: inner=cols, packet extends along cols and may straddle the col boundary.
215 // Column-major: inner=rows, packet extends along rows — never crosses the col boundary.
216 EIGEN_IF_CONSTEXPR (IsRowMajor) {
217 if (col + packetSize > boundary) return packetBoundary<LoadMode, PacketType>(row, col);
218 }
219 return m_lhsImpl.template packet<LoadMode, PacketType>(row, col);
220 }
221 }
222
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);
230 }
231
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();
236 if (row >= boundary)
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);
240 }
241 return m_lhsImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count);
242 } else {
243 const Index boundary = m_lhsCols.value();
244 if (col >= boundary)
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);
248 }
249 return m_lhsImpl.template packetSegment<LoadMode, PacketType>(row, col, begin, count);
250 }
251 }
252
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);
260 }
261
262 protected:
263 using Scalar = typename XprType::Scalar;
264
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);
272 }
273
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);
280 }
281
282 template <int LoadMode, typename PacketType>
283 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegmentBoundary(Index row, Index col, Index begin,
284 Index count) const {
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);
290 }
291
292 template <int LoadMode, typename PacketType>
293 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packetSegmentBoundaryLinear(Index index, Index begin,
294 Index count) const {
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);
299 }
300
301 LhsNested m_lhs;
302 RhsNested m_rhs;
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;
307};
308
309} // namespace internal
310
325template <typename Lhs, typename Rhs>
326EIGEN_DEVICE_FUNC inline const Concat<Horizontal, Lhs, Rhs> hcat(const DenseBase<Lhs>& lhs, const DenseBase<Rhs>& rhs) {
327 return Concat<Horizontal, Lhs, Rhs>(lhs.derived(), rhs.derived());
328}
329
344template <typename Lhs, typename Rhs>
345EIGEN_DEVICE_FUNC inline const Concat<Vertical, Lhs, Rhs> vcat(const DenseBase<Lhs>& lhs, const DenseBase<Rhs>& rhs) {
346 return Concat<Vertical, Lhs, Rhs>(lhs.derived(), rhs.derived());
347}
348
349} // end namespace Eigen
350
351#endif // EIGEN_CONCAT_OP_H
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