autodiff.cpp File Reference
#include "main.h"
#include <unsupported/Eigen/AutoDiff>

Classes

struct  TestFunc1< Scalar_, NX, NY >
 
struct  integratorFunctor< Scalar >
 

Macros

#define EIGEN_TEST_SPACE
 

Functions

template<typename Scalar >
EIGEN_DONT_INLINE Scalar foo (const Scalar &x, const Scalar &y)
 
template<typename Vector >
EIGEN_DONT_INLINE Vector::Scalar foo (const Vector &p)
 
template<typename Func >
void forward_jacobian_cpp11 (const Func &f)
 
template<typename Func >
void forward_jacobian (const Func &f)
 
template<int >
void test_autodiff_scalar ()
 
template<int >
void test_autodiff_vector ()
 
template<int >
void test_autodiff_jacobian ()
 
template<int >
void test_autodiff_hessian ()
 
double bug_1222 ()
 
double bug_1223 ()
 
void bug_1260 ()
 
double bug_1261 ()
 
double bug_1264 ()
 
double bug_1281 ()
 
 EIGEN_DECLARE_TEST (autodiff)
 

Macro Definition Documentation

◆ EIGEN_TEST_SPACE

#define EIGEN_TEST_SPACE

Function Documentation

◆ bug_1222()

double bug_1222 ( )
267  {
269  const double _cv1_3 = 1.0;
270  const AD chi_3 = 1.0;
271  // this line did not work, because operator+ returns ADS<DerType&>, which then cannot be converted to ADS<DerType>
272  const AD denom = chi_3 + _cv1_3;
273  return denom.value();
274 }
A scalar type replacement with automatic differentiation capability.
Definition: AutoDiffScalar.h:99

Referenced by EIGEN_DECLARE_TEST().

◆ bug_1223()

double bug_1223 ( )
276  {
277  using std::min;
279 
280  const double _cv1_3 = 1.0;
281  const AD chi_3 = 1.0;
282  const AD denom = 1.0;
283 
284 // failed because implementation of min attempts to construct ADS<DerType&> via constructor AutoDiffScalar(const Real&
285 // value) without initializing m_derivatives (which is a reference in this case)
286 #define EIGEN_TEST_SPACE
287  const AD t = min EIGEN_TEST_SPACE(denom / chi_3, 1.0);
288 
289  const AD t2 = min EIGEN_TEST_SPACE(denom / (chi_3 * _cv1_3), 1.0);
290 
291  return t.value() + t2.value();
292 }
#define EIGEN_TEST_SPACE
#define min(a, b)
Definition: datatypes.h:22
t
Definition: plotPSD.py:36

References EIGEN_TEST_SPACE, min, and plotPSD::t.

Referenced by EIGEN_DECLARE_TEST().

◆ bug_1260()

void bug_1260 ( )
295  {
296  Matrix4d A = Matrix4d::Ones();
297  Vector4d v = Vector4d::Ones();
298  A* v;
299 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186

References v.

Referenced by EIGEN_DECLARE_TEST().

◆ bug_1261()

double bug_1261 ( )
302  {
303  typedef AutoDiffScalar<Matrix2d> AD;
304  typedef Matrix<AD, 2, 1> VectorAD;
305 
306  VectorAD v(0., 0.);
307  const AD maxVal = v.maxCoeff();
308  const AD minVal = v.minCoeff();
309  return maxVal.value() + minVal.value();
310 }

References v.

Referenced by EIGEN_DECLARE_TEST().

◆ bug_1264()

double bug_1264 ( )
312  {
313  typedef AutoDiffScalar<Vector2d> AD;
314  const AD s = 0.;
315  const Matrix<AD, 3, 1> v1(0., 0., 0.);
316  const Matrix<AD, 3, 1> v2 = (s + 3.0) * v1;
317  return v2(0).value();
318 }
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
RealScalar s
Definition: level1_cplx_impl.h:130

References s, v1(), and v2().

◆ bug_1281()

double bug_1281 ( )
321  {
322  int n = 2;
323  typedef AutoDiffScalar<VectorXd> AD;
324  const AD c = 1.;
325  AD x0(2, n, 0);
326  AD y1 = (AD(c) + AD(c)) * x0;
327  y1 = x0 * (AD(c) + AD(c));
328  AD y2 = (-AD(c)) + x0;
329  y2 = x0 + (-AD(c));
330  AD y3 = (AD(c) * (-AD(c)) + AD(c)) * x0;
331  y3 = x0 * (AD(c) * (-AD(c)) + AD(c));
332  return (y1 + y2 + y3).value();
333 }
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Vector< double > x0(2, 0.0)
int c
Definition: calibrate.py:100

References calibrate::c, n, and Global::x0.

Referenced by EIGEN_DECLARE_TEST().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( autodiff  )
335  {
336  for (int i = 0; i < g_repeat; i++) {
337  CALL_SUBTEST_1(test_autodiff_scalar<1>());
338  CALL_SUBTEST_2(test_autodiff_vector<1>());
339  CALL_SUBTEST_3(test_autodiff_jacobian<1>());
340  CALL_SUBTEST_4(test_autodiff_hessian<1>());
341  }
342 
348 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
double bug_1222()
Definition: autodiff.cpp:267
void bug_1260()
Definition: autodiff.cpp:295
double bug_1261()
Definition: autodiff.cpp:302
double bug_1281()
Definition: autodiff.cpp:321
double bug_1223()
Definition: autodiff.cpp:276
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References bug_1222(), bug_1223(), bug_1260(), bug_1261(), bug_1281(), CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, Eigen::g_repeat, and i.

◆ foo() [1/2]

template<typename Scalar >
EIGEN_DONT_INLINE Scalar foo ( const Scalar x,
const Scalar y 
)
14  {
15  using namespace std;
16  // return x+std::sin(y);
17  EIGEN_ASM_COMMENT("mybegin");
18  // pow(float, int) promotes to pow(double, double)
19  return x * 2 - 1 + static_cast<Scalar>(pow(1 + x, 2)) + 2 * sqrt(y * y + 0) - 4 * sin(0 + x) + 2 * cos(y + 0) -
20  exp(Scalar(-0.5) * x * x + 0);
21  // return x+2*y*x;//x*2 -std::pow(x,2);//(2*y/x);// - y*2;
22  EIGEN_ASM_COMMENT("myend");
23 }
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
#define EIGEN_ASM_COMMENT(X)
Definition: Macros.h:972
SCALAR Scalar
Definition: bench_gemm.cpp:45
Scalar * y
Definition: level1_cplx_impl.h:128
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
list x
Definition: plotDoE.py:28

References cos(), EIGEN_ASM_COMMENT, Eigen::bfloat16_impl::exp(), Eigen::bfloat16_impl::pow(), sin(), sqrt(), plotDoE::x, and y.

Referenced by test_autodiff_scalar(), and test_autodiff_vector().

◆ foo() [2/2]

template<typename Vector >
EIGEN_DONT_INLINE Vector::Scalar foo ( const Vector p)
26  {
27  typedef typename Vector::Scalar Scalar;
28  return (p - Vector(Scalar(-1), Scalar(1.))).norm() + (p.array() * p.array()).sum() + p.dot(p);
29 }
float * p
Definition: Tutorial_Map_using.cpp:9
internal::traits< Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ > >::Scalar Scalar
Definition: PlainObjectBase.h:127
Matrix< Type, Size, 1 > Vector
\cpp11 SizeƗ1 vector of type Type.
Definition: Eigen/Eigen/src/Core/Matrix.h:515

References p.

◆ forward_jacobian()

template<typename Func >
void forward_jacobian ( const Func &  f)
170  {
171  typename Func::InputType x = Func::InputType::Random(f.inputs());
172  typename Func::ValueType y(f.values()), yref(f.values());
173  typename Func::JacobianType j(f.values(), f.inputs()), jref(f.values(), f.inputs());
174 
175  jref.setZero();
176  yref.setZero();
177  f(x, &yref, &jref);
178 
179  j.setZero();
180  y.setZero();
181  AutoDiffJacobian<Func> autoj(f);
182  autoj(x, &y, &j);
183 
184  VERIFY_IS_APPROX(y, yref);
185  VERIFY_IS_APPROX(j, jref);
186 }
Definition: AutoDiffJacobian.h:19
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: cxx11_tensor_map.cpp:237
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References f(), j, VERIFY_IS_APPROX, plotDoE::x, and y.

Referenced by test_autodiff_jacobian().

◆ forward_jacobian_cpp11()

template<typename Func >
void forward_jacobian_cpp11 ( const Func &  f)
137  {
138  typedef typename Func::ValueType::Scalar Scalar;
139  typedef typename Func::ValueType ValueType;
140  typedef typename Func::InputType InputType;
141  typedef typename AutoDiffJacobian<Func>::JacobianType JacobianType;
142 
143  InputType x = InputType::Random(InputType::RowsAtCompileTime);
144  ValueType y, yref;
145  JacobianType j, jref;
146 
147  const Scalar dt = internal::random<double>();
148 
149  jref.setZero();
150  yref.setZero();
151  f(x, &yref, &jref, dt);
152 
153  // std::cerr << "y, yref, jref: " << "\n";
154  // std::cerr << y.transpose() << "\n\n";
155  // std::cerr << yref << "\n\n";
156  // std::cerr << jref << "\n\n";
157 
158  AutoDiffJacobian<Func> autoj(f);
159  autoj(x, &y, &j, dt);
160 
161  // std::cerr << "y j (via autodiff): " << "\n";
162  // std::cerr << y.transpose() << "\n\n";
163  // std::cerr << j << "\n\n";
164 
165  VERIFY_IS_APPROX(y, yref);
166  VERIFY_IS_APPROX(j, jref);
167 }

References f(), j, VERIFY_IS_APPROX, plotDoE::x, and y.

Referenced by test_autodiff_jacobian().

◆ test_autodiff_hessian()

template<int >
void test_autodiff_hessian ( )
224  {
225  typedef AutoDiffScalar<VectorXd> AD;
226  typedef Matrix<AD, Eigen::Dynamic, 1> VectorAD;
227  typedef AutoDiffScalar<VectorAD> ADD;
228  typedef Matrix<ADD, Eigen::Dynamic, 1> VectorADD;
229  VectorADD x(2);
230  double s1 = internal::random<double>(), s2 = internal::random<double>(), s3 = internal::random<double>(),
231  s4 = internal::random<double>();
232  x(0).value() = s1;
233  x(1).value() = s2;
234 
235  // set unit vectors for the derivative directions (partial derivatives of the input vector)
236  x(0).derivatives().resize(2);
237  x(0).derivatives().setZero();
238  x(0).derivatives()(0) = 1;
239  x(1).derivatives().resize(2);
240  x(1).derivatives().setZero();
241  x(1).derivatives()(1) = 1;
242 
243  // repeat partial derivatives for the inner AutoDiffScalar
244  x(0).value().derivatives() = VectorXd::Unit(2, 0);
245  x(1).value().derivatives() = VectorXd::Unit(2, 1);
246 
247  // set the hessian matrix to zero
248  for (int idx = 0; idx < 2; idx++) {
249  x(0).derivatives()(idx).derivatives() = VectorXd::Zero(2);
250  x(1).derivatives()(idx).derivatives() = VectorXd::Zero(2);
251  }
252 
253  ADD y = sin(AD(s3) * x(0) + AD(s4) * x(1));
254 
255  VERIFY_IS_APPROX(y.value().derivatives()(0), y.derivatives()(0).value());
256  VERIFY_IS_APPROX(y.value().derivatives()(1), y.derivatives()(1).value());
257  VERIFY_IS_APPROX(y.value().derivatives()(0), s3 * std::cos(s1 * s3 + s2 * s4));
258  VERIFY_IS_APPROX(y.value().derivatives()(1), s4 * std::cos(s1 * s3 + s2 * s4));
259  VERIFY_IS_APPROX(y.derivatives()(0).derivatives(), -std::sin(s1 * s3 + s2 * s4) * Vector2d(s3 * s3, s4 * s3));
260  VERIFY_IS_APPROX(y.derivatives()(1).derivatives(), -std::sin(s1 * s3 + s2 * s4) * Vector2d(s3 * s4, s4 * s4));
261 
262  ADD z = x(0) * x(1);
263  VERIFY_IS_APPROX(z.derivatives()(0).derivatives(), Vector2d(0, 1));
264  VERIFY_IS_APPROX(z.derivatives()(1).derivatives(), Vector2d(1, 0));
265 }
double Zero
Definition: pseudosolid_node_update_elements.cc:35

References cos(), sin(), VERIFY_IS_APPROX, plotDoE::x, y, and oomph::PseudoSolidHelper::Zero.

◆ test_autodiff_jacobian()

template<int >
void test_autodiff_jacobian ( )
214  {
221 }
void forward_jacobian(const Func &f)
Definition: autodiff.cpp:170
void forward_jacobian_cpp11(const Func &f)
Definition: autodiff.cpp:137
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
Definition: autodiff.cpp:32
Definition: autodiff.cpp:96

References CALL_SUBTEST, forward_jacobian(), and forward_jacobian_cpp11().

◆ test_autodiff_scalar()

template<int >
void test_autodiff_scalar ( )
190  {
191  Vector2f p = Vector2f::Random();
192  typedef AutoDiffScalar<Vector2f> AD;
193  AD ax(p.x(), Vector2f::UnitX());
194  AD ay(p.y(), Vector2f::UnitY());
195  AD res = foo<AD>(ax, ay);
196  VERIFY_IS_APPROX(res.value(), foo(p.x(), p.y()));
197 }
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
EIGEN_DONT_INLINE Scalar foo(const Scalar &x, const Scalar &y)
Definition: autodiff.cpp:14
ax
Definition: plotDoE.py:39

References plotDoE::ax, foo(), p, res, and VERIFY_IS_APPROX.

◆ test_autodiff_vector()

template<int >
void test_autodiff_vector ( )
201  {
202  Vector2f p = Vector2f::Random();
203  typedef AutoDiffScalar<Vector2f> AD;
204  typedef Matrix<AD, 2, 1> VectorAD;
205  VectorAD ap = p.cast<AD>();
206  ap.x().derivatives() = Vector2f::UnitX();
207  ap.y().derivatives() = Vector2f::UnitY();
208 
209  AD res = foo<VectorAD>(ap);
210  VERIFY_IS_APPROX(res.value(), foo(p));
211 }
Scalar * ap
Definition: level2_cplx_impl.h:161

References ap, foo(), p, res, and VERIFY_IS_APPROX.