5 #ifndef __pinocchio_python_math_multiprecision_boost_number_hpp__
6 #define __pinocchio_python_math_multiprecision_boost_number_hpp__
10 #include <boost/python.hpp>
11 #include <boost/python/return_value_policy.hpp>
19 template<
class Backend>
20 struct get_backend_precision
24 template<
unsigned Digits10, ::boost::multiprecision::mpfr_allocation_type AllocateType>
25 struct get_backend_precision<::boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>>
39 template<
class Backend, ::boost::multiprecision::expression_
template_option ExpressionTemplates>
43 typedef ::boost::multiprecision::number<Backend, ExpressionTemplates>
Scalar;
45 static PyObject *
run(
void *
data,
void * )
48 Backend & backend = mpfr_scalar.backend();
50 if (backend.data()[0]._mpfr_d == 0)
59 bp::object
m(boost::ref(mpfr_scalar));
74 template<
typename BoostNumber>
76 :
public boost::python::def_visitor<BoostNumberPythonVisitor<BoostNumber>>
80 template<
class PyClass>
83 cl.def(bp::init<>(
"Default constructor.", bp::arg(
"self")))
84 .def(bp::init<BoostNumber>(
"Copy constructor.", bp::args(
"self",
"value")))
92 .def(bp::init<std::string>(
"Constructor from a string.", bp::args(
"self",
"str_value")));
96 cl.def(bp::self + bp::self)
97 .def(bp::self += bp::self)
98 .def(bp::self - bp::self)
99 .def(bp::self -= bp::self)
100 .def(bp::self * bp::self)
101 .def(bp::self *= bp::self)
102 .def(bp::self / bp::self)
103 .def(bp::self /= bp::self)
105 .def(bp::self < bp::self)
106 .def(bp::self <= bp::self)
107 .def(bp::self > bp::self)
108 .def(bp::self >= bp::self)
109 .def(bp::self == bp::self)
110 .def(bp::self != bp::self)
111 .def(bp::self_ns::pow(bp::self_ns::self,
long()));
114 cl.def(
"str", &BoostNumber::str, bp::args(
"self",
"precision",
"scientific"))
117 "default_precision",
static_cast<unsigned (*)()
>(BoostNumber::default_precision),
118 "Get the default precision of the class.")
120 "default_precision",
static_cast<void (*)(
unsigned)
>(BoostNumber::default_precision),
121 bp::arg(
"digits10"),
"Set the default precision of the class.")
122 .staticmethod(
"default_precision")
125 "precision",
static_cast<unsigned (BoostNumber::*)() const
>(&BoostNumber::precision),
126 bp::arg(
"self"),
"Get the precision of this.")
128 "precision",
static_cast<void (BoostNumber::*)(
unsigned)
>(&BoostNumber::precision),
129 bp::args(
"self",
"digits10"),
"Set the precision of this.")
131 .def(
"__float__", &cast<double>, bp::arg(
"self"),
"Cast to float.")
132 .def(
"__int__", &cast<int64_t>, bp::arg(
"self"),
"Cast to int.")
134 .def(
"__str__", &
print, bp::arg(
"self"))
135 .def(
"__repr__", &
print, bp::arg(
"self"))
139 "Set the precision when printing values.")
140 .staticmethod(
"set_display_precision")
144 "Get the precision when printing values.",
145 bp::return_value_policy<bp::copy_non_const_reference>())
146 .staticmethod(
"get_display_precision")
154 static void expose(
const std::string & type_name)
156 bp::class_<BoostNumber>(type_name.c_str(),
"", bp::no_init)
159 eigenpy::registerNewType<BoostNumber>();
160 eigenpy::registerCommonUfunc<BoostNumber>();
162 #define IMPLICITLY_CONVERTIBLE(T1, T2) bp::implicitly_convertible<T1, T2>();
174 #undef IMPLICITLY_CONVERTIBLE
176 eigenpy::registerCast<BoostNumber, double>(
false);
177 eigenpy::registerCast<double, BoostNumber>(
true);
178 eigenpy::registerCast<BoostNumber, float>(
false);
179 eigenpy::registerCast<float, BoostNumber>(
true);
180 eigenpy::registerCast<BoostNumber, long>(
false);
181 eigenpy::registerCast<long, BoostNumber>(
true);
182 eigenpy::registerCast<BoostNumber, int>(
false);
183 eigenpy::registerCast<int, BoostNumber>(
true);
185 eigenpy::registerCast<BoostNumber, int64_t>(
false);
186 eigenpy::registerCast<int64_t, BoostNumber>(
true);
191 static T cast(
const BoostNumber &
self)
193 return static_cast<T>(
self);
196 static std::string
print(
const BoostNumber &
self)
208 static int precision = BoostNumber::default_precision();
224 #endif // ifndef __pinocchio_python_math_multiprecision_boost_number_hpp__