source: trunk/test_more/zdouble.cpp @ 3738

Last change on this file since 3738 was 3738, checked in by bradbell, 4 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: b70557cfc2eac09419362b050f688a8b59e112ec
end hash code: 73a24bb54131f169c29c2cd2393c10bdaecb5d95

commit 73a24bb54131f169c29c2cd2393c10bdaecb5d95
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 19:39:32 2015 -0700

Remove invisible white space.

commit 8025af3892e1afa816df2b8f9fc26b0c7f551c45
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 19:39:29 2015 -0700

Remove zdouble from examples (its been deprecated).

commit ddd8f11e2f5fdf4888fa6646769f8f393a8edb4a
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 18:28:24 2015 -0700

Remove invisible white space.

commit 3a172d4c4c2a911caf6a758fdac010535a9a543b
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 18:27:54 2015 -0700

Deprecate the nan(zero) function.


wish_list.omh: remove numeric limits wish list item.

commit be98c44883967329622f0318208d4bd3fa55e94f
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 16:55:15 2015 -0700

Add quiet_NaN to CppAD::numeric_limits.


base_complex.hpp: use CPPAD_NUMERIC_LIMITS for complex<float>.

commit 8d2a16b97f38db5439395c5ddf94445f4f3328b0
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 15:38:36 2015 -0700

Move examples and tests from limits.cpp to num_limits.cpp.

commit cf81f99c067a0ad6a7a7e8900ac33c1c4a7794ff
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 14:03:39 2015 -0700

Move limits -> numeric_limits.

commit 61443ef555090628a02b75159a8397c6aaf1b445
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 10:13:03 2015 -0700

Remove invisible white space.

commit a3d379197febc72f6a1cc4959155b48a00fc7cec
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 10:12:50 2015 -0700

  1. Move zdouble to base_require and deprecated.
  2. Move limits to ad_valued section.


limits.hpp: add static to prototypes.

commit 4873c18c4ae21c1204b4cfb2ded93215ac69b461
Author: Brad Bell <bradbell@…>
Date: Sun Oct 4 07:49:16 2015 -0700

  1. Move base type numeric limits to a separate documentaiton section.
  2. Add macro to aid in setting numeric limits for a base type.
  3. Remove mention of depreicated epsilon function (where no longer needed).
File size: 2.9 KB
Line 
1/* $Id$ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7        Eclipse Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12
13/*
14$begin zdouble.cpp$$
15$spell
16        zdouble
17$$
18
19$section zdouble: Example and Test$$
20
21$code
22$verbatim%test_more/zdouble.cpp%0%// BEGIN C++%// END C++%1%$$
23$$
24
25$end
26*/
27// BEGIN C++
28# include <cppad/cppad.hpp>
29
30namespace {
31        template <class Base> bool test(bool is_double)
32        {       bool ok = true;
33                Base eps = 10. * std::numeric_limits<double>::epsilon();
34
35                typedef CppAD::AD<Base>   a1type;
36                typedef CppAD::AD<a1type> a2type;
37
38                // value during taping
39                size_t n = 2;
40                CPPAD_TESTVECTOR(Base) x(n);
41                x[0] = 0.0;
42                x[1] = 0.0;
43
44                // declare independent variable
45                CPPAD_TESTVECTOR(a2type) a2x(n);
46                for (size_t j = 0; j < n; j++)
47                        a2x[j] = a2type( a1type(x[j]) );
48                Independent(a2x);
49
50                // zero and one as a2type values
51                a2type a2zero = a2type(0.0);
52                a2type a2one  = a2type(1.0);
53
54                // h(x) = x[0] / x[1] if x[1] > x[0] else 1.0
55                a2type h_x = CondExpGt(a2x[1], a2x[0], a2x[0] / a2x[1], a2one);
56
57                // f(x) = h(x) if x[0] > 0.0 else 0.0
58                //      = x[0] / x[1] if x[1] > x[0]  and x[0] > 0.0
59                //      = 1.0         if x[0] >= x[1] and x[0] > 0.0
60                //      = 0.0         if x[0] <= 0.0
61                a2type f_x = CondExpGt(a2x[0], a2zero, h_x, a2one);
62
63                // define the function f(x)
64                size_t m = 1;
65                CPPAD_TESTVECTOR(a2type) a2y(m);
66                a2y[0] = f_x;
67                CppAD::ADFun<a1type> af1;
68                af1.Dependent(a2x, a2y);
69
70                // Define function g(x) = gradient of f(x)
71                CPPAD_TESTVECTOR(a1type) a1x(n), a1z(n), a1w(m);
72                for (size_t j = 0; j < n; j++)
73                        a1x[j] = a1type(x[j]);
74                a1w[0] = a1type(1.0);
75                Independent(a1x);
76                af1.Forward(0, a1x);
77                a1z = af1.Reverse(1, a1w);
78                CppAD::ADFun<Base> g;
79                g.Dependent(a1x, a1z);
80
81                // check result for a case where f(x) = 0.0;
82                CPPAD_TESTVECTOR(Base) z(2);
83                x[0] = 0.0;
84                x[1] = 0.0;
85                z    = g.Forward(0, x);
86                ok &= z[0] == 0.0;
87                ok &= z[1] == 0.0;
88
89                // check result for a case where f(x) = 1.0;
90                x[0] = 1.0;
91                x[1] = 0.5;
92                z    = g.Forward(0, x);
93                ok &= z[0] == 0.0;
94                ok &= z[1] == 0.0;
95
96                // check result for a case where f(x) = x[0] / x[1];
97                x[0] = 1.0;
98                x[1] = 2.0;
99                z    = g.Forward(0, x);
100                ok &= CppAD::NearEqual(z[0], 1.0/x[1], eps, eps);
101                ok &= CppAD::NearEqual(z[1], - x[0]/(x[1]*x[1]), eps, eps);
102
103                return ok;
104        }
105}
106
107bool zdouble(void)
108{       bool ok = true;
109        using CppAD::AD;
110        using CppAD::NearEqual;
111        using CppAD::zdouble;
112        //
113        bool is_double = false;
114        ok &= test<zdouble>(is_double);
115        //
116        is_double = true;
117        ok &= test<double>(is_double);
118        //
119        return ok;
120}
121
122// END C++
Note: See TracBrowser for help on using the repository browser.