| 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> |
| 16 | #include <vector> |
| 17 | |
| 18 | using arr = py::array; |
| 19 | using arr_t = py::array_t<uint16_t, 0>; |
| Dean Moldovan | 16afbce | 2017-03-13 19:17:18 +0100 | [diff] [blame] | 20 | static_assert(std::is_same<arr_t::value_type, uint16_t>::value, ""); |
| Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 21 | |
| Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 22 | template<typename... Ix> arr data(const arr& a, Ix... index) { |
| Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 23 | return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...)); |
| 24 | } |
| 25 | |
| Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 26 | template<typename... Ix> arr data_t(const arr_t& a, Ix... index) { |
| Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 27 | return arr(a.size() - a.index_at(index...), a.data(index...)); |
| 28 | } |
| 29 | |
| 30 | arr& mutate_data(arr& a) { |
| 31 | auto ptr = (uint8_t *) a.mutable_data(); |
| 32 | for (size_t i = 0; i < a.nbytes(); i++) |
| 33 | ptr[i] = (uint8_t) (ptr[i] * 2); |
| 34 | return a; |
| 35 | } |
| 36 | |
| 37 | arr_t& mutate_data_t(arr_t& a) { |
| 38 | auto ptr = a.mutable_data(); |
| 39 | for (size_t i = 0; i < a.size(); i++) |
| 40 | ptr[i]++; |
| 41 | return a; |
| 42 | } |
| 43 | |
| Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 44 | template<typename... Ix> arr& mutate_data(arr& a, Ix... index) { |
| Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 45 | auto ptr = (uint8_t *) a.mutable_data(index...); |
| 46 | for (size_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) |
| 47 | ptr[i] = (uint8_t) (ptr[i] * 2); |
| 48 | return a; |
| 49 | } |
| 50 | |
| Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 51 | 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] | 52 | auto ptr = a.mutable_data(index...); |
| 53 | for (size_t i = 0; i < a.size() - a.index_at(index...); i++) |
| 54 | ptr[i]++; |
| 55 | return a; |
| 56 | } |
| 57 | |
| Sylvain Corlay | 5027c4f | 2016-11-16 08:53:37 -0800 | [diff] [blame] | 58 | template<typename... Ix> size_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); } |
| 59 | template<typename... Ix> size_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); } |
| 60 | template<typename... Ix> size_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); } |
| 61 | template<typename... Ix> size_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); } |
| 62 | template<typename... Ix> size_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); } |
| 63 | 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] | 64 | |
| 65 | #define def_index_fn(name, type) \ |
| 66 | sm.def(#name, [](type a) { return name(a); }); \ |
| 67 | sm.def(#name, [](type a, int i) { return name(a, i); }); \ |
| 68 | sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \ |
| 69 | sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); }); |
| 70 | |
| Ivan Smirnov | 91b3d68 | 2016-08-29 02:41:05 +0100 | [diff] [blame] | 71 | test_initializer numpy_array([](py::module &m) { |
| Ivan Smirnov | aca6bca | 2016-09-08 23:03:35 +0100 | [diff] [blame] | 72 | auto sm = m.def_submodule("array"); |
| 73 | |
| 74 | sm.def("ndim", [](const arr& a) { return a.ndim(); }); |
| 75 | sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); }); |
| 76 | sm.def("shape", [](const arr& a, size_t dim) { return a.shape(dim); }); |
| 77 | sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); }); |
| 78 | sm.def("strides", [](const arr& a, size_t dim) { return a.strides(dim); }); |
| 79 | sm.def("writeable", [](const arr& a) { return a.writeable(); }); |
| 80 | sm.def("size", [](const arr& a) { return a.size(); }); |
| 81 | sm.def("itemsize", [](const arr& a) { return a.itemsize(); }); |
| 82 | sm.def("nbytes", [](const arr& a) { return a.nbytes(); }); |
| 83 | sm.def("owndata", [](const arr& a) { return a.owndata(); }); |
| 84 | |
| 85 | def_index_fn(data, const arr&); |
| 86 | def_index_fn(data_t, const arr_t&); |
| 87 | def_index_fn(index_at, const arr&); |
| 88 | def_index_fn(index_at_t, const arr_t&); |
| 89 | def_index_fn(offset_at, const arr&); |
| 90 | def_index_fn(offset_at_t, const arr_t&); |
| 91 | def_index_fn(mutate_data, arr&); |
| 92 | def_index_fn(mutate_data_t, arr_t&); |
| 93 | def_index_fn(at_t, const arr_t&); |
| 94 | def_index_fn(mutate_at_t, arr_t&); |
| Wenzel Jakob | 43f6aa6 | 2016-10-12 23:34:06 +0200 | [diff] [blame] | 95 | |
| 96 | sm.def("make_f_array", [] { |
| 97 | return py::array_t<float>({ 2, 2 }, { 4, 8 }); |
| 98 | }); |
| 99 | |
| 100 | sm.def("make_c_array", [] { |
| 101 | return py::array_t<float>({ 2, 2 }, { 8, 4 }); |
| 102 | }); |
| Wenzel Jakob | 369e9b3 | 2016-10-13 00:57:42 +0200 | [diff] [blame] | 103 | |
| 104 | sm.def("wrap", [](py::array a) { |
| 105 | return py::array( |
| 106 | a.dtype(), |
| 107 | std::vector<size_t>(a.shape(), a.shape() + a.ndim()), |
| 108 | std::vector<size_t>(a.strides(), a.strides() + a.ndim()), |
| 109 | a.data(), |
| 110 | a |
| 111 | ); |
| 112 | }); |
| Wenzel Jakob | fac7c09 | 2016-10-13 10:37:52 +0200 | [diff] [blame] | 113 | |
| 114 | struct ArrayClass { |
| 115 | int data[2] = { 1, 2 }; |
| 116 | ArrayClass() { py::print("ArrayClass()"); } |
| 117 | ~ArrayClass() { py::print("~ArrayClass()"); } |
| 118 | }; |
| 119 | |
| 120 | py::class_<ArrayClass>(sm, "ArrayClass") |
| 121 | .def(py::init<>()) |
| 122 | .def("numpy_view", [](py::object &obj) { |
| 123 | py::print("ArrayClass::numpy_view()"); |
| 124 | ArrayClass &a = obj.cast<ArrayClass&>(); |
| 125 | return py::array_t<int>({2}, {4}, a.data, obj); |
| 126 | } |
| 127 | ); |
| Wenzel Jakob | 496feac | 2016-10-28 00:37:07 +0200 | [diff] [blame] | 128 | |
| Wenzel Jakob | 030d10e | 2016-10-28 01:23:42 +0200 | [diff] [blame] | 129 | sm.def("function_taking_uint64", [](uint64_t) { }); |
| Dean Moldovan | 4de2710 | 2016-11-16 01:35:22 +0100 | [diff] [blame] | 130 | |
| 131 | sm.def("isinstance_untyped", [](py::object yes, py::object no) { |
| 132 | return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no); |
| 133 | }); |
| 134 | |
| 135 | sm.def("isinstance_typed", [](py::object o) { |
| 136 | return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o); |
| 137 | }); |
| 138 | |
| 139 | sm.def("default_constructors", []() { |
| 140 | return py::dict( |
| 141 | "array"_a=py::array(), |
| 142 | "array_t<int32>"_a=py::array_t<std::int32_t>(), |
| 143 | "array_t<double>"_a=py::array_t<double>() |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | sm.def("converting_constructors", [](py::object o) { |
| 148 | return py::dict( |
| 149 | "array"_a=py::array(o), |
| 150 | "array_t<int32>"_a=py::array_t<std::int32_t>(o), |
| 151 | "array_t<double>"_a=py::array_t<double>(o) |
| 152 | ); |
| 153 | }); |
| Jason Rhinelander | ee2e5a5 | 2017-02-24 05:33:31 -0500 | [diff] [blame] | 154 | |
| Jason Rhinelander | c44fe6f | 2017-02-26 18:03:00 -0500 | [diff] [blame] | 155 | // Overload resolution tests: |
| 156 | sm.def("overloaded", [](py::array_t<double>) { return "double"; }); |
| 157 | sm.def("overloaded", [](py::array_t<float>) { return "float"; }); |
| 158 | sm.def("overloaded", [](py::array_t<int>) { return "int"; }); |
| 159 | sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; }); |
| 160 | sm.def("overloaded", [](py::array_t<long long>) { return "long long"; }); |
| 161 | sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; }); |
| 162 | sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; }); |
| 163 | |
| 164 | sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; }); |
| 165 | sm.def("overloaded2", [](py::array_t<double>) { return "double"; }); |
| 166 | sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; }); |
| 167 | sm.def("overloaded2", [](py::array_t<float>) { return "float"; }); |
| 168 | |
| 169 | // Only accept the exact types: |
| 170 | sm.def("overloaded3", [](py::array_t<int>) { return "int"; }, py::arg().noconvert()); |
| 171 | sm.def("overloaded3", [](py::array_t<double>) { return "double"; }, py::arg().noconvert()); |
| 172 | |
| 173 | // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but |
| 174 | // rather that float gets converted via the safe (conversion to double) overload: |
| 175 | sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; }); |
| 176 | sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; }); |
| 177 | |
| 178 | // But we do allow conversion to int if forcecast is enabled (but only if no overload matches |
| 179 | // without conversion) |
| 180 | sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; }); |
| 181 | sm.def("overloaded5", [](py::array_t<double>) { return "double"; }); |
| 182 | |
| Jason Rhinelander | ee2e5a5 | 2017-02-24 05:33:31 -0500 | [diff] [blame] | 183 | // Issue 685: ndarray shouldn't go to std::string overload |
| 184 | sm.def("issue685", [](std::string) { return "string"; }); |
| 185 | sm.def("issue685", [](py::array) { return "array"; }); |
| 186 | sm.def("issue685", [](py::object) { return "other"; }); |
| Ivan Smirnov | 91b3d68 | 2016-08-29 02:41:05 +0100 | [diff] [blame] | 187 | }); |