source: trunk/test_more/mul_cskip.cpp @ 3732

Last change on this file since 3732 was 3689, checked in by bradbell, 5 years ago

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

commit b564688a382b6791e27f44b9ade9e753fa6a467d
Author: Brad Bell <bradbell@…>
Date: Tue May 26 22:49:38 2015 -0700

  1. Fix bug/cond_exp_1.sh: result of comparision unknow during forward zero.
  2. Add automated test for the bug: mul_cskip.cpp.

commit a736d47d603413ff6796d8aa86679435c1f9f563
Author: Brad Bell <bradbell@…>
Date: Tue May 26 19:23:43 2015 -0700

Remove trialing white space.

commit e0b8a00599c27e26fe2ba71d22e3241cbf48d042
Author: Brad Bell <bradbell@…>
Date: Tue May 26 19:23:30 2015 -0700

Modify the check routines so they work both in svn and git as follows:

  1. Change list_files.sh so uses repository listing for both git and snv.
  2. Use new list_files.sh in all the check routines.
File size: 1.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# include <iostream>
13# include <cppad/cppad.hpp>
14
15// Test multiple level conditional skip where value of comparision is
16// uncertain during forward mode base Base value can be a variable.
17bool mul_cskip(void)
18{       bool ok = true;
19        using namespace CppAD;
20        using CppAD::vector;
21
22        typedef AD<double>  a1type;
23        typedef AD<a1type>  a2type;
24
25        size_t n = 2;
26        size_t m = 1;
27        vector<double> x(n), y(m);
28        x[0] = 0.0;
29        x[1] = 1.0;
30
31        // start recording a2type operations
32        vector<a2type> a2x(n), a2y(m);
33        for (size_t j = 0; j < n; j++)
34                a2x[j] = a2type( a1type(x[j]) );
35        Independent(a2x);
36
37        // a1f(x) = x_0 * x_1 if x[0] == 1
38        //         0.0       otherwise
39        a2type a2zero = a2type(0.0);
40        a2type a2one  = a2type(1.0);
41        a2type a2p    = a2x[0] * a2x[1];
42        a2y[0]        = CondExpEq(a2x[0], a2one, a2p, a2zero);
43        ADFun<a1type> a1f(a2x, a2y);
44
45        // Optimization will check to see if we can skip part of conditional
46        // expression that is not used.
47        a1f.optimize();
48
49        // f(x) = x_0 * x_1 if x[0] == 1
50        //        0.0       otherwise
51        vector<a1type> a1x(n), a1y(m);
52        for (size_t j = 0; j < n; j++)
53                a1x[j] = a1type(x[j]);
54        Independent(a1x);
55        a1y = a1f.Forward(0, a1x);
56        CppAD::ADFun<double> f(a1x, a1y);
57
58        // check case where x[0] == 1
59        x[0] = 1.0;
60        x[1] = 2.0;
61        y = f.Forward(0, x);
62        ok &= y[0] == x[1];
63
64        // check case where x[0] != 1
65        x[0] = 3.0;
66        x[1] = 2.0;
67        y = f.Forward(0, x);
68        ok &= y[0] == 0.0;
69
70        return ok;
71}
Note: See TracBrowser for help on using the repository browser.