Changeset 2240 for trunk/test_more


Ignore:
Timestamp:
Dec 31, 2011 12:33:55 AM (8 years ago)
Author:
bradbell
Message:
  1. Fix bug in abs function with AD< AD<double> > arguments.
  2. Define derivative of abs as sign function.
  3. Add sign function to AD<Base>, Base requirements, and corresponding examples.

check_op_code.sh: porper alphabetic order drops Op from SinOp? and SinhOp?...
abs.cpp: change example to new derivative specification.
base_adolc.hpp: add definition of abs in CppAD namespace.
abs.cpp: test that demonstraces bug in abs for AD< AD<double> >.

Location:
trunk/test_more
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test_more/abs.cpp

    r1370 r2240  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    1818
    1919
    20 bool Abs(void)
    21 {       // test if CppAD::abs uses if statement for value computations
     20bool abs(void)
     21{       // test if CppAD::abs uses if statement during forward computations
    2222        bool ok = true;
    2323
     
    2727        typedef CppAD::AD< ADdouble > ADDdouble;
    2828
    29         CPPAD_TEST_VECTOR< ADdouble > u(1);
    30         u[0] = double(0);
    31         Independent(u);
     29        // af(x) = |x|
     30        CPPAD_TEST_VECTOR< 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);
    3235
    33         CPPAD_TEST_VECTOR< ADDdouble > v(1);
    34         v[0] = ADDdouble(u[0]);
    35         CppAD::Independent(v);
     36        // f(x) = |x|
     37        CPPAD_TEST_VECTOR< 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);
    3642
    37         CPPAD_TEST_VECTOR< ADDdouble > w(1);
    38         w[0] = CppAD::abs(v[0]);
     43        // compute derivative of af at a positive argument
     44        CPPAD_TEST_VECTOR< 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.);
    3950
    40         // f(v) = |w|
    41         CppAD::ADFun< ADdouble > f(v, w);
     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.);
    4257
    43         // extract the value of w
    44         CPPAD_TEST_VECTOR< ADdouble > x(1);
    45         x[0] = CppAD::Value(w[0]);
    46        
    47         // g(u) = |u|
    48         CppAD::ADFun<double> g(u, x);
     58        // compute derivative of f at zero argument
     59        CPPAD_TEST_VECTOR<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.);
    4965
    50         // compute direction derivative of f at zero in positive direction
    51         CPPAD_TEST_VECTOR< ADdouble > dv(1);
    52         CPPAD_TEST_VECTOR< ADdouble > dw(1);
    53         dv[0] = 1;
    54         dw    = f.Forward(1, dv);
    55         ok   &= (dw[0] == 1);
    56 
    57         // compute direction derivative of g at zero in positive direction
    58         CPPAD_TEST_VECTOR<double> du(1);
    59         CPPAD_TEST_VECTOR<double> dx(1);
    60         du[0] = 1;
    61         dx    = g.Forward(1, du);
    62         ok   &= (dx[0] == 1);
     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.);
    6372
    6473        return ok;
  • trunk/test_more/test_more.cpp

    r2057 r2240  
    1818
    1919// various examples / tests
    20 extern bool Abs(void);
     20extern bool abs(void);
    2121extern bool Acos(void);
    2222extern bool Add(void);
     
    121121        // This line is used by test_one.sh
    122122
    123         ok &= Run( Abs,             "Abs"            );
     123        ok &= Run( abs,             "abs"            );
    124124        ok &= Run( Acos,            "Acos"           );
    125125        ok &= Run( Add,             "Add"            );
Note: See TracChangeset for help on using the changeset viewer.