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 };
58 } // namespace Eigen
59 
60 template <typename Scalar>
61 struct CustomType {
63 
64  explicit CustomType(const Scalar& value) : m_value(value) {}
65 
66  CustomType operator*(const CustomType& other) const {
67  return CustomType(m_value * other.m_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 
79  void operator+=(const CustomType& other) { m_value += other.m_value; }
80  void operator-=(const CustomType& other) { m_value -= other.m_value; }
81  void operator*=(const CustomType& other) { m_value *= other.m_value; }
82  void operator/=(const CustomType& other) { m_value /= other.m_value; }
83 
84  void operator=(const Scalar& value) { m_value = value; }
85 
86  bool operator==(const CustomType& other) const {
87  return m_value == other.m_value;
88  }
89  bool operator!=(const CustomType& other) const {
90  return m_value != other.m_value;
91  }
92 
93  bool operator<=(const CustomType& other) const {
94  return m_value <= other.m_value;
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 
106  CustomType operator-() const { return CustomType(-m_value); }
107 
108  operator Scalar() const { return m_value; }
109 
110  std::string print() const {
111  std::stringstream ss;
112  ss << "value: " << m_value << std::endl;
113  return ss.str();
114  }
115 
116  friend std::ostream& operator<<(std::ostream& os, const CustomType& X) {
117  os << X.m_value;
118  return os;
119  }
120 
121  // protected:
122 
123  Scalar m_value;
124 };
125 
126 template <typename Scalar>
127 Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic, Eigen::Dynamic> create(
128  int rows, int cols) {
129  typedef Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic, Eigen::Dynamic>
130  Matrix;
131  return Matrix(rows, cols);
132 }
133 
134 template <typename Scalar>
135 void print(const Eigen::Matrix<CustomType<Scalar>, Eigen::Dynamic,
136  Eigen::Dynamic>& mat) {
137  std::cout << mat << std::endl;
138 }
139 
140 template <typename Scalar>
141 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> build_matrix(int rows,
142  int cols) {
143  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix;
144  return Matrix(rows, cols);
145 }
146 
147 template <typename Scalar>
148 void expose_custom_type(const std::string& name) {
149  using namespace Eigen;
150  namespace bp = boost::python;
151 
152  typedef CustomType<Scalar> Type;
153 
154  bp::class_<Type>(name.c_str(), bp::init<Scalar>(bp::arg("value")))
155 
156  .def(bp::self + bp::self)
157  .def(bp::self - bp::self)
158  .def(bp::self * bp::self)
159  .def(bp::self / bp::self)
160 
161  .def(bp::self += bp::self)
162  .def(bp::self -= bp::self)
163  .def(bp::self *= bp::self)
164  .def(bp::self /= bp::self)
165 
166  .def("__repr__", &Type::print);
167 
168  int code = eigenpy::registerNewType<Type>();
169  std::cout << "code: " << code << std::endl;
170  eigenpy::registerCommonUfunc<Type>();
171 }
172 
174  using namespace Eigen;
175  namespace bp = boost::python;
177 
178  expose_custom_type<double>("CustomDouble");
179  typedef CustomType<double> DoubleType;
180  typedef Eigen::Matrix<DoubleType, Eigen::Dynamic, Eigen::Dynamic>
181  DoubleMatrix;
184  bp::def("create_double", create<double>);
185 
186  expose_custom_type<float>("CustomFloat");
187  typedef CustomType<float> FloatType;
188  typedef Eigen::Matrix<FloatType, Eigen::Dynamic, Eigen::Dynamic> FloatMatrix;
191  bp::def("create_float", create<float>);
192 
193  bp::def("build_matrix", build_matrix<double>);
194 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
195  bp::def("print", print<double>);
196  bp::def("print", print<float>);
197 #endif
198 
199  eigenpy::registerCast<DoubleType, double>(true);
200  eigenpy::registerCast<double, DoubleType>(true);
201  eigenpy::registerCast<DoubleType, int>(false);
202  eigenpy::registerCast<int, DoubleType>(true);
203  eigenpy::registerCast<DoubleType, long long>(false);
204  eigenpy::registerCast<long long, DoubleType>(true);
205  eigenpy::registerCast<DoubleType, long>(false);
206  eigenpy::registerCast<long, DoubleType>(true);
207  eigenpy::registerCast<FloatType, double>(true);
208  eigenpy::registerCast<double, FloatType>(false);
209  eigenpy::registerCast<FloatType, long long>(false);
210  eigenpy::registerCast<long long, FloatType>(true);
211  eigenpy::registerCast<FloatType, int>(false);
212  eigenpy::registerCast<int, FloatType>(true);
213  eigenpy::registerCast<FloatType, long>(false);
214  eigenpy::registerCast<long, FloatType>(true);
215 
216  bp::implicitly_convertible<double, DoubleType>();
217  bp::implicitly_convertible<DoubleType, double>();
218 }
CustomType::CustomType
CustomType()
Definition: user_type.cpp:62
CustomType::operator<
bool operator<(const CustomType &other) const
Definition: user_type.cpp:96
CustomType::operator==
bool operator==(const CustomType &other) const
Definition: user_type.cpp:86
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:135
eigenpy::enableEigenPy
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:29
CustomType::operator=
void operator=(const Scalar &value)
Definition: user_type.cpp:84
Eigen::NumTraits< CustomType< Scalar > >::Literal
CustomType< Scalar > Literal
Definition: user_type.cpp:22
CustomType::operator*=
void operator*=(const CustomType &other)
Definition: user_type.cpp:81
CustomType::operator<<
friend std::ostream & operator<<(std::ostream &os, const CustomType &X)
Definition: user_type.cpp:116
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:6
user-type.hpp
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(user_type)
Definition: user_type.cpp:173
CustomType::operator-
CustomType operator-() const
Definition: user_type.cpp:106
CustomType::print
std::string print() const
Definition: user_type.cpp:110
CustomType::operator>=
bool operator>=(const CustomType &other) const
Definition: user_type.cpp:99
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:79
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:171
CustomType::m_value
Scalar m_value
Definition: user_type.cpp:123
CustomType::operator>
bool operator>(const CustomType &other) const
Definition: user_type.cpp:102
CustomType::operator+
CustomType operator+(const CustomType &other) const
Definition: user_type.cpp:69
Eigen::NumTraits< CustomType< Scalar > >::digits10
static int digits10()
Definition: user_type.cpp:56
test_eigen_ref.mat
mat
Definition: test_eigen_ref.py:140
CustomType
Definition: user_type.cpp:13
ufunc.hpp
test_matrix.value
float value
Definition: test_matrix.py:161
CustomType::operator/=
void operator/=(const CustomType &other)
Definition: user_type.cpp:82
build_matrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > build_matrix(int rows, int cols)
Definition: user_type.cpp:141
CustomType::operator*
CustomType operator*(const CustomType &other) const
Definition: user_type.cpp:66
setup.name
name
Definition: setup.in.py:179
eigenpy.hpp
CustomType::operator-
CustomType operator-(const CustomType &other) const
Definition: user_type.cpp:72
CustomType::CustomType
CustomType(const Scalar &value)
Definition: user_type.cpp:64
test_LDLT.X
X
Definition: test_LDLT.py:19
create
Eigen::Matrix< CustomType< Scalar >, Eigen::Dynamic, Eigen::Dynamic > create(int rows, int cols)
Definition: user_type.cpp:127
CustomType::operator!=
bool operator!=(const CustomType &other) const
Definition: user_type.cpp:89
expose_custom_type
void expose_custom_type(const std::string &name)
Definition: user_type.cpp:148
Eigen::NumTraits< CustomType< Scalar > >::NonInteger
CustomType< Scalar > NonInteger
Definition: user_type.cpp:21
test_complex.cols
int cols
Definition: test_complex.py:7
CustomType::operator-=
void operator-=(const CustomType &other)
Definition: user_type.cpp:80
CustomType::operator<=
bool operator<=(const CustomType &other) const
Definition: user_type.cpp:93
eigenpy::EigenFromPyConverter
Definition: eigen-from-python.hpp:429
CustomType::operator/
CustomType operator/(const CustomType &other) const
Definition: user_type.cpp:75


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Tue Jan 23 2024 03:15:01