4#ifndef EIGEN_SHAPE_CHECKS_H
5#define EIGEN_SHAPE_CHECKS_H
8#include "../InternalHeaderCheck.h"
16constexpr int size_at_compile_time(
int rows,
int cols) {
17 if (rows == 0 || cols == 0)
return 0;
18 if (rows == Dynamic || cols == Dynamic)
return Dynamic;
19 if (rows > (std::numeric_limits<int>::max)() / cols)
return Dynamic;
23template <
typename T,
typename =
void>
24struct shape_expression {
28struct shape_expression<T, void_t<decltype(std::declval<const T&>().derived())>> {
29 using type = remove_all_t<decltype(std::declval<const T&>().derived())>;
33template <
typename T,
typename =
void>
34struct shape_rows : std::integral_constant<int, Dynamic> {};
36struct shape_rows<T, void_t<decltype(traits<remove_all_t<T>>::RowsAtCompileTime)>>
37 : std::integral_constant<int, traits<remove_all_t<T>>::RowsAtCompileTime> {};
39template <
typename T,
typename =
void>
40struct shape_cols : std::integral_constant<int, Dynamic> {};
42struct shape_cols<T, void_t<decltype(traits<remove_all_t<T>>::ColsAtCompileTime)>>
43 : std::integral_constant<int, traits<remove_all_t<T>>::ColsAtCompileTime> {};
45template <
typename Expr,
typename =
void>
46struct shape_max_dimensions {
47 static constexpr int Rows = Dynamic;
48 static constexpr int Cols = Dynamic;
51template <
typename Expr>
52struct shape_max_dimensions<Expr, void_t<decltype(traits<remove_all_t<Expr>>::MaxRowsAtCompileTime),
53 decltype(traits<remove_all_t<Expr>>::MaxColsAtCompileTime)>> {
54 static constexpr int Rows = traits<remove_all_t<Expr>>::MaxRowsAtCompileTime;
55 static constexpr int Cols = traits<remove_all_t<Expr>>::MaxColsAtCompileTime;
59struct compile_time_shape {
60 static constexpr int Rows = shape_rows<T>::value;
61 static constexpr int Cols = shape_cols<T>::value;
62 static constexpr int Size = size_at_compile_time(Rows, Cols);
63 static constexpr int MaxRows = shape_max_dimensions<T>::Rows;
64 static constexpr int MaxCols = shape_max_dimensions<T>::Cols;
67template <
int Left,
int Right,
int LeftMax = Dynamic,
int RightMax = Dynamic>
68struct dimension_relation {
69 static constexpr bool Known = Left != Dynamic && Right != Dynamic;
71 static constexpr bool Compatible = (!Known || Left == Right) &&
72 (Left == Dynamic || RightMax == Dynamic || Left <= RightMax) &&
73 (Right == Dynamic || LeftMax == Dynamic || Right <= LeftMax);
74 static constexpr bool Proven = Known && Compatible;
77template <
typename Left,
typename Right>
78using matching_rows = dimension_relation<compile_time_shape<Left>::Rows, compile_time_shape<Right>::Rows>;
79template <
typename Left,
typename Right>
80using matching_cols = dimension_relation<compile_time_shape<Left>::Cols, compile_time_shape<Right>::Cols>;
81template <
typename Left,
typename Right>
82using matching_sizes = dimension_relation<compile_time_shape<Left>::Size, compile_time_shape<Right>::Size>;
83template <
typename SizeExpr,
typename MatrixExpr,
bool RowAxis>
84using size_matches_axis =
85 dimension_relation<compile_time_shape<SizeExpr>::Size,
86 RowAxis ? compile_time_shape<MatrixExpr>::Rows : compile_time_shape<MatrixExpr>::Cols>;
87template <
typename Left,
typename Right>
88struct matching_shapes {
89 using Rows = matching_rows<Left, Right>;
90 using Cols = matching_cols<Left, Right>;
91 static constexpr bool Known = Rows::Known && Cols::Known;
93 static constexpr bool Compatible = (compile_time_shape<Left>::Size == 0 && compile_time_shape<Right>::Size == 0) ||
94 (Rows::Compatible && Cols::Compatible);
95 static constexpr bool Proven = Rows::Proven && Cols::Proven;
97template <
typename Left,
typename Right>
98using product_dimensions = dimension_relation<compile_time_shape<Left>::Cols, compile_time_shape<Right>::Rows>;
100using square_dimensions = dimension_relation<compile_time_shape<T>::Rows, compile_time_shape<T>::Cols>;
102template <
typename T,
int Rows,
int Cols>
103using reshape_dimensions = dimension_relation<size_at_compile_time(Rows, Cols), compile_time_shape<T>::Size>;
105template <
typename Solver,
typename Rhs,
bool Transposed = false>
106using solver_rhs_dimensions =
107 dimension_relation<Transposed ? compile_time_shape<Solver>::Cols : compile_time_shape<Solver>::Rows,
108 compile_time_shape<Rhs>::Rows>;
111struct vector_dimensions {
112 static constexpr bool Proven = compile_time_shape<T>::Rows == 1 || compile_time_shape<T>::Cols == 1;
113 static constexpr bool Known =
114 Proven || (compile_time_shape<T>::Rows != Dynamic && compile_time_shape<T>::Cols != Dynamic);
115 static constexpr bool Compatible = !Known || Proven;
119struct nonempty_extent {
120 static constexpr bool Known = Extent != Dynamic;
121 static constexpr bool Compatible = !Known || Extent > 0;
122 static constexpr bool Proven = Known && Compatible;
126using nonempty_dimensions = nonempty_extent<compile_time_shape<T>::Size>;
128template <
typename T,
int Rows,
int Cols>
129struct fixed_dimensions {
130 using RowRelation = dimension_relation<compile_time_shape<T>::Rows, Rows>;
131 using ColRelation = dimension_relation<compile_time_shape<T>::Cols, Cols>;
132 static constexpr bool Known = RowRelation::Known && ColRelation::Known;
133 static constexpr bool Compatible = RowRelation::Compatible && ColRelation::Compatible;
134 static constexpr bool Proven = RowRelation::Proven && ColRelation::Proven;
145#define EIGEN_INTERNAL_SHAPE_ASSERT(Proven, Condition) \
147 EIGEN_IF_CONSTEXPR (!(Proven)) { \
148 (void)sizeof(static_cast<bool>(Condition)); \
149 eigen_assert(Condition); \
153#define EIGEN_INTERNAL_DEBUG_SHAPE_ASSERT(Proven, Condition) \
155 EIGEN_IF_CONSTEXPR (!(Proven)) { \
156 (void)sizeof(static_cast<bool>(Condition)); \
157 eigen_internal_assert(Condition); \
162#define EIGEN_INTERNAL_SHAPE_TYPE(Xpr) \
163 typename Eigen::internal::shape_expression<Eigen::internal::remove_all_t<decltype(Xpr)>>::type
166#define EIGEN_INTERNAL_CHECK_SHAPE(Condition, Message, ...) \
168 using ShapeRelation = __VA_ARGS__; \
169 EIGEN_STATIC_ASSERT(ShapeRelation::Compatible, Message) \
170 EIGEN_IF_CONSTEXPR (!ShapeRelation::Proven) { \
171 (void)sizeof(static_cast<bool>(Condition)); \
172 eigen_assert(Condition); \
176#define EIGEN_INTERNAL_ASSERT_SAME_ROWS_MSG(Lhs, Rhs, Message) \
177 EIGEN_INTERNAL_CHECK_SHAPE( \
178 (Lhs).rows() == (Rhs).rows() && (Message), YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES, \
179 Eigen::internal::matching_rows<EIGEN_INTERNAL_SHAPE_TYPE(Lhs), EIGEN_INTERNAL_SHAPE_TYPE(Rhs)>)
180#define EIGEN_INTERNAL_ASSERT_SAME_ROWS(Lhs, Rhs) \
181 EIGEN_INTERNAL_ASSERT_SAME_ROWS_MSG(Lhs, Rhs, "Both expressions must have the same number of rows")
183#define EIGEN_INTERNAL_ASSERT_SAME_SHAPE_MSG(Lhs, Rhs, Message) \
185 using ShapeRelation = \
186 Eigen::internal::matching_shapes<EIGEN_INTERNAL_SHAPE_TYPE(Lhs), EIGEN_INTERNAL_SHAPE_TYPE(Rhs)>; \
187 EIGEN_STATIC_ASSERT(ShapeRelation::Compatible, YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES) \
188 EIGEN_INTERNAL_SHAPE_ASSERT(ShapeRelation::Rows::Proven, (Lhs).rows() == (Rhs).rows() && (Message)); \
189 EIGEN_INTERNAL_SHAPE_ASSERT(ShapeRelation::Cols::Proven, (Lhs).cols() == (Rhs).cols() && (Message)); \
191#define EIGEN_INTERNAL_ASSERT_SAME_SHAPE(Lhs, Rhs) \
192 EIGEN_INTERNAL_ASSERT_SAME_SHAPE_MSG(Lhs, Rhs, "Both expressions must have the same dimensions")
194#define EIGEN_INTERNAL_ASSERT_SAME_COLS_MSG(Lhs, Rhs, Message) \
195 EIGEN_INTERNAL_CHECK_SHAPE( \
196 (Lhs).cols() == (Rhs).cols() && (Message), YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES, \
197 Eigen::internal::matching_cols<EIGEN_INTERNAL_SHAPE_TYPE(Lhs), EIGEN_INTERNAL_SHAPE_TYPE(Rhs)>)
198#define EIGEN_INTERNAL_ASSERT_SAME_COLS(Lhs, Rhs) \
199 EIGEN_INTERNAL_ASSERT_SAME_COLS_MSG(Lhs, Rhs, "Both expressions must have the same number of columns")
201#define EIGEN_INTERNAL_ASSERT_SAME_SIZE_MSG(Lhs, Rhs, Message) \
202 EIGEN_INTERNAL_CHECK_SHAPE( \
203 (Lhs).size() == (Rhs).size() && (Message), YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES, \
204 Eigen::internal::matching_sizes<EIGEN_INTERNAL_SHAPE_TYPE(Lhs), EIGEN_INTERNAL_SHAPE_TYPE(Rhs)>)
205#define EIGEN_INTERNAL_ASSERT_SAME_SIZE(Lhs, Rhs) \
206 EIGEN_INTERNAL_ASSERT_SAME_SIZE_MSG(Lhs, Rhs, "Both expressions must have the same number of coefficients")
208#define EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(Lhs, Rhs, Message) \
209 EIGEN_INTERNAL_CHECK_SHAPE( \
210 (Lhs).cols() == (Rhs).rows() && (Message), INVALID_MATRIX_PRODUCT, \
211 Eigen::internal::product_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Lhs), EIGEN_INTERNAL_SHAPE_TYPE(Rhs)>)
212#define EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE(Lhs, Rhs) \
213 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG( \
215 "invalid matrix product; " \
216 "if you wanted a coeff-wise or a dot product use the respective explicit functions")
220#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(SizeExpr, MatrixExpr, RowAxis, Message) \
221 EIGEN_INTERNAL_CHECK_SHAPE( \
222 (SizeExpr).size() == ((RowAxis) ? (MatrixExpr).rows() : (MatrixExpr).cols()) && (Message), \
223 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES, \
224 Eigen::internal::size_matches_axis<EIGEN_INTERNAL_SHAPE_TYPE(SizeExpr), EIGEN_INTERNAL_SHAPE_TYPE(MatrixExpr), \
226#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS(SizeExpr, MatrixExpr, RowAxis) \
227 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(SizeExpr, MatrixExpr, RowAxis, \
228 "Coefficient count must match the selected matrix dimension")
229#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS_MSG(SizeExpr, MatrixExpr, Message) \
230 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(SizeExpr, MatrixExpr, true, Message)
231#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_COLS_MSG(SizeExpr, MatrixExpr, Message) \
232 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS_MSG(SizeExpr, MatrixExpr, false, Message)
233#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS(SizeExpr, MatrixExpr) \
234 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS(SizeExpr, MatrixExpr, true)
235#define EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_COLS(SizeExpr, MatrixExpr) \
236 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_AXIS(SizeExpr, MatrixExpr, false)
238#define EIGEN_INTERNAL_ASSERT_SQUARE_MSG(Xpr, Message) \
239 EIGEN_INTERNAL_SHAPE_ASSERT((Eigen::internal::square_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>::Proven), \
240 (Xpr).rows() == (Xpr).cols() && (Message))
241#define EIGEN_INTERNAL_ASSERT_SQUARE(Xpr) \
242 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(Xpr, "This operation requires a square matrix")
245#define EIGEN_INTERNAL_ASSERT_VECTOR_IF(Enabled, Xpr) \
246 EIGEN_INTERNAL_CHECK_SHAPE( \
247 ((Xpr).rows() == 1 || (Xpr).cols() == 1) && "This operation requires a vector", \
248 YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX, \
249 std::conditional_t<Enabled, Eigen::internal::vector_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>, \
250 Eigen::internal::dimension_relation<1, 1>>)
251#define EIGEN_INTERNAL_ASSERT_VECTOR(Xpr) EIGEN_INTERNAL_ASSERT_VECTOR_IF(true, Xpr)
253#define EIGEN_INTERNAL_ASSERT_SCALAR(Xpr) \
254 EIGEN_INTERNAL_CHECK_SHAPE( \
255 ((Xpr).rows() == 1 && (Xpr).cols() == 1) && "This operation requires a 1-by-1 expression", \
256 THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS, \
257 Eigen::internal::fixed_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr), 1, 1>)
259#define EIGEN_INTERNAL_ASSERT_NONEMPTY_MSG(Xpr, Message) \
260 EIGEN_INTERNAL_SHAPE_ASSERT((Eigen::internal::nonempty_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>::Proven), \
261 (Xpr).rows() > 0 && (Xpr).cols() > 0 && (Message))
262#define EIGEN_INTERNAL_ASSERT_NONEMPTY(Xpr) EIGEN_INTERNAL_ASSERT_NONEMPTY_MSG(Xpr, "you are using an empty matrix")
264#define EIGEN_INTERNAL_ASSERT_NONEMPTY_AXIS(Xpr, Vertical) \
266 using ShapeRelation = Eigen::internal::nonempty_extent< \
267 (Vertical) ? Eigen::internal::compile_time_shape<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>::Rows \
268 : Eigen::internal::compile_time_shape<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>::Cols>; \
269 EIGEN_INTERNAL_SHAPE_ASSERT(ShapeRelation::Proven, \
270 ((Vertical) ? (Xpr).rows() : (Xpr).cols()) > 0 && "you are using an empty matrix"); \
274#define EIGEN_INTERNAL_ASSERT_NONEMPTY_SIZE_MSG(Xpr, Size, Message) \
275 EIGEN_INTERNAL_SHAPE_ASSERT((Eigen::internal::nonempty_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr)>::Proven), \
276 (Size) > 0 && (Message))
277#define EIGEN_INTERNAL_ASSERT_NONEMPTY_SIZE(Xpr, Size) \
278 EIGEN_INTERNAL_ASSERT_NONEMPTY_SIZE_MSG(Xpr, Size, "you are using an empty matrix")
280#define EIGEN_INTERNAL_ASSERT_RESHAPE_SIZE(Xpr, Rows, Cols) \
281 EIGEN_INTERNAL_CHECK_SHAPE( \
282 (Rows) * (Cols) == (Xpr).rows() * (Xpr).cols() && "Reshaping must preserve the number of coefficients", \
283 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES, \
284 Eigen::internal::reshape_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Xpr), Rows, Cols>)
286#define EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(Solver, Rhs, Transposed, Message) \
287 EIGEN_INTERNAL_CHECK_SHAPE(((Transposed) ? (Solver).cols() : (Solver).rows()) == (Rhs).rows() && (Message), \
288 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES, \
289 Eigen::internal::solver_rhs_dimensions<EIGEN_INTERNAL_SHAPE_TYPE(Solver), \
290 EIGEN_INTERNAL_SHAPE_TYPE(Rhs), Transposed>)
291#define EIGEN_INTERNAL_ASSERT_SOLVER_RHS(Solver, Rhs, Transposed) \
292 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(Solver, Rhs, Transposed, \
293 "solve(): invalid number of rows of the right hand side")