test_numpy_vectorize.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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").def(py::init<int>());
  43. m.def("vec_passthrough", py::vectorize(
  44. [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
  45. return *a + b + c.at(0) + d + e + f.value + g;
  46. }
  47. ));
  48. // test_method_vectorization
  49. struct VectorizeTestClass {
  50. VectorizeTestClass(int v) : value{v} {};
  51. float method(int x, float y) { return y + (float) (x + value); }
  52. int value = 0;
  53. };
  54. py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
  55. vtc .def(py::init<int>())
  56. .def_readwrite("value", &VectorizeTestClass::value);
  57. // Automatic vectorizing of methods
  58. vtc.def("method", py::vectorize(&VectorizeTestClass::method));
  59. // test_trivial_broadcasting
  60. // Internal optimization test for whether the input is trivially broadcastable:
  61. py::enum_<py::detail::broadcast_trivial>(m, "trivial")
  62. .value("f_trivial", py::detail::broadcast_trivial::f_trivial)
  63. .value("c_trivial", py::detail::broadcast_trivial::c_trivial)
  64. .value("non_trivial", py::detail::broadcast_trivial::non_trivial);
  65. m.def("vectorized_is_trivial", [](
  66. py::array_t<int, py::array::forcecast> arg1,
  67. py::array_t<float, py::array::forcecast> arg2,
  68. py::array_t<double, py::array::forcecast> arg3
  69. ) {
  70. ssize_t ndim;
  71. std::vector<ssize_t> shape;
  72. std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
  73. return py::detail::broadcast(buffers, ndim, shape);
  74. });
  75. }