Skip to content

Commit 81391d2

Browse files
committed
unittest: add test where block is edited from C++ function
isolates problem with conversion
1 parent bbb129d commit 81391d2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

unittest/eigen_ref.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ Eigen::Ref<MatType> getBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
3737
return mat.block(i, j, n, m);
3838
}
3939

40+
template <typename MatType>
41+
Eigen::Ref<MatType> editBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
42+
Eigen::DenseIndex j, Eigen::DenseIndex n,
43+
Eigen::DenseIndex m) {
44+
auto B = mat.block(i, j, n, m);
45+
Eigen::Map<VectorXd> view(B.data(), B.size());
46+
view.setLinSpaced(1., (double)view.size());
47+
return mat;
48+
}
49+
4050
template <typename MatType>
4151
void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
4252
mat.fill(value);
@@ -98,6 +108,7 @@ BOOST_PYTHON_MODULE(eigen_ref) {
98108
Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>);
99109

100110
bp::def("getBlock", &getBlock<MatrixXd>);
111+
bp::def("editBlock", &editBlock<MatrixXd>);
101112

102113
bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>())
103114
.def_readonly("J", &modify_block::J)

unittest/python/test_eigen_ref.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ def test(mat):
3737

3838
mat.fill(0.0)
3939
mat_as_C_order = np.array(mat, order="F")
40+
mat_copy = mat_as_C_order.copy()
4041
getBlock(mat_as_C_order, 0, 0, 3, 2)[:, :] = 1.0
4142

4243
assert np.all(mat_as_C_order[:3, :2] == np.ones((3, 2)))
4344

45+
editBlock(mat_as_C_order, 0, 0, 3, 2)
46+
mat_copy[:3, :2] = np.arange(6).reshape(3, 2)
47+
48+
assert np.all(mat_as_C_order == mat_copy)
49+
4450
class ModifyBlockImpl(modify_block):
4551
def __init__(self):
4652
super().__init__()

0 commit comments

Comments
 (0)