1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 Bradley M. Bell
3 
4 CppAD is distributed under the terms of the
5              Eclipse Public License Version 2.0.
6 
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10       GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12 
13 /*
14 $begin integer.cpp$$
15 $spell
16     Cpp
17     cstddef
18 $$
19 
20 $section Convert From AD to Integer: Example and Test$$
21 
22 
23 $srcthisfile%0%// BEGIN C++%// END C++%1%$$
24 
25 $end
26 */
27 // BEGIN C++
28 
29 # include <cppad/cppad.hpp>
30 
Integer(void)31 bool Integer(void)
32 {   bool ok = true;
33     using CppAD::AD;
34     using CppAD::Integer;
35 
36     // domain space vector
37     size_t n = 2;
38     CPPAD_TESTVECTOR(AD<double>) x(n);
39     x[0] = 3.5;
40     x[1] = 4.5;
41 
42     // check integer before recording
43     ok &= (Integer(x[0]) == 3);
44     ok &= (Integer(x[1]) == 4);
45 
46     // start recording
47 
48     // declare independent variables and start tape recording
49     CppAD::Independent(x);
50 
51     // check integer during recording
52     ok &= (Integer(x[0]) == 3);
53     ok &= (Integer(x[1]) == 4);
54 
55     // check integer for VecAD element
56     CppAD::VecAD<double> v(1);
57     AD<double> zero(0);
58     v[zero] = 2;
59     ok &= (Integer(v[zero]) == 2);
60 
61     // range space vector
62     size_t m = 1;
63     CPPAD_TESTVECTOR(AD<double>) y(m);
64     y[0] = - x[1];
65 
66     // create f: x -> y and stop recording
67     CppAD::ADFun<double> f(x, y);
68 
69     // check integer after recording
70     ok &= (Integer(x[0]) ==  3.);
71     ok &= (Integer(x[1]) ==  4.);
72     ok &= (Integer(y[0]) == -4.);
73 
74     return ok;
75 }
76 // END C++
77