source: trunk/test_more/general/forward.cpp @ 3941

Last change on this file since 3941 was 3941, checked in by bradbell, 2 years ago

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

commit c8c4cc081accff3628e7e66370ec01e4c99afe8d
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 23:16:39 2017 -0600

Changes automatically generated by the autotools.

commit f4392bc3eee8f6d0ccd45a0bb3be51181e211680
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 23:11:56 2017 -0600

  1. Add colpack_jac.cpp example (rename colpack_jac.cpp->colpack_jacobian.cpp).
  2. Add colpack_hescpp example (rename colpack_hes.cpp->colpack_hessian.cpp).


test_one.sh.in: adapt to using test_boolofvoid for testing.
sparse_hes.hpp: fix bug in cppad.symmetric case.

commit 086b8a8709b0c9cb01ce2cf8bc7910e903105ff7
Author: Brad Bell <bradbell@…>
Date: Thu Jun 1 08:54:59 2017 -0600

  1. Fix bug in use of colpack (see kludge in comments).
  2. Fix colpack.symmetric (not general) and add colpack.general.
  3. Deprecate colpack.star.
  4. More autotools from install to deprecated.
  5. Advance to cppad-20170601.

commit 23f26c060648f5c6fc62a1598c659aeccc5ca46f
Author: Brad Bell <bradbell@…>
Date: Tue May 30 08:14:04 2017 -0700

Advance to cppad-20170530.

commit 97f8c08509865d1bfb7ec2e5cd557ddc979f8412
Author: Brad Bell <bradbell@…>
Date: Tue May 30 07:38:47 2017 -0700

debug_rel branch:
There is a problem with speed sparse_hessian debug that goes back to master.
Supresss debug in cppad speed tests until it is fixed.

commit 39ea0d7d9c041784ccd26ce80d19a7ab02752818
Author: Brad Bell <bradbell@…>
Date: Mon May 29 22:34:22 2017 -0700

debug_rel branch:
run_cmake.sh: fix debug_none case.
CMakeLists.txt: use cppad_debug_which to determine debug or release.
CMakeLists.txt: let set_compile_flags determkine build type.

commit 191553e54dca407207789cf0d7c6c27fe6188775
Author: Brad Bell <bradbell@…>
Date: Mon May 29 19:53:08 2017 -0700

debug_rel branch:
Use set_compile_flags in introduction.

commit fba276a84e58d9a0d0944168d5706b7446beb32c
Author: Brad Bell <bradbell@…>
Date: Mon May 29 19:46:30 2017 -0700

debug_rel branch:
Use set_compile_flags in eample/multi_thread subdirectories.

commit 66c8cdb266fa3af29b211b8c870a3aed7a13b021
Author: Brad Bell <bradbell@…>
Date: Mon May 29 18:56:48 2017 -0700

debug_rel branch:
Use set_compile_flags in speed directory.

commit c431b15ee7714d3106234bc527ba2f9a836750e1
Author: Brad Bell <bradbell@…>
Date: Mon May 29 18:36:51 2017 -0700

debug_rel branch:
Convert cppad_ipopt to use set_compile_flags and cppad_debug_which.


CMakeLists.txt: alwasy compile for release to reduce testing time.

commit 2c95b0019f1b665fb14b9f00b049e8b5fb11f89d
Author: Brad Bell <bradbell@…>
Date: Mon May 29 16:55:07 2017 -0700

debug_rel branch:
Add cppad_debug_which to the cmake command line.

commit fd8d1498cf6dc092deca41f764cbb2a001a4cc88
Author: Brad Bell <bradbell@…>
Date: Mon May 29 08:14:23 2017 -0700

debug_rel branch:
Change random_debug_release -> set_compile_flags.

commit 159f5a5aa09012213a52f4ed1c9f0607129a5fe7
Author: Brad Bell <bradbell@…>
Date: Mon May 29 06:50:43 2017 -0700

debug_rel branch:
Update the autotools automatically generated build files.


batch_edit.sh: Start comments about a plan for editing all the source files.
get_sacado.sh: advance to trilions-12.10.11.
makefile.am: advance to trilinos-12.10.1

commit 302153317cd296ec6f927c3202cf96bf38594bbb
Author: Brad Bell <bradbell@…>
Date: Mon May 29 05:20:00 2017 -0700

debug_rel branch:
Add error message if sacado configuration file does not exist.

commit 3f01a631ae808c3a1359e53e1cd55e9a0ea88711
Author: Brad Bell <bradbell@…>
Date: Mon May 29 04:24:00 2017 -0700

debug_rel branch:
CMakeLists.txt: automate naming of libraries Sacado needs.
checkpoint.cpp: fix warnings.

commit dd240928c0c8b6972a8197c985ccc01f08b8886b
Author: Brad Bell <bradbell@…>
Date: Sun May 28 08:25:20 2017 -0700

debug_rel branch:
sparse_sub_hes.cpp: add missing cases found by clang compiler.

commit 30a0c35f1ac50ec425be9a2b7b026284026eccd7
Author: Brad Bell <bradbell@…>
Date: Sun May 28 07:57:36 2017 -0700

debug_rel branch:
eigen_cholesky.hpp: fix compiler warning.
harmonic_time.cpp: remove include that is not used.
forward_active.cpp: fix compiler warning.

commit 4876d14e49dc235865b1574fb38a55cf5ea7a422
Author: Brad Bell <bradbell@…>
Date: Sun May 28 06:19:48 2017 -0700

debug_rel branch:
random_debug_release.cmake: fix comment, remove message replaced by random_choice_0123 in output.
multiple_solution.cpp: fix warnings with clang compiler.
eigen_cholesky.hpp: fix warnings with clang compiler.
compare_change.cpp: fix CPPAD_DEBUG_AND_RELEASE case.

commit 2c51a18f35188d04d2f94069382439580e23f4ac
Author: Brad Bell <bradbell@…>
Date: Sat May 27 21:04:37 2017 -0700

debug_rel branch:
Advance version to cppad-20170527.

commit 4500887b362537774b05e954ad2a95b65a7b8ba0
Author: Brad Bell <bradbell@…>
Date: Sat May 27 09:04:56 2017 -0700

debug_rel branch:
Ramdomly select debug or release flags in example directory.


CMakeLists.txt: always debug for multi_threed examples.

commit 140b5269a0b1a30643894e5a7a8c9a5eb1310301
Author: Brad Bell <bradbell@…>
Date: Sat May 27 08:10:51 2017 -0700

debug_rel branch:
Changing how we set all debug and release flags.

commit e6fb2639db1288fb75de4030b5906df1e41756f9
Author: Brad Bell <bradbell@…>
Date: Sat May 27 07:30:24 2017 -0700

debug_rel branch:
Replace use of cppad_extra_debug by CPPAD_DEBUG_AND_RELEASE.

commit fbbfd0f6e94862174a8a7a17308489ffddb28084
Author: Brad Bell <bradbell@…>
Date: Sat May 27 05:55:58 2017 -0700

debug_rel branch:
Improve random selection of which files are build for release or debug.


forward.cpp: use new -DCPPAD_DEBUG_AND_RELEASE flag.

commit 284be366fb5e2f685a0c71ea6a0e3f74584bf187
Author: Brad Bell <bradbell@…>
Date: Thu May 25 07:39:32 2017 -0700

debug_rel branch:
Add test that failed before change to player.


player.hpp: Fix so it has the same size in debug and release more.
checkpoint.cpp: fix warning when compiling for release.
run_cmake.sh: prepare to use random number to switch debug and release set.
CMakeLists.txt: switch to only test debug (for now).

commit f32375b77e3825628fee6cb160f691a32c48b796
Author: Brad Bell <bradbell@…>
Date: Wed May 24 12:04:27 2017 -0700

debug_rel branch:
forward.cpp: fix a warning during release build.

commit 5fcc7eb78ae8de9f1dbc6c4f0c76fe38e8aeba95
Author: Brad Bell <bradbell@…>
Date: Wed May 24 10:11:12 2017 -0700

debug_rel branch:
CMakeLists.txt: make easy to mix debug and release builds.
eigen_mat_inv.hpp: fix release version warning.

commit 696266f3d62079f5e3bfb1a0f60a7e4f8134e068
Author: Brad Bell <bradbell@…>
Date: Wed May 24 05:43:29 2017 -0700

push_git2svn.py: user ./build in place of ./build/work.
testvector.hpp: improve comments about replacing CPPAD_TESTVECTOR.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/* --------------------------------------------------------------------------
2CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
3
4CppAD is distributed under multiple licenses. This distribution is under
5the terms of the
6                    Eclipse Public License Version 1.0.
7
8A copy of this license is included in the COPYING file of this distribution.
9Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
10-------------------------------------------------------------------------- */
11
12/*
13Two old Forward example now used just for valiadation testing
14*/
15
16# include <cppad/cppad.hpp>
17
18namespace { // Begin empty namespace
19
20template <typename VectorDouble> // vector class, elements of type double
21bool ForwardCases(void)
22{       bool ok = true;
23
24        using namespace CppAD;
25        using CppAD::NearEqual;
26        double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
27
28        // independent variable vector
29        CPPAD_TESTVECTOR(AD<double>) X(2);
30        X[0] = 0.;
31        X[1] = 1.;
32        Independent(X);
33
34        // compute product of elements in X
35        CPPAD_TESTVECTOR(AD<double>) Y(1);
36        Y[0] = X[0] * X[0] * X[1];
37
38        // create function object F : X -> Y
39        ADFun<double> F(X, Y);
40
41        // use zero order to evaluate F[ (3, 4) ]
42        VectorDouble x0( F.Domain() );
43        VectorDouble y0( F.Range() );
44        x0[0]    = 3.;
45        x0[1]    = 4.;
46        y0       = F.Forward(0, x0);
47        ok      &= NearEqual(y0[0] , x0[0]*x0[0]*x0[1], eps99, eps99);
48
49        // evaluate derivative of F in X[0] direction
50        VectorDouble x1( F.Domain() );
51        VectorDouble y1( F.Range() );
52        x1[0]    = 1.;
53        x1[1]    = 0.;
54        y1       = F.Forward(1, x1);
55        ok      &= NearEqual(y1[0] , 2.*x0[0]*x0[1], eps99, eps99);
56
57        // evaluate second derivative of F in X[0] direction
58        VectorDouble x2( F.Domain() );
59        VectorDouble y2( F.Range() );
60        x2[0]       = 0.;
61        x2[1]       = 0.;
62        y2          = F.Forward(2, x2);
63        double F_00 = 2. * y2[0];
64        ok         &= NearEqual(F_00, 2.*x0[1], eps99, eps99);
65
66        // evalute derivative of F in X[1] direction
67        x1[0]    = 0.;
68        x1[1]    = 1.;
69        y1       = F.Forward(1, x1);
70        ok      &= NearEqual(y1[0] , x0[0]*x0[0], eps99, eps99);
71
72        // evaluate second derivative of F in X[1] direction
73        y2          = F.Forward(2, x2);
74        double F_11 = 2. * y2[0];
75        ok         &= NearEqual(F_11, 0., eps99, eps99);
76
77        // evalute derivative of F in X[0] + X[1] direction
78        x1[0]    = 1.;
79        x1[1]    = 1.;
80        y1       = F.Forward(1, x1);
81        ok      &= NearEqual(y1[0], 2.*x0[0]*x0[1] + x0[0]*x0[0], eps99, eps99);
82
83        // use second derivative of F in X[0] direction to
84        // compute second partial of F w.r.t X[1] w.r.t X[2]
85        y2          = F.Forward(2, x2);
86        double F_01 = y2[0] - F_00 / 2. - F_11 / 2.;
87        ok         &= NearEqual(F_01 , 2.*x0[0], eps99, eps99);
88
89        return ok;
90}
91
92bool ForwardOlder(void)
93{       bool ok = true;
94
95        using namespace CppAD;
96        using CppAD::NearEqual;
97        double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
98
99        // independent variable vector
100        CPPAD_TESTVECTOR(AD<double>) U(3);
101        U[0] = 0.; U[1] = 1.; U[2] = 2.;
102        Independent(U);
103
104        // compute sum and product of elements in U
105        AD<double> sum  = 0.;
106        AD<double> prod = 1.;
107        size_t i;
108        for(i = 0; i < 3; i++)
109        {       sum  += U[i];
110                prod *= U[i];
111        }
112
113        // dependent variable vector
114        CPPAD_TESTVECTOR(AD<double>) V(2);
115        V[0] = sum;
116        V[1] = prod;
117
118        // V = f(U)
119        ADFun<double> f(U, V);
120
121        // use ADFun object to evaluate f[ (1, 2, 3)^T ] -----------------
122        CPPAD_TESTVECTOR(double) u0( f.Domain() );
123        CPPAD_TESTVECTOR(double) v0( f.Range() );
124        size_t p;
125        p     = 0;
126        u0[0] = 1.; u0[1] = 2.; u0[2] = 3.;
127        v0    = f.Forward(p, u0);
128
129        // direct evaluation of f[ u0 ]
130        CPPAD_TESTVECTOR(double) f0(2);
131        f0[0] = u0[0] + u0[1] + u0[2];
132        f0[1] = u0[0] * u0[1] * u0[2];
133
134        // compare values
135        ok &= NearEqual(v0[0] , f0[0], eps99, eps99);
136        ok &= NearEqual(v0[1] , f0[1], eps99, eps99);
137
138        // use ADFun object to evaluate f^(1) [ u0 ] * u1 -----------------
139        CPPAD_TESTVECTOR(double) u1( f.Domain() );
140        CPPAD_TESTVECTOR(double) v1( f.Range() );
141        p     = 1;
142        u1[0] = 1.; u1[1] = 1.; u1[2] = 1.;
143        v1    = f.Forward(p, u1);
144
145        // direct evaluation of gradients of components of f
146        CPPAD_TESTVECTOR(double) g0(3), g1(3);
147        g0[0] =          1.; g0[1] =          1.; g0[2] =          1.;
148        g1[0] = u0[1]*u0[2]; g1[1] = u0[0]*u0[2]; g1[2] = u0[0]*u0[1];
149
150        // compare values
151        ok &= NearEqual(v1[0] ,
152                g0[0]*u1[0] + g0[1]*u1[1] + g0[2]*u1[2] , eps99, eps99);
153        ok &= NearEqual(v1[1] ,
154                g1[0]*u1[0] + g1[1]*u1[1] + g1[2]*u1[2] , eps99, eps99);
155
156        // use ADFun object to evaluate ------------------------------------
157        // (1/2) * { f^(1)[ u0 ] * u2 + u1^T * f^(2)[ u0 ] * u1 }
158        CPPAD_TESTVECTOR(double) u2( f.Domain() );
159        CPPAD_TESTVECTOR(double) v2( f.Range() );
160        p     = 2;
161        u2[0] = .5; u2[1] = .4; u2[2] = .3;
162        v2    = f.Forward(p, u2);
163
164        // direct evaluation of Hessian of second components of f
165        // (the Hessian of the first component is zero)
166        CPPAD_TESTVECTOR(double) H1(9);
167        H1[0] =    0.; H1[1] = u0[2]; H1[2] = u0[1];
168        H1[3] = u0[2]; H1[4] =    0.; H1[5] = u0[0];
169        H1[6] = u0[1]; H1[7] = u0[0]; H1[8] =    0.;
170
171        // compare values
172        ok &= NearEqual(v2[0] ,
173                g0[0]*u2[0] + g0[1]*u2[1] + g0[2]*u2[2] , eps99, eps99);
174
175        size_t j;
176        double v2_1 = 0.;
177        for(i = 0; i < 3; i++)
178        {       v2_1 += g1[i] * u2[i];
179                for(j = 0; j < 3; j++)
180                        v2_1 += .5 * u1[i] * H1[i * 3 + j] * u1[j];
181        }
182        ok &= NearEqual(v2[1], v2_1, eps99, eps99);
183
184
185        return ok;
186}
187
188# ifndef NDEBUG
189# ifndef CPPAD_DEBUG_AND_RELEASE
190void my_error_handler(
191        bool known           ,
192        int  line            ,
193        const char *file     ,
194        const char *exp      ,
195        const char *msg      )
196{       // error hander must not return, so throw an exception
197        std::string message = msg;
198        throw message;
199}
200
201bool forward_nan(void)
202{
203
204        using CppAD::vector;
205        using CppAD::AD;
206
207        size_t n = 2, m = 1;
208        vector< AD<double> > a_x(n), a_y(m);
209        a_x[0] = 1.;
210        a_x[1] = 2.;
211        Independent(a_x);
212        a_y[0] = a_x[0] / a_x[1];
213        CppAD::ADFun<double> f(a_x, a_y);
214        //
215        vector<double> x(n), y(m);
216        x[0] = 0.;
217        x[1] = 0.;
218
219        // replace the default CppAD error handler
220        CppAD::ErrorHandler info(my_error_handler);
221
222        bool ok = false;
223        try {
224                y    = f.Forward(0, x);
225        }
226        catch( std::string msg )
227        {       // check that the message contains
228                // "vector_size = " and "file_name = "
229                ok = msg.find("vector_size = ") != std::string::npos;
230                ok = msg.find("file_name = ") != std::string::npos;
231        }
232
233        return ok;
234}
235# endif
236# endif
237} // END empty namespace
238
239# include <vector>
240# include <valarray>
241bool Forward(void)
242{       bool ok = true;
243        ok &= ForwardCases< CppAD::vector  <double> >();
244        ok &= ForwardCases< std::vector    <double> >();
245        ok &= ForwardCases< std::valarray  <double> >();
246        ok &= ForwardOlder();
247# ifndef NDEBUG
248# ifndef CPPAD_DEBUG_AND_RELEASE
249        // CppAD does not check for nan when NDEBUG is defined
250        ok &= forward_nan();
251# endif
252# endif
253        return ok;
254}
Note: See TracBrowser for help on using the repository browser.