File: get_started.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,567 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: 2024 Bradley M. Bell
// ---------------------------------------------------------------------------
/*
{xrst_begin valvector_get_started.cpp}

Getting Started Using valvector as a CppAD Base Class
#####################################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end valvector_get_started.cpp}
-------------------------------------------------------------------------------
*/
// BEGIN C++
# include <cppad/example/valvector/class.hpp>
# include <cppad/cppad.hpp>
bool get_started(void)
{  // ok
   bool ok = true;
   //
   // ad_valvector
   typedef CppAD::AD<valvector> ad_valvector;
   //
   // ax
   CPPAD_TESTVECTOR( ad_valvector ) ax(2);
   ax[0] = valvector( {1.0} );
   ax[1] = valvector( {2.0} );
   CppAD::Independent(ax);
   //
   // f
   CPPAD_TESTVECTOR( ad_valvector ) ay(1);
   ay[0] = ax[0] * ax[1];
   CppAD::ADFun<valvector> f(ax, ay);
   //
   // x
   CPPAD_TESTVECTOR( valvector ) x(2);
   x[0] = valvector( {1.0, 2.0, 3.0} );
   x[1] = valvector( {4.0, 3.0, 2.0} );
   //
   // y
   CPPAD_TESTVECTOR( valvector ) y(1);
   y = f.Forward(0, x);
   //
   // ok
   valvector check;
   check = valvector( {4.0, 6.0, 6.0} );
   ok   &= y[0] == check;
   //
   //  dw
   CPPAD_TESTVECTOR( valvector ) w(1), dw(2);
   w[0] = valvector( 1.0 );
   dw   = f.Reverse(1, w);
   //
   // ok
   ok &= dw[0] == x[1];
   ok &= dw[1] == x[0];
   //
   return ok;
}
// END C++