blob: b1f820813d7eb5d7242cc8feca8feff0c95ee061 [file] [log] [blame]
Wenzel Jakobd4258ba2015-07-26 16:33:49 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02003 arguments
Wenzel Jakobd4258ba2015-07-26 16:33:49 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakobd4258ba2015-07-26 16:33:49 +02006
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020011#include "pybind11_tests.h"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/numpy.h>
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020013
14double my_func(int x, float y, double z) {
Dean Moldovan81511be2016-09-07 00:50:10 +020015 py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z));
Boris Schäling20ee9352016-05-28 12:26:18 +020016 return (float) x*y*z;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020017}
18
Wenzel Jakob43398a82015-07-28 16:12:20 +020019std::complex<double> my_func3(std::complex<double> c) {
20 return c * std::complex<double>(2.f);
21}
22
Jason Rhinelanderf3ce00e2017-03-26 00:51:40 -030023struct VectorizeTestClass {
24 VectorizeTestClass(int v) : value{v} {};
25 float method(int x, float y) { return y + (float) (x + value); }
26 int value = 0;
27};
28
29struct NonPODClass {
30 NonPODClass(int v) : value{v} {}
31 int value;
32};
33
Jason Rhinelander52f4be82016-09-03 14:54:22 -040034test_initializer numpy_vectorize([](py::module &m) {
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020035 // Vectorize all arguments of a function (though non-vector arguments are also allowed)
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020036 m.def("vectorized_func", py::vectorize(my_func));
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020037
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020038 // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
39 m.def("vectorized_func2",
Wenzel Jakobb50872a2015-10-13 17:38:22 +020040 [](py::array_t<int> x, py::array_t<float> y, float z) {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020041 return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
42 }
43 );
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020044
45 // Vectorize a complex-valued function
Wenzel Jakob43398a82015-07-28 16:12:20 +020046 m.def("vectorized_func3", py::vectorize(my_func3));
Wenzel Jakobb47a9de2016-05-19 16:02:09 +020047
48 /// Numpy function which only accepts specific data types
Dean Moldovan665e8802016-08-12 22:28:31 +020049 m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
50 m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
51 m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -030052
53
Jason Rhinelanderf3ce00e2017-03-26 00:51:40 -030054 // Passthrough test: references and non-pod types should be automatically passed through (in the
55 // function definition below, only `b`, `d`, and `g` are vectorized):
56 py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
57 m.def("vec_passthrough", py::vectorize(
58 [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
59 return *a + b + c.at(0) + d + e + f.value + g;
60 }
61 ));
62
63 py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
64 vtc .def(py::init<int>())
65 .def_readwrite("value", &VectorizeTestClass::value);
66
67 // Automatic vectorizing of methods
68 vtc.def("method", py::vectorize(&VectorizeTestClass::method));
69
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -030070 // Internal optimization test for whether the input is trivially broadcastable:
Jason Rhinelanderb0292c12017-03-18 21:11:59 -030071 py::enum_<py::detail::broadcast_trivial>(m, "trivial")
72 .value("f_trivial", py::detail::broadcast_trivial::f_trivial)
73 .value("c_trivial", py::detail::broadcast_trivial::c_trivial)
74 .value("non_trivial", py::detail::broadcast_trivial::non_trivial);
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -030075 m.def("vectorized_is_trivial", [](
76 py::array_t<int, py::array::forcecast> arg1,
77 py::array_t<float, py::array::forcecast> arg2,
78 py::array_t<double, py::array::forcecast> arg3
79 ) {
Cris Luengo30d43c42017-04-14 14:33:44 -060080 ssize_t ndim;
81 std::vector<ssize_t> shape;
Jason Rhinelanderae5a8f72017-03-15 00:57:56 -030082 std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
83 return py::detail::broadcast(buffers, ndim, shape);
84 });
Jason Rhinelander52f4be82016-09-03 14:54:22 -040085});