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