Ivan Smirnov | 91b3d68 | 2016-08-29 02:41:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | tests/test_numpy_array.cpp -- test core array functionality |
| 3 | |
| 4 | Copyright (c) 2016 Ivan Smirnov <i.s.smirnov@gmail.com> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "pybind11_tests.h" |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 11 | |
Ivan Smirnov | 91b3d68 | 2016-08-29 02:41:05 +0100 | [diff] [blame] | 12 | #include <pybind11/numpy.h> |
| 13 | #include <pybind11/stl.h> |
| 14 | |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 15 | #include <cstdint> |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 16 | |
Eric Cousineau | e9ca89f | 2018-03-20 16:55:29 -0400 | [diff] [blame] | 17 | // Size / dtype checks. |
| 18 | struct DtypeCheck { |
| 19 | py::dtype numpy{}; |
| 20 | py::dtype pybind11{}; |
| 21 | }; |
| 22 | |
| 23 | template <typename T> |
| 24 | DtypeCheck get_dtype_check(const char* name) { |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 25 | py::module_ np = py::module_::import("numpy"); |
Eric Cousineau | e9ca89f | 2018-03-20 16:55:29 -0400 | [diff] [blame] | 26 | DtypeCheck check{}; |
| 27 | check.numpy = np.attr("dtype")(np.attr(name)); |
| 28 | check.pybind11 = py::dtype::of<T>(); |
| 29 | return check; |
| 30 | } |
| 31 | |
| 32 | std::vector<DtypeCheck> get_concrete_dtype_checks() { |
| 33 | return { |
| 34 | // Normalization |
| 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") |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | struct DtypeSizeCheck { |
| 47 | std::string name{}; |
| 48 | int size_cpp{}; |
| 49 | int size_numpy{}; |
| 50 | // For debugging. |
| 51 | py::dtype dtype{}; |
| 52 | }; |
| 53 | |
| 54 | template <typename T> |
| 55 | DtypeSizeCheck get_dtype_size_check() { |
| 56 | DtypeSizeCheck check{}; |
| 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>(); |
| 61 | return check; |
| 62 | } |
| 63 | |
| 64 | std::vector<DtypeSizeCheck> get_platform_dtype_size_checks() { |
| 65 | return { |
| 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>(), |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | // Arrays. |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 78 | using arr = py::array; |
| 79 | using arr_t = py::array_t<uint16_t, 0>; |
Dean Moldovan | 16afbce | 2017-03-13 19:17:18 +0100 | [diff] [blame] | 80 | static_assert(std::is_same<arr_t::value_type, uint16_t>::value, ""); |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 81 | |
Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 82 | template<typename... Ix> arr data(const arr& a, Ix... index) { |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 83 | return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...)); |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 86 | template<typename... Ix> arr data_t(const arr_t& a, Ix... index) { |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 87 | return arr(a.size() - a.index_at(index...), a.data(index...)); |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 90 | template<typename... Ix> arr& mutate_data(arr& a, Ix... index) { |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 91 | auto ptr = (uint8_t *) a.mutable_data(index...); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 92 | for (ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 93 | ptr[i] = (uint8_t) (ptr[i] * 2); |
| 94 | return a; |
| 95 | } |
| 96 | |
Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 97 | template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) { |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 98 | auto ptr = a.mutable_data(index...); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 99 | for (ssize_t i = 0; i < a.size() - a.index_at(index...); i++) |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 100 | ptr[i]++; |
| 101 | return a; |
| 102 | } |
| 103 | |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 104 | template<typename... Ix> ssize_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); } |
| 105 | template<typename... Ix> ssize_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); } |
| 106 | template<typename... Ix> ssize_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); } |
| 107 | template<typename... Ix> ssize_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); } |
| 108 | template<typename... Ix> ssize_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); } |
Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 109 | template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; } |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 110 | |
| 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); }); |
| 116 | |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 117 | template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) { |
| 118 | if (r.ndim() != 2) throw std::domain_error("error: ndim != 2"); |
| 119 | py::list l; |
| 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)); |
| 123 | l.append(r.ndim()); |
| 124 | l.append(r.itemsize()); |
| 125 | l.append(r.shape(0)); |
| 126 | l.append(r.shape(1)); |
| 127 | l.append(r.size()); |
| 128 | l.append(r.nbytes()); |
| 129 | return l.release(); |
| 130 | } |
| 131 | |
Axel Huebl | 000aabb | 2019-06-11 14:00:05 +0200 | [diff] [blame] | 132 | // note: declaration at local scope would create a dangling reference! |
| 133 | static int data_i = 42; |
| 134 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 135 | TEST_SUBMODULE(numpy_array, sm) { |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 136 | try { py::module_::import("numpy"); } |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 137 | catch (...) { return; } |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 138 | |
Eric Cousineau | e9ca89f | 2018-03-20 16:55:29 -0400 | [diff] [blame] | 139 | // test_dtypes |
| 140 | py::class_<DtypeCheck>(sm, "DtypeCheck") |
| 141 | .def_readonly("numpy", &DtypeCheck::numpy) |
| 142 | .def_readonly("pybind11", &DtypeCheck::pybind11) |
| 143 | .def("__repr__", [](const DtypeCheck& self) { |
| 144 | return py::str("<DtypeCheck numpy={} pybind11={}>").format( |
| 145 | self.numpy, self.pybind11); |
| 146 | }); |
| 147 | sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks); |
| 148 | |
| 149 | py::class_<DtypeSizeCheck>(sm, "DtypeSizeCheck") |
| 150 | .def_readonly("name", &DtypeSizeCheck::name) |
| 151 | .def_readonly("size_cpp", &DtypeSizeCheck::size_cpp) |
| 152 | .def_readonly("size_numpy", &DtypeSizeCheck::size_numpy) |
| 153 | .def("__repr__", [](const DtypeSizeCheck& self) { |
| 154 | return py::str("<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>").format( |
| 155 | self.name, self.size_cpp, self.size_numpy, self.dtype); |
| 156 | }); |
| 157 | sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks); |
| 158 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 159 | // test_array_attributes |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 160 | sm.def("ndim", [](const arr& a) { return a.ndim(); }); |
| 161 | sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); }); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 162 | sm.def("shape", [](const arr& a, ssize_t dim) { return a.shape(dim); }); |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 163 | sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); }); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 164 | sm.def("strides", [](const arr& a, ssize_t dim) { return a.strides(dim); }); |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 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(); }); |
| 170 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 171 | // test_index_offset |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 172 | def_index_fn(index_at, const arr&); |
| 173 | def_index_fn(index_at_t, const arr_t&); |
| 174 | def_index_fn(offset_at, const arr&); |
| 175 | def_index_fn(offset_at_t, const arr_t&); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 176 | // test_data |
| 177 | def_index_fn(data, const arr&); |
| 178 | def_index_fn(data_t, const arr_t&); |
| 179 | // test_mutate_data, test_mutate_readonly |
Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 180 | def_index_fn(mutate_data, arr&); |
| 181 | def_index_fn(mutate_data_t, arr_t&); |
| 182 | def_index_fn(at_t, const arr_t&); |
| 183 | def_index_fn(mutate_at_t, arr_t&); |
Wenzel Jakob | 43f6aa6 | 2016-10-12 23:34:06 +0200 | [diff] [blame] | 184 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 185 | // test_make_c_f_array |
| 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 }); }); |
Wenzel Jakob | 43f6aa6 | 2016-10-12 23:34:06 +0200 | [diff] [blame] | 188 | |
Naotoshi Seo | 5ef1af1 | 2018-05-06 13:59:25 +0000 | [diff] [blame] | 189 | // test_empty_shaped_array |
| 190 | sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); }); |
Axel Huebl | 000aabb | 2019-06-11 14:00:05 +0200 | [diff] [blame] | 191 | // test numpy scalars (empty shape, ndim==0) |
| 192 | sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); }); |
Naotoshi Seo | 5ef1af1 | 2018-05-06 13:59:25 +0000 | [diff] [blame] | 193 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 194 | // test_wrap |
Wenzel Jakob | 369e9b3 | 2016-10-13 00:57:42 +0200 | [diff] [blame] | 195 | sm.def("wrap", [](py::array a) { |
| 196 | return py::array( |
| 197 | a.dtype(), |
Jason Rhinelander | 5f38386 | 2017-04-07 15:49:54 -0400 | [diff] [blame] | 198 | {a.shape(), a.shape() + a.ndim()}, |
| 199 | {a.strides(), a.strides() + a.ndim()}, |
Wenzel Jakob | 369e9b3 | 2016-10-13 00:57:42 +0200 | [diff] [blame] | 200 | a.data(), |
| 201 | a |
| 202 | ); |
| 203 | }); |
Wenzel Jakob | fac7c09 | 2016-10-13 10:37:52 +0200 | [diff] [blame] | 204 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 205 | // test_numpy_view |
Wenzel Jakob | fac7c09 | 2016-10-13 10:37:52 +0200 | [diff] [blame] | 206 | struct ArrayClass { |
| 207 | int data[2] = { 1, 2 }; |
| 208 | ArrayClass() { py::print("ArrayClass()"); } |
| 209 | ~ArrayClass() { py::print("~ArrayClass()"); } |
| 210 | }; |
Wenzel Jakob | fac7c09 | 2016-10-13 10:37:52 +0200 | [diff] [blame] | 211 | py::class_<ArrayClass>(sm, "ArrayClass") |
| 212 | .def(py::init<>()) |
| 213 | .def("numpy_view", [](py::object &obj) { |
| 214 | py::print("ArrayClass::numpy_view()"); |
Henry Schreiner | ce88e94 | 2020-09-10 23:20:47 -0400 | [diff] [blame] | 215 | auto &a = obj.cast<ArrayClass&>(); |
Wenzel Jakob | fac7c09 | 2016-10-13 10:37:52 +0200 | [diff] [blame] | 216 | return py::array_t<int>({2}, {4}, a.data, obj); |
| 217 | } |
| 218 | ); |
Wenzel Jakob | 496feac | 2016-10-28 00:37:07 +0200 | [diff] [blame] | 219 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 220 | // test_cast_numpy_int64_to_uint64 |
Wenzel Jakob | 030d10e | 2016-10-28 01:23:42 +0200 | [diff] [blame] | 221 | sm.def("function_taking_uint64", [](uint64_t) { }); |
Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 222 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 223 | // test_isinstance |
Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 224 | sm.def("isinstance_untyped", [](py::object yes, py::object no) { |
| 225 | return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no); |
| 226 | }); |
Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 227 | sm.def("isinstance_typed", [](py::object o) { |
| 228 | return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o); |
| 229 | }); |
| 230 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 231 | // test_constructors |
Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 232 | sm.def("default_constructors", []() { |
| 233 | return py::dict( |
| 234 | "array"_a=py::array(), |
| 235 | "array_t<int32>"_a=py::array_t<std::int32_t>(), |
| 236 | "array_t<double>"_a=py::array_t<double>() |
| 237 | ); |
| 238 | }); |
Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 239 | sm.def("converting_constructors", [](py::object o) { |
| 240 | return py::dict( |
| 241 | "array"_a=py::array(o), |
| 242 | "array_t<int32>"_a=py::array_t<std::int32_t>(o), |
| 243 | "array_t<double>"_a=py::array_t<double>(o) |
| 244 | ); |
| 245 | }); |
Jason Rhinelander | ee2e5a5 | 2017-02-24 05:33:31 -0500 | [diff] [blame] | 246 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 247 | // test_overload_resolution |
Jason Rhinelander | c44fe6f | 2017-02-26 18:03:00 -0500 | [diff] [blame] | 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"; }); |
| 255 | |
| 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"; }); |
| 260 | |
| 261 | // Only accept the exact types: |
| 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()); |
| 264 | |
| 265 | // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but |
| 266 | // rather that float gets converted via the safe (conversion to double) overload: |
| 267 | sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; }); |
| 268 | sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; }); |
| 269 | |
| 270 | // But we do allow conversion to int if forcecast is enabled (but only if no overload matches |
| 271 | // without conversion) |
| 272 | sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; }); |
| 273 | sm.def("overloaded5", [](py::array_t<double>) { return "double"; }); |
| 274 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 275 | // test_greedy_string_overload |
Jason Rhinelander | ee2e5a5 | 2017-02-24 05:33:31 -0500 | [diff] [blame] | 276 | // Issue 685: ndarray shouldn't go to std::string overload |
| 277 | sm.def("issue685", [](std::string) { return "string"; }); |
| 278 | sm.def("issue685", [](py::array) { return "array"; }); |
| 279 | sm.def("issue685", [](py::object) { return "other"; }); |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 280 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 281 | // test_array_unchecked_fixed_dims |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 282 | sm.def("proxy_add2", [](py::array_t<double> a, double v) { |
| 283 | auto r = a.mutable_unchecked<2>(); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 284 | for (ssize_t i = 0; i < r.shape(0); i++) |
| 285 | for (ssize_t j = 0; j < r.shape(1); j++) |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 286 | r(i, j) += v; |
| 287 | }, py::arg().noconvert(), py::arg()); |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 288 | |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 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>(); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 292 | for (ssize_t i = 0; i < r.shape(0); i++) |
| 293 | for (ssize_t j = 0; j < r.shape(1); j++) |
| 294 | for (ssize_t k = 0; k < r.shape(2); k++) |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 295 | r(i, j, k) = start++; |
| 296 | return a; |
| 297 | }); |
| 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>(); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 301 | for (ssize_t k = 0; k < r.shape(2); k++) |
| 302 | for (ssize_t j = 0; j < r.shape(1); j++) |
| 303 | for (ssize_t i = 0; i < r.shape(0); i++) |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 304 | r(i, j, k) = start++; |
| 305 | return a; |
| 306 | }); |
| 307 | sm.def("proxy_squared_L2_norm", [](py::array_t<double> a) { |
| 308 | auto r = a.unchecked<1>(); |
| 309 | double sumsq = 0; |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 310 | for (ssize_t i = 0; i < r.shape(0); i++) |
Jason Rhinelander | 423a49b | 2017-03-19 01:14:23 -0300 | [diff] [blame] | 311 | sumsq += r[i] * r(i); // Either notation works for a 1D array |
| 312 | return sumsq; |
| 313 | }); |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 314 | |
| 315 | sm.def("proxy_auxiliaries2", [](py::array_t<double> a) { |
| 316 | auto r = a.unchecked<2>(); |
| 317 | auto r2 = a.mutable_unchecked<2>(); |
| 318 | return auxiliaries(r, r2); |
| 319 | }); |
| 320 | |
Yannick Jadoul | 56784c4 | 2020-10-02 19:07:04 +0200 | [diff] [blame] | 321 | sm.def("proxy_auxiliaries1_const_ref", [](py::array_t<double> a) { |
| 322 | const auto &r = a.unchecked<1>(); |
| 323 | const auto &r2 = a.mutable_unchecked<1>(); |
| 324 | return r(0) == r2(0) && r[0] == r2[0]; |
| 325 | }); |
| 326 | |
| 327 | sm.def("proxy_auxiliaries2_const_ref", [](py::array_t<double> a) { |
| 328 | const auto &r = a.unchecked<2>(); |
| 329 | const auto &r2 = a.mutable_unchecked<2>(); |
| 330 | return r(0, 0) == r2(0, 0); |
| 331 | }); |
| 332 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 333 | // test_array_unchecked_dyn_dims |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 334 | // Same as the above, but without a compile-time dimensions specification: |
| 335 | sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) { |
| 336 | auto r = a.mutable_unchecked(); |
| 337 | if (r.ndim() != 2) throw std::domain_error("error: ndim != 2"); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 338 | for (ssize_t i = 0; i < r.shape(0); i++) |
| 339 | for (ssize_t j = 0; j < r.shape(1); j++) |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 340 | r(i, j) += v; |
| 341 | }, py::arg().noconvert(), py::arg()); |
| 342 | sm.def("proxy_init3_dyn", [](double start) { |
| 343 | py::array_t<double, py::array::c_style> a({ 3, 3, 3 }); |
| 344 | auto r = a.mutable_unchecked(); |
| 345 | if (r.ndim() != 3) throw std::domain_error("error: ndim != 3"); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 346 | for (ssize_t i = 0; i < r.shape(0); i++) |
| 347 | for (ssize_t j = 0; j < r.shape(1); j++) |
| 348 | for (ssize_t k = 0; k < r.shape(2); k++) |
Jason Rhinelander | 773339f | 2017-03-20 17:48:38 -0300 | [diff] [blame] | 349 | r(i, j, k) = start++; |
| 350 | return a; |
| 351 | }); |
| 352 | sm.def("proxy_auxiliaries2_dyn", [](py::array_t<double> a) { |
| 353 | return auxiliaries(a.unchecked(), a.mutable_unchecked()); |
| 354 | }); |
| 355 | |
| 356 | sm.def("array_auxiliaries2", [](py::array_t<double> a) { |
| 357 | return auxiliaries(a, a); |
| 358 | }); |
Jason Rhinelander | 5749b50 | 2017-04-10 11:05:26 -0400 | [diff] [blame] | 359 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 360 | // test_array_failures |
Jason Rhinelander | 5749b50 | 2017-04-10 11:05:26 -0400 | [diff] [blame] | 361 | // Issue #785: Uninformative "Unknown internal error" exception when constructing array from empty object: |
| 362 | sm.def("array_fail_test", []() { return py::array(py::object()); }); |
| 363 | sm.def("array_t_fail_test", []() { return py::array_t<double>(py::object()); }); |
Cris Luengo | 30d43c4 | 2017-04-14 14:33:44 -0600 | [diff] [blame] | 364 | // Make sure the error from numpy is being passed through: |
| 365 | sm.def("array_fail_test_negative_size", []() { int c = 0; return py::array(-1, &c); }); |
| 366 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 367 | // test_initializer_list |
Jason Rhinelander | 51d18aa | 2017-04-28 11:06:16 -0400 | [diff] [blame] | 368 | // Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 369 | sm.def("array_initializer_list1", []() { return py::array_t<float>(1); }); // { 1 } also works, but clang warns about it |
| 370 | sm.def("array_initializer_list2", []() { return py::array_t<float>({ 1, 2 }); }); |
| 371 | sm.def("array_initializer_list3", []() { return py::array_t<float>({ 1, 2, 3 }); }); |
| 372 | sm.def("array_initializer_list4", []() { return py::array_t<float>({ 1, 2, 3, 4 }); }); |
uentity | 083a021 | 2017-04-13 21:41:55 +0500 | [diff] [blame] | 373 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 374 | // test_array_resize |
uentity | 083a021 | 2017-04-13 21:41:55 +0500 | [diff] [blame] | 375 | // reshape array to 2D without changing size |
| 376 | sm.def("array_reshape2", [](py::array_t<double> a) { |
Henry Schreiner | ce88e94 | 2020-09-10 23:20:47 -0400 | [diff] [blame] | 377 | const auto dim_sz = (ssize_t)std::sqrt(a.size()); |
uentity | 083a021 | 2017-04-13 21:41:55 +0500 | [diff] [blame] | 378 | if (dim_sz * dim_sz != a.size()) |
| 379 | throw std::domain_error("array_reshape2: input array total size is not a squared integer"); |
| 380 | a.resize({dim_sz, dim_sz}); |
| 381 | }); |
| 382 | |
| 383 | // resize to 3D array with each dimension = N |
| 384 | sm.def("array_resize3", [](py::array_t<double> a, size_t N, bool refcheck) { |
| 385 | a.resize({N, N, N}, refcheck); |
| 386 | }); |
| 387 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 388 | // test_array_create_and_resize |
uentity | 083a021 | 2017-04-13 21:41:55 +0500 | [diff] [blame] | 389 | // return 2D array with Nrows = Ncols = N |
| 390 | sm.def("create_and_resize", [](size_t N) { |
| 391 | py::array_t<double> a; |
| 392 | a.resize({N, N}); |
| 393 | std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.); |
| 394 | return a; |
| 395 | }); |
Wenzel Jakob | d4b37a2 | 2018-08-28 00:23:59 +0200 | [diff] [blame] | 396 | |
Yannick Jadoul | 3e448c0 | 2020-08-04 14:45:55 +0200 | [diff] [blame] | 397 | sm.def("index_using_ellipsis", [](py::array a) { |
| 398 | return a[py::make_tuple(0, py::ellipsis(), 0)]; |
| 399 | }); |
Yannick Jadoul | 9df1383 | 2020-09-15 14:50:51 +0200 | [diff] [blame] | 400 | |
| 401 | // test_argument_conversions |
| 402 | sm.def("accept_double", |
| 403 | [](py::array_t<double, 0>) {}, |
| 404 | py::arg("a")); |
| 405 | sm.def("accept_double_forcecast", |
| 406 | [](py::array_t<double, py::array::forcecast>) {}, |
| 407 | py::arg("a")); |
| 408 | sm.def("accept_double_c_style", |
| 409 | [](py::array_t<double, py::array::c_style>) {}, |
| 410 | py::arg("a")); |
| 411 | sm.def("accept_double_c_style_forcecast", |
| 412 | [](py::array_t<double, py::array::forcecast | py::array::c_style>) {}, |
| 413 | py::arg("a")); |
| 414 | sm.def("accept_double_f_style", |
| 415 | [](py::array_t<double, py::array::f_style>) {}, |
| 416 | py::arg("a")); |
| 417 | sm.def("accept_double_f_style_forcecast", |
| 418 | [](py::array_t<double, py::array::forcecast | py::array::f_style>) {}, |
| 419 | py::arg("a")); |
| 420 | sm.def("accept_double_noconvert", |
| 421 | [](py::array_t<double, 0>) {}, |
| 422 | py::arg("a").noconvert()); |
| 423 | sm.def("accept_double_forcecast_noconvert", |
| 424 | [](py::array_t<double, py::array::forcecast>) {}, |
| 425 | py::arg("a").noconvert()); |
| 426 | sm.def("accept_double_c_style_noconvert", |
| 427 | [](py::array_t<double, py::array::c_style>) {}, |
| 428 | py::arg("a").noconvert()); |
| 429 | sm.def("accept_double_c_style_forcecast_noconvert", |
| 430 | [](py::array_t<double, py::array::forcecast | py::array::c_style>) {}, |
| 431 | py::arg("a").noconvert()); |
| 432 | sm.def("accept_double_f_style_noconvert", |
| 433 | [](py::array_t<double, py::array::f_style>) {}, |
| 434 | py::arg("a").noconvert()); |
| 435 | sm.def("accept_double_f_style_forcecast_noconvert", |
| 436 | [](py::array_t<double, py::array::forcecast | py::array::f_style>) {}, |
| 437 | py::arg("a").noconvert()); |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 438 | } |