14 #define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) 16 #define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) 47 return os <<
"p:" << v.bool_ <<
"," << v.uint_ <<
"," << v.float_ <<
"," << v.ldbl_;
56 return os <<
"n:a=" << v.a <<
";b=" << v.b;
77 std::array<char, 3>
b;
86 return os <<
"c:" << v.
cflt <<
"," << v.
cdbl;
92 std::array<uint8_t, 3>
c;
93 std::array<float, 2>
d[4];
111 for (
size_t i = 0;
i < 3 && v.
a[
i];
i++) os << v.
a[
i];
113 for (
size_t i = 0;
i < 3 && v.
b[
i];
i++) os << v.
b[
i];
119 for (
int i = 0;
i < 3;
i++) {
123 for (
int j = 0;
j < 3;
j++)
124 os << v.
a[
i][
j] <<
',';
125 os << v.
a[
i][3] <<
'}';
127 os <<
"},b={" << v.
b[0] <<
',' << v.
b[1];
128 os <<
"},c={" <<
int(v.
c[0]) <<
',' <<
int(v.
c[1]) <<
',' <<
int(v.
c[2]);
130 for (
int i = 0;
i < 4;
i++) {
133 os <<
'{' << v.
d[
i][0] <<
',' << v.
d[
i][1] <<
'}';
139 return os <<
"e1=" << (v.e1 ==
E1::A ?
"A" :
"B") <<
",e2=" << (v.e2 ==
E2::X ?
"X" :
"Y");
142 template <
typename T>
144 return py::array(py::buffer_info(
nullptr,
sizeof(
T),
145 py::format_descriptor<T>::format(),
146 1, { n }, {
sizeof(
T) }));
149 #define SET_TEST_VALS(s, i) do { \ 150 s.bool_ = (i) % 2 != 0; \ 151 s.uint_ = (uint32_t) (i); \ 152 s.float_ = (float) (i) * 1.5f; \ 153 s.ldbl_ = (long double) (i) * -2.5L; } while (0) 155 template <
typename S>
157 auto arr = mkarray_via_buffer<S>(
n);
158 auto req =
arr.request();
159 auto ptr =
static_cast<S*
>(req.ptr);
160 for (
size_t i = 0;
i <
n;
i++) {
166 template <
typename S>
168 const auto req = arr.request();
169 const auto ptr =
static_cast<S*
>(req.ptr);
172 std::stringstream
ss;
180 using arr_t = py::array_t<int32_t, 0>;
182 std::vector<int32_t>
data { 1, 2, 3, 4, 5, 6 };
183 std::vector<ssize_t> shape { 3, 2 };
184 std::vector<ssize_t> strides { 8, 4 };
187 auto vptr = (
void *)
ptr;
188 auto dtype = py::dtype(
"int32");
190 py::buffer_info buf_ndim1(vptr, 4,
"i", 6);
191 py::buffer_info buf_ndim1_null(
nullptr, 4,
"i", 6);
192 py::buffer_info buf_ndim2(vptr, 4,
"i", 2, shape, strides);
193 py::buffer_info buf_ndim2_null(
nullptr, 4,
"i", 2, shape, strides);
196 auto req =
arr.request();
197 for (
int i = 0; i < 6; i++) ((
int32_t *) req.ptr)[
i] = i + 1;
203 case 10:
return arr_t(shape, strides,
ptr);
209 case 16:
return arr_t(buf_ndim2);
212 case 20:
return fill(
arr_t(shape, strides));
215 case 23:
return fill(
arr_t(shape));
218 case 26:
return fill(
arr_t(buf_ndim2_null));
219 case 27:
return fill(
py::array(buf_ndim2_null));
224 case 33:
return arr_t(buf_ndim1);
227 case 40:
return fill(
arr_t(6));
230 case 43:
return fill(
arr_t(buf_ndim1_null));
231 case 44:
return fill(
py::array(buf_ndim1_null));
238 list.append(py::dtype(
"int32"));
239 list.append(py::dtype(std::string(
"float64")));
240 list.append(py::dtype::from_args(
py::str(
"bool")));
244 offsets.append(py::int_(1)); offsets.append(py::int_(10)); dict[
"offsets"] =
offsets;
245 formats.append(py::dtype(
"int32")); formats.append(py::dtype(
"float64")); dict[
"formats"] = formats;
246 dict[
"itemsize"] = py::int_(20);
247 list.append(py::dtype::from_args(dict));
248 list.append(py::dtype(names, formats, offsets, 20));
249 list.append(py::dtype(py::buffer_info((
void *) 0,
sizeof(
unsigned int),
"I", 1)));
250 list.append(py::dtype(py::buffer_info((
void *) 0, 0,
"T{i:a:f:b:}", 1)));
258 try { py::module::import(
"numpy"); }
259 catch (...) {
return; }
262 py::class_<SimpleStruct>(
m,
"SimpleStruct");
276 py::class_<PackedStruct>(
m,
"PackedStruct");
287 py::detail::npy_format_descriptor<A>::register_dtype(
290 py::detail::npy_format_descriptor<B>::register_dtype(
291 std::vector<py::detail::field_descriptor>{}
295 m.def(
"create_rec_simple", &create_recarray<SimpleStruct>);
296 m.def(
"create_rec_packed", &create_recarray<PackedStruct>);
297 m.def(
"create_rec_nested", [](
size_t n) {
298 py::array_t<NestedStruct, 0>
arr = mkarray_via_buffer<NestedStruct>(
n);
299 auto req = arr.request();
300 auto ptr =
static_cast<NestedStruct*
>(req.ptr);
301 for (
size_t i = 0;
i <
n;
i++) {
307 m.def(
"create_rec_partial", &create_recarray<PartialStruct>);
308 m.def(
"create_rec_partial_nested", [](
size_t n) {
309 py::array_t<PartialNestedStruct, 0>
arr = mkarray_via_buffer<PartialNestedStruct>(
n);
310 auto req = arr.request();
312 for (
size_t i = 0;
i <
n;
i++) {
317 m.def(
"print_rec_simple", &print_recarray<SimpleStruct>);
318 m.def(
"print_rec_packed", &print_recarray<PackedStruct>);
319 m.def(
"print_rec_nested", &print_recarray<NestedStruct>);
322 m.def(
"get_format_unbound", []() {
return py::format_descriptor<UnboundStruct>::format(); });
323 m.def(
"print_format_descriptors", []() {
325 for (
const auto &fmt : {
326 py::format_descriptor<SimpleStruct>::format(),
327 py::format_descriptor<PackedStruct>::format(),
328 py::format_descriptor<NestedStruct>::format(),
329 py::format_descriptor<PartialStruct>::format(),
330 py::format_descriptor<PartialNestedStruct>::format(),
331 py::format_descriptor<StringStruct>::format(),
332 py::format_descriptor<ArrayStruct>::format(),
333 py::format_descriptor<EnumStruct>::format(),
334 py::format_descriptor<ComplexStruct>::format()
342 m.def(
"print_dtypes", []() {
344 for (
const py::handle &
d : {
345 py::dtype::of<SimpleStruct>(),
346 py::dtype::of<PackedStruct>(),
347 py::dtype::of<NestedStruct>(),
348 py::dtype::of<PartialStruct>(),
349 py::dtype::of<PartialNestedStruct>(),
350 py::dtype::of<StringStruct>(),
351 py::dtype::of<ArrayStruct>(),
352 py::dtype::of<EnumStruct>(),
353 py::dtype::of<StructWithUglyNames>(),
354 py::dtype::of<ComplexStruct>()
360 m.def(
"test_dtype_methods", []() {
362 auto dt1 = py::dtype::of<int32_t>();
363 auto dt2 = py::dtype::of<SimpleStruct>();
364 list.append(dt1); list.append(dt2);
365 list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields()));
366 list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize()));
369 struct TrailingPaddingStruct {
374 m.def(
"trailing_padding_dtype", []() {
return py::dtype::of<TrailingPaddingStruct>(); });
377 m.def(
"create_string_array", [](
bool non_empty) {
378 py::array_t<StringStruct, 0>
arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
380 auto req = arr.request();
382 for (
ssize_t i = 0;
i < req.size * req.itemsize;
i++)
383 static_cast<char*>(req.ptr)[
i] = 0;
384 ptr[1].a[0] =
'a';
ptr[1].b[0] =
'a';
385 ptr[2].a[0] =
'a';
ptr[2].b[0] =
'a';
386 ptr[3].a[0] =
'a';
ptr[3].b[0] =
'a';
388 ptr[2].a[1] =
'b';
ptr[2].b[1] =
'b';
389 ptr[3].a[1] =
'b';
ptr[3].b[1] =
'b';
391 ptr[3].a[2] =
'c';
ptr[3].b[2] =
'c';
395 m.def(
"print_string_array", &print_recarray<StringStruct>);
398 m.def(
"create_array_array", [](
size_t n) {
399 py::array_t<ArrayStruct, 0>
arr = mkarray_via_buffer<ArrayStruct>(
n);
401 for (
size_t i = 0;
i <
n;
i++) {
402 for (
size_t j = 0;
j < 3;
j++)
403 for (
size_t k = 0; k < 4; k++)
404 ptr[
i].
a[
j][k] =
char(
'A' + (
i * 100 +
j * 10 + k) % 26);
405 for (
size_t j = 0;
j < 2;
j++)
407 for (
size_t j = 0;
j < 3;
j++)
409 for (
size_t j = 0;
j < 4;
j++)
410 for (
size_t k = 0; k < 2; k++)
415 m.def(
"print_array_array", &print_recarray<ArrayStruct>);
418 m.def(
"create_enum_array", [](
size_t n) {
419 py::array_t<EnumStruct, 0>
arr = mkarray_via_buffer<EnumStruct>(
n);
420 auto ptr = (EnumStruct *) arr.mutable_data();
421 for (
size_t i = 0;
i <
n;
i++) {
422 ptr[
i].e1 =
static_cast<E1>(-1 + ((
int)
i % 2) * 2);
423 ptr[
i].e2 =
static_cast<E2>(1 + (
i % 2));
427 m.def(
"print_enum_array", &print_recarray<EnumStruct>);
430 m.def(
"create_complex_array", [](
size_t n) {
431 py::array_t<ComplexStruct, 0>
arr = mkarray_via_buffer<ComplexStruct>(
n);
433 for (
size_t i = 0;
i <
n;
i++) {
434 ptr[
i].cflt.real(
float(
i));
435 ptr[
i].cflt.imag(
float(
i) + 0.25
f);
436 ptr[
i].cdbl.real(
double(
i) + 0.5);
437 ptr[
i].cdbl.imag(
double(
i) + 0.75);
441 m.def(
"print_complex_array", &print_recarray<ComplexStruct>);
447 struct CompareStruct {
453 m.def(
"compare_buffer_info", []() {
462 m.def(
"buffer_to_dtype", [](py::buffer& buf) {
return py::dtype(buf.request()); });
466 m.def(
"f_packed", [](PackedStruct s) {
return s.uint_ * 10; });
467 m.def(
"f_nested", [](NestedStruct s) {
return s.a.uint_ * 10; });
473 m.def(
"dtype_wrapper", [](py::object
d) {
return py::dtype::from_args(std::move(d)); });
py::array mkarray_via_buffer(size_t n)
py::list test_dtype_ctors()
#define SET_TEST_VALS(s, i)
#define PYBIND11_NUMPY_DTYPE_EX(Type,...)
for(size_t i=1;i< poses.size();++i)
#define PYBIND11_PACKED(cls)
py::array_t< uint16_t, 0 > arr_t
py::array_t< S, 0 > create_recarray(size_t n)
py::list print_recarray(py::array_t< S, 0 > arr)
#define PYBIND11_NUMPY_DTYPE(Type,...)
std::complex< float > cflt
TEST_SUBMODULE(numpy_dtypes, m)
std::array< float, 2 > d[4]
std::complex< double > cdbl
static const Line3 l(Rot3(), 1, 1)
unsigned __int64 uint64_t
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Eigen::Triplet< double > T
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
std::ostream & operator<<(std::ostream &os, const SimpleStruct &v)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set offsets
static std::stringstream ss
std::array< uint8_t, 3 > c
ofstream os("timeSchurFactors.csv")
py::array_t< int32_t, 0 > test_array_ctors(int i)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x