pybind / pybind/pybind11

[FEAT] py::vectorize for py::args

Open
#2,827 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

I have defined a function signature like [](Type& self, py::args args) -> double where each argument is a scalar (specifically, std::variant<double, int, std::string>), and it would be convenient for my use case if I could wrap this with py::vectorize, where it would do the same as for a static number of arguments (e.g. [](Type& self, double arg1, int arg2) -> double etc.). (although I don't care to vectorize the string)
Do you think this is something that could be supported?

An alternative I thought of (with the help of @henryiii ) would be to first broadcast the arrays and flatten them in pure python and pass them down to an implementation that can handle 1D py::array like the example at the end of https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#vectorizing-functions but that comes at the expense of having to hold the broadcasted arguments in memory for some time.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source files or tests; start with the py::vectorize documentation and its linked NumPy vectorizing example. Compare the existing fixed-arity behavior with py::args, including broadcasting and mixed scalar types, then establish the supported semantics and tests needed before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.