Skip to content

Commit

Permalink
Replace removed itemset method (#72)
Browse files Browse the repository at this point in the history
Numpy 2.0 removes the itemset Python method from
the API
Switch to setting array values via direct array
interface for pybind11::object types

Mutable array access is unchecked for dimension
boundaries
  • Loading branch information
johnwparent authored Jul 8, 2024
1 parent ef4e178 commit f1213df
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions include/pybind11/eigen/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ eigen_array_cast(typename props::Type const &src, handle base = handle(), bool w
{(size_t) src.size()},
nullptr,
empty_base);
auto _m_arr = a.mutable_unchecked<object, 1>();

constexpr bool is_row = props::fixed_rows && props::rows == 1;
for (ssize_t i = 0; i < src.size(); ++i) {
const Scalar src_val = is_row ? src(0, i) : src(i, 0);
Expand All @@ -291,21 +293,25 @@ eigen_array_cast(typename props::Type const &src, handle base = handle(), bool w
if (!value_) {
return handle();
}
a.attr("itemset")(i, value_);

_m_arr[i] = value_;
}
} else {
a = array(npy_format_descriptor<Scalar>::dtype(),
{(size_t) src.rows(), (size_t) src.cols()},
nullptr,
empty_base);
auto _m_arr = a.mutable_unchecked<object, 2>();

for (ssize_t i = 0; i < src.rows(); ++i) {
for (ssize_t j = 0; j < src.cols(); ++j) {
auto value_ = reinterpret_steal<object>(
make_caster<Scalar>::cast(src(i, j), policy, empty_base));
if (!value_) {
return handle();
}
a.attr("itemset")(i, j, value_);

_m_arr(i,j) = value_;
}
}
}
Expand Down

0 comments on commit f1213df

Please sign in to comment.