Changeset 1452 for trunk/test_more


Ignore:
Timestamp:
Jul 6, 2009 10:59:58 AM (11 years ago)
Author:
bradbell
Message:

trunk.new: New interface to reverse LdpOp? operation (and fix bug).

vec_ad.cpp: add new test case to demonstrate bug in VecAD operation.
where_new_09.omh: user's view of the changes.
load_op.hpp: new interface for reverse LdpOp? operation (also fixes bug).
reverse_sweep.hpp: use new interface to LdpOp?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test_more/vec_ad.cpp

    r1370 r1452  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-09 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    173173}
    174174
     175# include <limits>
     176bool SecondOrderReverse(void)
     177{       // Bradley M. Bell 2009-07-06
     178        // Reverse mode for LdpOp was only modifying the highest order partial
     179        // This test demonstrated the bug
     180        bool ok = true;
     181        using CppAD::AD;
     182        using CppAD::NearEqual;
     183        double eps = 10. * std::numeric_limits<double>::epsilon();
     184
     185        size_t n = 1;
     186        CPPAD_TEST_VECTOR< AD<double> > X(n);
     187        X[0] = 2.;
     188        CppAD::Independent(X);
     189
     190        size_t m = 1;
     191        CPPAD_TEST_VECTOR< AD<double> > Y(m);
     192
     193        // The LdpOp instruction corresponds to operations with VecAD vectors.
     194        CppAD::VecAD<double> Z(1);
     195        AD<double> zero = 0;
     196        Z[zero] = X[0] + 1;
     197
     198        // Compute a function where the second order partial for y
     199        // depends on the first order partials for z
     200        // This will use the LdpOp instruction because the index
     201        // access to z is the parameter zero.
     202        Y[0] = Z[zero] * Z[zero];
     203
     204        CppAD::ADFun<double> f(X, Y);
     205
     206        // first order forward
     207        CPPAD_TEST_VECTOR<double> dx(n);
     208        size_t p = 1;
     209        dx[0]    = 1.;
     210        f.Forward(p, dx);
     211
     212        // second order reverse (test exp_if_true case)
     213        CPPAD_TEST_VECTOR<double> w(m), dw(2 * n);
     214        w[0] = 1.;
     215        p    = 2;
     216        dw = f.Reverse(p, w);
     217
     218        // check first derivative in dw
     219        double check = 2. * (Value( X[0] ) + 1.);
     220        ok &= NearEqual(dw[0], check, eps, eps);
     221
     222        // check second derivative in dw
     223        check = 2.;
     224        ok &= NearEqual(dw[1], check, eps, eps);
     225
     226        return ok;
     227}
     228
    175229} // END empty namespace
    176230
     
    179233        ok &= VecADTestOne();
    180234        ok &= VecADTestTwo();
    181         return ok;
    182 }
     235        ok &= SecondOrderReverse();
     236        return ok;
     237}
Note: See TracChangeset for help on using the changeset viewer.