user_type.cpp 5.48 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
100
101
102
103
104
105
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); }
  
  std::string print() const
  {
    std::stringstream ss;
    ss << "value: " << m_value << std::endl;
    return ss.str();
  }
Justin Carpentier's avatar
Justin Carpentier committed
106
107
108
109
110
111
  
  friend std::ostream & operator <<(std::ostream & os, const CustomType & X)
  {
    os << X.m_value;
    return os;
  }
112
113
114
115
116
117
118
119
120
121
122
123
124
 
protected:
  
  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
125
126
127
128
129
130
template<typename Scalar>
void print(const Eigen::Matrix<CustomType<Scalar>,Eigen::Dynamic,Eigen::Dynamic> & mat)
{
  std::cout << mat << std::endl;
}

131
132
133
134
135
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
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)
  ;
  
161
162
  int code = eigenpy::registerNewType<Type>();
  std::cout << "code: " << code << std::endl;
163
164
165
166
167
168
169
170
171
172
173
174
175
  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
176
  eigenpy::EigenFromPyConverter<DoubleMatrix>::registration();
177
178
179
180
181
182
  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
183
  eigenpy::EigenFromPyConverter<FloatMatrix>::registration();
184
185
186
  bp::def("create_float",create<float>);
  
  bp::def("build_matrix",build_matrix<double>);
Justin Carpentier's avatar
Justin Carpentier committed
187
188
  bp::def("print",print<double>);
  bp::def("print",print<float>);
189
}