File: assign.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (80 lines) | stat: -rw-r--r-- 2,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
Old example, now used just for validation testing
*/

# include <cppad/cppad.hpp>

bool assign(void)
{  bool ok = true;
   using CppAD::AD;

   // domain space vector
   size_t n = 3;
   CPPAD_TESTVECTOR(AD<double>) x(n);
   x[0]     = 2;      // AD<double> = int
   x[1]     = 3.;     // AD<double> = double
   x[2]     = x[1];   // AD<double> = AD<double>

   // declare independent variables and start tape recording
   CppAD::Independent(x);

   // range space vector
   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>) y(m);

   // assign an AD<Base> object equal to an independent variable
   // (choose the first independent variable to check a special case)
   // use the value returned by the assignment (for another assignment)
   y[0] = y[1] = x[0];

   // assign an AD<Base> object equal to an expression
   y[1] = x[1] + 1.;
   y[2] = x[2] + 2.;

   // check that all the resulting components of y depend on x
   ok &= Variable(y[0]);  // y[0] = x[0]
   ok &= Variable(y[1]);  // y[1] = x[1] + 1
   ok &= Variable(y[2]);  // y[2] = x[2] + 2

   // construct f : x -> y and stop the tape recording
   CppAD::ADFun<double> f(x, y);

   // check variable values
   ok &= ( y[0] == 2.);
   ok &= ( y[1] == 4.);
   ok &= ( y[2] == 5.);

   // compute partials w.r.t x[1]
   CPPAD_TESTVECTOR(double) dx(n);
   CPPAD_TESTVECTOR(double) dy(m);
   dx[0] = 0.;
   dx[1] = 1.;
   dx[2] = 0.;
   dy   = f.Forward(1, dx);
   ok  &= (dy[0] == 0.);  // dy[0] / dx[1]
   ok  &= (dy[1] == 1.);  // dy[1] / dx[1]
   ok  &= (dy[2] == 0.);  // dy[2] / dx[1]

   // compute the derivative y[2]
   CPPAD_TESTVECTOR(double)  w(m);
   CPPAD_TESTVECTOR(double) dw(n);
   w[0] = 0.;
   w[1] = 0.;
   w[2] = 1.;
   dw   = f.Reverse(1, w);
   ok  &= (dw[0] == 0.);  // dy[2] / dx[0]
   ok  &= (dw[1] == 0.);  // dy[2] / dx[1]
   ok  &= (dw[2] == 1.);  // dy[2] / dx[2]

   // assign a VecAD<Base>::reference
   CppAD::VecAD<double> v(1);
   AD<double> zero(0);
   v[zero] = 5.;
   ok     &= (v[0] == 5.);

   return ok;
}