5 #ifndef __pinocchio_math_tensor_hpp__ 6 #define __pinocchio_math_tensor_hpp__ 8 #include "pinocchio/fwd.hpp" 10 #if !EIGEN_VERSION_AT_LEAST(3,2,90) 11 #define EIGEN_DEVICE_FUNC 14 #ifdef PINOCCHIO_WITH_EIGEN_TENSOR_MODULE 15 #include <unsupported/Eigen/CXX11/Tensor> 17 #if (__cplusplus <= 199711L && EIGEN_COMP_MSVC < 1900) || defined(__CUDACC__) || defined(EIGEN_AVOID_STL_ARRAY) 19 template <
typename T, std::
size_t n>
23 EIGEN_STRONG_INLINE T& operator[] (
size_t index)
24 {
return values[index]; }
26 EIGEN_STRONG_INLINE
const T& operator[] (
size_t index)
const 27 {
return values[index]; }
30 EIGEN_STRONG_INLINE T& front() {
return values[0]; }
32 EIGEN_STRONG_INLINE
const T& front()
const {
return values[0]; }
35 EIGEN_STRONG_INLINE T& back() {
return values[n-1]; }
37 EIGEN_STRONG_INLINE
const T& back()
const {
return values[n-1]; }
39 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
40 static std::size_t size() {
return n; }
45 template<
class T, std::
size_t n>
48 for (std::size_t i = 0; i < n; ++i) {
49 if (lhs[i] != rhs[i]) {
56 template<
class T, std::
size_t n>
65 template <
typename T, std::
size_t N>
using array = std::array<T, N>;
73 #ifndef PINOCCHIO_WITH_EIGEN_TENSOR_MODULE 76 template<
typename Scalar_,
int NumIndices_,
int Options_ = 0,
typename IndexType = Eigen::DenseIndex>
79 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
81 typedef Scalar_ Scalar;
85 NumIndices = NumIndices_
87 typedef IndexType Index;
90 inline Tensor& base() {
return *
this; }
91 inline const Tensor& base()
const {
return *
this; }
93 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
94 Dimensions& dimensions() {
return m_dimensions; }
95 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
96 const Dimensions& dimensions()
const {
return m_dimensions; }
98 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank()
const 103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n)
const 105 assert(n <= NumIndices &&
"n is larger than the dimension of the tensor.");
106 return m_dimensions[n];
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size()
const 111 return m_storage.size();
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data()
116 return m_storage.data();
119 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar *data()
const 121 return m_storage.data();
125 EIGEN_STRONG_INLINE
Tensor& setZero()
127 return setConstant(Scalar(0));
131 EIGEN_STRONG_INLINE
Tensor& setConstant(
const Scalar & val)
133 m_storage.setConstant(val);
138 EIGEN_STRONG_INLINE
Tensor& setRandom()
140 m_storage.setRandom();
144 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Tensor()
149 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(
const Tensor & other)
150 : m_storage(other.m_storage)
151 , m_dimensions(other.m_dimensions)
155 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
explicit Tensor(Index dim1)
158 m_dimensions[0] = dim1;
159 EIGEN_STATIC_ASSERT(1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index dim1, Index dim2)
163 : m_storage(dim1*dim2)
165 m_dimensions[0] = dim1;
166 m_dimensions[1] = dim2;
167 EIGEN_STATIC_ASSERT(2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
170 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index dim1, Index dim2, Index dim3)
171 : m_storage(dim1*dim2*dim3)
173 m_dimensions[0] = dim1;
174 m_dimensions[1] = dim2;
175 m_dimensions[2] = dim3;
176 EIGEN_STATIC_ASSERT(3 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
179 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index dim1, Index dim2, Index dim3, Index dim4)
180 : m_storage(dim1*dim2*dim3*dim4)
182 m_dimensions[0] = dim1;
183 m_dimensions[1] = dim2;
184 m_dimensions[2] = dim3;
185 m_dimensions[3] = dim4;
186 EIGEN_STATIC_ASSERT(4 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
189 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor(Index dim1, Index dim2, Index dim3, Index dim4, Index dim5)
190 : m_storage(dim1*dim2*dim3*dim4*dim5)
192 m_dimensions[0] = dim1;
193 m_dimensions[1] = dim2;
194 m_dimensions[2] = dim3;
195 m_dimensions[3] = dim4;
196 m_dimensions[4] = dim5;
197 EIGEN_STATIC_ASSERT(5 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
201 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0)
const 203 EIGEN_STATIC_ASSERT(1 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
204 return m_storage.coeff(i0);
208 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1)
const 210 EIGEN_STATIC_ASSERT(2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
211 return m_storage.coeff(i0 + i1 * m_dimensions[0]);
215 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2)
const 217 EIGEN_STATIC_ASSERT(3 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
218 return m_storage.coeff(i0 + i1 * m_dimensions[0] + i2 * m_dimensions[1] * m_dimensions[0]);
222 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3)
const 224 EIGEN_STATIC_ASSERT(4 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
225 return coeff(i0 + i1 * m_dimensions[0] + i2 * m_dimensions[1] * m_dimensions[0] + i3 * m_dimensions[2] * m_dimensions[1] * m_dimensions[0]);
229 EIGEN_STRONG_INLINE
const Scalar& operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
const 231 EIGEN_STATIC_ASSERT(5 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
232 return coeff(i0 + i1 * m_dimensions[0] + i2 * m_dimensions[1] * m_dimensions[0] + i3 * m_dimensions[2] * m_dimensions[1] * m_dimensions[0] + i4 * m_dimensions[3] * m_dimensions[2] * m_dimensions[1] * m_dimensions[0]);
239 Index size = Index(1);
240 for(i = 0; i < NumIndices; i++)
242 Eigen::internal::check_rows_cols_for_overflow<Eigen::Dynamic>::run(size, dimensions[i]);
243 size *= dimensions[i];
246 for(i = 0; i < NumIndices; i++)
247 m_dimensions[i] = dimensions[i];
249 bool size_changed = size != this->size();
250 if(size_changed) m_storage.resize(size);
252 #ifdef EIGEN_INITIALIZE_COEFFS 255 #if defined(EIGEN_INITIALIZE_MATRICES_BY_ZERO) 256 m_storage.fill(Scalar(0));
257 #elif defined(EIGEN_INITIALIZE_MATRICES_BY_NAN) 258 m_storage.fill(std::numeric_limits<Scalar>::quiet_NaN());
264 EIGEN_DEVICE_FUNC
bool operator==(
const Tensor & other)
const 266 return m_storage == other.m_storage;
269 EIGEN_DEVICE_FUNC
bool operator!=(
const Tensor & other)
const 271 return m_storage != other.m_storage;
276 typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,Options> StorageType;
277 StorageType m_storage;
279 Dimensions m_dimensions;
286 template<
typename Scalar_,
int NumIndices_,
int Options_ = 0,
typename IndexType = Eigen::DenseIndex>
287 using Tensor = Eigen::Tensor<Scalar_,NumIndices_,Options_,IndexType>;
289 #endif // ifndef PINOCCHIO_WITH_EIGEN_TENSOR_MODULE 293 #if !EIGEN_VERSION_AT_LEAST(3,2,90) 294 #undef EIGEN_DEVICE_FUNC 297 #endif // ifndef __pinocchio_math_tensor_hpp__
Main pinocchio namespace.