File: vec_ad_par.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 (65 lines) | stat: -rw-r--r-- 1,596 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
// 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
// ----------------------------------------------------------------------------

/*
Test the use of the parameters in VecAD element assignments
*/

# include <cppad/cppad.hpp>

typedef CppAD::AD<double>      ADdouble;
typedef CppAD::AD< ADdouble > ADDdouble;

bool VecADPar(void)
{
   using namespace CppAD;

   bool ok = true;

   CPPAD_TESTVECTOR( ADdouble ) x(2);
   x[0] = 0;
   x[1] = 0;
   Independent(x);

   CPPAD_TESTVECTOR( ADDdouble ) y(1);
   y[0] = 1;
   Independent(y);

   VecAD< ADdouble > v(2);
   ADDdouble zero(0);
   ADDdouble one(1);
   v[zero] = x[0]; // these two parameter values are equal,
   v[one]  = x[1]; // but they are not identically equal

   CPPAD_TESTVECTOR( ADDdouble ) z(1);
   z[0] = v[zero] + v[one];

   // f(y) = x[0] + x[1]
   ADFun< ADdouble > f(y, z);
   CPPAD_TESTVECTOR( ADdouble ) a( f.Domain() );
   CPPAD_TESTVECTOR( ADdouble ) b( f.Range() );

   // fy = f(y) = x[0] + x[1]
   a[0]  = 0.;
   b     = f.Forward(0, a);

   // check value of f
   ok &= b[0] == (x[0] + x[1]);

   // g(x) = x[0] + x[1];
   ADFun<double> g(x, b);
   CPPAD_TESTVECTOR( double ) c( g.Domain() );
   CPPAD_TESTVECTOR( double ) d( g.Range() );

   // d = g(1, 2)
   c[0] = 1.;   // these tow values are not equal and correspond
   c[1] = 2.;   // to replacements for the equal parameter values above
   d = g.Forward(0, c);

   // check function value
   ok &= (d[0] == c[0] + c[1]);

   return ok;
}