27 check.numpy = np.attr(
"dtype")(np.attr(name));
28 check.pybind11 = py::dtype::of<T>();
35 get_dtype_check<std::int8_t>(
"int8"),
36 get_dtype_check<std::uint8_t>(
"uint8"),
37 get_dtype_check<std::int16_t>(
"int16"),
38 get_dtype_check<std::uint16_t>(
"uint16"),
39 get_dtype_check<std::int32_t>(
"int32"),
40 get_dtype_check<std::uint32_t>(
"uint32"),
41 get_dtype_check<std::int64_t>(
"int64"),
42 get_dtype_check<std::uint64_t>(
"uint64")
57 check.name = py::type_id<T>();
58 check.size_cpp =
sizeof(
T);
59 check.dtype = py::dtype::of<T>();
60 check.size_numpy =
check.dtype.attr(
"itemsize").template cast<int>();
66 get_dtype_size_check<short>(),
67 get_dtype_size_check<unsigned short>(),
68 get_dtype_size_check<int>(),
69 get_dtype_size_check<unsigned int>(),
70 get_dtype_size_check<long>(),
71 get_dtype_size_check<unsigned long>(),
72 get_dtype_size_check<long long>(),
73 get_dtype_size_check<unsigned long long>(),
79 using arr_t = py::array_t<uint16_t, 0>;
82 template<
typename... Ix>
arr data(
const arr&
a, Ix... index) {
83 return arr(a.nbytes() - a.offset_at(index...), (
const uint8_t *) a.data(index...));
87 return arr(a.size() - a.index_at(index...), a.data(index...));
91 auto ptr = (
uint8_t *) a.mutable_data(index...);
92 for (
ssize_t i = 0;
i < a.nbytes() - a.offset_at(index...);
i++)
98 auto ptr = a.mutable_data(index...);
99 for (
ssize_t i = 0;
i < a.size() - a.index_at(index...);
i++)
111 #define def_index_fn(name, type) \ 112 sm.def(#name, [](type a) { return name(a); }); \ 113 sm.def(#name, [](type a, int i) { return name(a, i); }); \ 114 sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \ 115 sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); }); 118 if (r.ndim() != 2)
throw std::domain_error(
"error: ndim != 2");
120 l.append(*r.data(0, 0));
121 l.append(*
r2.mutable_data(0, 0));
122 l.append(r.data(0, 1) ==
r2.mutable_data(0, 1));
124 l.append(r.itemsize());
125 l.append(r.shape(0));
126 l.append(r.shape(1));
128 l.append(r.nbytes());
136 try { py::module::import(
"numpy"); }
137 catch (...) {
return; }
140 py::class_<DtypeCheck>(sm,
"DtypeCheck")
144 return py::str(
"<DtypeCheck numpy={} pybind11={}>").format(
149 py::class_<DtypeSizeCheck>(sm,
"DtypeSizeCheck")
154 return py::str(
"<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>").format(
155 self.
name,
self.size_cpp,
self.size_numpy,
self.
dtype);
160 sm.def(
"ndim", [](
const arr&
a) {
return a.ndim(); });
161 sm.def(
"shape", [](
const arr& a) {
return arr(a.ndim(), a.shape()); });
162 sm.def(
"shape", [](
const arr& a,
ssize_t dim) {
return a.shape(dim); });
163 sm.def(
"strides", [](
const arr& a) {
return arr(a.ndim(), a.strides()); });
164 sm.def(
"strides", [](
const arr& a,
ssize_t dim) {
return a.strides(dim); });
165 sm.def(
"writeable", [](
const arr& a) {
return a.writeable(); });
166 sm.def(
"size", [](
const arr& a) {
return a.size(); });
167 sm.def(
"itemsize", [](
const arr& a) {
return a.itemsize(); });
168 sm.def(
"nbytes", [](
const arr& a) {
return a.nbytes(); });
169 sm.def(
"owndata", [](
const arr& a) {
return a.owndata(); });
186 sm.def(
"make_f_array", [] {
return py::array_t<float>({ 2, 2 }, { 4, 8 }); });
187 sm.def(
"make_c_array", [] {
return py::array_t<float>({ 2, 2 }, { 8, 4 }); });
190 sm.def(
"make_empty_shaped_array", [] {
return py::array(py::dtype(
"f"), {}, {}); });
192 sm.def(
"scalar_int", []() {
return py::array(py::dtype(
"i"), {}, {}, &
data_i); });
198 {a.shape(), a.shape() + a.ndim()},
199 {a.strides(), a.strides() + a.ndim()},
207 int data[2] = { 1, 2 };
208 ArrayClass() {
py::print(
"ArrayClass()"); }
209 ~ArrayClass() {
py::print(
"~ArrayClass()"); }
211 py::class_<ArrayClass>(sm,
"ArrayClass")
213 .def(
"numpy_view", [](py::object &obj) {
215 auto &a = obj.cast<ArrayClass&>();
216 return py::array_t<int>({2}, {4}, a.data, obj);
221 sm.def(
"function_taking_uint64", [](
uint64_t) { });
224 sm.def(
"isinstance_untyped", [](py::object yes, py::object no) {
225 return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
227 sm.def(
"isinstance_typed", [](py::object o) {
228 return py::isinstance<py::array_t<double>>(o) && !
py::isinstance<py::array_t<int>>(o);
232 sm.def(
"default_constructors", []() {
235 "array_t<int32>"_a=py::array_t<std::int32_t>(),
236 "array_t<double>"_a=py::array_t<double>()
239 sm.def(
"converting_constructors", [](py::object o) {
242 "array_t<int32>"_a=py::array_t<std::int32_t>(o),
243 "array_t<double>"_a=py::array_t<double>(o)
248 sm.def(
"overloaded", [](py::array_t<double>) {
return "double"; });
249 sm.def(
"overloaded", [](py::array_t<float>) {
return "float"; });
250 sm.def(
"overloaded", [](py::array_t<int>) {
return "int"; });
251 sm.def(
"overloaded", [](py::array_t<unsigned short>) {
return "unsigned short"; });
252 sm.def(
"overloaded", [](py::array_t<long long>) {
return "long long"; });
253 sm.def(
"overloaded", [](py::array_t<std::complex<double>>) {
return "double complex"; });
254 sm.def(
"overloaded", [](py::array_t<std::complex<float>>) {
return "float complex"; });
256 sm.def(
"overloaded2", [](py::array_t<std::complex<double>>) {
return "double complex"; });
257 sm.def(
"overloaded2", [](py::array_t<double>) {
return "double"; });
258 sm.def(
"overloaded2", [](py::array_t<std::complex<float>>) {
return "float complex"; });
259 sm.def(
"overloaded2", [](py::array_t<float>) {
return "float"; });
262 sm.def(
"overloaded3", [](py::array_t<int>) {
return "int"; },
py::arg().noconvert());
263 sm.def(
"overloaded3", [](py::array_t<double>) {
return "double"; },
py::arg().noconvert());
267 sm.def(
"overloaded4", [](py::array_t<long long, 0>) {
return "long long"; });
268 sm.def(
"overloaded4", [](py::array_t<double, 0>) {
return "double"; });
272 sm.def(
"overloaded5", [](py::array_t<unsigned int>) {
return "unsigned int"; });
273 sm.def(
"overloaded5", [](py::array_t<double>) {
return "double"; });
277 sm.def(
"issue685", [](std::string) {
return "string"; });
278 sm.def(
"issue685", [](
py::array) {
return "array"; });
279 sm.def(
"issue685", [](py::object) {
return "other"; });
282 sm.def(
"proxy_add2", [](py::array_t<double> a,
double v) {
283 auto r = a.mutable_unchecked<2>();
289 sm.def(
"proxy_init3", [](
double start) {
290 py::array_t<double, py::array::c_style>
a({ 3, 3, 3 });
291 auto r = a.mutable_unchecked<3>();
294 for (
ssize_t k = 0; k < r.shape(2); k++)
295 r(
i,
j, k) = start++;
298 sm.def(
"proxy_init3F", [](
double start) {
299 py::array_t<double, py::array::f_style>
a({ 3, 3, 3 });
300 auto r = a.mutable_unchecked<3>();
301 for (
ssize_t k = 0; k < r.shape(2); k++)
304 r(
i,
j, k) = start++;
307 sm.def(
"proxy_squared_L2_norm", [](py::array_t<double> a) {
308 auto r = a.unchecked<1>();
311 sumsq += r[
i] * r(
i);
315 sm.def(
"proxy_auxiliaries2", [](py::array_t<double> a) {
316 auto r = a.unchecked<2>();
317 auto r2 = a.mutable_unchecked<2>();
323 sm.def(
"proxy_add2_dyn", [](py::array_t<double> a,
double v) {
324 auto r = a.mutable_unchecked();
325 if (r.ndim() != 2)
throw std::domain_error(
"error: ndim != 2");
330 sm.def(
"proxy_init3_dyn", [](
double start) {
331 py::array_t<double, py::array::c_style>
a({ 3, 3, 3 });
332 auto r = a.mutable_unchecked();
333 if (r.ndim() != 3)
throw std::domain_error(
"error: ndim != 3");
336 for (
ssize_t k = 0; k < r.shape(2); k++)
337 r(
i,
j, k) = start++;
340 sm.def(
"proxy_auxiliaries2_dyn", [](py::array_t<double> a) {
341 return auxiliaries(a.unchecked(), a.mutable_unchecked());
344 sm.def(
"array_auxiliaries2", [](py::array_t<double> a) {
350 sm.def(
"array_fail_test", []() {
return py::array(py::object()); });
351 sm.def(
"array_t_fail_test", []() {
return py::array_t<double>(py::object()); });
353 sm.def(
"array_fail_test_negative_size", []() {
int c = 0;
return py::array(-1, &c); });
357 sm.def(
"array_initializer_list1", []() {
return py::array_t<float>(1); });
358 sm.def(
"array_initializer_list2", []() {
return py::array_t<float>({ 1, 2 }); });
359 sm.def(
"array_initializer_list3", []() {
return py::array_t<float>({ 1, 2, 3 }); });
360 sm.def(
"array_initializer_list4", []() {
return py::array_t<float>({ 1, 2, 3, 4 }); });
364 sm.def(
"array_reshape2", [](py::array_t<double> a) {
366 if (dim_sz * dim_sz != a.size())
367 throw std::domain_error(
"array_reshape2: input array total size is not a squared integer");
368 a.resize({dim_sz, dim_sz});
372 sm.def(
"array_resize3", [](py::array_t<double> a,
size_t N,
bool refcheck) {
373 a.resize({
N,
N, N}, refcheck);
378 sm.def(
"create_and_resize", [](
size_t N) {
379 py::array_t<double>
a;
381 std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
385 sm.def(
"index_using_ellipsis", [](
py::array a) {
390 sm.def(
"accept_double",
391 [](py::array_t<double, 0>) {},
393 sm.def(
"accept_double_forcecast",
394 [](py::array_t<double, py::array::forcecast>) {},
396 sm.def(
"accept_double_c_style",
397 [](py::array_t<double, py::array::c_style>) {},
399 sm.def(
"accept_double_c_style_forcecast",
400 [](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
402 sm.def(
"accept_double_f_style",
403 [](py::array_t<double, py::array::f_style>) {},
405 sm.def(
"accept_double_f_style_forcecast",
406 [](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
408 sm.def(
"accept_double_noconvert",
409 [](py::array_t<double, 0>) {},
411 sm.def(
"accept_double_forcecast_noconvert",
412 [](py::array_t<double, py::array::forcecast>) {},
414 sm.def(
"accept_double_c_style_noconvert",
415 [](py::array_t<double, py::array::c_style>) {},
417 sm.def(
"accept_double_c_style_forcecast_noconvert",
418 [](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
420 sm.def(
"accept_double_f_style_noconvert",
421 [](py::array_t<double, py::array::f_style>) {},
423 sm.def(
"accept_double_f_style_forcecast_noconvert",
424 [](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
void print(const Matrix &A, const string &s, ostream &stream)
ssize_t index_at_t(const arr_t &a, Ix...idx)
TEST_SUBMODULE(numpy_array, sm)
DtypeSizeCheck get_dtype_size_check()
arr data_t(const arr_t &a, Ix...index)
arr data(const arr &a, Ix...index)
py::array_t< uint16_t, 0 > arr_t
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Pose2 T2(M_PI/2.0, Point2(0.0, 2.0))
bool isinstance(handle obj)
ssize_t index_at(const arr &a, Ix...idx)
static const Line3 l(Rot3(), 1, 1)
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
py::handle auxiliaries(T &&r, T2 &&r2)
ssize_t at_t(const arr_t &a, Ix...idx)
unsigned __int64 uint64_t
Eigen::Triplet< double > T
ssize_t offset_at_t(const arr_t &a, Ix...idx)
#define def_index_fn(name, type)
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
std::vector< DtypeCheck > get_concrete_dtype_checks()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
arr_t & mutate_data_t(arr_t &a, Ix...index)
void check(bool b, bool ref)
arr_t & mutate_at_t(arr_t &a, Ix...idx)
std::vector< DtypeSizeCheck > get_platform_dtype_size_checks()
Annotation for function names.
arr & mutate_data(arr &a, Ix...index)
DtypeCheck get_dtype_check(const char *name)
ssize_t offset_at(const arr &a, Ix...idx)