2015-07-26 14:33:49 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array
|
2015-07-29 15:51:54 +00:00
|
|
|
arguments
|
2015-07-26 14:33:49 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
#include "pybind11_tests.h"
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/numpy.h>
|
2015-07-26 14:33:49 +00:00
|
|
|
|
|
|
|
double my_func(int x, float y, double z) {
|
2016-09-06 22:50:10 +00:00
|
|
|
py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z));
|
2016-05-28 10:26:18 +00:00
|
|
|
return (float) x*y*z;
|
2015-07-26 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 14:12:20 +00:00
|
|
|
std::complex<double> my_func3(std::complex<double> c) {
|
|
|
|
return c * std::complex<double>(2.f);
|
|
|
|
}
|
|
|
|
|
2017-03-26 03:51:40 +00:00
|
|
|
struct VectorizeTestClass {
|
|
|
|
VectorizeTestClass(int v) : value{v} {};
|
|
|
|
float method(int x, float y) { return y + (float) (x + value); }
|
|
|
|
int value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NonPODClass {
|
|
|
|
NonPODClass(int v) : value{v} {}
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
test_initializer numpy_vectorize([](py::module &m) {
|
2015-07-29 15:51:54 +00:00
|
|
|
// Vectorize all arguments of a function (though non-vector arguments are also allowed)
|
2015-07-26 14:33:49 +00:00
|
|
|
m.def("vectorized_func", py::vectorize(my_func));
|
2015-07-29 15:51:54 +00:00
|
|
|
|
2015-07-26 14:33:49 +00:00
|
|
|
// Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
|
|
|
|
m.def("vectorized_func2",
|
2015-10-13 15:38:22 +00:00
|
|
|
[](py::array_t<int> x, py::array_t<float> y, float z) {
|
2015-07-26 14:33:49 +00:00
|
|
|
return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
|
|
|
|
}
|
|
|
|
);
|
2015-07-29 15:51:54 +00:00
|
|
|
|
|
|
|
// Vectorize a complex-valued function
|
2015-07-28 14:12:20 +00:00
|
|
|
m.def("vectorized_func3", py::vectorize(my_func3));
|
2016-05-19 14:02:09 +00:00
|
|
|
|
|
|
|
/// Numpy function which only accepts specific data types
|
2016-08-12 20:28:31 +00:00
|
|
|
m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
|
|
|
|
m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
|
|
|
|
m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
|
|
|
|
|
2017-03-26 03:51:40 +00:00
|
|
|
// Passthrough test: references and non-pod types should be automatically passed through (in the
|
|
|
|
// function definition below, only `b`, `d`, and `g` are vectorized):
|
|
|
|
py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
|
|
|
|
m.def("vec_passthrough", py::vectorize(
|
|
|
|
[](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
|
|
|
|
return *a + b + c.at(0) + d + e + f.value + g;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
|
|
|
|
vtc .def(py::init<int>())
|
|
|
|
.def_readwrite("value", &VectorizeTestClass::value);
|
|
|
|
|
|
|
|
// Automatic vectorizing of methods
|
|
|
|
vtc.def("method", py::vectorize(&VectorizeTestClass::method));
|
|
|
|
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
// Internal optimization test for whether the input is trivially broadcastable:
|
2017-03-19 00:11:59 +00:00
|
|
|
py::enum_<py::detail::broadcast_trivial>(m, "trivial")
|
|
|
|
.value("f_trivial", py::detail::broadcast_trivial::f_trivial)
|
|
|
|
.value("c_trivial", py::detail::broadcast_trivial::c_trivial)
|
|
|
|
.value("non_trivial", py::detail::broadcast_trivial::non_trivial);
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
m.def("vectorized_is_trivial", [](
|
|
|
|
py::array_t<int, py::array::forcecast> arg1,
|
|
|
|
py::array_t<float, py::array::forcecast> arg2,
|
|
|
|
py::array_t<double, py::array::forcecast> arg3
|
|
|
|
) {
|
2017-04-14 20:33:44 +00:00
|
|
|
ssize_t ndim;
|
|
|
|
std::vector<ssize_t> shape;
|
Stop forcing c-contiguous in py::vectorize
The only part of the vectorize code that actually needs c-contiguous is
the "trivial" broadcast; for non-trivial arguments, the code already
uses strides properly (and so handles C-style, F-style, neither, slices,
etc.)
This commit rewrites `broadcast` to additionally check for C-contiguous
storage, then takes off the `c_style` flag for the arguments, which
will keep the functionality more or less the same, except for no longer
requiring an array copy for non-c-contiguous input arrays.
Additionally, if we're given a singleton slice (e.g. a[0::4, 0::4] for a
4x4 or smaller array), we no longer fail triviality because the trivial
code path never actually uses the strides on a singleton.
2017-03-15 03:57:56 +00:00
|
|
|
std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
|
|
|
|
return py::detail::broadcast(buffers, ndim, shape);
|
|
|
|
});
|
2016-09-03 18:54:22 +00:00
|
|
|
});
|