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.

85 lines
3.4 KiB

8 years ago
8 years ago
8 years ago
  1. /*
  2. tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
  3. arguments
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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. std::complex<double> my_func3(std::complex<double> c) {
  15. return c * std::complex<double>(2.f);
  16. }
  17. struct VectorizeTestClass {
  18. VectorizeTestClass(int v) : value{v} {};
  19. float method(int x, float y) { return y + (float) (x + value); }
  20. int value = 0;
  21. };
  22. struct NonPODClass {
  23. NonPODClass(int v) : value{v} {}
  24. int value;
  25. };
  26. test_initializer numpy_vectorize([](py::module &m) {
  27. // Vectorize all arguments of a function (though non-vector arguments are also allowed)
  28. m.def("vectorized_func", py::vectorize(my_func));
  29. // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
  30. m.def("vectorized_func2",
  31. [](py::array_t<int> x, py::array_t<float> y, float z) {
  32. return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
  33. }
  34. );
  35. // Vectorize a complex-valued function
  36. m.def("vectorized_func3", py::vectorize(my_func3));
  37. /// Numpy function which only accepts specific data types
  38. m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
  39. m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
  40. m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
  41. // Passthrough test: references and non-pod types should be automatically passed through (in the
  42. // function definition below, only `b`, `d`, and `g` are vectorized):
  43. py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
  44. m.def("vec_passthrough", py::vectorize(
  45. [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
  46. return *a + b + c.at(0) + d + e + f.value + g;
  47. }
  48. ));
  49. py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
  50. vtc .def(py::init<int>())
  51. .def_readwrite("value", &VectorizeTestClass::value);
  52. // Automatic vectorizing of methods
  53. vtc.def("method", py::vectorize(&VectorizeTestClass::method));
  54. // Internal optimization test for whether the input is trivially broadcastable:
  55. py::enum_<py::detail::broadcast_trivial>(m, "trivial")
  56. .value("f_trivial", py::detail::broadcast_trivial::f_trivial)
  57. .value("c_trivial", py::detail::broadcast_trivial::c_trivial)
  58. .value("non_trivial", py::detail::broadcast_trivial::non_trivial);
  59. m.def("vectorized_is_trivial", [](
  60. py::array_t<int, py::array::forcecast> arg1,
  61. py::array_t<float, py::array::forcecast> arg2,
  62. py::array_t<double, py::array::forcecast> arg3
  63. ) {
  64. ssize_t ndim;
  65. std::vector<ssize_t> shape;
  66. std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
  67. return py::detail::broadcast(buffers, ndim, shape);
  68. });
  69. });