You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue where the first few elements of an Eigen::VectorXf are incorrect when accessed from Python after being exposed as a read-only property using def_prop_ro. Specifically, the first four elements appear to have erroneous values. Using def to expose the same data as a method, or using std::vector instead of Eigen::VectorXf, works correctly. This suggests a potential incompatibility or bug related to how def_prop_ro handles Eigen::VectorXf.
The results are shown in the figure:
Reproducible example code
#include <nanobind/nanobind.h>
#include <nanobind/eigen/dense.h>
#include <Eigen/Dense>
#include <vector>
namespace nb = nanobind;
class test_Histogram2D {
public:
test_Histogram2D(float min, float max, int bins) {
bins_ = bins;
min_ = min;
max_ = max;
}
Eigen::VectorXf centers_eigen() const {
Eigen::VectorXf centers(bins_);
for (int i = 0; i < bins_; i++) {
centers(i) = get_bin_center(i);
}
return centers;
}
std::vector<float> centers_stdvector() const {
std::vector<float> centers(bins_);
for (int i = 0; i < bins_; i++) {
centers[i] = get_bin_center(i);
}
return centers;
}
float get_bin_center(int idx) const {
return min_ + (idx + 0.5f) * (max_ - min_) / (float)bins_;
}
private:
int bins_;
float min_;
float max_;
};
NB_MODULE(example, m) {
nb::class_<test_Histogram2D>(m, "test_Histogram2D")
.def(nb::init<float, float, int>())
.def_prop_ro("centers_eigen", &test_Histogram2D::centers_eigen)
.def("centers_method", &test_Histogram2D::centers_eigen) // Working method version
.def("centers_stdvector", &test_Histogram2D::centers_stdvector); //Also working
}
The text was updated successfully, but these errors were encountered:
Problem description
Description
I'm encountering an issue where the first few elements of an
Eigen::VectorXf
are incorrect when accessed from Python after being exposed as a read-only property usingdef_prop_ro
. Specifically, the first four elements appear to have erroneous values. Usingdef
to expose the same data as a method, or usingstd::vector
instead ofEigen::VectorXf
, works correctly. This suggests a potential incompatibility or bug related to howdef_prop_ro
handlesEigen::VectorXf
.The results are shown in the figure:
Reproducible example code
The text was updated successfully, but these errors were encountered: