source: trunk/test_more/assign.cpp @ 3793

Last change on this file since 3793 was 3779, checked in by bradbell, 4 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: cf5ca238670f67655fbbf3e23727437f7ed7b182
end hash code: 5661e2a629cceb77e7b703225bc1f3f8cf422129

commit 5661e2a629cceb77e7b703225bc1f3f8cf422129
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 04:23:41 2016 -0700

push_git2svn.py: Include changes in copyright date when checking for difference.

commit f0e9d149dc00ccf410a268389e3dcfd95499b863
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 03:57:09 2016 -0700

Advance copyright year to 2016.

commit 01d25679b7baddca878129635b0758f4ebb263fb
Author: Brad Bell <bradbell@…>
Date: Fri Jan 1 03:54:41 2016 -0700

download.omh: improve the no documentation download instructions.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: assign.cpp 3779 2016-01-01 11:26:11Z bradbell $
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 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/*
13Old example, now used just for validation testing
14*/
15
16# include <cppad/cppad.hpp>
17
18bool assign(void)
19{       bool ok = true;
20        using CppAD::AD;
21
22        // domain space vector
23        size_t n = 3;
24        CPPAD_TESTVECTOR(AD<double>) x(n);
25        x[0]     = 2;      // AD<double> = int
26        x[1]     = 3.;     // AD<double> = double
27        x[2]     = x[1];   // AD<double> = AD<double>
28
29        // declare independent variables and start tape recording
30        CppAD::Independent(x);
31
32        // range space vector
33        size_t m = 3;
34        CPPAD_TESTVECTOR(AD<double>) y(m);
35
36        // assign an AD<Base> object equal to an independent variable
37        // (choose the first independent variable to check a special case)
38        // use the value returned by the assignment (for another assignment)
39        y[0] = y[1] = x[0];
40
41        // assign an AD<Base> object equal to an expression
42        y[1] = x[1] + 1.;
43        y[2] = x[2] + 2.;
44
45        // check that all the resulting components of y depend on x
46        ok &= Variable(y[0]);  // y[0] = x[0]
47        ok &= Variable(y[1]);  // y[1] = x[1] + 1
48        ok &= Variable(y[2]);  // y[2] = x[2] + 2
49
50        // construct f : x -> y and stop the tape recording
51        CppAD::ADFun<double> f(x, y);
52
53        // check variable values
54        ok &= ( y[0] == 2.);
55        ok &= ( y[1] == 4.);
56        ok &= ( y[2] == 5.);
57
58        // compute partials w.r.t x[1]
59        CPPAD_TESTVECTOR(double) dx(n);
60        CPPAD_TESTVECTOR(double) dy(m);
61        dx[0] = 0.;
62        dx[1] = 1.;
63        dx[2] = 0.;
64        dy   = f.Forward(1, dx);
65        ok  &= (dy[0] == 0.);  // dy[0] / dx[1]
66        ok  &= (dy[1] == 1.);  // dy[1] / dx[1]
67        ok  &= (dy[2] == 0.);  // dy[2] / dx[1]
68
69        // compute the derivative y[2]
70        CPPAD_TESTVECTOR(double)  w(m);
71        CPPAD_TESTVECTOR(double) dw(n);
72        w[0] = 0.;
73        w[1] = 0.;
74        w[2] = 1.;
75        dw   = f.Reverse(1, w);
76        ok  &= (dw[0] == 0.);  // dy[2] / dx[0]
77        ok  &= (dw[1] == 0.);  // dy[2] / dx[1]
78        ok  &= (dw[2] == 1.);  // dy[2] / dx[2]
79
80        // assign a VecAD<Base>::reference
81        CppAD::VecAD<double> v(1);
82        AD<double> zero(0);
83        v[zero] = 5.;
84        ok     &= (v[0] == 5.);
85
86        return ok;
87}
Note: See TracBrowser for help on using the repository browser.