test_numpy_vectorize.cpp revision 12391
110389SAndreas.Sandberg@ARM.com/*
210389SAndreas.Sandberg@ARM.com    tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
310389SAndreas.Sandberg@ARM.com    arguments
410389SAndreas.Sandberg@ARM.com
510389SAndreas.Sandberg@ARM.com    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
610389SAndreas.Sandberg@ARM.com
710389SAndreas.Sandberg@ARM.com    All rights reserved. Use of this source code is governed by a
810389SAndreas.Sandberg@ARM.com    BSD-style license that can be found in the LICENSE file.
910389SAndreas.Sandberg@ARM.com*/
1010389SAndreas.Sandberg@ARM.com
1110389SAndreas.Sandberg@ARM.com#include "pybind11_tests.h"
1210389SAndreas.Sandberg@ARM.com#include <pybind11/numpy.h>
1310389SAndreas.Sandberg@ARM.com
1410389SAndreas.Sandberg@ARM.comdouble my_func(int x, float y, double z) {
1510389SAndreas.Sandberg@ARM.com    py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z));
1610389SAndreas.Sandberg@ARM.com    return (float) x*y*z;
1710389SAndreas.Sandberg@ARM.com}
1810389SAndreas.Sandberg@ARM.com
1910389SAndreas.Sandberg@ARM.comTEST_SUBMODULE(numpy_vectorize, m) {
2010389SAndreas.Sandberg@ARM.com    try { py::module::import("numpy"); }
2110389SAndreas.Sandberg@ARM.com    catch (...) { return; }
2210389SAndreas.Sandberg@ARM.com
2310389SAndreas.Sandberg@ARM.com    // test_vectorize, test_docs, test_array_collapse
2410389SAndreas.Sandberg@ARM.com    // Vectorize all arguments of a function (though non-vector arguments are also allowed)
2510389SAndreas.Sandberg@ARM.com    m.def("vectorized_func", py::vectorize(my_func));
2610389SAndreas.Sandberg@ARM.com
2710389SAndreas.Sandberg@ARM.com    // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
2810389SAndreas.Sandberg@ARM.com    m.def("vectorized_func2",
2910389SAndreas.Sandberg@ARM.com        [](py::array_t<int> x, py::array_t<float> y, float z) {
3010389SAndreas.Sandberg@ARM.com            return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
3110389SAndreas.Sandberg@ARM.com        }
3210389SAndreas.Sandberg@ARM.com    );
3310389SAndreas.Sandberg@ARM.com
3410389SAndreas.Sandberg@ARM.com    // Vectorize a complex-valued function
3510389SAndreas.Sandberg@ARM.com    m.def("vectorized_func3", py::vectorize(
3610389SAndreas.Sandberg@ARM.com        [](std::complex<double> c) { return c * std::complex<double>(2.f); }
3710389SAndreas.Sandberg@ARM.com    ));
3810389SAndreas.Sandberg@ARM.com
3910389SAndreas.Sandberg@ARM.com    // test_type_selection
4010389SAndreas.Sandberg@ARM.com    // Numpy function which only accepts specific data types
4110389SAndreas.Sandberg@ARM.com    m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
4213665Sandreas.sandberg@arm.com    m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
4313665Sandreas.sandberg@arm.com    m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
4410389SAndreas.Sandberg@ARM.com
4510389SAndreas.Sandberg@ARM.com
4610389SAndreas.Sandberg@ARM.com    // test_passthrough_arguments
4710389SAndreas.Sandberg@ARM.com    // Passthrough test: references and non-pod types should be automatically passed through (in the
4810389SAndreas.Sandberg@ARM.com    // function definition below, only `b`, `d`, and `g` are vectorized):
4910389SAndreas.Sandberg@ARM.com    struct NonPODClass {
5010389SAndreas.Sandberg@ARM.com        NonPODClass(int v) : value{v} {}
5110389SAndreas.Sandberg@ARM.com        int value;
5212237Sandreas.sandberg@arm.com    };
53    py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
54    m.def("vec_passthrough", py::vectorize(
55        [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
56            return *a + b + c.at(0) + d + e + f.value + g;
57        }
58    ));
59
60    // test_method_vectorization
61    struct VectorizeTestClass {
62        VectorizeTestClass(int v) : value{v} {};
63        float method(int x, float y) { return y + (float) (x + value); }
64        int value = 0;
65    };
66    py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
67    vtc .def(py::init<int>())
68        .def_readwrite("value", &VectorizeTestClass::value);
69
70    // Automatic vectorizing of methods
71    vtc.def("method", py::vectorize(&VectorizeTestClass::method));
72
73    // test_trivial_broadcasting
74    // Internal optimization test for whether the input is trivially broadcastable:
75    py::enum_<py::detail::broadcast_trivial>(m, "trivial")
76        .value("f_trivial", py::detail::broadcast_trivial::f_trivial)
77        .value("c_trivial", py::detail::broadcast_trivial::c_trivial)
78        .value("non_trivial", py::detail::broadcast_trivial::non_trivial);
79    m.def("vectorized_is_trivial", [](
80                py::array_t<int, py::array::forcecast> arg1,
81                py::array_t<float, py::array::forcecast> arg2,
82                py::array_t<double, py::array::forcecast> arg3
83                ) {
84        ssize_t ndim;
85        std::vector<ssize_t> shape;
86        std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
87        return py::detail::broadcast(buffers, ndim, shape);
88    });
89}
90