15 # define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) 17 # define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) 48 return os <<
"p:" << v.bool_ <<
"," << v.uint_ <<
"," << v.float_ <<
"," << v.ldbl_;
57 return os <<
"n:a=" << v.a <<
";b=" << v.b;
78 std::array<char, 3>
b;
87 return os <<
"c:" << v.
cflt <<
"," << v.
cdbl;
93 std::array<uint8_t, 3>
c;
94 std::array<float, 2>
d[4];
112 for (
size_t i = 0;
i < 3 && (v.
a[
i] != 0);
i++) {
116 for (
size_t i = 0;
i < 3 && (v.
b[
i] != 0);
i++) {
124 for (
int i = 0;
i < 3;
i++) {
129 for (
int j = 0;
j < 3;
j++) {
130 os << v.
a[
i][
j] <<
',';
132 os << v.
a[
i][3] <<
'}';
134 os <<
"},b={" << v.
b[0] <<
',' << v.
b[1];
135 os <<
"},c={" <<
int(v.
c[0]) <<
',' <<
int(v.
c[1]) <<
',' <<
int(v.
c[2]);
137 for (
int i = 0;
i < 4;
i++) {
141 os <<
'{' << v.
d[
i][0] <<
',' << v.
d[
i][1] <<
'}';
147 return os <<
"e1=" << (v.e1 ==
E1::A ?
"A" :
"B") <<
",e2=" << (v.e2 ==
E2::X ?
"X" :
"Y");
150 template <
typename T>
156 #define SET_TEST_VALS(s, i) \ 158 (s).bool_ = (i) % 2 != 0; \ 159 (s).uint_ = (uint32_t) (i); \ 160 (s).float_ = (float) (i) *1.5f; \ 161 (s).ldbl_ = (long double) (i) * -2.5L; \ 164 template <
typename S>
166 auto arr = mkarray_via_buffer<S>(
n);
167 auto req =
arr.request();
168 auto *ptr =
static_cast<S *
>(req.ptr);
169 for (
size_t i = 0;
i <
n;
i++) {
175 template <
typename S>
177 const auto req = arr.request();
178 auto *
const ptr =
static_cast<S *
>(req.ptr);
181 std::stringstream
ss;
189 using arr_t = py::array_t<int32_t, 0>;
191 std::vector<int32_t>
data{1, 2, 3, 4, 5, 6};
192 std::vector<py::ssize_t> shape{3, 2};
193 std::vector<py::ssize_t>
strides{8, 4};
195 auto *ptr =
data.data();
196 auto *vptr = (
void *) ptr;
197 auto dtype = py::dtype(
"int32");
199 py::buffer_info buf_ndim1(vptr, 4,
"i", 6);
200 py::buffer_info buf_ndim1_null(
nullptr, 4,
"i", 6);
201 py::buffer_info buf_ndim2(vptr, 4,
"i", 2, shape,
strides);
202 py::buffer_info buf_ndim2_null(
nullptr, 4,
"i", 2, shape,
strides);
205 auto req =
arr.request();
206 for (
int i = 0; i < 6; i++) {
221 return arr_t(shape, ptr);
227 return arr_t(buf_ndim2);
238 return fill(
arr_t(shape));
244 return fill(
arr_t(buf_ndim2_null));
249 return arr_t(6, ptr);
255 return arr_t(buf_ndim1);
260 return fill(
arr_t(6));
266 return fill(
arr_t(buf_ndim1_null));
275 list.append(py::dtype(
"int32"));
276 list.append(py::dtype(std::string(
"float64")));
277 list.append(py::dtype::from_args(
py::str(
"bool")));
282 dict[
"names"] =
names;
283 offsets.append(py::int_(1));
284 offsets.append(py::int_(10));
286 formats.append(py::dtype(
"int32"));
287 formats.append(py::dtype(
"float64"));
288 dict[
"formats"] = formats;
289 dict[
"itemsize"] = py::int_(20);
290 list.append(py::dtype::from_args(dict));
291 list.append(py::dtype(names, formats, offsets, 20));
292 list.append(py::dtype(py::buffer_info((
void *)
nullptr,
sizeof(
unsigned int),
"I", 1)));
293 list.append(py::dtype(py::buffer_info((
void *)
nullptr, 0,
"T{i:a:f:b:}", 1)));
294 list.append(py::dtype(py::detail::npy_api::NPY_DOUBLE_));
303 py::module_::import(
"numpy");
304 }
catch (
const py::error_already_set &) {
309 py::class_<SimpleStruct>(
m,
"SimpleStruct")
320 .def_static(
"fromtuple", [](
const py::tuple &tup) {
322 throw py::cast_error(
"Invalid size");
325 tup[1].cast<uint32_t>(),
326 tup[2].cast<
float>(),
327 tup[3].cast<long double>()};
342 py::class_<PackedStruct>(
m,
"PackedStruct");
346 #ifdef PYBIND11_NEVER_DEFINED_EVER 351 NotPOD() :
v(
"hi"){};
358 py::detail::npy_format_descriptor<A>::register_dtype({});
359 py::detail::npy_format_descriptor<B>::register_dtype(
360 std::vector<py::detail::field_descriptor>{});
363 m.def(
"create_rec_simple", &create_recarray<SimpleStruct>);
364 m.def(
"create_rec_packed", &create_recarray<PackedStruct>);
365 m.def(
"create_rec_nested", [](
size_t n) {
366 py::array_t<NestedStruct, 0>
arr = mkarray_via_buffer<NestedStruct>(
n);
367 auto req = arr.request();
368 auto *ptr =
static_cast<NestedStruct *
>(req.ptr);
369 for (
size_t i = 0;
i <
n;
i++) {
375 m.def(
"create_rec_partial", &create_recarray<PartialStruct>);
376 m.def(
"create_rec_partial_nested", [](
size_t n) {
377 py::array_t<PartialNestedStruct, 0>
arr = mkarray_via_buffer<PartialNestedStruct>(
n);
378 auto req = arr.request();
380 for (
size_t i = 0;
i <
n;
i++) {
385 m.def(
"print_rec_simple", &print_recarray<SimpleStruct>);
386 m.def(
"print_rec_packed", &print_recarray<PackedStruct>);
387 m.def(
"print_rec_nested", &print_recarray<NestedStruct>);
391 m.def(
"print_format_descriptors", []() {
408 std::vector<const char *> dtype_names{
409 "byte",
"short",
"intc",
"int_",
"longlong",
"ubyte",
"ushort",
410 "uintc",
"uint",
"ulonglong",
"half",
"single",
"double",
"longdouble",
411 "csingle",
"cdouble",
"clongdouble",
"bool_",
"datetime64",
"timedelta64",
"object_"};
413 m.def(
"print_dtypes", []() {
415 for (
const py::handle &
d : {py::dtype::of<SimpleStruct>(),
416 py::dtype::of<PackedStruct>(),
417 py::dtype::of<NestedStruct>(),
418 py::dtype::of<PartialStruct>(),
419 py::dtype::of<PartialNestedStruct>(),
420 py::dtype::of<StringStruct>(),
421 py::dtype::of<ArrayStruct>(),
422 py::dtype::of<EnumStruct>(),
423 py::dtype::of<StructWithUglyNames>(),
424 py::dtype::of<ComplexStruct>()}) {
430 m.def(
"test_dtype_kind", [dtype_names]() {
432 for (
const auto &dt_name : dtype_names) {
433 list.append(py::dtype(dt_name).kind());
437 m.def(
"test_dtype_char_", [dtype_names]() {
439 for (
const auto &dt_name : dtype_names) {
440 list.append(py::dtype(dt_name).char_());
444 m.def(
"test_dtype_num", [dtype_names]() {
446 for (
const auto &dt_name : dtype_names) {
447 list.append(py::dtype(dt_name).num());
451 m.def(
"test_dtype_byteorder", [dtype_names]() {
453 for (
const auto &dt_name : dtype_names) {
454 list.append(py::dtype(dt_name).byteorder());
458 m.def(
"test_dtype_alignment", [dtype_names]() {
460 for (
const auto &dt_name : dtype_names) {
461 list.append(py::dtype(dt_name).alignment());
465 m.def(
"test_dtype_flags", [dtype_names]() {
467 for (
const auto &dt_name : dtype_names) {
468 list.append(py::dtype(dt_name).flags());
472 m.def(
"test_dtype_methods", []() {
474 auto dt1 = py::dtype::of<int32_t>();
475 auto dt2 = py::dtype::of<SimpleStruct>();
478 list.append(py::bool_(dt1.has_fields()));
479 list.append(py::bool_(dt2.has_fields()));
480 list.append(py::int_(dt1.itemsize()));
481 list.append(py::int_(dt2.itemsize()));
484 struct TrailingPaddingStruct {
489 m.def(
"trailing_padding_dtype", []() {
return py::dtype::of<TrailingPaddingStruct>(); });
492 m.def(
"create_string_array", [](
bool non_empty) {
493 py::array_t<StringStruct, 0>
arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
495 auto req = arr.request();
498 static_cast<char *
>(req.ptr)[
i] = 0;
517 m.def(
"print_string_array", &print_recarray<StringStruct>);
520 m.def(
"create_array_array", [](
size_t n) {
521 py::array_t<ArrayStruct, 0>
arr = mkarray_via_buffer<ArrayStruct>(
n);
523 for (
size_t i = 0;
i <
n;
i++) {
524 for (
size_t j = 0;
j < 3;
j++) {
525 for (
size_t k = 0; k < 4; k++) {
526 ptr[
i].a[
j][k] = char(
'A' + (
i * 100 +
j * 10 + k) % 26);
529 for (
size_t j = 0;
j < 2;
j++) {
532 for (
size_t j = 0;
j < 3;
j++) {
535 for (
size_t j = 0;
j < 4;
j++) {
536 for (
size_t k = 0; k < 2; k++) {
543 m.def(
"print_array_array", &print_recarray<ArrayStruct>);
546 m.def(
"create_enum_array", [](
size_t n) {
547 py::array_t<EnumStruct, 0>
arr = mkarray_via_buffer<EnumStruct>(
n);
548 auto *ptr = (EnumStruct *) arr.mutable_data();
549 for (
size_t i = 0;
i <
n;
i++) {
550 ptr[
i].e1 =
static_cast<E1>(-1 + ((
int)
i % 2) * 2);
551 ptr[
i].e2 =
static_cast<E2>(1 + (
i % 2));
555 m.def(
"print_enum_array", &print_recarray<EnumStruct>);
558 m.def(
"create_complex_array", [](
size_t n) {
559 py::array_t<ComplexStruct, 0>
arr = mkarray_via_buffer<ComplexStruct>(
n);
561 for (
size_t i = 0;
i <
n;
i++) {
562 ptr[
i].cflt.real(
float(
i));
563 ptr[
i].cflt.imag(
float(
i) + 0.25
f);
564 ptr[
i].cdbl.real(
double(
i) + 0.5);
565 ptr[
i].cdbl.imag(
double(
i) + 0.75);
569 m.def(
"print_complex_array", &print_recarray<ComplexStruct>);
575 struct CompareStruct {
581 m.def(
"compare_buffer_info", []() {
584 py::buffer_info(
nullptr,
sizeof(
float),
"f", 1))));
586 py::buffer_info(
nullptr,
sizeof(
int),
"I", 1))));
588 py::buffer_info(
nullptr,
sizeof(
long),
"l", 1))));
590 py::buffer_info(
nullptr,
sizeof(
long),
sizeof(
long) ==
sizeof(
int) ?
"i" :
"q", 1))));
592 py::buffer_info(
nullptr,
sizeof(CompareStruct),
"T{?:x:3xI:y:f:z:}", 1))));
595 m.def(
"buffer_to_dtype", [](py::buffer &buf) {
return py::dtype(buf.request()); });
599 m.def(
"f_simple", f_simple);
600 m.def(
"f_packed", [](PackedStruct
s) {
return s.uint_ * 10; });
601 m.def(
"f_nested", [](NestedStruct s) {
return s.a.uint_ * 10; });
606 m.def(
"f_simple_pass_thru_vectorized",
py::vectorize(f_simple_pass_thru));
609 m.def(
"register_dtype",
613 m.def(
"dtype_wrapper", [](
const py::object &
d) {
return py::dtype::from_args(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,...)
#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)
detail::vectorize_helper< Return(*)(Args...), Return, Args... > vectorize(Return(*f)(Args...))
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Array< int, Dynamic, 1 > v
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)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
size_t len(handle h)
Get the length of a Python object.
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