File: cppad_eigen.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (89 lines) | stat: -rw-r--r-- 2,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
Test of Eigen Interface to CppAD Scalar Types
$end
*/
# include <cppad/example/cppad_eigen.hpp>

bool cppad_eigen(void)
{  bool ok = true;
   using CppAD::AD;
   using Eigen::Dynamic;
   using Eigen::Matrix;

   typedef Eigen::NumTraits<AD<double> >         traits;

   ok &= traits::IsComplex              == 0;
   ok &= traits::IsInteger              == 0;
   ok &= traits::IsSigned               == 1;
   ok &= traits::RequireInitialization  == 1;
   ok &= traits::ReadCost               == 1;
   ok &= traits::AddCost                == 2;
   ok &= traits::MulCost                == 2;

   ok &= traits::epsilon() ==
      std::numeric_limits<double>::epsilon();
   ok &= traits::dummy_precision() ==
      100.* std::numeric_limits<double>::epsilon();
   ok &= traits::highest() ==
      std::numeric_limits<double>::max();
   ok &= traits::lowest() ==
      std::numeric_limits<double>::min();
   ok &= std::isnan(traits::quiet_NaN());
   ok &= std::isinf(traits::infinity());

   AD<double> x = 2.0;
   ok  &= conj(x)  == x;
   ok  &= real(x)  == x;
   ok  &= imag(x)  == 0.0;
   ok  &= abs2(x)  == 4.0;

   ok  &= (!std::isinf(x));
   ok  &= (!std::isnan(x));

   x = traits::quiet_NaN();
   ok  &= std::isnan(x);

   x = traits::infinity();
   ok  &= std::isinf(x);

   // Outputting a matrix used to fail before partial specialization of
   // struct significant_decimals_default_impl in cppad_eigen.hpp.
   Matrix< AD<double>, 1, 1> X;
   X(0, 0) = AD<double>(1);
   std::stringstream stream_out;
   stream_out << X;
   ok &= "1" == stream_out.str();

   // multiplying three matrices together used to cause warning
   // before making ctor from arbitrary type to AD<Base> explicit.
   typedef CppAD::AD<double> AScalar;
   Matrix<AScalar, Dynamic, Dynamic> A(1,1), B(1,1), C(1,1), D(1,1);
   A(0,0) = 1.0;
   B(0,0) = 2.0;
   C(0,0) = 3.0;
   D      = A * B * C;
   ok    &= D(0,0) == 6.0 ;

   // Multiplying Eigen objects (such as matrices) of element types used to
   // fail before appropriate "ScalarBinaryOpTraits" was added.
   const int nn = 3;
   Matrix<double,  nn, nn> d_matrix = Matrix<double,  nn, nn>::Zero();
   Matrix<AScalar, nn, nn> a_matrix = Matrix<AScalar, nn, nn>::Zero();
   a_matrix  = a_matrix * d_matrix;
   a_matrix  = d_matrix * a_matrix;
   ok       &= a_matrix(0, 0) == AScalar(0);

   // Test multiply matrix elements of mixed types
   // (This worked before corresponding ScalarBinaryOpTraits was added.)
   AScalar a_scalar = a_matrix(0,0) * d_matrix(0,0);
   ok              &= a_scalar == AScalar(0);
   a_scalar         = d_matrix(0, 0) * a_matrix(0, 0);
   ok              &= a_scalar == AScalar(0);

   return ok;
}