source: trunk/test_more/abs.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1/* $Id: abs.cpp 2506 2012-10-24 19:36:49Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 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/*
14Test of directional derivative in AD< AD< double > > case.
15*/
16
17# include <cppad/cppad.hpp>
18
19
20bool abs(void)
21{       // test if CppAD::abs uses if statement during forward computations
22        bool ok = true;
23
24        using CppAD::Independent;
25        using CppAD::ADFun;
26        typedef CppAD::AD<double>      ADdouble;
27        typedef CppAD::AD< ADdouble > ADDdouble;
28
29        // af(x) = |x|
30        CPPAD_TESTVECTOR( ADDdouble ) aax(1), aay(1);
31        aax[0] = ADDdouble(0.);
32        CppAD::Independent(aax);
33        aay[0] = CppAD::abs(aax[0]);
34        CppAD::ADFun< ADdouble > af(aax, aay);
35
36        // f(x) = |x|
37        CPPAD_TESTVECTOR( ADdouble ) ax(1), ay(1);
38        ax[0] = ADdouble(0.);
39        CppAD::Independent(ax);
40        ay    = af.Forward(0, ax);
41        CppAD::ADFun<double> f(ax, ay);
42
43        // compute derivative of af at a positive argument
44        CPPAD_TESTVECTOR( ADdouble ) adx(1), ady(1);
45        ax[0]  = 1.;
46        ay     = af.Forward(0, ax);
47        adx[0] = 1;
48        ady    = af.Forward(1, adx);
49        ok    &= (ady[0] == 1.);
50
51        // compute derivative of af at a zero argument
52        ax[0]  = 0.;
53        ay     = af.Forward(0, ax);
54        adx[0] = 1;
55        ady    = af.Forward(1, adx);
56        ok    &= (ady[0] == 0.);
57
58        // compute derivative of f at zero argument
59        CPPAD_TESTVECTOR(double) x(1), y(1), dx(1), dy(1);
60        x[0]  = 0.;
61        y     = f.Forward(0, x);
62        dx[0] = 1;
63        dy    = f.Forward(1, dx);
64        ok    &= (dy[0] == 0.);
65
66        // compute derivative of af at a negative argument
67        x[0]  = -1.;
68        y     = f.Forward(0, x);
69        dx[0] = 1;
70        dy    = f.Forward(1, dx);
71        ok    &= (dy[0] == -1.);
72
73        return ok;
74}
Note: See TracBrowser for help on using the repository browser.