You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.3 KiB

  1. /*
  2. tests/test_numpy_array.cpp -- test core array functionality
  3. Copyright (c) 2016 Ivan Smirnov <i.s.smirnov@gmail.com>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include <pybind11/numpy.h>
  9. #include <pybind11/stl.h>
  10. #include <cstdint>
  11. #include <vector>
  12. using arr = py::array;
  13. using arr_t = py::array_t<uint16_t, 0>;
  14. template<typename... Ix> arr data(const arr& a, Ix... index) {
  15. return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...));
  16. }
  17. template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
  18. return arr(a.size() - a.index_at(index...), a.data(index...));
  19. }
  20. arr& mutate_data(arr& a) {
  21. auto ptr = (uint8_t *) a.mutable_data();
  22. for (size_t i = 0; i < a.nbytes(); i++)
  23. ptr[i] = (uint8_t) (ptr[i] * 2);
  24. return a;
  25. }
  26. arr_t& mutate_data_t(arr_t& a) {
  27. auto ptr = a.mutable_data();
  28. for (size_t i = 0; i < a.size(); i++)
  29. ptr[i]++;
  30. return a;
  31. }
  32. template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
  33. auto ptr = (uint8_t *) a.mutable_data(index...);
  34. for (size_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
  35. ptr[i] = (uint8_t) (ptr[i] * 2);
  36. return a;
  37. }
  38. template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) {
  39. auto ptr = a.mutable_data(index...);
  40. for (size_t i = 0; i < a.size() - a.index_at(index...); i++)
  41. ptr[i]++;
  42. return a;
  43. }
  44. template<typename... Ix> size_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
  45. template<typename... Ix> size_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
  46. template<typename... Ix> size_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
  47. template<typename... Ix> size_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
  48. template<typename... Ix> size_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
  49. template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; }
  50. #define def_index_fn(name, type) \
  51. sm.def(#name, [](type a) { return name(a); }); \
  52. sm.def(#name, [](type a, int i) { return name(a, i); }); \
  53. sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \
  54. sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); });
  55. test_initializer numpy_array([](py::module &m) {
  56. auto sm = m.def_submodule("array");
  57. sm.def("ndim", [](const arr& a) { return a.ndim(); });
  58. sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); });
  59. sm.def("shape", [](const arr& a, size_t dim) { return a.shape(dim); });
  60. sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); });
  61. sm.def("strides", [](const arr& a, size_t dim) { return a.strides(dim); });
  62. sm.def("writeable", [](const arr& a) { return a.writeable(); });
  63. sm.def("size", [](const arr& a) { return a.size(); });
  64. sm.def("itemsize", [](const arr& a) { return a.itemsize(); });
  65. sm.def("nbytes", [](const arr& a) { return a.nbytes(); });
  66. sm.def("owndata", [](const arr& a) { return a.owndata(); });
  67. def_index_fn(data, const arr&);
  68. def_index_fn(data_t, const arr_t&);
  69. def_index_fn(index_at, const arr&);
  70. def_index_fn(index_at_t, const arr_t&);
  71. def_index_fn(offset_at, const arr&);
  72. def_index_fn(offset_at_t, const arr_t&);
  73. def_index_fn(mutate_data, arr&);
  74. def_index_fn(mutate_data_t, arr_t&);
  75. def_index_fn(at_t, const arr_t&);
  76. def_index_fn(mutate_at_t, arr_t&);
  77. sm.def("make_f_array", [] {
  78. return py::array_t<float>({ 2, 2 }, { 4, 8 });
  79. });
  80. sm.def("make_c_array", [] {
  81. return py::array_t<float>({ 2, 2 }, { 8, 4 });
  82. });
  83. sm.def("wrap", [](py::array a) {
  84. return py::array(
  85. a.dtype(),
  86. std::vector<size_t>(a.shape(), a.shape() + a.ndim()),
  87. std::vector<size_t>(a.strides(), a.strides() + a.ndim()),
  88. a.data(),
  89. a
  90. );
  91. });
  92. struct ArrayClass {
  93. int data[2] = { 1, 2 };
  94. ArrayClass() { py::print("ArrayClass()"); }
  95. ~ArrayClass() { py::print("~ArrayClass()"); }
  96. };
  97. py::class_<ArrayClass>(sm, "ArrayClass")
  98. .def(py::init<>())
  99. .def("numpy_view", [](py::object &obj) {
  100. py::print("ArrayClass::numpy_view()");
  101. ArrayClass &a = obj.cast<ArrayClass&>();
  102. return py::array_t<int>({2}, {4}, a.data, obj);
  103. }
  104. );
  105. sm.def("function_taking_uint64", [](uint64_t) { });
  106. sm.def("isinstance_untyped", [](py::object yes, py::object no) {
  107. return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
  108. });
  109. sm.def("isinstance_typed", [](py::object o) {
  110. return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
  111. });
  112. sm.def("default_constructors", []() {
  113. return py::dict(
  114. "array"_a=py::array(),
  115. "array_t<int32>"_a=py::array_t<std::int32_t>(),
  116. "array_t<double>"_a=py::array_t<double>()
  117. );
  118. });
  119. sm.def("converting_constructors", [](py::object o) {
  120. return py::dict(
  121. "array"_a=py::array(o),
  122. "array_t<int32>"_a=py::array_t<std::int32_t>(o),
  123. "array_t<double>"_a=py::array_t<double>(o)
  124. );
  125. });
  126. });