pybind / pybind/pybind11

Automatic conversion of derived classes from Eigen vector/matrix types to numpy array

Open
#1,338 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

Consider the following class inherited from Eigen::VectorXd that prevents client code to resize it:

class VectorXdConstSize : public VectorXd
{
public:
    using VectorXd::VectorXd;

    VectorXdConstSize() = delete;

    template<typename Derived>
    auto operator=(const Eigen::MatrixBase<Derived>& other) -> VectorXdConstSize&
    {
        Ref<VectorXd> aux(*this);
        aux = other;
        return *this;
    }
    
    auto resize(Index) -> void = delete;
    
    operator Ref<VectorXd>() { return *this; }
    operator Ref<const VectorXd>() const { return *this; }
};

How can I get pybind11 to automatically cast this type to numpy array, the same way it already works for Eigen types (e.g., VectorXd, Ref, etc.)?

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

No files or tests are named. Start by locating pybind11's existing Eigen-to-NumPy conversion path and compare how it handles VectorXd and Ref with the shown derived class. Done means the derived Eigen vector or matrix class is automatically cast to a NumPy array without requiring a separate binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, numpy
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.