source: trunk/test_more/test_vector.cpp @ 3741

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

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

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/* $Id: test_vector.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 deprecated CPPAD_TEST_VECTOR macro (should use CPPAD_TESTVECTOR instead
15*/
16# include <cppad/cppad.hpp>
17
18// this test is coppied from example/add.cpp on 2012-06-06
19bool test_vector(void)
20{       bool ok = true;
21        using CppAD::AD;
22        using CppAD::NearEqual;
23
24        // domain space vector
25        size_t n  = 1;
26        double x0 = 0.5;
27        CPPAD_TEST_VECTOR< AD<double> > x(n);
28        x[0]      = x0; 
29
30        // declare independent variables and start tape recording
31        CppAD::Independent(x);
32
33        // some binary addition operations
34        AD<double> a = x[0] + 1.; // AD<double> + double
35        AD<double> b = a    + 2;  // AD<double> + int
36        AD<double> c = 3.   + b;  // double     + AD<double>
37        AD<double> d = 4    + c;  // int        + AD<double>
38
39        // range space vector
40        size_t m = 1;
41        CPPAD_TEST_VECTOR< AD<double> > y(m);
42        y[0] = d + x[0];          // AD<double> + AD<double>
43
44        // create f: x -> y and stop tape recording
45        CppAD::ADFun<double> f(x, y); 
46
47        // check value
48        ok &= NearEqual(y[0] , 2. * x0 + 10,  1e-10 , 1e-10);
49
50        // forward computation of partials w.r.t. x[0]
51        CPPAD_TEST_VECTOR< double > dx(n);
52        CPPAD_TEST_VECTOR< double > dy(m);
53        dx[0] = 1.;
54        dy    = f.Forward(1, dx);
55        ok   &= NearEqual(dy[0], 2., 1e-10, 1e-10);
56
57        // reverse computation of derivative of y[0]
58        CPPAD_TEST_VECTOR< double >  w(m);
59        CPPAD_TEST_VECTOR< double > dw(n);
60        w[0]  = 1.;
61        dw    = f.Reverse(1, w);
62        ok   &= NearEqual(dw[0], 2., 1e-10, 1e-10);
63
64        // use a VecAD<Base>::reference object with addition
65        CppAD::VecAD<double> v(1);
66        AD<double> zero(0);
67        v[zero] = a;
68        AD<double> result = v[zero] + 2;
69        ok     &= (result == b);
70
71        return ok;
72}
Note: See TracBrowser for help on using the repository browser.