pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
cppad.hpp
1//
2// Copyright (c) 2018-2023 CNRS INRIA
3//
4
5#pragma once
6
7// IWYU pragma: always_keep
8
9#define PINOCCHIO_WITH_CPPAD_SUPPORT
10
11// IWYU pragma: begin_keep
12#include <cmath>
13
14#include <Eigen/Core>
15#include <Eigen/Geometry>
16
17#include <boost/version.hpp>
18#include <boost/math/constants/constants.hpp>
19#include <boost/mpl/int.hpp>
20
21#include <cppad/cppad.hpp>
22
23#include "pinocchio/fwd.hpp"
24#include "pinocchio/macros.hpp"
25#include "pinocchio/eigen-common.hpp"
26#include "pinocchio/fwd.hpp"
27
28#include "pinocchio/utils/static-if.hpp"
29
30#include "pinocchio/math.hpp"
31#include "pinocchio/spatial.hpp"
32// IWYU pragma: end_keep
33
34namespace boost
35{
36 namespace math
37 {
38 namespace constants
39 {
40 namespace detail
41 {
42 template<typename Scalar>
43 struct constant_pi<CppAD::AD<Scalar>> : constant_pi<Scalar>
44 {
45 typedef CppAD::AD<Scalar> ADScalar;
46
47 template<int N>
48 static inline ADScalar get(const mpl::int_<N> & n)
49 {
50 return ADScalar(constant_pi<Scalar>::get(n));
51 }
52
53#if BOOST_VERSION >= 107700
54 template<class T, T value>
55 static inline ADScalar get(const std::integral_constant<T, value> & n)
56 {
57 return ADScalar(constant_pi<Scalar>::get(n));
58 }
59#else
60 template<class T, T value>
61 static inline ADScalar get(const boost::integral_constant<T, value> & n)
62 {
63 return ADScalar(constant_pi<Scalar>::get(n));
64 }
65#endif
66 };
67 } // namespace detail
68 } // namespace constants
69 } // namespace math
70} // namespace boost
71
72namespace Eigen
73{
74 namespace internal
75 {
76 // Specialization of Eigen::internal::cast_impl for CppAD input types
77 template<typename Scalar>
78 struct cast_impl<CppAD::AD<Scalar>, Scalar>
79 {
80 EIGEN_DEVICE_FUNC
81 static inline Scalar run(const CppAD::AD<Scalar> & x)
82 {
83 return CppAD::Value(x);
84 }
85 };
86 } // namespace internal
87} // namespace Eigen
88
89// Source from #include <cppad/example/cppad_eigen.hpp>
90namespace Eigen
91{
92 template<class Base>
93 struct NumTraits<CppAD::AD<Base>>
94 { // type that corresponds to the real part of an AD<Base> value
95 typedef CppAD::AD<Base> Real;
96 // type for AD<Base> operations that result in non-integer values
97 typedef CppAD::AD<Base> NonInteger;
98 // type to use for numeric literals such as "2" or "0.5".
99 typedef CppAD::AD<Base> Literal;
100 // type for nested value inside an AD<Base> expression tree
101 typedef CppAD::AD<Base> Nested;
102
103 enum
104 {
105 // does not support complex Base types
106 IsComplex = 0,
107 // does not support integer Base types
108 IsInteger = 0,
109 // only support signed Base types
110 IsSigned = 1,
111 // must initialize an AD<Base> object
112 RequireInitialization = 1,
113 // computational cost of the corresponding operations
114 ReadCost = 1,
115 AddCost = 2,
116 MulCost = 2
117 };
118
119 // machine epsilon with type of real part of x
120 // (use assumption that Base is not complex)
121 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline CppAD::AD<Base> epsilon(void)
122 {
123 return CppAD::numeric_limits<CppAD::AD<Base>>::epsilon();
124 }
125
126 // relaxed version of machine epsilon for comparison of different
127 // operations that should result in the same value
128 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline CppAD::AD<Base> dummy_precision(void)
129 {
130 return 100. * CppAD::numeric_limits<CppAD::AD<Base>>::epsilon();
131 }
132
133 // minimum normalized positive value
134 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline CppAD::AD<Base> lowest(void)
135 {
136 return CppAD::numeric_limits<CppAD::AD<Base>>::min();
137 }
138
139 // maximum finite value
140 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline CppAD::AD<Base> highest(void)
141 {
142 return CppAD::numeric_limits<CppAD::AD<Base>>::max();
143 }
144
145 // number of decimal digits that can be represented without change.
146 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits10(void)
147 {
148 return CppAD::numeric_limits<CppAD::AD<Base>>::digits10;
149 }
150
151 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int digits()
152 {
153 return NumTraits<Base>::digits();
154 }
155
156#if EIGEN_VERSION_AT_LEAST(3, 4, 90)
157 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static inline int max_digits10()
158 {
159 return NumTraits<Base>::max_digits10();
160 }
161#endif
162 };
163} // namespace Eigen
164
165// Source from #include <cppad/example/cppad_eigen.hpp>
166namespace CppAD
167{
168 // functions that return references
169 template<class Base>
170 const AD<Base> & conj(const AD<Base> & x)
171 {
172 return x;
173 }
174 template<class Base>
175 const AD<Base> & real(const AD<Base> & x)
176 {
177 return x;
178 }
179
180 // functions that return values (note abs is defined by cppad.hpp)
181 template<class Base>
182 AD<Base> imag(const AD<Base> & /*x*/)
183 {
184 return CppAD::AD<Base>(0.);
185 }
186 template<class Base>
187 AD<Base> abs2(const AD<Base> & x)
188 {
189 return x * x;
190 }
191
192 template<typename Scalar>
193 AD<Scalar> min(const AD<Scalar> & x, const AD<Scalar> & y)
194 {
195 using ::pinocchio::internal::if_then_else;
196 using ::pinocchio::internal::LT;
197 return if_then_else(LT, y, x, y, x);
198 }
199
200 template<typename Scalar>
201 AD<Scalar> max(const AD<Scalar> & x, const AD<Scalar> & y)
202 {
203 using ::pinocchio::internal::if_then_else;
204 using ::pinocchio::internal::LT;
205 return if_then_else(LT, x, y, y, x);
206 }
207} // namespace CppAD
208
209namespace CppAD
210{
211 template<class Scalar>
212 bool isfinite(const AD<Scalar> & x)
213 {
214 return isfinite(Value(x));
215 }
216} // namespace CppAD
217
218// IWYU pragma: begin_exports
219#include "pinocchio/src/autodiff/cppad/utils/static-if.hxx"
220#include "pinocchio/src/autodiff/cppad/math/quaternion.hxx"
221#include "pinocchio/src/autodiff/cppad/math/taylor-series-expansion.hxx"
222#include "pinocchio/src/autodiff/cppad/spatial/se3-tpl.hxx"
223#include "pinocchio/src/autodiff/cppad/spatial/log.hxx"
224#include "pinocchio/src/autodiff/cppad/algorithm/aba.hxx"
225// IWYU pragma: end_exports