source: trunk/test_more/base_adolc.cpp @ 3520

Last change on this file since 3520 was 2794, checked in by bradbell, 7 years ago
  1. Use CPPAD_NULL, intead of 0, for null pointer.

check_if_0.sh: Ignore subdirectories of new directories.
jenkins.sh: output logs when an error occurs.
acos_op.hpp: avoid use of q (will use it for an order index).
asin_op.hpp: avoid use of q (will use it for an order index).
forward_sweep.hpp: chnage d to p, use const in prototype.
div_op.hpp: minor edit of white space.
atom_usead_2.cpp: use ADFUN to compute variable/parameter information.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1/* $Id: base_adolc.cpp 2794 2013-05-02 08:20:30Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 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# include <adolc/adouble.h>
13# include <adolc/taping.h>
14# include <adolc/interfaces.h>
15
16// adouble definitions not in Adolc distribution and
17// required in order to use CppAD::AD<adouble>
18# include <cppad/example/base_adolc.hpp>
19
20# include <cppad/cppad.hpp>
21
22bool base_adolc(void) 
23{       bool ok = true;                   // initialize test result
24
25        typedef adouble             ADdouble;  // for first level of taping
26        typedef CppAD::AD<ADdouble> ADDdouble; // for second level of taping
27        size_t n = 4;                          // number independent variables
28
29        CPPAD_TESTVECTOR(ADdouble)   a_x(n);
30        CPPAD_TESTVECTOR(ADDdouble) aa_x(n);
31
32        // value of the independent variables
33        int tag = 0;                         // Adolc setup
34        int keep = 1;
35        trace_on(tag, keep);
36        size_t j;
37        for(j = 0; j < n; j++)
38                a_x[j] <<= double(n - j);  // a_x is independent for ADdouble
39        for(j = 0; j < n; j++)
40                aa_x[j] = a_x[j];          // track how aa_x depends on a_x
41        CppAD::Independent(aa_x);          // aa_x is independent for ADDdouble
42         
43
44        // compute function
45        size_t m = 5;
46        CPPAD_TESTVECTOR(ADDdouble) aa_f(m);    // dependent variables
47
48        // different cases of conditional expressions
49        aa_f[0] = CondExpLt(aa_x[0], aa_x[1], aa_x[2], aa_x[3]);
50        aa_f[1] = CondExpLe(aa_x[0], aa_x[1], aa_x[2], aa_x[3]);
51        aa_f[2] = CondExpEq(aa_x[0], aa_x[1], aa_x[2], aa_x[3]);
52        aa_f[3] = CondExpGe(aa_x[0], aa_x[1], aa_x[2], aa_x[3]);
53        aa_f[4] = CondExpGt(aa_x[0], aa_x[1], aa_x[2], aa_x[3]);
54
55        // declare inner function (just to stop inner taping)
56        CppAD::ADFun<ADdouble> a_F(aa_x, aa_f);
57
58        // set values for outer function same as inner function
59        // (corresponding to the tape of adobule operations)
60        double f_j;
61        for(j = 0; j < m; j++)
62                Value(aa_f[j]) >>= f_j;
63        trace_off();
64
65        // arrays for Adolc zos_forward
66        double *x = CPPAD_NULL, *y = CPPAD_NULL;
67        x = CPPAD_TRACK_NEW_VEC(n, x);
68        y = CPPAD_TRACK_NEW_VEC(m, y);
69
70        // switch order of arguments from when taping was done
71        for(j = 0; j < n; j++)
72                x[j] = double(j);
73        zos_forward(tag, int(m), int(n), keep, x, y);
74        // CondExpLt(0, 1, 2, 3)
75        ok &= (y[0] == double(2));
76        // CondExpLe(0, 1, 2, 3)
77        ok &= (y[1] == double(2));
78        // CondExpEq(0, 1, 2, 3)
79        ok &= (y[2] == double(3));
80        // CondExpGe(0, 1, 2, 3)
81        ok &= (y[3] == double(3));
82        // CondExpGt(0, 1, 2, 3)
83        ok &= (y[4] == double(3));
84
85        // set left = right and true < false
86        x[0] = x[1] = 1.;
87        x[2] = 2.;
88        x[3] = 3.;
89        zos_forward(tag, int(m), int(n), keep, x, y);
90        // CondExpLt(1, 1, 2, 3)
91        ok &= (y[0] == double(3));
92        // CondExpLe(1, 1, 2, 3)
93        ok &= (y[1] == double(2));
94        // CondExpEq(1, 1, 2, 3)
95        ok &= (y[2] == double(2));
96        // CondExpGe(1, 1, 2, 3)
97        ok &= (y[3] == double(2));
98        // CondExpGt(1, 1, 2, 3)
99        ok &= (y[4] == double(3));
100
101        CPPAD_TRACK_DEL_VEC(x);
102        CPPAD_TRACK_DEL_VEC(y);
103        return ok;
104}
105// END PROGRAM
Note: See TracBrowser for help on using the repository browser.