|
Packit |
534379 |
/*
|
|
Packit |
534379 |
tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
|
|
Packit |
534379 |
arguments
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
All rights reserved. Use of this source code is governed by a
|
|
Packit |
534379 |
BSD-style license that can be found in the LICENSE file.
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "pybind11_tests.h"
|
|
Packit |
534379 |
#include <pybind11/numpy.h>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
double my_func(int x, float y, double z) {
|
|
Packit |
534379 |
py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z));
|
|
Packit |
534379 |
return (float) x*y*z;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
TEST_SUBMODULE(numpy_vectorize, m) {
|
|
Packit |
534379 |
try { py::module::import("numpy"); }
|
|
Packit |
534379 |
catch (...) { return; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_vectorize, test_docs, test_array_collapse
|
|
Packit |
534379 |
// Vectorize all arguments of a function (though non-vector arguments are also allowed)
|
|
Packit |
534379 |
m.def("vectorized_func", py::vectorize(my_func));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
|
|
Packit |
534379 |
m.def("vectorized_func2",
|
|
Packit |
534379 |
[](py::array_t<int> x, py::array_t<float> y, float z) {
|
|
Packit |
534379 |
return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// Vectorize a complex-valued function
|
|
Packit |
534379 |
m.def("vectorized_func3", py::vectorize(
|
|
Packit |
534379 |
[](std::complex<double> c) { return c * std::complex<double>(2.f); }
|
|
Packit |
534379 |
));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_type_selection
|
|
Packit |
534379 |
// Numpy function which only accepts specific data types
|
|
Packit |
534379 |
m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
|
|
Packit |
534379 |
m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
|
|
Packit |
534379 |
m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_passthrough_arguments
|
|
Packit |
534379 |
// Passthrough test: references and non-pod types should be automatically passed through (in the
|
|
Packit |
534379 |
// function definition below, only `b`, `d`, and `g` are vectorized):
|
|
Packit |
534379 |
struct NonPODClass {
|
|
Packit |
534379 |
NonPODClass(int v) : value{v} {}
|
|
Packit |
534379 |
int value;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
|
|
Packit |
534379 |
m.def("vec_passthrough", py::vectorize(
|
|
Packit |
534379 |
[](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
|
|
Packit |
534379 |
return *a + b + c.at(0) + d + e + f.value + g;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_method_vectorization
|
|
Packit |
534379 |
struct VectorizeTestClass {
|
|
Packit |
534379 |
VectorizeTestClass(int v) : value{v} {};
|
|
Packit |
534379 |
float method(int x, float y) { return y + (float) (x + value); }
|
|
Packit |
534379 |
int value = 0;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
|
|
Packit |
534379 |
vtc .def(py::init<int>())
|
|
Packit |
534379 |
.def_readwrite("value", &VectorizeTestClass::value);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// Automatic vectorizing of methods
|
|
Packit |
534379 |
vtc.def("method", py::vectorize(&VectorizeTestClass::method));
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// test_trivial_broadcasting
|
|
Packit |
534379 |
// Internal optimization test for whether the input is trivially broadcastable:
|
|
Packit |
534379 |
py::enum_<py::detail::broadcast_trivial>(m, "trivial")
|
|
Packit |
534379 |
.value("f_trivial", py::detail::broadcast_trivial::f_trivial)
|
|
Packit |
534379 |
.value("c_trivial", py::detail::broadcast_trivial::c_trivial)
|
|
Packit |
534379 |
.value("non_trivial", py::detail::broadcast_trivial::non_trivial);
|
|
Packit |
534379 |
m.def("vectorized_is_trivial", [](
|
|
Packit |
534379 |
py::array_t<int, py::array::forcecast> arg1,
|
|
Packit |
534379 |
py::array_t<float, py::array::forcecast> arg2,
|
|
Packit |
534379 |
py::array_t<double, py::array::forcecast> arg3
|
|
Packit |
534379 |
) {
|
|
Packit |
534379 |
ssize_t ndim;
|
|
Packit |
534379 |
std::vector<ssize_t> shape;
|
|
Packit |
534379 |
std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
|
|
Packit |
534379 |
return py::detail::broadcast(buffers, ndim, shape);
|
|
Packit |
534379 |
});
|
|
Packit |
534379 |
}
|