test_numpy_vectorize.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
  3. arguments
  4. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include <pybind11/numpy.h>
  10. double my_func(int x, float y, double z) {
  11. py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z));
  12. return (float) x*y*z;
  13. }
  14. TEST_SUBMODULE(numpy_vectorize, m) {
  15. try { py::module_::import("numpy"); }
  16. catch (...) { return; }
  17. // test_vectorize, test_docs, test_array_collapse
  18. // Vectorize all arguments of a function (though non-vector arguments are also allowed)
  19. m.def("vectorized_func", py::vectorize(my_func));
  20. // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
  21. m.def("vectorized_func2",
  22. [](py::array_t<int> x, py::array_t<float> y, float z) {
  23. return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
  24. }
  25. );
  26. // Vectorize a complex-valued function
  27. m.def("vectorized_func3", py::vectorize(
  28. [](std::complex<double> c) { return c * std::complex<double>(2.f); }
  29. ));
  30. // test_type_selection
  31. // NumPy function which only accepts specific data types
  32. m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
  33. m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
  34. m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
  35. // test_passthrough_arguments
  36. // Passthrough test: references and non-pod types should be automatically passed through (in the
  37. // function definition below, only `b`, `d`, and `g` are vectorized):
  38. struct NonPODClass {
  39. NonPODClass(int v) : value{v} {}
  40. int value;
  41. };
  42. py::class_<NonPODClass>(m, "NonPODClass")
  43. .def(py::init<int>())
  44. .def_readwrite("value", &NonPODClass::value);
  45. m.def("vec_passthrough", py::vectorize(
  46. [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
  47. return *a + b + c.at(0) + d + e + f.value + g;
  48. }
  49. ));
  50. // test_method_vectorization
  51. struct VectorizeTestClass {
  52. VectorizeTestClass(int v) : value{v} {};
  53. float method(int x, float y) { return y + (float) (x + value); }
  54. int value = 0;
  55. };
  56. py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
  57. vtc .def(py::init<int>())
  58. .def_readwrite("value", &VectorizeTestClass::value);
  59. // Automatic vectorizing of methods
  60. vtc.def("method", py::vectorize(&VectorizeTestClass::method));
  61. // test_trivial_broadcasting
  62. // Internal optimization test for whether the input is trivially broadcastable:
  63. py::enum_<py::detail::broadcast_trivial>(m, "trivial")
  64. .value("f_trivial", py::detail::broadcast_trivial::f_trivial)
  65. .value("c_trivial", py::detail::broadcast_trivial::c_trivial)
  66. .value("non_trivial", py::detail::broadcast_trivial::non_trivial);
  67. m.def("vectorized_is_trivial", [](
  68. py::array_t<int, py::array::forcecast> arg1,
  69. py::array_t<float, py::array::forcecast> arg2,
  70. py::array_t<double, py::array::forcecast> arg3
  71. ) {
  72. py::ssize_t ndim;
  73. std::vector<py::ssize_t> shape;
  74. std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
  75. return py::detail::broadcast(buffers, ndim, shape);
  76. });
  77. m.def("add_to", py::vectorize([](NonPODClass& x, int a) { x.value += a; }));
  78. }