Verified Commit bbb129d3 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

test: add more test for Eigen::Ref

parent cd361037
Pipeline #18398 passed with stage
in 10 minutes and 21 seconds
......@@ -30,6 +30,13 @@ void setOnes(Eigen::Ref<MatType> mat) {
mat.setOnes();
}
template <typename MatType>
Eigen::Ref<MatType> getBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
Eigen::DenseIndex j, Eigen::DenseIndex n,
Eigen::DenseIndex m) {
return mat.block(i, j, n, m);
}
template <typename MatType>
void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
mat.fill(value);
......@@ -90,6 +97,8 @@ BOOST_PYTHON_MODULE(eigen_ref) {
bp::def("asConstRef", (const Eigen::Ref<const MatrixXd> (*)(
Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>);
bp::def("getBlock", &getBlock<MatrixXd>);
bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>())
.def_readonly("J", &modify_block::J)
.def("modify", &modify_block::modify)
......
......@@ -22,8 +22,25 @@ def test(mat):
assert np.all(ref == mat)
const_ref = asConstRef(mat)
# import pdb; pdb.set_trace()
assert np.all(const_ref == mat)
mat.fill(0.0)
fill(mat[:3, :2], 1.0)
assert np.all(mat[:3, :2] == np.ones((3, 2)))
mat.fill(0.0)
fill(mat[:2, :3], 1.0)
assert np.all(mat[:2, :3] == np.ones((2, 3)))
mat.fill(0.0)
mat_as_C_order = np.array(mat, order="F")
getBlock(mat_as_C_order, 0, 0, 3, 2)[:, :] = 1.0
assert np.all(mat_as_C_order[:3, :2] == np.ones((3, 2)))
class ModifyBlockImpl(modify_block):
def __init__(self):
super().__init__()
......@@ -32,12 +49,9 @@ def test(mat):
mat[:, :] = 1.0
modify = ModifyBlockImpl()
print("Field J init:\n{}".format(modify.J))
modify.modify(2, 3)
print("Field J after:\n{}".format(modify.J))
Jref = np.zeros((10, 10))
Jref[:2, :3] = 1.0
print("Should be:\n{}".format(Jref))
assert np.array_equal(Jref, modify.J)
......
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