user_type.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #include <iostream>
6 #include <sstream>
7 
8 #include "eigenpy/eigenpy.hpp"
9 #include "eigenpy/ufunc.hpp"
10 #include "eigenpy/user-type.hpp"
11 
12 template <typename Scalar>
13 struct CustomType;
14 
15 namespace Eigen {
18 template <typename Scalar>
19 struct NumTraits<CustomType<Scalar> > {
24 
25  enum {
26  // does not support complex Base types
27  IsComplex = 0,
28  // does not support integer Base types
29  IsInteger = 0,
30  // only support signed Base types
31  IsSigned = 1,
32  // must initialize an AD<Base> object
33  RequireInitialization = 1,
34  // computational cost of the corresponding operations
35  ReadCost = 1,
36  AddCost = 2,
37  MulCost = 2
38  };
39 
41  return CustomType<Scalar>(std::numeric_limits<Scalar>::epsilon());
42  }
43 
45  return CustomType<Scalar>(NumTraits<Scalar>::dummy_precision());
46  }
47 
49  return CustomType<Scalar>(std::numeric_limits<Scalar>::max());
50  }
51 
53  return CustomType<Scalar>(std::numeric_limits<Scalar>::min());
54  }
55 
56  static int digits10() { return std::numeric_limits<Scalar>::digits10; }
57  static int max_digits10() {
58  return std::numeric_limits<Scalar>::max_digits10;
59  }
60 };
61 } // namespace Eigen
62 
63 template <typename Scalar>
64 struct CustomType {
66 
67  explicit CustomType(const Scalar& value) : m_value(value) {}
68 
69  CustomType operator*(const CustomType& other) const {
70  return CustomType(m_value * other.m_value);
71  }
72  CustomType operator+(const CustomType& other) const {
73  return CustomType(m_value + other.m_value);
74  }
75  CustomType operator-(const CustomType& other) const {
76  return CustomType(m_value - other.m_value);
77  }
78  CustomType operator/(const CustomType& other) const {
79  return CustomType(m_value / other.m_value);
80  }
81 
82  void operator+=(const CustomType& other) { m_value += other.m_value; }
83  void operator-=(const CustomType& other) { m_value -= other.m_value; }
84  void operator*=(const CustomType& other) { m_value *= other.m_value; }
85  void operator/=(const CustomType& other) { m_value /= other.m_value; }
86 
87  void operator=(const Scalar& value) { m_value = value; }
88 
89  bool operator==(const CustomType& other) const {
90  return m_value == other.m_value;
91  }
92  bool operator!=(const CustomType& other) const {
93  return m_value != other.m_value;
94  }
95 
96  bool operator<=(const CustomType& other) const {
97  return m_value <= other.m_value;
98  }
99  bool operator<(const CustomType& other) const {
100  return m_value < other.m_value;
101  }
102  bool operator>=(const CustomType& other) const {
103  return m_value >= other.m_value;
104  }
105  bool operator>(const CustomType& other) const {
106  return m_value > other.m_value;
107  }
108 
109  CustomType operator-() const { return CustomType(-m_value); }
110 
111  operator Scalar() const { return m_value; }
112 
113  std::string print() const {
114  std::stringstream ss;
115  ss << "value: " << m_value << std::endl;
116  return ss.str();
117  }
118 
119  friend std::ostream& operator<<(std::ostream& os, const CustomType& X) {
120  os << X.m_value;
121  return os;
122  }
123 
124  // protected:
125 
126  Scalar m_value;
127 };
128 
129 template <typename Scalar>
130 Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic, Eigen::Dynamic> create(
131  int rows, int cols) {
132  typedef Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic, Eigen::Dynamic>
133  Matrix;
134  return Matrix(rows, cols);
135 }
136 
137 template <typename Scalar>
138 void print(const Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic,
139  Eigen::Dynamic>& mat) {
140  std::cout << mat << std::endl;
141 }
142 
143 template <typename Scalar>
144 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> build_matrix(int rows,
145  int cols) {
146  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix;
147  return Matrix(rows, cols);
148 }
149 
150 template <typename Scalar>
151 void expose_custom_type(const std::string& name) {
152  using namespace Eigen;
153  namespace bp = boost::python;
154 
155  typedef CustomType<Scalar> Type;
156 
157  bp::class_<Type>(name.c_str(), bp::init<Scalar>(bp::arg("value")))
158 
159  .def(bp::self + bp::self)
160  .def(bp::self - bp::self)
161  .def(bp::self * bp::self)
162  .def(bp::self / bp::self)
163 
164  .def(bp::self += bp::self)
165  .def(bp::self -= bp::self)
166  .def(bp::self *= bp::self)
167  .def(bp::self /= bp::self)
168 
169  .def("__repr__", &Type::print);
170 
171  int code = eigenpy::registerNewType<Type>();
172  std::cout << "code: " << code << std::endl;
173  eigenpy::registerCommonUfunc<Type>();
174 }
175 
177  using namespace Eigen;
178  namespace bp = boost::python;
180 
181  expose_custom_type<double>("CustomDouble");
182  typedef CustomType<double> DoubleType;
183  typedef Eigen::Matrix<DoubleType, Eigen::Dynamic, Eigen::Dynamic>
184  DoubleMatrix;
187  bp::def("create_double", create<double>);
188 
189  expose_custom_type<float>("CustomFloat");
190  typedef CustomType<float> FloatType;
191  typedef Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic> FloatMatrix;
194  bp::def("create_float", create<float>);
195 
196  bp::def("build_matrix", build_matrix<double>);
197 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
198  bp::def("print", print<double>);
199  bp::def("print", print<float>);
200 #endif
201 
202  eigenpy::registerCast<DoubleType, double>(true);
203  eigenpy::registerCast<double, DoubleType>(true);
204  eigenpy::registerCast<DoubleType, int>(false);
205  eigenpy::registerCast<int, DoubleType>(true);
206  eigenpy::registerCast<DoubleType, long long>(false);
207  eigenpy::registerCast<long long, DoubleType>(true);
208  eigenpy::registerCast<DoubleType, long>(false);
209  eigenpy::registerCast<long, DoubleType>(true);
210  eigenpy::registerCast<FloatType, double>(true);
211  eigenpy::registerCast<double, FloatType>(false);
212  eigenpy::registerCast<FloatType, long long>(false);
213  eigenpy::registerCast<long long, FloatType>(true);
214  eigenpy::registerCast<FloatType, int>(false);
215  eigenpy::registerCast<int, FloatType>(true);
216  eigenpy::registerCast<FloatType, long>(false);
217  eigenpy::registerCast<long, FloatType>(true);
218 
219  bp::implicitly_convertible<double, DoubleType>();
220  bp::implicitly_convertible<DoubleType, double>();
221 }
CustomType::CustomType
CustomType()
Definition: user_type.cpp:65
CustomType::operator<
bool operator<(const CustomType &other) const
Definition: user_type.cpp:99
CustomType::operator==
bool operator==(const CustomType &other) const
Definition: user_type.cpp:89
boost::python
Definition: alignment.hpp:49
Eigen
Definition: complex.cpp:7
print
void print(const Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > &mat)
Definition: user_type.cpp:138
eigenpy::enableEigenPy
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:43
CustomType::operator=
void operator=(const Scalar &value)
Definition: user_type.cpp:87
Eigen::NumTraits< CustomType< Scalar > >::Literal
CustomType< Scalar > Literal
Definition: user_type.cpp:22
CustomType::operator*=
void operator*=(const CustomType &other)
Definition: user_type.cpp:84
CustomType::operator<<
friend std::ostream & operator<<(std::ostream &os, const CustomType &X)
Definition: user_type.cpp:119
Eigen::NumTraits< CustomType< Scalar > >::Nested
CustomType< Scalar > Nested
Definition: user_type.cpp:23
Eigen::NumTraits< CustomType< Scalar > >::Real
CustomType< Scalar > Real
Definition: user_type.cpp:20
test_complex.rows
int rows
Definition: test_complex.py:4
user-type.hpp
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(user_type)
Definition: user_type.cpp:176
CustomType::operator-
CustomType operator-() const
Definition: user_type.cpp:109
CustomType::print
std::string print() const
Definition: user_type.cpp:113
CustomType::operator>=
bool operator>=(const CustomType &other) const
Definition: user_type.cpp:102
Eigen::NumTraits< CustomType< Scalar > >::epsilon
static CustomType< Scalar > epsilon()
Definition: user_type.cpp:40
Eigen::NumTraits< CustomType< Scalar > >::highest
static CustomType< Scalar > highest()
Definition: user_type.cpp:48
CustomType::operator+=
void operator+=(const CustomType &other)
Definition: user_type.cpp:82
Eigen::NumTraits< CustomType< Scalar > >::dummy_precision
static CustomType< Scalar > dummy_precision()
Definition: user_type.cpp:44
Eigen::NumTraits< CustomType< Scalar > >::lowest
static CustomType< Scalar > lowest()
Definition: user_type.cpp:52
eigenpy::EigenToPyConverter::registration
static void registration()
Definition: eigen-to-python.hpp:167
CustomType::m_value
Scalar m_value
Definition: user_type.cpp:126
CustomType::operator>
bool operator>(const CustomType &other) const
Definition: user_type.cpp:105
CustomType::operator+
CustomType operator+(const CustomType &other) const
Definition: user_type.cpp:72
Eigen::NumTraits< CustomType< Scalar > >::digits10
static int digits10()
Definition: user_type.cpp:56
test_eigen_ref.mat
mat
Definition: test_eigen_ref.py:137
CustomType
Definition: user_type.cpp:13
Eigen::NumTraits< CustomType< Scalar > >::max_digits10
static int max_digits10()
Definition: user_type.cpp:57
ufunc.hpp
test_matrix.value
float value
Definition: test_matrix.py:161
CustomType::operator/=
void operator/=(const CustomType &other)
Definition: user_type.cpp:85
build_matrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > build_matrix(int rows, int cols)
Definition: user_type.cpp:144
CustomType::operator*
CustomType operator*(const CustomType &other) const
Definition: user_type.cpp:69
setup.name
name
Definition: setup.in.py:179
eigenpy.hpp
CustomType::operator-
CustomType operator-(const CustomType &other) const
Definition: user_type.cpp:75
CustomType::CustomType
CustomType(const Scalar &value)
Definition: user_type.cpp:67
create
Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > create(int rows, int cols)
Definition: user_type.cpp:130
CustomType::operator!=
bool operator!=(const CustomType &other) const
Definition: user_type.cpp:92
expose_custom_type
void expose_custom_type(const std::string &name)
Definition: user_type.cpp:151
Eigen::NumTraits< CustomType< Scalar > >::NonInteger
CustomType< Scalar > NonInteger
Definition: user_type.cpp:21
test_complex.cols
int cols
Definition: test_complex.py:5
CustomType::operator-=
void operator-=(const CustomType &other)
Definition: user_type.cpp:83
CustomType::operator<=
bool operator<=(const CustomType &other) const
Definition: user_type.cpp:96
X
Definition: deprecation_policy.cpp:19
eigenpy::EigenFromPyConverter
Definition: eigen-from-python.hpp:423
CustomType::operator/
CustomType operator/(const CustomType &other) const
Definition: user_type.cpp:78


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45