user_type.cpp 6.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * Copyright 2020 INRIA
 */

#include "eigenpy/eigenpy.hpp"
#include "eigenpy/user-type.hpp"
#include "eigenpy/ufunc.hpp"

#include <iostream>
#include <sstream>

Justin Carpentier's avatar
Justin Carpentier committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template<typename Scalar> struct CustomType;

namespace Eigen
{
  /// @brief Eigen::NumTraits<> specialization for casadi::SX
  ///
  template<typename Scalar>
  struct NumTraits< CustomType<Scalar> >
  {
    typedef CustomType<Scalar> Real;
    typedef CustomType<Scalar> NonInteger;
    typedef CustomType<Scalar> Literal;
    typedef CustomType<Scalar> Nested;
    
    enum {
      // does not support complex Base types
      IsComplex             = 0 ,
      // does not support integer Base types
      IsInteger             = 0 ,
      // only support signed Base types
      IsSigned              = 1 ,
      // must initialize an AD<Base> object
      RequireInitialization = 1 ,
      // computational cost of the corresponding operations
      ReadCost              = 1 ,
      AddCost               = 2 ,
      MulCost               = 2
    };
    
41
    static CustomType<Scalar> epsilon()
Justin Carpentier's avatar
Justin Carpentier committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    {
      return CustomType<Scalar>(std::numeric_limits<Scalar>::epsilon());
    }
    
    static CustomType<Scalar> dummy_precision()
    {
      return CustomType<Scalar>(NumTraits<Scalar>::dummy_precision());
    }
    
    static CustomType<Scalar> highest()
    {
      return CustomType<Scalar>(std::numeric_limits<Scalar>::max());
    }
    
    static CustomType<Scalar> lowest()
    {
      return CustomType<Scalar>(std::numeric_limits<Scalar>::min());
    }
    
    static int digits10()
    {
      return std::numeric_limits<Scalar>::digits10;
    }
  };
} // namespace Eigen


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
template<typename Scalar>
struct CustomType
{
  CustomType() {}
  
  explicit CustomType(const Scalar & value)
  : m_value(value)
  {}
  
  CustomType operator*(const CustomType & other) const { return CustomType(m_value * other.m_value); }
  CustomType operator+(const CustomType & other) const { return CustomType(m_value + other.m_value); }
  CustomType operator-(const CustomType & other) const { return CustomType(m_value - other.m_value); }
  CustomType operator/(const CustomType & other) const { return CustomType(m_value / other.m_value); }
  
  void operator+=(const CustomType & other) { m_value += other.m_value; }
  void operator-=(const CustomType & other) { m_value -= other.m_value; }
  void operator*=(const CustomType & other) { m_value *= other.m_value; }
  void operator/=(const CustomType & other) { m_value /= other.m_value; }
  
  void operator=(const Scalar & value) { m_value = value; }
  
  bool operator==(const CustomType & other) const { return m_value == other.m_value; }
  bool operator!=(const CustomType & other) const { return m_value != other.m_value; }
  
  bool operator<=(const CustomType & other) const { return m_value <= other.m_value; }
  bool operator<(const CustomType & other) const { return m_value < other.m_value; }
  bool operator>=(const CustomType & other) const { return m_value >= other.m_value; }
  bool operator>(const CustomType & other) const { return m_value > other.m_value; }
  
  CustomType operator-() const { return CustomType(-m_value); }
  
100
101
102
103
104
  operator Scalar () const
  {
    return m_value;
  }
  
105
106
107
108
109
110
  std::string print() const
  {
    std::stringstream ss;
    ss << "value: " << m_value << std::endl;
    return ss.str();
  }
Justin Carpentier's avatar
Justin Carpentier committed
111
112
113
114
115
116
  
  friend std::ostream & operator <<(std::ostream & os, const CustomType & X)
  {
    os << X.m_value;
    return os;
  }
117
 
118
//protected:
119
120
121
122
123
124
125
126
127
128
129
  
  Scalar m_value;
};

template<typename Scalar>
Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> create(int rows, int cols)
{
  typedef Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> Matrix;
  return Matrix(rows,cols);
}

Justin Carpentier's avatar
Justin Carpentier committed
130
131
132
133
134
135
template<typename Scalar>
void print(const Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> & mat)
{
  std::cout << mat << std::endl;
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
template<typename Scalar>
Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> build_matrix(int rows, int cols)
{
  typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> Matrix;
  return Matrix(rows,cols);
}

template<typename Scalar>
void expose_custom_type(const std::string & name)
{
  using namespace Eigen;
  namespace bp = boost::python;
  
  typedef CustomType<Scalar> Type;
  
  bp::class_<Type>(name.c_str(),bp::init<Scalar>(bp::arg("value")))
  
  .def(bp::self + bp::self)
  .def(bp::self - bp::self)
  .def(bp::self * bp::self)
  .def(bp::self / bp::self)
  
  .def(bp::self += bp::self)
  .def(bp::self -= bp::self)
  .def(bp::self *= bp::self)
  .def(bp::self /= bp::self)
  
  .def("__repr__",&Type::print)
  ;
  
166
167
  int code = eigenpy::registerNewType<Type>();
  std::cout << "code: " << code << std::endl;
168
169
170
171
172
173
174
175
176
177
178
179
180
  eigenpy::registerCommonUfunc<Type>();
}

BOOST_PYTHON_MODULE(user_type)
{
  using namespace Eigen;
  namespace bp = boost::python;
  eigenpy::enableEigenPy();
  
  expose_custom_type<double>("CustomDouble");
  typedef CustomType<double> DoubleType;
  typedef Eigen::Matrix<DoubleType,Eigen::Dynamic,Eigen::Dynamic> DoubleMatrix;
  eigenpy::EigenToPyConverter<DoubleMatrix>::registration();
Justin Carpentier's avatar
Justin Carpentier committed
181
  eigenpy::EigenFromPyConverter<DoubleMatrix>::registration();
182
183
184
185
186
187
  bp::def("create_double",create<double>);
  
  expose_custom_type<float>("CustomFloat");
  typedef CustomType<float> FloatType;
  typedef Eigen::Matrix<FloatType,Eigen::Dynamic,Eigen::Dynamic> FloatMatrix;
  eigenpy::EigenToPyConverter<FloatMatrix>::registration();
Justin Carpentier's avatar
Justin Carpentier committed
188
  eigenpy::EigenFromPyConverter<FloatMatrix>::registration();
189
190
191
  bp::def("create_float",create<float>);
  
  bp::def("build_matrix",build_matrix<double>);
Justin Carpentier's avatar
Justin Carpentier committed
192
193
  bp::def("print",print<double>);
  bp::def("print",print<float>);
194
195
196
197
198
199
200
201
202
203
204
205
  
  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>();
206
}