math/matrix.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2020 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_math_matrix_hpp__
6 #define __pinocchio_math_matrix_hpp__
7 
8 #include "pinocchio/macros.hpp"
9 #include "pinocchio/math/fwd.hpp"
11 
12 #include <boost/type_traits.hpp>
13 #include <Eigen/Dense>
14 
15 namespace pinocchio
16 {
17 
18  template<typename Derived>
19  inline bool hasNaN(const Eigen::DenseBase<Derived> & m)
20  {
21  return !((m.derived().array() == m.derived().array()).all());
22  }
23 
24  namespace internal
25  {
26  template<
27  typename MatrixLike,
28  bool value = is_floating_point<typename MatrixLike::Scalar>::value>
29  struct isZeroAlgo
30  {
31  typedef typename MatrixLike::Scalar Scalar;
32  typedef typename MatrixLike::RealScalar RealScalar;
33 
34  static bool run(
35  const Eigen::MatrixBase<MatrixLike> & mat,
36  const RealScalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
37  {
38  return mat.isZero(prec);
39  }
40  };
41 
42  template<typename MatrixLike>
43  struct isZeroAlgo<MatrixLike, false>
44  {
45  typedef typename MatrixLike::Scalar Scalar;
46  typedef typename MatrixLike::RealScalar RealScalar;
47 
48  static bool run(
49  const Eigen::MatrixBase<MatrixLike> & /*vec*/,
50  const RealScalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
51  {
53  return true;
54  }
55  };
56  } // namespace internal
57 
58  template<typename MatrixLike>
59  inline bool isZero(
60  const Eigen::MatrixBase<MatrixLike> & m,
61  const typename MatrixLike::RealScalar & prec =
62  Eigen::NumTraits<typename MatrixLike::Scalar>::dummy_precision())
63  {
65  }
66 
67  template<typename M1, typename M2>
69  {
70 #if EIGEN_VERSION_AT_LEAST(3, 2, 90)
71  typedef typename Eigen::Product<M1, M2> type;
72 #else
73  typedef typename Eigen::ProductReturnType<M1, M2>::Type type;
74 #endif
75  };
76 
77  template<typename Scalar, typename Matrix>
79  {
80 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
81  typedef Eigen::CwiseBinaryOp<
82  EIGEN_CAT(EIGEN_CAT(Eigen::internal::scalar_, product), _op) < Scalar,
85  const Matrix > type;
86 #elif EIGEN_VERSION_AT_LEAST(3, 2, 90)
87  typedef Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<Scalar>, const Matrix> type;
88 #else
89  typedef const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<Scalar>, const Matrix>
91 #endif
92  };
93 
94  template<typename Matrix, typename Scalar>
96  {
97 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
98  typedef Eigen::CwiseBinaryOp<
99  EIGEN_CAT(EIGEN_CAT(Eigen::internal::scalar_, product), _op) <
101  Scalar>,
102  const Matrix,
104 #elif EIGEN_VERSION_AT_LEAST(3, 2, 90)
105  typedef Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<Scalar>, const Matrix> type;
106 #else
107  typedef const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<Scalar>, const Matrix>
109 #endif
110  };
111 
112  namespace internal
113  {
114  template<
115  typename MatrixLike,
118  {
119  typedef typename MatrixLike::Scalar Scalar;
120  typedef typename MatrixLike::RealScalar RealScalar;
121 
122  static bool run(
123  const Eigen::MatrixBase<MatrixLike> & mat,
124  const RealScalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
125  {
126  return mat.isUnitary(prec);
127  }
128  };
129 
130  template<typename MatrixLike>
131  struct isUnitaryAlgo<MatrixLike, false>
132  {
133  typedef typename MatrixLike::Scalar Scalar;
134  typedef typename MatrixLike::RealScalar RealScalar;
135 
136  static bool run(
137  const Eigen::MatrixBase<MatrixLike> & /*vec*/,
138  const RealScalar & prec = Eigen::NumTraits<Scalar>::dummy_precision())
139  {
141  return true;
142  }
143  };
144  } // namespace internal
145 
154  template<typename MatrixLike>
155  inline bool isUnitary(
156  const Eigen::MatrixBase<MatrixLike> & mat,
157  const typename MatrixLike::RealScalar & prec =
158  Eigen::NumTraits<typename MatrixLike::Scalar>::dummy_precision())
159  {
161  }
162 
163  namespace internal
164  {
165  template<
166  typename VectorLike,
167  bool value = is_floating_point<typename VectorLike::Scalar>::value>
169  {
170  typedef typename VectorLike::Scalar Scalar;
171  typedef typename VectorLike::RealScalar RealScalar;
172 
173  static bool run(
174  const Eigen::MatrixBase<VectorLike> & vec,
175  const RealScalar & prec = Eigen::NumTraits<RealScalar>::dummy_precision())
176  {
177  return math::fabs(static_cast<RealScalar>(vec.norm() - RealScalar(1))) <= prec;
178  }
179  };
180 
181  template<typename VectorLike>
182  struct isNormalizedAlgo<VectorLike, false>
183  {
184  typedef typename VectorLike::Scalar Scalar;
185  typedef typename VectorLike::RealScalar RealScalar;
186 
187  static bool run(
188  const Eigen::MatrixBase<VectorLike> & /*vec*/,
189  const RealScalar & prec = Eigen::NumTraits<RealScalar>::dummy_precision())
190  {
192  return true;
193  }
194  };
195  } // namespace internal
196 
205  template<typename VectorLike>
206  inline bool isNormalized(
207  const Eigen::MatrixBase<VectorLike> & vec,
208  const typename VectorLike::RealScalar & prec =
209  Eigen::NumTraits<typename VectorLike::Scalar>::dummy_precision())
210  {
211  EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorLike);
213  }
214 
215  namespace internal
216  {
217  template<
218  typename VectorLike,
219  bool value = is_floating_point<typename VectorLike::Scalar>::value>
221  {
222  static void run(const Eigen::MatrixBase<VectorLike> & vec)
223  {
224  return vec.const_cast_derived().normalize();
225  }
226  };
227 
228  template<typename VectorLike>
229  struct normalizeAlgo<VectorLike, false>
230  {
231  static void run(const Eigen::MatrixBase<VectorLike> & vec)
232  {
233  using namespace internal;
234  typedef typename VectorLike::RealScalar RealScalar;
235  typedef typename VectorLike::Scalar Scalar;
236  const RealScalar z = vec.squaredNorm();
237  const Scalar sqrt_z = if_then_else(GT, z, Scalar(0), math::sqrt(z), Scalar(1));
238  vec.const_cast_derived() /= sqrt_z;
239  }
240  };
241  } // namespace internal
242 
248  template<typename VectorLike>
249  inline void normalize(const Eigen::MatrixBase<VectorLike> & vec)
250  {
251  EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorLike);
252  internal::normalizeAlgo<VectorLike>::run(vec.const_cast_derived());
253  }
254 
255  namespace internal
256  {
257  template<typename Scalar>
259  {
260  template<typename MatrixIn, typename MatrixOut>
261  static void
262  run(const Eigen::MatrixBase<MatrixIn> & m_in, const Eigen::MatrixBase<MatrixOut> & dest)
263  {
264  MatrixOut & dest_ = PINOCCHIO_EIGEN_CONST_CAST(MatrixOut, dest);
265  dest_.noalias() = m_in.inverse();
266  }
267  };
268 
269  } // namespace internal
270 
271  template<typename MatrixIn, typename MatrixOut>
272  inline void
273  inverse(const Eigen::MatrixBase<MatrixIn> & m_in, const Eigen::MatrixBase<MatrixOut> & dest)
274  {
275  MatrixOut & dest_ = PINOCCHIO_EIGEN_CONST_CAST(MatrixOut, dest);
277  m_in, dest_);
278  }
279 
280 } // namespace pinocchio
281 
282 #endif // #ifndef __pinocchio_math_matrix_hpp__
pinocchio::MatrixMatrixProduct
Definition: math/matrix.hpp:68
pinocchio::internal::normalizeAlgo< VectorLike, false >::run
static void run(const Eigen::MatrixBase< VectorLike > &vec)
Definition: math/matrix.hpp:231
pinocchio::MatrixScalarProduct::type
const typedef Eigen::CwiseUnaryOp< Eigen::internal::scalar_multiple_op< Scalar >, const Matrix > type
Definition: math/matrix.hpp:108
common_symbols.type
type
Definition: common_symbols.py:35
pinocchio::internal::isNormalizedAlgo::Scalar
VectorLike::Scalar Scalar
Definition: math/matrix.hpp:170
test-cpp2pybind11.m
m
Definition: test-cpp2pybind11.py:25
pinocchio::internal::normalizeAlgo::run
static void run(const Eigen::MatrixBase< VectorLike > &vec)
Definition: math/matrix.hpp:222
static-if.hpp
pinocchio::internal::CallCorrectMatrixInverseAccordingToScalar::run
static void run(const Eigen::MatrixBase< MatrixIn > &m_in, const Eigen::MatrixBase< MatrixOut > &dest)
Definition: math/matrix.hpp:262
pinocchio::internal::isZeroAlgo::RealScalar
MatrixLike::RealScalar RealScalar
Definition: math/matrix.hpp:32
macros.hpp
pinocchio::isUnitary
bool isUnitary(const Eigen::MatrixBase< MatrixLike > &mat, const typename MatrixLike::RealScalar &prec=Eigen::NumTraits< typename MatrixLike::Scalar >::dummy_precision())
Check whether the input matrix is Unitary within the given precision.
Definition: math/matrix.hpp:155
pinocchio::isZero
bool isZero(const Eigen::MatrixBase< MatrixLike > &m, const typename MatrixLike::RealScalar &prec=Eigen::NumTraits< typename MatrixLike::Scalar >::dummy_precision())
Definition: math/matrix.hpp:59
pinocchio::MatrixScalarProduct
Definition: math/matrix.hpp:95
pinocchio::internal::GT
@ GT
Definition: utils/static-if.hpp:21
pinocchio::internal::isZeroAlgo::Scalar
MatrixLike::Scalar Scalar
Definition: math/matrix.hpp:31
PINOCCHIO_EIGEN_CONST_CAST
#define PINOCCHIO_EIGEN_CONST_CAST(TYPE, OBJ)
Macro for an automatic const_cast.
Definition: eigen-macros.hpp:51
pinocchio::python::Scalar
context::Scalar Scalar
Definition: admm-solver.cpp:29
vec
vec
pinocchio::internal::if_then_else
if_then_else_impl< LhsType, RhsType, ThenType, ElseType >::ReturnType if_then_else(const ComparisonOperators op, const LhsType &lhs_value, const RhsType &rhs_value, const ThenType &then_value, const ElseType &else_value)
Definition: utils/static-if.hpp:89
pinocchio::internal::CallCorrectMatrixInverseAccordingToScalar
Definition: math/matrix.hpp:258
pinocchio::internal::isUnitaryAlgo
Definition: math/matrix.hpp:117
pinocchio::internal::isNormalizedAlgo::run
static bool run(const Eigen::MatrixBase< VectorLike > &vec, const RealScalar &prec=Eigen::NumTraits< RealScalar >::dummy_precision())
Definition: math/matrix.hpp:173
pinocchio::internal::normalizeAlgo
Definition: math/matrix.hpp:220
pinocchio::isNormalized
bool isNormalized(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &q, const Scalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Check whether a configuration vector is normalized within the given precision provided by prec.
Definition: joint-configuration.hpp:933
pinocchio::internal::isUnitaryAlgo< MatrixLike, false >::RealScalar
MatrixLike::RealScalar RealScalar
Definition: math/matrix.hpp:134
pinocchio::internal::isZeroAlgo::run
static bool run(const Eigen::MatrixBase< MatrixLike > &mat, const RealScalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: math/matrix.hpp:34
pinocchio::internal::isZeroAlgo< MatrixLike, false >::run
static bool run(const Eigen::MatrixBase< MatrixLike > &, const RealScalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: math/matrix.hpp:48
pinocchio::internal::isUnitaryAlgo< MatrixLike, false >::Scalar
MatrixLike::Scalar Scalar
Definition: math/matrix.hpp:133
pinocchio::internal::isUnitaryAlgo::RealScalar
MatrixLike::RealScalar RealScalar
Definition: math/matrix.hpp:120
mat
mat
pinocchio::is_floating_point
Definition: math/fwd.hpp:17
fwd.hpp
pinocchio::internal::isZeroAlgo< MatrixLike, false >::Scalar
MatrixLike::Scalar Scalar
Definition: math/matrix.hpp:45
value
float value
pinocchio::inverse
void inverse(const Eigen::MatrixBase< MatrixIn > &m_in, const Eigen::MatrixBase< MatrixOut > &dest)
Definition: math/matrix.hpp:273
pinocchio::internal::isZeroAlgo
Definition: math/matrix.hpp:29
pinocchio::MatrixMatrixProduct::type
Eigen::ProductReturnType< M1, M2 >::Type type
Definition: math/matrix.hpp:73
pinocchio::internal::isUnitaryAlgo< MatrixLike, false >::run
static bool run(const Eigen::MatrixBase< MatrixLike > &, const RealScalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: math/matrix.hpp:136
pinocchio::internal::isUnitaryAlgo::Scalar
MatrixLike::Scalar Scalar
Definition: math/matrix.hpp:119
pinocchio::hasNaN
bool hasNaN(const Eigen::DenseBase< Derived > &m)
Definition: math/matrix.hpp:19
pinocchio::internal::isUnitaryAlgo::run
static bool run(const Eigen::MatrixBase< MatrixLike > &mat, const RealScalar &prec=Eigen::NumTraits< Scalar >::dummy_precision())
Definition: math/matrix.hpp:122
pinocchio::internal::isNormalizedAlgo
Definition: math/matrix.hpp:168
pinocchio::internal::isZeroAlgo< MatrixLike, false >::RealScalar
MatrixLike::RealScalar RealScalar
Definition: math/matrix.hpp:46
pinocchio::ScalarMatrixProduct
Definition: math/matrix.hpp:78
pinocchio::internal::isNormalizedAlgo< VectorLike, false >::RealScalar
VectorLike::RealScalar RealScalar
Definition: math/matrix.hpp:185
pinocchio::internal::isNormalizedAlgo::RealScalar
VectorLike::RealScalar RealScalar
Definition: math/matrix.hpp:171
pinocchio::internal::isNormalizedAlgo< VectorLike, false >::run
static bool run(const Eigen::MatrixBase< VectorLike > &, const RealScalar &prec=Eigen::NumTraits< RealScalar >::dummy_precision())
Definition: math/matrix.hpp:187
pinocchio::internal::isNormalizedAlgo< VectorLike, false >::Scalar
VectorLike::Scalar Scalar
Definition: math/matrix.hpp:184
pinocchio::ScalarMatrixProduct::type
const typedef Eigen::CwiseUnaryOp< Eigen::internal::scalar_multiple_op< Scalar >, const Matrix > type
Definition: math/matrix.hpp:90
pinocchio::normalize
void normalize(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorType > &qout)
Normalize a configuration vector.
Definition: joint-configuration.hpp:887
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
PINOCCHIO_UNUSED_VARIABLE
#define PINOCCHIO_UNUSED_VARIABLE(var)
Helper to declare that a parameter is unused.
Definition: include/pinocchio/macros.hpp:72


pinocchio
Author(s):
autogenerated on Mon Dec 16 2024 03:41:03