Vectorizing POD types
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
In the documentation for vectorization, this is mentioned:
Only arithmetic, complex, and POD types passed by value or by const & reference are vectorized; all other arguments are passed through as-is.
However, it seems that if we use a POD type, py::vectorize does not work as expected. When I check the tests, it seems we do test that non-POD types pass through without vectorization, but there are no tests/examples for how to vectorize POD types.
Reproducible example code
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
struct PODClass {
uint32_t value;
};
PYBIND11_MODULE(example, m) {
pybind11::class_<PODClass>(m, "PODClass")
.def(pybind11::init<>())
.def_readwrite("value", &PODClass::value);
PYBIND11_NUMPY_DTYPE(PODClass, value);
m.def("pod_passthrough", pybind11::vectorize(
[](PODClass a) {
return a;
}
));
static_assert(std::is_pod<PODClass>::value); // To ensure we actually do have a POD type
}
This compiles. When we try to use in python, however:
import example
import numpy as np
object_1 = example.PODClass()
object_1.value = 10
object_2 = example.PODClass()
object_2.value = 20
vec = np.array([object_1, object_2])
example.pod_passthrough(vec)
> example.pod_passthrough(a)
E TypeError: pod_passthrough(): incompatible function arguments. The following argument types are supported:
E 1. (arg0: numpy.ndarray[pybind11_tests.numpy_vectorize.PODClass]) -> object
E
E Invoked with: array([<pybind11_tests.numpy_vectorize.PODClass object at 0x7f662db9de30>,
E <pybind11_tests.numpy_vectorize.PODClass object at 0x7f6638a3d970>],
E dtype=object)
Not sure if this is expected or I am doing something wrong.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tests/test_numpy_vectorize.cpp around the existing non-POD passthrough test and compare it with the documented vectorization behavior in the linked NumPy documentation. Reproduce the POD example, then determine the expected handling and ensure the tests and documentation agree on what should happen.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100