Skip to content
Snippets Groups Projects
Commit d6766a20 authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

unittest/eigen_ref: tidy up test and cpp file

unittest/eigen_ref.cpp: asConstRef should take input as Ref<const T>
parent af87fa1a
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ using namespace eigenpy; ...@@ -15,7 +15,7 @@ using namespace eigenpy;
template <typename MatType> template <typename MatType>
void printMatrix(const Eigen::Ref<const MatType> mat) { void printMatrix(const Eigen::Ref<const MatType> mat) {
if (MatType::IsVectorAtCompileTime) std::cout << "isVector" << std::endl; if (MatType::IsVectorAtCompileTime) std::cout << "isVector" << std::endl;
std::cout << "size: cols " << mat.cols() << " rows " << mat.rows() std::cout << "input size: cols " << mat.cols() << " rows " << mat.rows()
<< std::endl; << std::endl;
std::cout << mat << std::endl; std::cout << mat << std::endl;
} }
...@@ -61,17 +61,19 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) { ...@@ -61,17 +61,19 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& value) {
template <typename MatType> template <typename MatType>
Eigen::Ref<MatType> asRef(const int rows, const int cols) { Eigen::Ref<MatType> asRef(const int rows, const int cols) {
static MatType mat(rows, cols); static MatType mat(rows, cols);
std::cout << "mat:\n" << mat << std::endl; std::cout << "create ref to matrix of size (" << rows << "," << cols << ")\n";
return mat; return mat;
} }
template <typename MatType> template <typename MatType>
Eigen::Ref<MatType> asRef(Eigen::Ref<MatType> mat) { Eigen::Ref<MatType> asRef(Eigen::Ref<MatType> mat) {
std::cout << "create Ref to input mutable Ref\n";
return Eigen::Ref<MatType>(mat); return Eigen::Ref<MatType>(mat);
} }
template <typename MatType> template <typename MatType>
const Eigen::Ref<const MatType> asConstRef(Eigen::Ref<MatType> mat) { const Eigen::Ref<const MatType> asConstRef(Eigen::Ref<const MatType> mat) {
std::cout << "create const Ref to input\n";
return Eigen::Ref<const MatType>(mat); return Eigen::Ref<const MatType>(mat);
} }
...@@ -82,8 +84,8 @@ struct modify_block { ...@@ -82,8 +84,8 @@ struct modify_block {
virtual void call(Eigen::Ref<MatrixXd> mat) = 0; virtual void call(Eigen::Ref<MatrixXd> mat) = 0;
}; };
struct modify_wrap : modify_block, bp::wrapper<modify_block> { struct modify_block_wrap : modify_block, bp::wrapper<modify_block> {
modify_wrap() : modify_block() {} modify_block_wrap() : modify_block() {}
void call(Eigen::Ref<MatrixXd> mat) { this->get_override("call")(mat); } void call(Eigen::Ref<MatrixXd> mat) { this->get_override("call")(mat); }
}; };
...@@ -112,20 +114,20 @@ BOOST_PYTHON_MODULE(eigen_ref) { ...@@ -112,20 +114,20 @@ BOOST_PYTHON_MODULE(eigen_ref) {
bp::def("fillVec", fill<VectorXd>); bp::def("fillVec", fill<VectorXd>);
bp::def("fill", fill<MatrixXd>); bp::def("fill", fill<MatrixXd>);
bp::def("asRef", bp::def<Eigen::Ref<MatrixXd> (*)(const int, const int)>("asRef",
(Eigen::Ref<MatrixXd>(*)(const int, const int))asRef<MatrixXd>); asRef<MatrixXd>);
bp::def("asRef", bp::def<Eigen::Ref<MatrixXd> (*)(Eigen::Ref<MatrixXd>)>("asRef",
(Eigen::Ref<MatrixXd>(*)(Eigen::Ref<MatrixXd>))asRef<MatrixXd>); asRef<MatrixXd>);
bp::def("asConstRef", (const Eigen::Ref<const MatrixXd> (*)( bp::def("asConstRef", asConstRef<MatrixXd>);
Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>);
bp::def("getBlock", &getBlock<MatrixXd>); bp::def("getBlock", &getBlock<MatrixXd>);
bp::def("editBlock", &editBlock<MatrixXd>); bp::def("editBlock", &editBlock<MatrixXd>);
bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>()) bp::class_<modify_block_wrap, boost::noncopyable>("modify_block",
bp::init<>())
.def_readonly("J", &modify_block::J) .def_readonly("J", &modify_block::J)
.def("modify", &modify_block::modify) .def("modify", &modify_block::modify)
.def("call", bp::pure_virtual(&modify_wrap::call)); .def("call", bp::pure_virtual(&modify_block_wrap::call));
bp::class_<has_ref_member, boost::noncopyable>("has_ref_member", bp::init<>()) bp::class_<has_ref_member, boost::noncopyable>("has_ref_member", bp::init<>())
.def_readonly("J", &has_ref_member::J) .def_readonly("J", &has_ref_member::J)
......
...@@ -11,48 +11,68 @@ from eigen_ref import ( ...@@ -11,48 +11,68 @@ from eigen_ref import (
) )
def test(mat): def test_fill_print(mat):
print("print matrix:")
printMatrix(mat) printMatrix(mat)
print("calling fill():")
fill(mat, 1.0) fill(mat, 1.0)
print("print again:")
printMatrix(mat) printMatrix(mat)
assert np.array_equal(mat, np.full(mat.shape, 1.0)) assert np.array_equal(mat, np.full(mat.shape, 1.0))
def test_create_ref_to_static(mat):
# create ref to static:
print()
print("[asRef(int, int)]")
A_ref = asRef(mat.shape[0], mat.shape[1]) A_ref = asRef(mat.shape[0], mat.shape[1])
A_ref.fill(1.0) A_ref.fill(1.0)
A_ref[0, 1] = -1.0
print("make second reference:")
A_ref2 = asRef(mat.shape[0], mat.shape[1]) A_ref2 = asRef(mat.shape[0], mat.shape[1])
print(A_ref2)
assert np.array_equal(A_ref, A_ref2) assert np.array_equal(A_ref, A_ref2)
A_ref2.fill(0) A_ref2.fill(0)
assert np.array_equal(A_ref, A_ref2) assert np.array_equal(A_ref, A_ref2)
def test_create_ref(mat):
# create ref to input:
print("[asRef(mat)]")
ref = asRef(mat) ref = asRef(mat)
assert np.all(ref == mat) assert np.array_equal(ref, mat)
assert not (ref.flags.owndata)
assert ref.flags.writeable
print("[asConstRef]")
const_ref = asConstRef(mat) const_ref = asConstRef(mat)
assert np.all(const_ref == mat) print(const_ref.flags)
assert np.array_equal(const_ref, mat)
assert not (const_ref.flags.writeable)
assert not (const_ref.flags.owndata)
mat.fill(0.0) print("fill a slice")
mat[:, :] = 0.0
fill(mat[:3, :2], 1.0) fill(mat[:3, :2], 1.0)
assert np.array_equal(mat[:3, :2], np.ones((3, 2)))
assert np.all(mat[:3, :2] == np.ones((3, 2))) mat[:, :] = 0.0
mat.fill(0.0)
fill(mat[:2, :3], 1.0) fill(mat[:2, :3], 1.0)
assert np.array_equal(mat[:2, :3], np.ones((2, 3)))
assert np.all(mat[:2, :3] == np.ones((2, 3))) print("set mat data to arange()")
mat.fill(0.0) mat.fill(0.0)
mat[:, :] = np.arange(rows * cols).reshape(rows, cols) mat[:, :] = np.arange(rows * cols).reshape(rows, cols)
printMatrix(mat)
mat0 = mat.copy() mat0 = mat.copy()
mat_as_C_order = np.array(mat, order="F") mat_as_C_order = np.array(mat, order="F")
for i, rowsize, colsize in ([0, 3, 2], [1, 1, 2], [0, 3, 1]): for i, rowsize, colsize in ([0, 3, 2], [1, 1, 2], [0, 3, 1]):
print("taking block [{}:{}, {}:{}]".format(i, rowsize + i, 0, colsize)) print("taking block [{}:{}, {}:{}]".format(i, rowsize + i, 0, colsize))
B = getBlock(mat_as_C_order, i, 0, rowsize, colsize) B = getBlock(mat_as_C_order, i, 0, rowsize, colsize)
lhs = mat_as_C_order[i : rowsize + i, :colsize] lhs = mat_as_C_order[i : rowsize + i, :colsize]
print("should be:\n{}\ngot:\n{}".format(lhs, B))
assert np.array_equal(lhs, B.reshape(rowsize, colsize)) assert np.array_equal(lhs, B.reshape(rowsize, colsize))
B[:] = 1.0 B[:] = 1.0
...@@ -62,6 +82,7 @@ def test(mat): ...@@ -62,6 +82,7 @@ def test(mat):
mat_as_C_order[:, :] = mat0 mat_as_C_order[:, :] = mat0
mat_copy = mat_as_C_order.copy() mat_copy = mat_as_C_order.copy()
print("[editBlock]")
editBlock(mat_as_C_order, 0, 0, 3, 2) editBlock(mat_as_C_order, 0, 0, 3, 2)
mat_copy[:3, :2] = np.arange(6).reshape(3, 2) mat_copy[:3, :2] = np.arange(6).reshape(3, 2)
...@@ -91,9 +112,23 @@ def test(mat): ...@@ -91,9 +112,23 @@ def test(mat):
assert np.array_equal(hasref.J, J_true) assert np.array_equal(hasref.J, J_true)
rows = 10 def _do_test(mat):
cols = 30 test_fill_print(mat)
test_create_ref_to_static(mat)
test_create_ref(mat)
if __name__ == "__main__":
rows = 8
cols = 10
mat = np.ones((rows, cols), order="F") mat = np.ones((rows, cols), order="F")
mat[0, 0] = 0
mat[1:5, 1:5] = 6
_do_test(mat)
test(mat) mat = np.ones((rows, cols))
mat[2:4, 1:4] = 2
_do_test(mat)
mat_f = np.asfortranarray(mat)
_do_test(mat_f)
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include <type_traits> #include <type_traits>
#include "eigenpy/eigenpy.hpp" #include "eigenpy/eigenpy.hpp"
// include main first
#include "eigenpy/eigen-from-python.hpp"
#include "eigenpy/std-vector.hpp" #include "eigenpy/std-vector.hpp"
template <typename MatType> template <typename MatType>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment