diff --git a/unittest/python/test_user_type.py b/unittest/python/test_user_type.py index b2c5e696e5a8fa760ac75188462d5771106ce75a..80d5e1a24119e040da075e3b7935b29ced4250d5 100644 --- a/unittest/python/test_user_type.py +++ b/unittest/python/test_user_type.py @@ -5,8 +5,8 @@ import numpy as np rows = 10 cols = 20 -def test(mat): - mat[:] = mat.dtype.type(10.) +def test(dtype): + mat = np.ones((rows,cols),dtype=dtype) mat_copy = mat.copy() assert (mat == mat_copy).all() assert not (mat != mat_copy).all() @@ -25,18 +25,31 @@ def test(mat): mat_op = mat.dot(mat.T) mat_op = mat / mat - mat_op = -mat; + mat_op = -mat assert (mat >= mat).all() assert (mat <= mat).all() assert not (mat > mat).all() assert not (mat < mat).all() -mat = user_type.create_double(rows,cols) -test(mat) +def test_cast(from_dtype,to_dtype): + np.can_cast(from_dtype,to_dtype) -mat = user_type.create_float(rows,cols) -test(mat) + from_mat = np.zeros((rows,cols),dtype=from_dtype) + to_mat = from_mat.astype(dtype=to_dtype) + +test(user_type.CustomDouble) + +test_cast(user_type.CustomDouble,np.double) +test_cast(np.double,user_type.CustomDouble) + +test_cast(user_type.CustomDouble,np.int64) +test_cast(np.int64,user_type.CustomDouble) + +test_cast(user_type.CustomDouble,np.int32) +test_cast(np.int32,user_type.CustomDouble) + +test(user_type.CustomFloat) v = user_type.CustomDouble(1) a = np.array(v) diff --git a/unittest/user_type.cpp b/unittest/user_type.cpp index 8f231c9163b1aaf9cf49859c2ec06c499c47a866..7cf41ab21d73f15413462af764d4d075b29173f2 100644 --- a/unittest/user_type.cpp +++ b/unittest/user_type.cpp @@ -97,6 +97,11 @@ struct CustomType CustomType operator-() const { return CustomType(-m_value); } + operator Scalar () const + { + return m_value; + } + std::string print() const { std::stringstream ss; @@ -110,7 +115,7 @@ struct CustomType return os; } -protected: +//protected: Scalar m_value; }; @@ -186,4 +191,16 @@ BOOST_PYTHON_MODULE(user_type) bp::def("build_matrix",build_matrix<double>); bp::def("print",print<double>); bp::def("print",print<float>); + + eigenpy::registerCast<DoubleType,double>(true); + eigenpy::registerCast<double,DoubleType>(true); + eigenpy::registerCast<DoubleType,int32_t>(false); + eigenpy::registerCast<int32_t,DoubleType>(true); + eigenpy::registerCast<DoubleType,int64_t>(false); + eigenpy::registerCast<int64_t,DoubleType>(true); + eigenpy::registerCast<FloatType,int64_t>(false); + eigenpy::registerCast<int64_t,FloatType>(true); + + bp::implicitly_convertible<double,DoubleType>(); + bp::implicitly_convertible<DoubleType,double>(); }