Changeset 2272


Ignore:
Timestamp:
Jan 24, 2012 2:24:34 AM (8 years ago)
Author:
bradbell
Message:

Use better variable names and optimize in multi-level AD example.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/example/mul_level.cpp

    r1697 r2272  
    11/* $Id$ */
    22/* --------------------------------------------------------------------------
    3 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell
     3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
    44
    55CppAD is distributed under multiple licenses. This distribution is under
     
    7878{       bool ok = true;                          // initialize test result
    7979
    80         typedef CppAD::AD<double>   ADdouble;    // for one level of taping
    81         typedef CppAD::AD<ADdouble> ADDdouble;   // for two levels of taping
     80        typedef CppAD::AD<double>    A1_double;  // for one level of taping
     81        typedef CppAD::AD<A1_double> A2_double;  // for two levels of taping
    8282        size_t n = 5;                            // dimension for example
    8383        size_t j;                                // a temporary index variable
    8484
    8585        CPPAD_TEST_VECTOR<double>       x(n);
    86         CPPAD_TEST_VECTOR<ADdouble>   a_x(n);
    87         CPPAD_TEST_VECTOR<ADDdouble> aa_x(n);
     86        CPPAD_TEST_VECTOR<A1_double>  a1_x(n), a1_v(n), a1_dy(1) ;
     87        CPPAD_TEST_VECTOR<A2_double>  a2_x(n), a2_y(1);
    8888
    8989        // Values for the independent variables while taping the function f(x)
    9090        for(j = 0; j < n; j++)
    91                 aa_x[j] = double(j);
     91                a2_x[j] = a1_x[j] = x[j] = double(j);
    9292        // Declare the independent variable for taping f(x)
    93         CppAD::Independent(aa_x);
     93        CppAD::Independent(a2_x);
    9494
    9595        // Use AD< AD<double> > to tape the evaluation of f(x)
    96         CPPAD_TEST_VECTOR<ADDdouble> aa_f(1);
    97         aa_f[0] = f(aa_x);
     96        a2_y[0] = f(a2_x);
    9897
    99         // Declare a_F as the corresponding ADFun< AD<double> > function f(x)
     98        // Declare a1_F as the corresponding ADFun< AD<double> > value a2_y
    10099        // (make sure we do not run zero order forward during constructor)
    101         CppAD::ADFun<ADdouble> a_F;
    102         a_F.Dependent(aa_x, aa_f);
     100        CppAD::ADFun<A1_double> a1_F;
     101        a1_F.Dependent(a2_x, a2_y);
    103102
    104103        // Values for the independent variables while taping f'(x) * v
     104        // Declare the independent variable for taping f'(x) * v
     105        CppAD::Independent(a1_x);
     106        // set the argument value x for computing f'(x) * v
     107        a1_F.Forward(0, a1_x);
     108        // compute f'(x) * v
    105109        for(j = 0; j < n; j++)
    106                 a_x[j] = double(j);
    107         // Declare the independent variable for taping f'(x) * v
    108         CppAD::Independent(a_x);
    109         // set the argument value x for computing f'(x) * v
    110         a_F.Forward(0, a_x);
    111         // compute f'(x) * v
    112         CPPAD_TEST_VECTOR<ADdouble> a_v(n);
    113         CPPAD_TEST_VECTOR<ADdouble> a_df(1);
    114         for(j = 0; j < n; j++)
    115                 a_v[j] = double(n - j);
    116         a_df = a_F.Forward(1, a_v);
     110                a1_v[j] = double(n - j);
     111        a1_dy = a1_F.Forward(1, a1_v);
    117112
    118113        // declare dF as ADFun<double> function corresponding to f'(x) * v
    119         CppAD::ADFun<double> df(a_x, a_df);
     114        CppAD::ADFun<double> dF;
     115        dF.Dependent(a1_x, a1_dy);
    120116
    121         // compute the d/dx of f'(x) * v = f''(x) * v
     117        // optimize out operations not necessary for function f'(x) * v
     118        dF.optimize();
     119
     120        // Evaluate f'(x) * v
     121        dF.Forward(0, x);
     122
     123        // compute the d/dx of f'(x) * v = f''(x) * v = v
    122124        CPPAD_TEST_VECTOR<double> w(1);
    123125        CPPAD_TEST_VECTOR<double> dw(n);
    124126        w[0] = 1.;
    125         dw   = df.Reverse(1, w);
     127        dw   = dF.Reverse(1, w);
    126128
    127129        for(j = 0; j < n; j++)
    128                 ok &= CppAD::NearEqual(dw[j], a_v[j], 1e-10, 1e-10);
     130                ok &= CppAD::NearEqual(dw[j], a1_v[j], 1e-10, 1e-10);
    129131
    130132        return ok;
  • trunk/omh/whats_new_12.omh

    r2270 r2272  
    3232The purpose of this section is to
    3333assist you in learning about changes between various versions of CppAD.
     34
     35$head 01-23$$
     36Change variable notation and use $cref optimize$$ in
     37$cref mul_level.cpp$$.
    3438
    3539$head 01-20$$
Note: See TracChangeset for help on using the changeset viewer.