MCPcopy Create free account
hub / github.com/pybind/pybind11 / conformable

Method conformable

include/pybind11/eigen/matrix.h:173–218  ·  view source on GitHub ↗

Takes an input array and determines whether we can make it fit into the Eigen type. If the array is a vector, we attempt to fit it into either an Eigen 1xN or Nx1 vector (preferring the latter if it will fit in either, i.e. for a fully dynamic matrix type).

Source from the content-addressed store, hash-verified

171 // the array is a vector, we attempt to fit it into either an Eigen 1xN or Nx1 vector
172 // (preferring the latter if it will fit in either, i.e. for a fully dynamic matrix type).
173 static EigenConformable<row_major> conformable(const array &a) {
174 const auto dims = a.ndim();
175 if (dims < 1 || dims > 2) {
176 return false;
177 }
178
179 if (dims == 2) { // Matrix type: require exact match (or dynamic)
180
181 EigenIndex np_rows = a.shape(0), np_cols = a.shape(1),
182 np_rstride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar)),
183 np_cstride = a.strides(1) / static_cast<ssize_t>(sizeof(Scalar));
184 if ((fixed_rows && np_rows != rows) || (fixed_cols && np_cols != cols)) {
185 return false;
186 }
187
188 return {np_rows, np_cols, np_rstride, np_cstride};
189 }
190
191 // Otherwise we're storing an n-vector. Only one of the strides will be used, but
192 // whichever is used, we want the (single) numpy stride value.
193 const EigenIndex n = a.shape(0),
194 stride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar));
195
196 if (vector) { // Eigen type is a compile-time vector
197 if (fixed && size != n) {
198 return false; // Vector size mismatch
199 }
200 return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride};
201 }
202 if (fixed) {
203 // The type has a fixed size, but is not a vector: abort
204 return false;
205 }
206 if (fixed_cols) {
207 // Since this isn't a vector, cols must be != 1. We allow this only if it exactly
208 // equals the number of elements (rows is Dynamic, and so 1 row is allowed).
209 if (cols != n) {
210 return false;
211 }
212 return {1, n, stride};
213 } // Otherwise it's either fully dynamic, or column dynamic; both become a column vector
214 if (fixed_rows && rows != n) {
215 return false;
216 }
217 return {n, 1, stride};
218 }
219
220 static constexpr bool show_writeable
221 = is_eigen_dense_map<Type>::value && is_eigen_mutable_map<Type>::value;

Callers

nothing calls this directly

Calls 3

stridesMethod · 0.80
ndimMethod · 0.45
shapeMethod · 0.45

Tested by

no test coverage detected