- 3.0.2 core module.
CppADCodegenTrait.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <cppad/cg.hpp>
9 #include <cppad/cppad.hpp>
10 #include <cppad/example/cppad_eigen.hpp>
11 
12 
13 namespace ct {
14 namespace core {
15 namespace internal {
16 
17 
20 {
21 public:
22  typedef CppAD::AD<CppAD::cg::CG<double>> Scalar;
23 
24  inline static Scalar sin(const Scalar& x) { return CppAD::sin(x); }
25  inline static Scalar cos(const Scalar& x) { return CppAD::cos(x); }
26  inline static Scalar tan(const Scalar& x) { return CppAD::tan(x); }
27  inline static Scalar sinh(const Scalar& x) { return CppAD::sinh(x); }
28  inline static Scalar cosh(const Scalar& x) { return CppAD::cosh(x); }
29  inline static Scalar tanh(const Scalar& x) { return CppAD::tanh(x); }
30  inline static Scalar exp(const Scalar& x) { return CppAD::exp(x); }
31  inline static Scalar fabs(const Scalar& x) { return CppAD::fabs(x); }
32  inline static Scalar sqrt(const Scalar& x) { return CppAD::sqrt(x); }
34  template <int Dims>
35  inline static Eigen::Matrix<Scalar, Dims, 1> solve(const Eigen::Matrix<Scalar, Dims, Dims>& A,
36  const Eigen::Matrix<Scalar, Dims, 1>& b)
37  {
38  Eigen::Matrix<Scalar, Dims, Dims> LU;
39  Crout(A, LU);
40 
41  Eigen::Matrix<Scalar, Dims, 1> out;
42  solveCrout(LU, b, out);
43 
44  return out;
45  }
46 
47 
48 private:
49  // Custom LU factorization
50  template <typename T, int rowAndCol> //only square matrices
51  void static Crout(const Eigen::Matrix<T, rowAndCol, rowAndCol>& S, Eigen::Matrix<T, rowAndCol, rowAndCol>& D)
52  {
53  for (int k = 0; k < S.rows(); ++k)
54  {
55  for (int i = k; i < S.rows(); ++i)
56  {
57  T sum(0.);
58  for (int p = 0; p < k; ++p)
59  sum += D(i, p) * D(p, k);
60  D(i, k) = S(i, k) - sum; // not dividing by diagonals
61  }
62  for (int j = k + 1; j < S.rows(); ++j)
63  {
64  T sum(0.);
65  for (int p = 0; p < k; ++p)
66  sum += D(k, p) * D(p, j);
67  D(k, j) = (S(k, j) - sum) / D(k, k);
68  }
69  }
70  }
71 
72  // Custom LU solver
73  template <typename T, int rowAndCol>
74  void static solveCrout(const Eigen::Matrix<T, rowAndCol, rowAndCol>& LU,
75  const Eigen::Matrix<T, rowAndCol, 1>& b,
76  Eigen::Matrix<T, rowAndCol, 1>& x)
77  {
78  const int d = rowAndCol;
79  T y[d];
80  for (int i = 0; i < d; ++i)
81  {
82  T sum(0.0);
83  for (int k = 0; k < i; ++k)
84  sum += LU(i, k) * y[k];
85  y[i] = (b(i) - sum) / LU(i, i);
86  }
87  for (int i = d - 1; i >= 0; --i)
88  {
89  T sum(0.);
90  for (int k = i + 1; k < d; ++k)
91  sum += LU(i, k) * x(k);
92  x(i) = (y[i] - sum); // not dividing by diagonals
93  }
94  }
95 };
96 } // namespace internal
97 } // namespace core
98 } // namespace ct
static Scalar sinh(const Scalar &x)
Definition: CppADCodegenTrait.h:27
static Scalar sin(const Scalar &x)
Definition: CppADCodegenTrait.h:24
static Scalar tanh(const Scalar &x)
Definition: CppADCodegenTrait.h:29
static Scalar cosh(const Scalar &x)
Definition: CppADCodegenTrait.h:28
Trait defining basic math functions for CppAD CodeGen Auto-Diff types.
Definition: CppADCodegenTrait.h:19
static Scalar cos(const Scalar &x)
Definition: CppADCodegenTrait.h:25
for i
ct::core::StateVector< state_dim > x
static Scalar exp(const Scalar &x)
Definition: CppADCodegenTrait.h:30
static Scalar sqrt(const Scalar &x)
Definition: CppADCodegenTrait.h:32
static Scalar fabs(const Scalar &x)
Definition: CppADCodegenTrait.h:31
static Eigen::Matrix< Scalar, Dims, 1 > solve(const Eigen::Matrix< Scalar, Dims, Dims > &A, const Eigen::Matrix< Scalar, Dims, 1 > &b)
Solves a linear system of equations using an LU factorization.
Definition: CppADCodegenTrait.h:35
CppAD::AD< CppAD::cg::CG< double > > Scalar
Definition: CppADCodegenTrait.h:22
static Scalar tan(const Scalar &x)
Definition: CppADCodegenTrait.h:26