CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cppad_eigen.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_EXAMPLE_CPPAD_EIGEN_HPP
2 # define CPPAD_EXAMPLE_CPPAD_EIGEN_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
5 
6 CppAD is distributed under multiple licenses. This distribution is under
7 the terms of the
8  Eclipse Public License Version 1.0.
9 
10 A copy of this license is included in the COPYING file of this distribution.
11 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12 -------------------------------------------------------------------------- */
13 /*
14 $begin cppad_eigen.hpp$$
15 $spell
16  impl
17  typename
18  Real Real
19  inline
20  neg
21  eps
22  atan
23  Num
24  acos
25  asin
26  CppAD
27  std::numeric
28  enum
29  Mul
30  Eigen
31  cppad.hpp
32  namespace
33  struct
34  typedef
35  const
36  imag
37  sqrt
38  exp
39  cos
40 $$
41 $section Enable Use of Eigen Linear Algebra Package with CppAD$$
42 
43 $head Syntax$$
44 $codei%# include <cppad/example/cppad_eigen.hpp>%$$
45 $children%
46  cppad/example/eigen_plugin.hpp%
47  example/general/eigen_array.cpp%
48  example/general/eigen_det.cpp
49 %$$
50 
51 $head Purpose$$
52 Enables the use of the $cref/eigen/eigen_prefix/$$
53 linear algebra package with the type $icode%AD<%Base%>%$$; see
54 $href%
55  https://eigen.tuxfamily.org/dox/TopicCustomizing_CustomScalar.html%
56  custom scalar types
57 %$$.
58 
59 $head Example$$
60 The files $cref eigen_array.cpp$$ and $cref eigen_det.cpp$$
61 contain an example and test of this include file.
62 They return true if they succeed and false otherwise.
63 
64 $head Include Files$$
65 The file $code cppad_eigen.hpp$$ includes both
66 $code <cppad/cppad.hpp>$$ and $code <Eigen/Core>$$.
67 The file $cref eigen_plugin.hpp$$ defines $code value_type$$
68 in the Eigen matrix class so its vectors are
69 $cref/simple vectors/SimpleVector/$$
70 (not necessary for eigen-3.3.3 and later).
71 $srccode%cpp% */
72 # define EIGEN_MATRIXBASE_PLUGIN <cppad/example/eigen_plugin.hpp>
73 # include <Eigen/Core>
74 # include <cppad/cppad.hpp>
75 /* %$$
76 $head Eigen NumTraits$$
77 Eigen needs the following definitions to work properly
78 with $codei%AD<%Base%>%$$ scalars:
79 $srccode%cpp% */
80 namespace Eigen {
81  template <class Base> struct NumTraits< CppAD::AD<Base> >
82  { // type that corresponds to the real part of an AD<Base> value
84  // type for AD<Base> operations that result in non-integer values
86  // type to use for numeric literals such as "2" or "0.5".
88  // type for nested value inside an AD<Base> expression tree
90 
91  enum {
92  // does not support complex Base types
93  IsComplex = 0 ,
94  // does not support integer Base types
95  IsInteger = 0 ,
96  // only support signed Base types
97  IsSigned = 1 ,
98  // must initialize an AD<Base> object
99  RequireInitialization = 1 ,
100  // computational cost of the corresponding operations
101  ReadCost = 1 ,
102  AddCost = 2 ,
103  MulCost = 2
104  };
105 
106  // machine epsilon with type of real part of x
107  // (use assumption that Base is not complex)
110 
111  // relaxed version of machine epsilon for comparison of different
112  // operations that should result in the same value
114  { return 100. *
116  }
117 
118  // minimum normalized positive value
119  static CppAD::AD<Base> lowest(void)
120  { return CppAD::numeric_limits< CppAD::AD<Base> >::min(); }
121 
122  // maximum finite value
124  { return CppAD::numeric_limits< CppAD::AD<Base> >::max(); }
125 
126  // number of decimal digits that can be represented without change.
127  static int digits10(void)
128  { return CppAD::numeric_limits< CppAD::AD<Base> >::digits10; }
129  };
130 }
131 /* %$$
132 $head CppAD Namespace$$
133 Eigen also needs the following definitions to work properly
134 with $codei%AD<%Base%>%$$ scalars:
135 $srccode%cpp% */
136 namespace CppAD {
137  // functions that return references
138  template <class Base> const AD<Base>& conj(const AD<Base>& x)
139  { return x; }
140  template <class Base> const AD<Base>& real(const AD<Base>& x)
141  { return x; }
142 
143  // functions that return values (note abs is defined by cppad.hpp)
144  template <class Base> AD<Base> imag(const AD<Base>& x)
145  { return CppAD::AD<Base>(0.); }
146  template <class Base> AD<Base> abs2(const AD<Base>& x)
147  { return x * x; }
148 }
149 
150 /* %$$
151 $end
152 */
153 # endif
includes the entire CppAD package in the necessary order.
Definition: ad.hpp:34
AD< Base > imag(const AD< Base > &x)
static CppAD::AD< Base > dummy_precision(void)
static CppAD::AD< Base > highest(void)
All tthese defaults correspond to errors.
Type epsilon(void)
Definition: epsilon.hpp:56
AD< Base > abs2(const AD< Base > &x)
static CppAD::AD< Base > lowest(void)
const AD< Base > & conj(const AD< Base > &x)
static CppAD::AD< Base > epsilon(void)
const AD< Base > & real(const AD< Base > &x)