Commit 81391d26 authored by Wilson Jallet's avatar Wilson Jallet 🎬
Browse files

unittest: add test where block is edited from C++ function

isolates problem with conversion
parent bbb129d3
...@@ -37,6 +37,16 @@ Eigen::Ref<MatType> getBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i, ...@@ -37,6 +37,16 @@ Eigen::Ref<MatType> getBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
return mat.block(i, j, n, m); return mat.block(i, j, n, m);
} }
template <typename MatType>
Eigen::Ref<MatType> editBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
Eigen::DenseIndex j, Eigen::DenseIndex n,
Eigen::DenseIndex m) {
auto B = mat.block(i, j, n, m);
Eigen::Map<VectorXd> view(B.data(), B.size());
view.setLinSpaced(1., (double)view.size());
return mat;
}
template <typename MatType> template <typename MatType>
void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) { void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
mat.fill(value); mat.fill(value);
...@@ -98,6 +108,7 @@ BOOST_PYTHON_MODULE(eigen_ref) { ...@@ -98,6 +108,7 @@ BOOST_PYTHON_MODULE(eigen_ref) {
Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>); Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>);
bp::def("getBlock", &getBlock<MatrixXd>); bp::def("getBlock", &getBlock<MatrixXd>);
bp::def("editBlock", &editBlock<MatrixXd>);
bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>()) bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>())
.def_readonly("J", &modify_block::J) .def_readonly("J", &modify_block::J)
......
...@@ -37,10 +37,16 @@ def test(mat): ...@@ -37,10 +37,16 @@ def test(mat):
mat.fill(0.0) mat.fill(0.0)
mat_as_C_order = np.array(mat, order="F") mat_as_C_order = np.array(mat, order="F")
mat_copy = mat_as_C_order.copy()
getBlock(mat_as_C_order, 0, 0, 3, 2)[:, :] = 1.0 getBlock(mat_as_C_order, 0, 0, 3, 2)[:, :] = 1.0
assert np.all(mat_as_C_order[:3, :2] == np.ones((3, 2))) assert np.all(mat_as_C_order[:3, :2] == np.ones((3, 2)))
editBlock(mat_as_C_order, 0, 0, 3, 2)
mat_copy[:3, :2] = np.arange(6).reshape(3, 2)
assert np.all(mat_as_C_order == mat_copy)
class ModifyBlockImpl(modify_block): class ModifyBlockImpl(modify_block):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment